Skip to content

Commit

Permalink
chore: Fix startup issues #149
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Nov 3, 2024
1 parent 256bc77 commit 471df3c
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions src/core/co_initialize/co_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,69 +374,62 @@ void OnBackendLoad(uint16_t ftpPort, uint16_t ipcPort)
}

const void CoInitializer::InjectFrontendShims(uint16_t ftpPort, uint16_t ipcPort) {
static std::mutex mtx;
static std::condition_variable cv;
static bool hasSuccess = false;
static bool hasPaused = false;
std::mutex mtx;
std::condition_variable cv;
bool hasSuccess = false, hasPaused = false;

Logger.Log("Preparing to inject frontend shims...");

std::thread debuggerPauseThread;
try {
debuggerPauseThread = std::thread([] {
JavaScript::SharedJSMessageEmitter::InstanceRef().OnMessage("msg", [](const nlohmann::json& eventMessage, int listenerId) {
std::lock_guard<std::mutex> lock(mtx);

if (eventMessage.value("id", -1) == 65756) {
if (eventMessage.contains("error")) {
Logger.Warn("Failed to pause debugger, Steam is likely not yet loaded...");
Sockets::PostShared({ {"id", 65756 }, {"method", "Debugger.pause"} });
} else if (eventMessage.contains("result")) {
Logger.Log("Successfully sent debugger pause...");
hasSuccess = true;
}
}
if (eventMessage.contains("method") && eventMessage["method"] == "Debugger.paused") {
Logger.Log("Debugger has paused!");
hasPaused = true;
}
JavaScript::SharedJSMessageEmitter::InstanceRef().OnMessage("msg", [&](const nlohmann::json& eventMessage, int listenerId) {
std::lock_guard<std::mutex> lock(mtx);

if (eventMessage.value("id", -1) == 65756)
{
if (eventMessage.contains("error"))
{
Logger.Warn("Failed to pause debugger, Steam is likely not yet loaded...");
Sockets::PostShared({ {"id", 65756 }, {"method", "Debugger.pause"} });
}
else if (eventMessage.contains("result"))
{
Logger.Log("Successfully sent debugger pause...");
hasSuccess = true;
}
}
if (eventMessage.contains("method") && eventMessage["method"] == "Debugger.paused")
{
Logger.Log("Debugger has paused!");
hasPaused = true;
}

if (hasSuccess && hasPaused) {
try {
JavaScript::SharedJSMessageEmitter::InstanceRef().RemoveListener("msg", listenerId);
} catch (const std::exception& ex) {
LOG_ERROR("Error removing listener: {}", ex.what());
}
cv.notify_all();
Logger.Log("Notifying...");
}
});
});
} catch (const std::system_error& e) {
LOG_ERROR("Failed to create debuggerPauseThread: {}", e.what());
return;
}
if (hasSuccess && hasPaused)
{
try
{
JavaScript::SharedJSMessageEmitter::InstanceRef().RemoveListener("msg", listenerId);
}
catch (const std::exception& ex)
{
LOG_ERROR("Error removing listener: {}", ex.what());
}
cv.notify_all();
}
});

Sockets::PostShared({ {"id", 3422 }, {"method", "Debugger.enable"} });
Sockets::PostShared({ {"id", 65756 }, {"method", "Debugger.pause"} });

try {
try
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [] { return hasSuccess && hasPaused; });
} catch (const std::system_error& e) {
cv.wait(lock, [&] { return hasSuccess && hasPaused; });
}
catch (const std::system_error& e)
{
LOG_ERROR("Condition variable wait error: {}", e.what());
if (debuggerPauseThread.joinable()) {
debuggerPauseThread.join();
}
return;
}

Logger.Log("Waiting for thread to join...");

if (debuggerPauseThread.joinable()) {
debuggerPauseThread.join();
}

Logger.Log("Ready to inject shims!");
BackendCallbacks& backendHandler = BackendCallbacks::getInstance();
backendHandler.RegisterForLoad(std::bind(OnBackendLoad, ftpPort, ipcPort));
Expand Down

0 comments on commit 471df3c

Please sign in to comment.