From 89fedb6fdbbed23a2710137fd2813bd454e60fbc Mon Sep 17 00:00:00 2001 From: Ciremun Date: Fri, 1 Jul 2022 16:55:33 +0300 Subject: [PATCH] settings autosave, fixes, save menu visibility --- freedom/config.cpp | 22 ++++++++++++++-------- freedom/detours.cpp | 5 ++--- freedom/freedom.cpp | 43 +++++++++++++++++++++++++++---------------- include/config.h | 1 + include/detours.h | 6 ++++++ 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/freedom/config.cpp b/freedom/config.cpp index dcdde1a..7893c44 100644 --- a/freedom/config.cpp +++ b/freedom/config.cpp @@ -2,6 +2,7 @@ bool ar_lock = true; float ar_value = 10.0f; +bool mod_menu_visible = true; const char *get_imgui_ini_filename(HMODULE hMod) { @@ -27,25 +28,30 @@ const char *get_imgui_ini_filename(HMODULE hMod) return (const char *)&module_path_u8; } -static void FreedomHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) {} -static void FreedomHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*) {} -static void* FreedomHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { return (void *)1; } +static void FreedomHandler_ClearAll(ImGuiContext *ctx, ImGuiSettingsHandler *) {} +static void FreedomHandler_ApplyAll(ImGuiContext *ctx, ImGuiSettingsHandler *) {} +static void *FreedomHandler_ReadOpen(ImGuiContext *, ImGuiSettingsHandler *, const char *name) { return (void *)1; } -static void FreedomHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) +static void FreedomHandler_WriteAll(ImGuiContext *ctx, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf) { buf->reserve(buf->size() + (1 + 4) * 2); buf->appendf("[%s][%s]\n", handler->TypeName, "Settings"); buf->appendf("ar_lock=%d\n", (int)ar_lock); buf->appendf("ar_value=%.1f\n", ar_value); + buf->appendf("visible=%d\n", mod_menu_visible); buf->append("\n"); } -static void FreedomHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void*, const char* line) +static void FreedomHandler_ReadLine(ImGuiContext *, ImGuiSettingsHandler *, void *, const char *line) { - int ar_lock_i; + int ar_lock_i, mod_menu_visible_i; float ar_value_f; - if (sscanf(line, "ar_lock=%d", &ar_lock_i) == 1) { ar_lock = (bool)ar_lock_i; } - else if (sscanf(line, "ar_value=%f", &ar_value_f) == 1) { ar_value = ar_value_f; } + if (sscanf(line, "ar_lock=%d", &ar_lock_i) == 1) + ar_lock = ar_lock_i; + else if (sscanf(line, "ar_value=%f", &ar_value_f) == 1) + ar_value = ar_value_f; + else if (sscanf(line, "visible=%d", &mod_menu_visible_i) == 1) + mod_menu_visible = mod_menu_visible_i; } void set_imgui_ini_handler() diff --git a/freedom/detours.cpp b/freedom/detours.cpp index 43ac4f6..4678365 100644 --- a/freedom/detours.cpp +++ b/freedom/detours.cpp @@ -1,7 +1,4 @@ -#include "config.h" #include "detours.h" -#include "hook.h" -#include "dotnet_data_collector.h" twglSwapBuffers wglSwapBuffersGateway; void_trampoline ar_trampoline; @@ -12,6 +9,8 @@ uintptr_t parse_beatmap_metadata_jump_back = 0; uintptr_t approach_rate_offset_1 = 0; uintptr_t approach_rate_offset_2 = 0; +Hook SwapBuffersHook; + Hook ApproachRateHook1; Hook ApproachRateHook2; diff --git a/freedom/freedom.cpp b/freedom/freedom.cpp index 20efce2..04a8e6b 100644 --- a/freedom/freedom.cpp +++ b/freedom/freedom.cpp @@ -48,18 +48,16 @@ BOOL __stdcall freedom_update(HDC hDc) static bool ar_hooks_init = false; if (!init) { +#ifndef NDEBUG + AllocConsole(); + FILE* f; + freopen_s(&f, "CONOUT$", "w", stdout); + freopen_s(&f, "CONOUT$", "w", stderr); +#endif // NDEBUG g_process = GetCurrentProcess(); EnumWindows(find_osu_window, GetCurrentProcessId()); oWndProc = (WNDPROC)SetWindowLongPtrA(g_hwnd, GWLP_WNDPROC, (LONG_PTR)WndProc); - ar_hooks_init = init_ar_hooks(); - - if (ar_hooks_init && ar_lock) - enable_ar_hooks(); - - if (!ar_hooks_init) - ar_lock = false; - IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO &io = ImGui::GetIO(); @@ -67,6 +65,8 @@ BOOL __stdcall freedom_update(HDC hDc) set_imgui_ini_handler(); io.IniFilename = get_imgui_ini_filename(g_module); + ImGui::LoadIniSettingsFromDisk(io.IniFilename); + ImFontConfig config; config.OversampleH = config.OversampleV = 1; config.PixelSnapH = true; @@ -79,6 +79,14 @@ BOOL __stdcall freedom_update(HDC hDc) font = io.Fonts->Fonts[3]; + ar_hooks_init = init_ar_hooks(); + + if (ar_hooks_init && ar_lock) + enable_ar_hooks(); + + if (!ar_hooks_init) + ar_lock = false; + ImGui::StyleColorsDark(); ImGui_ImplWin32_Init(g_hwnd); ImGui_ImplOpenGL3_Init(); @@ -110,11 +118,13 @@ BOOL __stdcall freedom_update(HDC hDc) init = true; } - static bool main_window_visible = true; if (GetAsyncKeyState(VK_F11) & 1) - main_window_visible = !main_window_visible; + { + mod_menu_visible = !mod_menu_visible; + ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename); + } - if (!main_window_visible) + if (!mod_menu_visible) return wglSwapBuffersGateway(hDc); ImGui_ImplOpenGL3_NewFrame(); @@ -204,10 +214,15 @@ BOOL __stdcall freedom_update(HDC hDc) else { ImGui::SliderFloat("##AR", &ar_value, 0.0f, 11.0f, "AR: %.1f"); + if (ImGui::IsItemDeactivatedAfterEdit()) + ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename); } ImGui::SameLine(); if (ImGui::Checkbox("##ar_lock", &ar_lock)) + { ar_lock ? enable_ar_hooks() : disable_ar_hooks(); + ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename); + } if (!ar_hooks_init) { @@ -247,11 +262,7 @@ BOOL __stdcall freedom_update(HDC hDc) DWORD WINAPI freedom_main(HMODULE hModule) { - // AllocConsole(); - // FILE* f; - // freopen_s(&f, "CONOUT$", "w", stdout); - - Hook SwapBuffersHook("wglSwapBuffers", "opengl32.dll", (BYTE *)freedom_update, (BYTE *)&wglSwapBuffersGateway, 5); + SwapBuffersHook = Hook("wglSwapBuffers", "opengl32.dll", (BYTE *)freedom_update, (BYTE *)&wglSwapBuffersGateway, 5); SwapBuffersHook.Enable(); return 0; diff --git a/include/config.h b/include/config.h index 2d19ebc..4d8d82d 100644 --- a/include/config.h +++ b/include/config.h @@ -7,6 +7,7 @@ extern bool ar_lock; extern float ar_value; +extern bool mod_menu_visible; const char *get_imgui_ini_filename(HMODULE hMod); void set_imgui_ini_handler(); diff --git a/include/detours.h b/include/detours.h index 1a049c3..cd80a31 100644 --- a/include/detours.h +++ b/include/detours.h @@ -2,6 +2,10 @@ #include +#include "config.h" +#include "hook.h" +#include "dotnet_data_collector.h" + typedef BOOL(__stdcall *twglSwapBuffers)(HDC hDc); typedef void(__stdcall *void_trampoline)(); @@ -11,6 +15,8 @@ extern void_trampoline ar_trampoline; extern uintptr_t parse_beatmap_metadata_code_start; extern uintptr_t parse_beatmap_metadata_jump_back; +extern Hook SwapBuffersHook; + bool init_ar_hooks(); void enable_ar_hooks(); void disable_ar_hooks();