Skip to content

Commit

Permalink
settings autosave, fixes, save menu visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciremun committed Jul 1, 2022
1 parent b3912c5 commit 89fedb6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
22 changes: 14 additions & 8 deletions freedom/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions freedom/detours.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "config.h"
#include "detours.h"
#include "hook.h"
#include "dotnet_data_collector.h"

twglSwapBuffers wglSwapBuffersGateway;
void_trampoline ar_trampoline;
Expand All @@ -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;

Expand Down
43 changes: 27 additions & 16 deletions freedom/freedom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ 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();

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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
6 changes: 6 additions & 0 deletions include/detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <windows.h>

#include "config.h"
#include "hook.h"
#include "dotnet_data_collector.h"

typedef BOOL(__stdcall *twglSwapBuffers)(HDC hDc);
typedef void(__stdcall *void_trampoline)();

Expand All @@ -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();
Expand Down

0 comments on commit 89fedb6

Please sign in to comment.