aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/soyuz/library.hh2
-rw-r--r--include/soyuz/tray.hh2
-rw-r--r--soyuz/library.cc70
-rw-r--r--soyuz/soyuz.cc54
-rw-r--r--soyuz/tray.cc74
5 files changed, 136 insertions, 66 deletions
diff --git a/include/soyuz/library.hh b/include/soyuz/library.hh
index 8ada0bf..5074d2a 100644
--- a/include/soyuz/library.hh
+++ b/include/soyuz/library.hh
@@ -46,7 +46,7 @@ struct log_t {
) : level(level), value(std::move(value))
{}
- auto to_colorref() -> COLORREF;
+ [[nodiscard]] auto to_colorref() const -> COLORREF;
};
static auto CALLBACK enum_windows_proc(HWND, LPARAM) -> BOOL;
diff --git a/include/soyuz/tray.hh b/include/soyuz/tray.hh
index c4adc30..178600d 100644
--- a/include/soyuz/tray.hh
+++ b/include/soyuz/tray.hh
@@ -24,7 +24,7 @@ auto InitNotifyIconData() -> void;
namespace soyuz {
-auto log(const std::string &) -> void;
+[[maybe_unused]] auto log(const std::string &) -> void;
auto log(log_level, const std::string &) -> void;
}
diff --git a/soyuz/library.cc b/soyuz/library.cc
index 8b9ea8b..a39f4d7 100644
--- a/soyuz/library.cc
+++ b/soyuz/library.cc
@@ -24,9 +24,10 @@ namespace soyuz {
fmt::ostream log_file = fmt::output_file("soyuz.log");
-static BOOL CALLBACK enum_windows_proc(HWND hwnd, LPARAM lparam) {
+static auto CALLBACK enum_windows_proc(HWND hwnd, LPARAM lparam) -> BOOL {
int length = GetWindowTextLength(hwnd);
auto title = new CHAR[length + 1];
+
GetWindowText(hwnd, title, length);
if (strstr(title, LUNAR_WINDOW_NAME_BASE)) {
@@ -44,14 +45,17 @@ static BOOL CALLBACK enum_windows_proc(HWND hwnd, LPARAM lparam) {
auto find_lunar() -> DWORD {
HWND window = nullptr;
+ DWORD pid;
+
EnumWindows(enum_windows_proc, (LPARAM)&window);
int length = GetWindowTextLength(window);
auto title = new CHAR[length + 1];
+
GetWindowText(window, title, length);
- DWORD pid;
GetWindowThreadProcessId(window, &pid);
+
return pid;
}
@@ -61,16 +65,23 @@ auto delete_handle(DWORD pid) -> int {
FALSE,
pid
);
+ ULONG size = 1 << 10;
+ std::unique_ptr<BYTE []> buffer;
+
if (!lunar) {
- soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, fmt::format("could not open handle to lunar client: {}", GetLastError()));
+ soyuz::log(
+ soyuz::log_level::LOG_LEVEL_WARN,
+ fmt::format(
+ "could not open handle to lunar client: {}",
+ GetLastError()
+ )
+ );
return 1;
}
- ULONG size = 1 << 10;
- std::unique_ptr<BYTE[]> buffer;
for (;;) {
- buffer = std::make_unique<BYTE[]>(size);
+ buffer = std::make_unique<BYTE []>(size);
NTSTATUS status = NtQueryInformationProcess(
lunar,
@@ -83,15 +94,25 @@ auto delete_handle(DWORD pid) -> int {
if (NT_SUCCESS(status)) { break; }
if (status == STATUS_INFO_LENGTH_MISMATCH) { size += 1 << 10; continue; }
- soyuz::log(soyuz::log_level::LOG_LEVEL_DEBUG, "could not enumerate handle, skipping");
+ soyuz::log(
+ soyuz::log_level::LOG_LEVEL_DEBUG,
+ "could not enumerate handle, skipping"
+ );
return 1;
}
- auto *info = reinterpret_cast<PROCESS_HANDLE_SNAPSHOT_INFORMATION *>(buffer.get());
+ auto *info = reinterpret_cast<PROCESS_HANDLE_SNAPSHOT_INFORMATION *>(
+ buffer.get()
+ );
+
for (ULONG i = 0; i < info->NumberOfHandles; ++i) {
HANDLE h = info->Handles[i].HandleValue;
HANDLE target;
+ BYTE name_buffer[1 << 10];
+ WCHAR target_name[256];
+ DWORD session_id;
+
if (!DuplicateHandle(
lunar,
h,
@@ -102,7 +123,6 @@ auto delete_handle(DWORD pid) -> int {
DUPLICATE_SAME_ACCESS
)) { continue; }
- BYTE name_buffer[1 << 10];
NTSTATUS status = NtQueryObject(
target,
ObjectNameInformation,
@@ -110,19 +130,22 @@ auto delete_handle(DWORD pid) -> int {
sizeof(name_buffer),
nullptr
);
+
CloseHandle(target);
+
if (!NT_SUCCESS(status)) { continue; }
- WCHAR target_name[256];
- DWORD session_id;
ProcessIdToSessionId(pid, &session_id);
swprintf_s(target_name, DISCORD_IPC_NAMED_PIPE_NAME);
- size_t length = wcslen(target_name);
+ size_t length = wcslen(target_name);
auto *name = reinterpret_cast<UNICODE_STRING *>(name_buffer);
- if (name->Buffer && _wcsnicmp(name->Buffer, target_name, length) == 0) {
- soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "found lunar client's discord ipc named pipe");
+ if (name->Buffer && _wcsnicmp(name->Buffer, target_name, length) == 0) {
+ soyuz::log(
+ soyuz::log_level::LOG_LEVEL_INFO,
+ "found lunar client's discord ipc named pipe"
+ );
DuplicateHandle(
lunar,
h,
@@ -133,8 +156,10 @@ auto delete_handle(DWORD pid) -> int {
DUPLICATE_CLOSE_SOURCE
);
CloseHandle(target);
-
- soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "closed lunar client's discord ipc named pipe");
+ soyuz::log(
+ soyuz::log_level::LOG_LEVEL_INFO,
+ "closed lunar client's discord ipc named pipe"
+ );
return 0;
}
@@ -159,33 +184,36 @@ auto init_log_file() -> void {
}
auto close_log_file() -> void {
- soyuz::log(soyuz::log_level::LOG_LEVEL_DEBUG, "closing 'soyuz.log'"); log_file.close();
+ soyuz::log(soyuz::log_level::LOG_LEVEL_DEBUG, "closing 'soyuz.log'");
+ log_file.close();
}
auto exit(int exit_code) -> void {
// if (log_file.is_open()) { close_log_file(); }
+
close_log_file();
- ::exit(exit_code);
+ std::exit(exit_code);
}
auto current_date_time() -> std::string {
time_t now = time(nullptr);
- struct tm t_struct{};
+ struct tm t_struct {};
char buffer[80];
+
localtime_s(&t_struct, &now); // t_struct = *localtime(&now);
strftime(buffer, sizeof(buffer), "%Y-%m-%d.%X", &t_struct);
return buffer;
}
-auto log_t::to_colorref() -> COLORREF {
+auto log_t::to_colorref() const -> COLORREF {
switch (this->level) {
case LOG_LEVEL_TRACE: { return 0x00FF0000; } // blue
case LOG_LEVEL_DEBUG: { return 0x0000FF00; } // green
case LOG_LEVEL_INFO: { return 0x00000000; } // black
case LOG_LEVEL_WARN: { return 0x000080FF; } // orange
case LOG_LEVEL_ERROR: { return 0x000000FF; } // red
- default: { return 0x00000000; } // black
+ default: { return 0x00000000; } // black
}
}
diff --git a/soyuz/soyuz.cc b/soyuz/soyuz.cc
index db88b8a..dd1a32b 100644
--- a/soyuz/soyuz.cc
+++ b/soyuz/soyuz.cc
@@ -7,8 +7,6 @@
* @date 2021. August. 18.
*/
-#include <soyuz/soyuz.hh>
-
#pragma comment(lib, "ntdll.lib")
#include <fmt/format.h>
@@ -39,7 +37,6 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) {
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);
@@ -47,6 +44,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) {
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(255, 255, 255)));
+
if (!RegisterClassEx(&wincl)) { return 0; }
window = CreateWindowEx(
@@ -63,6 +61,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) {
instance,
nullptr
);
+
InitNotifyIconData();
ShowWindow(window, show);
@@ -74,18 +73,45 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) {
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");
+ 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::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");
+
+ 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()) {
/**
@@ -94,14 +120,22 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) {
* 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");
+ 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,
+ "unable to close lunar client's discord ipc named pipe"
+ );
}
}
}};
diff --git a/soyuz/tray.cc b/soyuz/tray.cc
index a1e914e..6d10279 100644
--- a/soyuz/tray.cc
+++ b/soyuz/tray.cc
@@ -16,68 +16,75 @@
#include <soyuz/resource.hh>
#include <soyuz/soyuz.hh>
-UINT WM_TASKBAR = 0;
+UINT WM_TASKBAR = 0;
HWND window;
HMENU menu;
NOTIFYICONDATA data;
-TCHAR tip[64] = TEXT(WINDOW_TRAY_NAME);
+TCHAR tip[64] = TEXT(WINDOW_TRAY_NAME);
char class_name[] = WINDOW_TRAY_NAME;
std::vector<soyuz::log_t> logs;
-LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+auto CALLBACK WindowProcedure(
+ HWND hwnd,
+ UINT message,
+ WPARAM wParam,
+ LPARAM lParam
+) -> LRESULT {
if (message == WM_TASKBAR && !IsWindowVisible(window)) {
- minimize(); return 0;
+ minimize();
+
+ return 0;
}
switch (message) {
- case WM_ACTIVATE:
- Shell_NotifyIcon(NIM_ADD, &data);
- break;
+ case WM_ACTIVATE: { Shell_NotifyIcon(NIM_ADD, &data); } break;
case WM_PAINT: {
PAINTSTRUCT ps;
RECT rect;
HDC hdc = BeginPaint(window, &ps);
+ int height = 5;
GetClientRect(hwnd, &rect);
- int height = 5;
- for (soyuz::log_t &i : logs) {
- SetTextColor(hdc, i.to_colorref());
- TextOut(hdc, 5, height, i.value.c_str(), (int)strlen(i.value.c_str()));
+ for (soyuz::log_t &log : logs) {
+ SetTextColor(hdc, log.to_colorref());
+ TextOut(
+ hdc,
+ 5,
+ height,
+ log.value.c_str(),
+ (int)strlen(log.value.c_str())
+ );
+
height += 20;
}
EndPaint(window, &ps);
} break;
- case WM_CREATE:
+ case WM_CREATE: {
ShowWindow(window, SW_HIDE);
+
menu = CreatePopupMenu();
- AppendMenu(menu, MF_STRING, ID_TRAY_EXIT, TEXT("Exit Soyuz"));
- break;
+ AppendMenu(menu, MF_STRING, ID_TRAY_EXIT, TEXT("Exit Soyuz"));
+ } break;
case WM_SYSCOMMAND:
switch(wParam & 0xFFF0) {
case SC_MINIMIZE:
- case SC_CLOSE:
- minimize(); return 0; // break;
+ case SC_CLOSE: { minimize(); return 0; } // break;
} break;
case WM_SYSICON: {
- switch (wParam) {
- case ID_TRAY_APP_ICON:
- SetForegroundWindow(window);
- break;
-
- default: ;
- }
+ if (wParam == ID_TRAY_APP_ICON) { SetForegroundWindow(window); }
if (lParam == WM_LBUTTONUP) {
restore();
} else if (lParam == WM_RBUTTONDOWN) {
POINT curPoint;
+
GetCursorPos(&curPoint);
SetForegroundWindow(window);
@@ -92,6 +99,7 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM
);
SendMessage(hwnd, WM_NULL, 0, 0);
+
if (clicked == ID_TRAY_EXIT) {
Shell_NotifyIcon(NIM_DELETE, &data);
PostQuitMessage(0);
@@ -101,6 +109,7 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM
case WM_NCHITTEST: {
LRESULT uHitTest = DefWindowProc(hwnd, WM_NCHITTEST, wParam, lParam);
+
if (uHitTest == HTCLIENT) {
return HTCAPTION;
} else {
@@ -108,13 +117,9 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM
}
}
- case WM_CLOSE:
- minimize(); return 0; // break;
-
- case WM_DESTROY:
- PostQuitMessage (0); break;
-
- default: ;
+ case WM_CLOSE: { minimize(); return 0; } // break;
+ case WM_DESTROY: { PostQuitMessage(0); } break;
+ default: {} break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
@@ -132,17 +137,19 @@ void InitNotifyIconData() {
data.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
data.uCallbackMessage = WM_SYSICON;
data.hIcon = (HICON)LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1));
+
strncpy_s(data.szTip, tip, sizeof(tip));
}
namespace soyuz {
-auto log(const std::string &message) -> void {
+[[maybe_unused]] auto log(const std::string &message) -> void {
if (logs.size() == 16) { logs.erase(logs.begin()); }
std::string to_log = fmt::format("[{}] {}", current_date_time(), message);
- logs.emplace_back(log_t(log_level::LOG_LEVEL_INFO, to_log)); write_log_file(to_log);
+ logs.emplace_back(log_t(log_level::LOG_LEVEL_INFO, to_log));
+ write_log_file(to_log);
RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW);
}
@@ -151,7 +158,8 @@ auto log(log_level level, const std::string &message) -> void {
std::string to_log = fmt::format("[{}] {}", current_date_time(), message);
- logs.emplace_back(log_t(level, to_log)); write_log_file(to_log);
+ logs.emplace_back(log_t(level, to_log));
+ write_log_file(to_log);
RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW);
}