feat: add atexit handler for cleanup

This commit is contained in:
2026-03-20 17:41:08 -03:00
parent b238f4f901
commit 3de7a7bb44
+34 -12
View File
@@ -62,12 +62,34 @@ bool setProxy(bool enable, const std::string& proxyAddr)
return true;
}
Proxy* g_proxy = nullptr;
bool running = true;
void cleanup()
{
static std::mutex cleanupMutex;
std::lock_guard<std::mutex> lock(cleanupMutex);
static bool cleaned = false;
if (cleaned) return;
cleaned = true;
if (g_proxy)
{
Log::info("Shutting down proxy");
g_proxy->Shutdown();
}
Log::info("Restoring system proxy settings");
setProxy(false, "");
}
BOOL WINAPI consoleHandler(DWORD dwType)
{
if (dwType == CTRL_C_EVENT || dwType == CTRL_CLOSE_EVENT)
if (dwType == CTRL_C_EVENT || dwType == CTRL_CLOSE_EVENT || dwType == CTRL_LOGOFF_EVENT || dwType == CTRL_SHUTDOWN_EVENT)
{
running = false;
cleanup();
exit(0);
return TRUE;
}
return FALSE;
@@ -244,8 +266,9 @@ int main()
{
Log::createConsole();
SetConsoleCtrlHandler(consoleHandler, TRUE);
atexit(cleanup);
Log::info("Unlocker init");
Log::info("Init");
loadCatalogOnStartup();
loadAllCustomItems();
@@ -253,9 +276,9 @@ int main()
/*
proxy setup
*/
Log::verbose("Starting proxy");
Proxy* proxy = new Proxy();
if (!proxy->Init())
Log::info("Starting proxy");
g_proxy = new Proxy();
if (!g_proxy->Init())
{
Log::error("Proxy failed to start");
return 1;
@@ -265,8 +288,10 @@ int main()
/*
listeners
*/
proxy->OnServerResponse.addListener([](const std::string& url, std::string& data) {
g_proxy->OnServerResponse.addListener([](const std::string& url, std::string& data) {
#ifdef _DEBUG
if (url.find("bhvrdbd.com") != std::string::npos) Log::verbose("BHVR api res: {}", url);
#endif
if (url.find("api/v1/extensions/store/getCatalogItems") != std::string::npos)
updateCatalog(data);
@@ -424,7 +449,7 @@ int main()
count++;
}
}
Log::info("Injected missing items and targeted perk tiers in {} character inventories", count);
Log::info("Added missing items and targeted perk tiers in {} character inventories", count);
}
else
Log::warning("No custom dumped items available to inject into character inventory!");
@@ -434,17 +459,14 @@ int main()
/*
pause
*/
Log::verbose("Proxy running (CTRL+C to stop)");
Log::info("Proxy running (CTRL+C to stop)");
while (running)
Sleep(100);
/*
cleanup
*/
Log::verbose("Shutting down proxy");
proxy->Shutdown();
delete proxy;
setProxy(false, "");
cleanup();
return 0;
}