From 9eede46b61940427f5fbbfd7544b9ab8a2b8883d Mon Sep 17 00:00:00 2001 From: redraskal Date: Sat, 2 Sep 2023 14:49:51 -0500 Subject: [PATCH] chore: substring path starting from end, publish c file --- .gitignore | 2 +- OBSReplayFolders.lua | 13 +++--- detect_game.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 detect_game.c diff --git a/.gitignore b/.gitignore index 4fb8064..0cbcaa3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -*.c *.cmd +*.zip # Compiled Lua sources luac.out diff --git a/OBSReplayFolders.lua b/OBSReplayFolders.lua index 35c3170..249982a 100644 --- a/OBSReplayFolders.lua +++ b/OBSReplayFolders.lua @@ -49,19 +49,16 @@ function get_running_game_title() if len == 0 then return nil end - local title = "" - local i = 1 local max = len - 4 - while i <= max do + local i = max + while i > 1 do local char = result:sub(i, i) if char == "\\" then - title = "" - else - title = title .. char + break end - i = i + 1 + i = i - 1 end - return title + return result:sub(i + 1, max) end function move(path, folder) diff --git a/detect_game.c b/detect_game.c new file mode 100644 index 0000000..35ba7fe --- /dev/null +++ b/detect_game.c @@ -0,0 +1,95 @@ +#include "pch.h" + +BOOL APIENTRY DllMain( HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} + +#include +#include +#include +#include + +#define MAX_TITLE_LENGTH 256 +static const char prefix[] = "C:\\Windows\\"; +static const char gameBarExe[] = "GameBar.exe"; + +BOOL TestFullscreen(HWND hwnd) { + WINDOWPLACEMENT wp; + wp.length = sizeof(WINDOWPLACEMENT); + if (!GetWindowPlacement(hwnd, &wp)) + return FALSE; + if (IsZoomed(hwnd)) + return TRUE; + RECT rcDesktop; + GetClientRect(GetDesktopWindow(), &rcDesktop); + return (wp.rcNormalPosition.left <= rcDesktop.left && + wp.rcNormalPosition.top <= rcDesktop.top && + wp.rcNormalPosition.right >= rcDesktop.right && + wp.rcNormalPosition.bottom >= rcDesktop.bottom); +} + +void ConvertTCHARToChar(const TCHAR* source, char* dest, size_t destSize) { +#ifdef _WIN32 + wcstombs_s(NULL, dest, destSize, source, _TRUNCATE); +#else + wcstombs(dest, source, destSize); +#endif +} + +__declspec(dllexport) int get_running_fullscreen_game_path(char* buffer, int bufferSize) { + HWND hwnd = NULL; + while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) { + if (TestFullscreen(hwnd)) { + TCHAR windowTitle[MAX_TITLE_LENGTH]; + GetWindowText(hwnd, windowTitle, MAX_TITLE_LENGTH); + + DWORD processId; + GetWindowThreadProcessId(hwnd, &processId); + + HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId); + if (hProcess == NULL) { + return 1; + } + + TCHAR executablePath[MAX_PATH]; + if (GetModuleFileNameEx(hProcess, NULL, executablePath, MAX_PATH) == 0) { + CloseHandle(hProcess); + return 1; + } + + CloseHandle(hProcess); + size_t exe_bufferSize = sizeof(executablePath) / sizeof(executablePath[0]); + + char* charPath = (char*)malloc(exe_bufferSize); + + ConvertTCHARToChar(executablePath, charPath, exe_bufferSize); + int result = strncmp(charPath, prefix, 11); + if (result == 0) { + continue; + } + + result = strcmp(charPath + strlen(charPath) - 11, gameBarExe); + if (result == 0) { + continue; + } + + strcpy_s(buffer, bufferSize, charPath); // Use charPath as a regular char array + free(charPath); + return 0; + + } + } + return 1; +}