// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only #pragma comment(lib, "ntdll.lib") #include #include #include #include #include #include extern UINT WM_TASKBAR; extern HWND window; extern HMENU menu; extern NOTIFYICONDATA data; extern TCHAR tip[64]; extern char class_name[]; extern std::vector logs; int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) { soyuz::init_log_file(); MSG messages; WNDCLASSEX wincl; WM_TASKBAR = RegisterWindowMessageA("TaskbarCreated"); wincl.hInstance = instance; wincl.lpszClassName = class_name; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof(WNDCLASSEX); wincl.hIcon = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); wincl.hIconSm = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); wincl.hCursor = LoadCursor(nullptr, IDC_ARROW); wincl.lpszMenuName = nullptr; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(255, 255, 255))); if (!RegisterClassEx(&wincl)) { return 0; } window = CreateWindowEx(0, class_name, class_name, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, nullptr, instance, nullptr); InitNotifyIconData(); ShowWindow(window, show); /** * Launch a new thread to take care of everything important * * https://medium.com/@vgasparyan1995/a-new-thread-in-c-20-jthread-ebd121ae8906 */ std::jthread soyuz{[](const std::stop_token &stop) -> void { // Check if Lunar Client is open, if not; close Soyuz DWORD pid = soyuz::find_lunar(); if (pid == 0 || pid == 3435973836) { soyuz::log(soyuz::log_level::LOG_LEVEL_ERROR, "could not locate lunar client"); soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, "this window will close in five seconds"); for (int i = 4; i > -1; --i) { std::this_thread::sleep_for(std::chrono::seconds(1)); soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, fmt::format("> {} second{}", soyuz::numbers_as_string[i], i != 1 ? "s" : "")); } soyuz::exit(1); } soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, fmt::format("located lunar client: pid {}", pid)); // GetLastError() soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "hooked lunar client"); soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "you may now close this window"); while (!stop.stop_requested()) { /** * Check if Lunar Client is open before every `delete_handle` run, if not; * timeout * * Thanks, @LorenzoHanssens (#1) */ pid = soyuz::find_lunar(); if (pid == 0 || pid == 3435973836) { soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, "could not locate lunar client, waiting 10 seconds"); std::this_thread::sleep_for(std::chrono::seconds(10)); } // If Lunar Client **is** open, close it's Discord IPC Named Pipe if (soyuz::delete_handle(pid) == 1) { soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, "unable to close lunar client's discord ipc named pipe,"); soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, "> waiting 10 seconds"); std::this_thread::sleep_for(std::chrono::seconds(10)); } } }}; while (GetMessage(&messages, nullptr, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); } soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "requesting exit"); soyuz.request_stop(); soyuz.detach(); soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "exiting"); soyuz::close_log_file(); return (int)messages.wParam; }