Skip to content

Commit

Permalink
wip: discord rpc for game state
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMcAvoy committed Dec 10, 2024
1 parent 59b4ef6 commit 4940ee8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 40 deletions.
65 changes: 32 additions & 33 deletions CarbonLauncher/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ int main(int argc, char* argv[]) {
if (ImGui::BeginTabItem("Mods")) {
auto dirIter = std::filesystem::directory_iterator(modsDir);
for (const auto& entry : dirIter) {
ImGui::BeginChild(entry.path().string().c_str(), ImVec2(0, 200), true);
ImGui::BeginChild(entry.path().string().c_str(), ImVec2(0, 150), true);
ImGui::TextWrapped(entry.path().filename().string().c_str());

bool isModEnabled = std::find(State.enabledMods.begin(), State.enabledMods.end(), entry.path()) != State.enabledMods.end();
Expand Down Expand Up @@ -299,7 +299,7 @@ int main(int argc, char* argv[]) {
const char* text = isGameRunning ? "Stop" : "Start";
ImVec2 pos = ImVec2((w - size.x) / 2, size.y);
ImGui::SetCursorPosX(pos.x);
ImGui::SetCursorPosY(h - pos.y - 24);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 10);
if (ImGui::Button(text, ImVec2(size.x + 20, size.y + 10))) {
if (isGameRunning) {
ShellExecuteA(NULL, "open", "taskkill", "/F /IM ScrapMechanic.exe", NULL, SW_SHOWNORMAL);
Expand All @@ -309,12 +309,7 @@ int main(int argc, char* argv[]) {

// Management of injection after message from CarbonSupervisor.
std::thread([&] {
// Inject CarbonSupervisor.dll
DWORD targetPID = GetProcID("ScrapMechanic.exe");
/*if (targetPID == 0) {
spdlog::error("Failed to get process ID of ScrapMechanic.exe");
return 1;
}*/

while (targetPID == 0) {
spdlog::info("Waiting for ScrapMechanic.exe to start");
Expand All @@ -338,42 +333,46 @@ int main(int argc, char* argv[]) {
return 1;
}

spdlog::info("Opened named pipe");

// Wait for the supervisor to send `loaded`
char buffer[7] = { 0 };
// Infinite loop receiving messages from the supervisor
char buffer[1024];
DWORD bytesRead;
if (!ReadFile(hPipe, buffer, sizeof(buffer), &bytesRead, nullptr)) {
spdlog::error("Failed to read from named pipe: {}", GetLastErrorAsString());
return 1;
}

buffer[bytesRead - 1] = '\0'; // Null-terminate after each read.

spdlog::info("Received message: {}", buffer);


while (strcmp(buffer, "loaded") != 0) {
// Clear the buffer before reading again
// Infinitely try to read from the pipe
while (true) {
memset(buffer, 0, sizeof(buffer));

if (!ReadFile(hPipe, buffer, sizeof(buffer), &bytesRead, nullptr)) {
spdlog::error("Failed to read from named pipe: {}", GetLastErrorAsString());
return 1;
}

buffer[bytesRead] = '\0'; // Null-terminate after each read.
std::string message = buffer;

spdlog::info("Received message: {}", message);

if (message == "loaded") {
// Inject mods
std::vector enabledModsClone = State.enabledMods;
for (auto& mod : enabledModsClone) {
if (!Inject(targetPID, hWnd, mod.string())) {
spdlog::error("Failed to inject mod: {}", mod.string());
continue;
}
}
}

spdlog::info("Received message: {}", buffer);
}

// Inject mods
std::vector enabledModsClone = State.enabledMods;
for (auto& mod : enabledModsClone) {
if (!Inject(targetPID, hWnd, mod.string())) {
spdlog::error("Failed to inject mod: {}", mod.string());
continue;
if (message.length() == 1 && isdigit(message[0])) {
if (message == "2") {
spdlog::info("TODO: Say in game");
}
else if (message == "3") {
spdlog::info("TODO: Say in menu");
}
else if (message == "1") {
spdlog::info("TODO: Say in loading screen");
}
}

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

return 0;
Expand Down
45 changes: 38 additions & 7 deletions CarbonSupervisor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
typedef unsigned int uint4_t;

struct Contraption {
/* 0x000 */ HWND hWnd;
/* 0x008 */ char pad_008[0x174];
/* 0x000 */ char pad_056[0x17C];
/* 0x17C */ uint4_t gameStateType;
/* 0x180 */ char pad_004[0x20];
/* 0x1A0 */ HWND hWnd;
};

template <typename T>
Expand All @@ -33,19 +34,21 @@ DWORD WINAPI ThreadProc(LPVOID lpParameter) {
return 1;
}

std::this_thread::sleep_for(std::chrono::seconds(2));
uintptr_t contraptionAddr = (uintptr_t)GetModuleHandle(nullptr) + 0x12674B8;
Contraption* contraption = FetchClass<Contraption*>(contraptionAddr);

auto contraptionAddr = (uintptr_t)GetModuleHandle(nullptr) + 0x12674B8;
auto contraption = FetchClass<Contraption*>(contraptionAddr);
while (contraption == nullptr) {
while (contraption == nullptr)
contraption = FetchClass<Contraption*>(contraptionAddr);

while (contraption->gameStateType < 1 || contraption->gameStateType > 3 || contraption == nullptr || contraption->hWnd == nullptr) {
contraption = FetchClass<Contraption*>(contraptionAddr);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

std::this_thread::sleep_for(std::chrono::seconds(1));
while (contraption->gameStateType == 1) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

std::this_thread::sleep_for(std::chrono::seconds(1));

// Open the supervisor pipe and send `loaded`
Expand All @@ -58,6 +61,34 @@ DWORD WINAPI ThreadProc(LPVOID lpParameter) {
}

for (;;) {
static uint4_t lastGameStateType = contraption->gameStateType;

if (contraption->gameStateType != lastGameStateType) {
lastGameStateType = contraption->gameStateType;

std::cout << "\n\n\n\n\nChanged game state type: " << contraption->gameStateType << "\n\n\n\n\n";

std::cout << "Game state type: " << contraption->gameStateType << std::endl;

// Send the game state type to the supervisor
memset(buffer, 0, sizeof(buffer));
//buffer[0] = contraption->gameStateType;
//_itoa(contraption->gameStateType, buffer, 10);
_itoa_s(contraption->gameStateType, buffer, 10);
// buffer -> "1" -> 0x31

if (!WriteFile(hPipe, buffer, sizeof(buffer), &bytesWritten, nullptr)) {
MessageBoxA(nullptr, "Failed to write to named pipe", "CarbonSupervisor", MB_OK);
return 1;
}

if (contraption->gameStateType == 3) {
break;
}

std::this_thread::sleep_for(std::chrono::seconds(1));
}

std::this_thread::sleep_for(std::chrono::seconds(1));
}

Expand Down

0 comments on commit 4940ee8

Please sign in to comment.