From: Patrick Date: Tue, 3 Oct 2023 18:38:21 +0000 (+0200) Subject: Why didnt I commit these X-Git-Url: https://gitweb.ps.run/autorec/commitdiff_plain/81f259240d157ddcb0cb692826855d7505f4a22f Why didnt I commit these --- diff --git a/build.cmd b/build.cmd index c447ce0..60d3380 100644 --- a/build.cmd +++ b/build.cmd @@ -3,5 +3,5 @@ REM cl src/mongoose.c -c REM rc res/res.rc -cl /EHsc src/main.cpp mongoose.obj /link user32.lib gdi32.lib shell32.lib Shlwapi.lib ws2_32.lib res/res.res -mt -manifest .\main.exe.manifest -outputresource:main.exe;1 \ No newline at end of file +cl /EHsc src/main.cpp src/mongoose.c /Fo"./out"/ /link user32.lib gdi32.lib shell32.lib Shlwapi.lib ws2_32.lib res/res.res /out:out/main.exe +REM mt -manifest res/main.exe.manifest -outputresource:main.exe;1 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4f4a866..7270504 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,10 @@ #define LAY_IMPLEMENTATION #include "layout.h" +#include +#include +#include + #include #include #include @@ -19,6 +23,9 @@ bool recording = false; HANDLE process = NULL; HWND hwnd; HINSTANCE hInstance; +HICON icon_white, icon_green, icon_red; + +std::vector gameExes; NOTIFYICONDATAA niData = { 0 }; @@ -36,9 +43,8 @@ HideNotificationIcon() Shell_NotifyIconA(NIM_DELETE, &niData); } -void changeIcon(HWND hwnd, HINSTANCE hInstance, WORD id) +void changeIcon(HWND hwnd, HICON icon) { - HICON icon = LoadIcon(hInstance, MAKEINTRESOURCE(id)); niData.hIcon = icon; if (! IsWindowVisible(hwnd)) Shell_NotifyIconA(NIM_MODIFY, &niData); @@ -52,14 +58,14 @@ void startRecording() { ws::sendRequest("StartRecord"); - changeIcon(hwnd, hInstance, IDI_ICON_GREEN); + changeIcon(hwnd, icon_green); } void stopRecording() { ws::sendRequest("StopRecord"); - changeIcon(hwnd, hInstance, IDI_ICON_RED); + changeIcon(hwnd, icon_red); } bool @@ -112,13 +118,53 @@ checkForegroundProcess(std::string exeName) HWND fgHwnd = GetForegroundWindow(); HANDLE fgHandle = getHwndProcess(fgHwnd); - char filename[1024]; - int len = GetModuleFileNameExA(fgHandle, NULL, filename, 1024); + char filename[MAX_PATH]; + int len = GetModuleFileNameExA(fgHandle, NULL, filename, MAX_PATH); + + if (strcmp(filename, exeName.c_str()) == 0) + return true; + PathStripPathA(filename); return strcmp(filename, exeName.c_str()) == 0; } +void ReadGameExes() +{ + std::ifstream ifs("games.txt"); + + gameExes.clear(); + + while (ifs) { + std::string str; + std::getline(ifs, str); + if (! str.empty()) + gameExes.push_back(str); + } + + ifs.close(); +} + +void WriteGameExes() +{ + std::ofstream ofs("games.txt", std::ios::trunc); + + for (const auto &exe : gameExes) + { + ofs.write(exe.c_str(), exe.size()); + ofs.write("\n", 1); + } + + ofs.close(); +} + +/* +TODO: + - Disconnect while recording +*/ + + + int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, @@ -128,13 +174,17 @@ WinMain(HINSTANCE hInstance, { hInstance = GetModuleHandle(0); + icon_white = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_WHITE)); + icon_green = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_GREEN)); + icon_red = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_RED)); + win::Window window("Title", "MyWindowClass", hInstance); hwnd = window.hwnd; niData.cbSize = sizeof(niData); niData.uID = 12345; niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_WHITE)); + niData.hIcon = icon_white; niData.hWnd = window.hwnd; niData.uCallbackMessage = ICON_MSG; niData.uVersion = NOTIFYICON_VERSION_4; @@ -176,11 +226,8 @@ WinMain(HINSTANCE hInstance, win::Button btnConnect(&window, "Connect", row1, 100, 25, 0, 0); btnConnect.onClick([&]() { - ws::connect("ws://127.0.0.1:4444"); - }); - win::Button btnTest(&window, "Test", row1, 100, 25, 0, 0); - btnTest.onClick([&]() { - changeIcon(window.hwnd, hInstance, IDI_ICON_GREEN); + if (! ws::isConnected) + ws::connect("ws://127.0.0.1:4455"); }); win::ListBox lstActiveProcesses(&window, row2, 0, 0, 0, LAY_FILL); @@ -193,6 +240,10 @@ WinMain(HINSTANCE hInstance, win::ListBox lstMonitoredProcesses(&window, row2, 0, 0, 0, LAY_FILL); lstMonitoredProcesses.addStyle(WS_VSCROLL); + ReadGameExes(); + for (const auto &exe : gameExes) + lstMonitoredProcesses.addString(exe); + win::Button btnUpdateWindows(&window, "Update", col1, 85, 25, 0, 0); win::Button btnStartMonitoringName(&window, "Exe name >>", col1, 85, 25, 0, 0); win::Button btnStartMonitoringPath(&window, "Full path >>", col1, 85, 25, 0, 0); @@ -220,12 +271,19 @@ WinMain(HINSTANCE hInstance, std::string selStr = lstActiveProcesses.getText(sel); - char *filename = new char[selStr.size()]; + char *filename = new char[selStr.size()+1]; std::memcpy(filename, selStr.c_str(), selStr.size()); + filename[selStr.size()] = '\0'; PathStripPathA(filename); - if (lstMonitoredProcesses.findString(std::string(filename)) == LB_ERR) - lstMonitoredProcesses.addString(std::string(filename)); + std::string filenameStr(filename); + + if (lstMonitoredProcesses.findString(filenameStr) == LB_ERR) + { + lstMonitoredProcesses.addString(filenameStr); + gameExes.push_back(filenameStr); + WriteGameExes(); + } delete[] filename; }); @@ -234,28 +292,47 @@ WinMain(HINSTANCE hInstance, if (sel < 0) return; std::string selStr = lstActiveProcesses.getText(sel); if (lstMonitoredProcesses.findString(selStr) == LB_ERR) - lstMonitoredProcesses.addString(selStr); + { + lstMonitoredProcesses.addString(selStr); + gameExes.push_back(selStr); + WriteGameExes(); + } }); btnStopMonitoring.onClick([&]() { int sel = lstMonitoredProcesses.getSelectedIndex(); if (sel < 0) return; lstMonitoredProcesses.remove(sel); + gameExes.erase(gameExes.begin() + sel); + WriteGameExes(); }); window.show(); window.setDefaultFont(); ws::onConnect = [&]() { - changeIcon(window.hwnd, hInstance, IDI_ICON_RED); + changeIcon(window.hwnd, icon_red); + btnConnect.setActive(false); }; + ws::onClose = [&]() { + changeIcon(window.hwnd, icon_white); + btnConnect.setActive(true); + }; + ws::onError = ws::onClose; ws::init(); - SetTimer(window.hwnd, 10123, 100, [](HWND, UINT, UINT_PTR, DWORD) { + window.setTimer(1000, [&btnConnect]() { + if (! ws::isConnected) + ws::connect("ws://127.0.0.1:4455"); + }); + + window.setTimer(100, []() { if (!recording) { - if (checkForegroundProcess("League of Legends.exe")) { - recording = true; - process = getHwndProcess(GetForegroundWindow()); - startRecording(); + for (auto exe : gameExes) { + if (checkForegroundProcess(exe)) { + recording = true; + process = getHwndProcess(GetForegroundWindow()); + startRecording(); + } } } else { if (!checkProcessRunning(process)) { diff --git a/src/win.h b/src/win.h index b8763ea..0a734c9 100644 --- a/src/win.h +++ b/src/win.h @@ -1,7 +1,3 @@ -#pragma comment(linker, "\"/manifestdependency:type='win32' \ -name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ -processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") - #include #include @@ -42,6 +38,10 @@ namespace win { SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style)); } + void setActive(bool active) + { + EnableWindow(hwnd, active); + } }; struct Window : Hwnd { @@ -95,6 +95,12 @@ namespace win std::vector< std::function>> handlers; + struct Timer { + bool active = true; + std::function f; + }; + std::vector timers; + Window(std::string title, std::string className, HINSTANCE hInstance) { WNDCLASSEXA wc; @@ -155,6 +161,19 @@ namespace win }, 0); } + void setTimer(UINT interval, std::function cb) + { + SetTimer(this->hwnd, timers.size() + 1000, interval, [](HWND hwnd, UINT uMsg, UINT_PTR uIdEvent, DWORD dwTime) { + Window *window = (Window*)GetWindowLongPtrA(hwnd, 0); + if (window == nullptr) + return; + + window->timers[uIdEvent-1000].f(); + }); + Timer t; + t.f = cb; + timers.push_back(t); + } }; struct Button : Hwnd diff --git a/src/ws.h b/src/ws.h index 4bebc1f..d3acb27 100644 --- a/src/ws.h +++ b/src/ws.h @@ -11,13 +11,16 @@ namespace ws mg_mgr mgr; mg_connection *c = nullptr; - bool done = false; + bool isConnected = false; std::function onConnect; + std::function onClose; + std::function onError; static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_WS_OPEN) { puts("Open"); + isConnected = true; } else if (ev == MG_EV_WS_MSG) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; printf("Msg: %.*s\n", (int)wm->data.len, wm->data.ptr); @@ -45,11 +48,15 @@ namespace ws if (ev == MG_EV_ERROR) { printf("Error: %s\n", (char*)ev_data); - done = true; + isConnected = false; + if (onError) + onError(); } if (ev == MG_EV_CLOSE) { puts("Close"); - done = true; + isConnected = false; + if (onClose) + onClose(); } } @@ -65,7 +72,7 @@ namespace ws void update() { - if (c && !done) + if (c) mg_mgr_poll(&mgr, 10); }