Skip to content

Commit

Permalink
save AR Lock and AR Value, #3 init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciremun committed Jul 1, 2022
1 parent 1ddc3ea commit b3912c5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
58 changes: 58 additions & 0 deletions freedom/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,61 @@

bool ar_lock = true;
float ar_value = 10.0f;

const char *get_imgui_ini_filename(HMODULE hMod)
{
static wchar_t module_path[MAX_PATH * 2];
DWORD module_path_length = GetModuleFileNameW(hMod, module_path, MAX_PATH * 2);
if (module_path_length == 0)
return 0;

static char module_path_u8[MAX_PATH * 2];
int module_path_u8_length = WideCharToMultiByte(CP_UTF8, 0, module_path, module_path_length, module_path_u8, MAX_PATH * 2, 0, 0);
if (module_path_u8_length == 0)
return 0;

module_path_u8[module_path_u8_length] = '\0';

DWORD backslash_index = module_path_u8_length - 1;
while (backslash_index)
if (module_path_u8[--backslash_index] == '\\')
break;

memcpy(module_path_u8 + backslash_index + 1, "freedom.ini", 12);

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_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->append("\n");
}

static void FreedomHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void*, const char* line)
{
int ar_lock_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; }
}

void set_imgui_ini_handler()
{
ImGuiSettingsHandler ini_handler;
ini_handler.TypeName = "Freedom";
ini_handler.TypeHash = ImHashStr("Freedom");
ini_handler.ClearAllFn = FreedomHandler_ClearAll;
ini_handler.ReadOpenFn = FreedomHandler_ReadOpen;
ini_handler.ReadLineFn = FreedomHandler_ReadLine;
ini_handler.ApplyAllFn = FreedomHandler_ApplyAll;
ini_handler.WriteAllFn = FreedomHandler_WriteAll;
ImGui::AddSettingsHandler(&ini_handler);
}
8 changes: 7 additions & 1 deletion freedom/freedom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

HWND g_hwnd = NULL;
HANDLE g_process = NULL;
HMODULE g_module = NULL;

WNDPROC oWndProc;
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
Expand Down Expand Up @@ -62,7 +63,9 @@ BOOL __stdcall freedom_update(HDC hDc)
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = NULL;

set_imgui_ini_handler();
io.IniFilename = get_imgui_ini_filename(g_module);

ImFontConfig config;
config.OversampleH = config.OversampleV = 1;
Expand Down Expand Up @@ -260,8 +263,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
g_module = hModule;
CloseHandle(CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)freedom_main,
hModule, 0, nullptr));
} break;
default:
break;
}
Expand Down
8 changes: 8 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#pragma once

#include <windows.h>

#include "imgui.h"
#include "imgui_internal.h"

extern bool ar_lock;
extern float ar_value;

const char *get_imgui_ini_filename(HMODULE hMod);
void set_imgui_ini_handler();

0 comments on commit b3912c5

Please sign in to comment.