From 3de7a7bb443a408b7781da7f13bb5a2a6d5229c7 Mon Sep 17 00:00:00 2001 From: neru Date: Fri, 20 Mar 2026 17:41:08 -0300 Subject: [PATCH] feat: add atexit handler for cleanup --- src/unlocker/main.cpp | 46 ++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/unlocker/main.cpp b/src/unlocker/main.cpp index c9d200b..1c799c2 100644 --- a/src/unlocker/main.cpp +++ b/src/unlocker/main.cpp @@ -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 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; } \ No newline at end of file