Skip to content

Commit

Permalink
feat: modtool support
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMcAvoy committed Jan 14, 2025
1 parent 85a157d commit f40962c
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 89 deletions.
8 changes: 4 additions & 4 deletions CarbonLauncher/CarbonLauncher.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@
<ItemGroup>
<ClCompile Include="src\guimanager.cpp" />
<ClCompile Include="src\discordmanager.cpp" />
<ClCompile Include="src\gamemanager.cpp" />
<ClCompile Include="src\processmanager.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\pipemanager.cpp" />
<ClCompile Include="src\repomanager.cpp" />
<ClCompile Include="src\modmanager.cpp" />
<ClCompile Include="src\state.cpp" />
<ClCompile Include="src\utils.cpp" />
<ClCompile Include="vendor\src\connection_win.cpp" />
Expand All @@ -163,9 +163,9 @@
<ClInclude Include="include\font\IconsFontAwesome6.h_fa-solid-900.ttf.h" />
<ClInclude Include="include\guimanager.h" />
<ClInclude Include="include\discordmanager.h" />
<ClInclude Include="include\gamemanager.h" />
<ClInclude Include="include\processmanager.h" />
<ClInclude Include="include\pipemanager.h" />
<ClInclude Include="include\repomanager.h" />
<ClInclude Include="include\modmanager.h" />
<ClInclude Include="include\state.h" />
<ClInclude Include="include\utils.h" />
<ClInclude Include="resource.h" />
Expand Down
3 changes: 0 additions & 3 deletions CarbonLauncher/include/discordmanager.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

/*#include <discord-game-sdk/discord_game_sdk.h>
#include <discord-game-sdk/discord.h>*/

#include <discord_rpc.h>

#include <string>
Expand Down
7 changes: 4 additions & 3 deletions CarbonLauncher/include/guimanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <GLFW/glfw3.h>

#include "modmanager.h"

namespace Carbon {
enum LogColour : WORD {
DARKGREEN = 2,
Expand All @@ -31,9 +33,6 @@ namespace Carbon {
GUIManager();
~GUIManager();

// Sets the render callback (called every frame on the main thread)
// @param callback The callback to call
// @note This is where you should put your ImGui code
void RenderCallback(std::function<void()> callback) {
this->renderCallback = callback;
}
Expand All @@ -45,6 +44,8 @@ namespace Carbon {
GLFWwindow* window;
std::function<void()> renderCallback;

ModTarget target = ModTarget::Game;

enum class Tab {
MyMods,
Discover,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,34 @@ namespace Carbon {
void Update();
};

struct Repo {
// The name of the repository (e.g. ScrappySM, Scrap-Mods)
std::string name;

// The link to the repositories website
std::string link;

// A list of all the mods
std::vector<Mod> mods;
enum ModTarget {
Game, // Scrap Mechanic
ModTool, // Scrap Mechanic Mod Tool
};

class RepoManager {
class ModManager {
public:
// Initializes the RepoManager and downloads the repos.json file
RepoManager();
~RepoManager();
ModManager();
~ModManager();

// Converts a JSON object to a Repo object
// @param json The JSON object to convert
// @return The converted Repo object (`json` -> `Repo`)
std::vector<Mod> URLToMods(const std::string& url);
std::pair<std::vector<Mod>, std::vector<Mod>> URLToMods(const std::string& url);

// Gets all the repos
// @return A vector of all the repos
std::vector<Mod>& GetMods() {
std::vector<Mod>& GetMods(ModTarget target) {
std::lock_guard<std::mutex> lock(this->repoMutex);
return this->mods;
return target == ModTarget::Game ? gameMods : modToolMods;
}

bool hasLoaded = false;

private:
std::vector<Mod> mods;
std::vector<Mod> gameMods;
std::vector<Mod> modToolMods;

std::optional<Mod> JSONToMod(const nlohmann::json& jMod);
std::mutex repoMutex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
namespace Carbon {
// Manages the game process including starting and stopping it
// along with monitoring the game's executable and loaded modules
class GameManager {
class ProcessManager {
public:
// Initializes the game manager and starts a thread
// listening for the game process
GameManager();
~GameManager();
ProcessManager();
~ProcessManager();

// Checks if the game is running
// @return True if the game is running, false otherwise
Expand All @@ -31,7 +31,7 @@ namespace Carbon {
// Starts the game and waits for it to be running
// This function will block until the game is running
// This function spawns a new thread to actually start the game process
void LaunchGame();
void LaunchProcess(const std::string& name);

// Stops the game forcefully
void KillGame();
Expand Down
10 changes: 5 additions & 5 deletions CarbonLauncher/include/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#include "guimanager.h"
#include "discordmanager.h"
#include "gamemanager.h"
#include "processmanager.h"
#include "pipemanager.h"
#include "repomanager.h"
#include "modmanager.h"

namespace Carbon {
struct LogMessage {
Expand Down Expand Up @@ -40,9 +40,9 @@ namespace Carbon {

Carbon::GUIManager guiManager;
Carbon::DiscordManager discordManager;
Carbon::GameManager gameManager;
Carbon::ProcessManager processManager;
Carbon::PipeManager pipeManager;
Carbon::RepoManager repoManager;
Carbon::ModManager modManager;

// The settings for the Carbon Launcher
Carbon::Settings settings;
Expand All @@ -52,7 +52,7 @@ namespace Carbon {
// The target process to manage (e.g. ScrapMechanic.exe)
// This should never be a process that does not have a Contraption
// located in the same place as ScrapMechanic.exe
const char* processTarget = "ScrapMechanic.exe";
// const char* processTarget = "ScrapMechanic.exe";
// const char* processTarget = "DummyGame.exe";
};
}; // namespace Carbon
Expand Down
4 changes: 2 additions & 2 deletions CarbonLauncher/src/discordmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ void DiscordManager::Update() {
case 2:
spdlog::trace("In a game!");

this->UpdateState(fmt::format("In a game! ({} mods loaded)", C.gameManager.GetLoadedCustomModules()));
this->UpdateState(fmt::format("In a game! ({} mods loaded)", C.processManager.GetLoadedCustomModules()));
break;
case 3:
spdlog::trace("In the main menu");
this->UpdateState(fmt::format("In the main menu with {} mods loaded", C.gameManager.GetLoadedCustomModules()));
this->UpdateState(fmt::format("In the main menu with {} mods loaded", C.processManager.GetLoadedCustomModules()));
break;
};

Expand Down
27 changes: 19 additions & 8 deletions CarbonLauncher/src/guimanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ void _GUI() {

ImGui::BeginChild("Control", ImVec2(0, 64), true);

if (C.gameManager.IsGameRunning()) {
if (C.processManager.IsGameRunning()) {
if (ImGui::Button(ICON_FA_STOP " Kill Game", ImVec2(128, 48))) {
C.gameManager.KillGame();
C.processManager.KillGame();
}

if (ImGui::IsItemHovered()) {
Expand All @@ -240,14 +240,25 @@ void _GUI() {
}
else {
if (ImGui::Button(ICON_FA_PLAY " Launch Game", ImVec2(128, 48))) {
C.gameManager.LaunchGame();
C.processManager.LaunchProcess(C.guiManager.target == ModTarget::Game ? "ScrapMechanic.exe" : "ModTool.exe");
}
}

ImGui::SameLine();

// Combo box for selecting the mod target
static const char* items[] = { "Game", "Mod Tool" };
static int item_current = 0;
ImGui::SetNextItemWidth(128);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 12);
if (ImGui::Combo("Target", &item_current, items, IM_ARRAYSIZE(items))) {
C.guiManager.target = item_current == 0 ? ModTarget::Game : ModTarget::ModTool;
}

ImGui::SameLine();

// Display if the pipe is connected or not if the game is running
if (C.gameManager.IsGameRunning()) {
if (C.processManager.IsGameRunning()) {
if (!C.pipeManager.IsConnected()) {
static ImVec4 colours[3] = {};
if (colours[0].x == 0) {
Expand All @@ -274,7 +285,7 @@ void _GUI() {

if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("The pipe is disconnected, the game may not be running correctly and Carbon Launcher cannot communicate with it.");
ImGui::Text("The pipe is disconnected, the game may not be running correctly and Carbon Launcher cannot communicate with it, did you launch the game from the launcher?");
ImGui::EndTooltip();
}

Expand Down Expand Up @@ -337,7 +348,7 @@ void _GUI() {
auto renderMod = [&](Mod& mod) -> void {
ImGui::BeginChild(mod.ghRepo.c_str(), ImVec2(0, 72), false);

float buttons = mod.wantsUpdate ? 2.75 : 2;
float buttons = mod.wantsUpdate ? 2.75f : 2.0f;
ImGui::BeginChild("Details", ImVec2(ImGui::GetContentRegionAvail().x - (64 * buttons), 0), false);

ImGui::SetWindowFontScale(1.2f);
Expand Down Expand Up @@ -420,13 +431,13 @@ void _GUI() {
};

auto renderMods = [&](bool mustBeInstalled) -> void {
for (auto& mod : C.repoManager.GetMods()) {
for (auto& mod : C.modManager.GetMods(C.guiManager.target)) {
if (mod.installed == mustBeInstalled) {
renderMod(mod);
}
}

if (std::all_of(C.repoManager.GetMods().begin(), C.repoManager.GetMods().end(), [&](Mod& mod) -> bool {
if (std::all_of(C.modManager.GetMods(C.guiManager.target).begin(), C.modManager.GetMods(C.guiManager.target).end(), [&](Mod& mod) -> bool {
return mod.installed != mustBeInstalled;
})) {
if (mustBeInstalled) {
Expand Down
Loading

0 comments on commit f40962c

Please sign in to comment.