Skip to content

Commit

Permalink
refactor clrhost
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciremun committed May 10, 2024
1 parent c84b46d commit 9a337a7
Show file tree
Hide file tree
Showing 13 changed files with 768 additions and 704 deletions.
832 changes: 683 additions & 149 deletions freedom/clrhost.cpp

Large diffs are not rendered by default.

64 changes: 31 additions & 33 deletions freedom/features/discord_rpc.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
#include "features/discord_rpc.h"

DWORD discord_rich_presence_code_start = 0;
DWORD discord_rich_presence_jump_back = 0;
DWORD discord_rich_presence_state_string_ptr = NULL;
DWORD discord_rich_presence_large_text_string_ptr = NULL;
DWORD discord_rich_presence_small_text_string_ptr = NULL;
intptr_t drpc_code_start = 0;
intptr_t drpc_jump_back = 0;
intptr_t drpc_state_string_ptr = NULL;
intptr_t drpc_state_string_gc_handle = NULL;
intptr_t drpc_large_text_string_ptr = NULL;
intptr_t drpc_large_text_string_gc_handle = NULL;
intptr_t drpc_small_text_string_ptr = NULL;
intptr_t drpc_small_text_string_gc_handle = NULL;

Hook<Detour32> DiscordRichPresenceHook;

void init_discord_rpc()
{
VARIANT v = invoke_csharp_method(L"Freedom.Utils", L"GetSetPresencePtr");
if (variant_ok(v))
drpc_code_start = get_set_presence_ptr();
if (drpc_code_start)
{
discord_rich_presence_code_start = v.intVal;
discord_rich_presence_jump_back = discord_rich_presence_code_start + 0x5;
}
if (discord_rich_presence_code_start)
{
DiscordRichPresenceHook = Hook<Detour32>(discord_rich_presence_code_start, (BYTE *)set_discord_rich_presence, 5);
if (cfg_discord_rich_presence_enabled)
drpc_jump_back = drpc_code_start + 0x5;
DiscordRichPresenceHook = Hook<Detour32>(drpc_code_start, (BYTE *)set_drpc, 5);
if (cfg_drpc_enabled)
{
enable_discord_rich_presence_hooks();
enable_drpc_hooks();

if (cfg_discord_rich_presence_state[0] != '\0')
set_discord_rpc_str(discord_rich_presence_state_wchar, cfg_discord_rich_presence_state, &discord_rich_presence_state_string_ptr);
if (cfg_drpc_state[0] != '\0')
set_discord_rpc_str(drpc_state_wchar, cfg_drpc_state, &drpc_state_string_ptr, &drpc_state_string_gc_handle);

if (cfg_discord_rich_presence_large_text[0] != '\0')
set_discord_rpc_str(discord_rich_presence_large_text_wchar, cfg_discord_rich_presence_large_text, &discord_rich_presence_large_text_string_ptr);
if (cfg_drpc_large_text[0] != '\0')
set_discord_rpc_str(drpc_large_text_wchar, cfg_drpc_large_text, &drpc_large_text_string_ptr, &drpc_large_text_string_gc_handle);

if (cfg_discord_rich_presence_small_text[0] != '\0')
set_discord_rpc_str(discord_rich_presence_small_text_wchar, cfg_discord_rich_presence_small_text, &discord_rich_presence_small_text_string_ptr);
if (cfg_drpc_small_text[0] != '\0')
set_discord_rpc_str(drpc_small_text_wchar, cfg_drpc_small_text, &drpc_small_text_string_ptr, &drpc_small_text_string_gc_handle);
}
}
}

void set_discord_rpc_str(wchar_t *w_str, char *c_str, DWORD *output_str_ptr)
void set_discord_rpc_str(wchar_t *w_str, char *c_str, intptr_t *output_str_ptr, intptr_t *output_str_gc_handle)
{
invoke_csharp_method(L"Freedom.Utils", L"FreeCSharpString", w_str);
if (*output_str_gc_handle)
free_managed_string(*output_str_gc_handle);
int wchars_count = MultiByteToWideChar(CP_UTF8, 0, c_str, -1, NULL, 0);
int bytes_written = MultiByteToWideChar(CP_UTF8, 0, c_str, -1, w_str, wchars_count);
w_str[bytes_written] = '\0';
VARIANT v = invoke_csharp_method(L"Freedom.Utils", L"GetCSharpStringPtr", w_str);
if (variant_ok(v))
*output_str_ptr = v.intVal;
*output_str_ptr = allocate_managed_string(w_str, output_str_gc_handle);
}

void enable_discord_rich_presence_hooks()
void enable_drpc_hooks()
{
DiscordRichPresenceHook.Enable();
}

void disable_discord_rich_presence_hooks()
void disable_drpc_hooks()
{
DiscordRichPresenceHook.Disable();
}

__declspec(naked) void set_discord_rich_presence()
__declspec(naked) void set_drpc()
{
__asm {
push esi
mov eax, discord_rich_presence_state_string_ptr
mov eax, drpc_state_string_ptr
mov dword ptr [edx+0x4], eax
mov esi, discord_rich_presence_large_text_string_ptr
mov esi, drpc_large_text_string_ptr
mov eax, [edx+0x10]
mov dword ptr [eax+0x8], esi
mov esi, discord_rich_presence_small_text_string_ptr
mov esi, drpc_small_text_string_ptr
mov dword ptr [eax+0x10], esi
pop esi
push ebp
mov ebp,esp
push edi
push esi
jmp [discord_rich_presence_jump_back]
jmp [drpc_jump_back]
}
}
2 changes: 1 addition & 1 deletion freedom/scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ void destroy_hooks_except_swap()
disable_replay_hooks();
disable_flashlight_hooks();
disable_score_multiplier_hooks();
disable_discord_rich_presence_hooks();
disable_drpc_hooks();
disable_timewarp_hooks();
disable_hidden_remover_hooks();
disable_nt_user_send_input_patch();
Expand Down
32 changes: 16 additions & 16 deletions freedom/ui/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool cfg_replay_hardrock = false;
int cfg_relax_style = 'a'; // alternate
bool cfg_score_multiplier_enabled = false;
float cfg_score_multiplier_value = 1.f;
bool cfg_discord_rich_presence_enabled = false;
bool cfg_drpc_enabled = false;
bool cfg_flashlight_enabled = false;
bool cfg_timewarp_enabled = false;
double cfg_timewarp_playback_rate = 200.0;
Expand All @@ -22,12 +22,12 @@ bool cfg_aimbot_lock = false;
bool cfg_hidden_remover_enabled = false;
bool cfg_show_debug_log = false;

char cfg_discord_rich_presence_state[512] = {0};
char cfg_discord_rich_presence_large_text[512] = {0};
char cfg_discord_rich_presence_small_text[512] = {0};
wchar_t discord_rich_presence_state_wchar[512] = {0};
wchar_t discord_rich_presence_large_text_wchar[512] = {0};
wchar_t discord_rich_presence_small_text_wchar[512] = {0};
char cfg_drpc_state[512] = {0};
char cfg_drpc_large_text[512] = {0};
char cfg_drpc_small_text[512] = {0};
wchar_t drpc_state_wchar[512] = {0};
wchar_t drpc_large_text_wchar[512] = {0};
wchar_t drpc_small_text_wchar[512] = {0};

const char *get_imgui_ini_filename(HMODULE hMod)
{
Expand Down Expand Up @@ -101,10 +101,10 @@ static void ConfigHandler_WriteAll(ImGuiContext *ctx, ImGuiSettingsHandler *hand
buf->appendf("replay_keys=%d\n", (int)cfg_replay_keys);
buf->appendf("sm_lock=%d\n", (int)cfg_score_multiplier_enabled);
buf->appendf("sm_value=%.2f\n", cfg_score_multiplier_value);
buf->appendf("drpc=%d\n", (int)cfg_discord_rich_presence_enabled);
buf->appendf("drpc_state=%s\n", cfg_discord_rich_presence_state);
buf->appendf("drpc_large=%s\n", cfg_discord_rich_presence_large_text);
buf->appendf("drpc_small=%s\n", cfg_discord_rich_presence_small_text);
buf->appendf("drpc=%d\n", (int)cfg_drpc_enabled);
buf->appendf("drpc_state=%s\n", cfg_drpc_state);
buf->appendf("drpc_large=%s\n", cfg_drpc_large_text);
buf->appendf("drpc_small=%s\n", cfg_drpc_small_text);
buf->appendf("fl=%d\n", (int)cfg_flashlight_enabled);
buf->appendf("hd=%d\n", (int)cfg_hidden_remover_enabled);
buf->appendf("tw_lock=%d\n", (int)cfg_timewarp_enabled);
Expand All @@ -117,7 +117,7 @@ static void ConfigHandler_WriteAll(ImGuiContext *ctx, ImGuiSettingsHandler *hand
static void ConfigHandler_ReadLine(ImGuiContext *, ImGuiSettingsHandler *, void *, const char *line)
{
int ar_lock_i, cs_lock_i, od_lock_i, mod_menu_visible_i, font_size_i,
relax_lock_i, aimbot_lock_i, spins_per_minute_i, discord_rich_presence_enabled_i,
relax_lock_i, aimbot_lock_i, spins_per_minute_i, drpc_enabled_i,
hidden_remover_enabled_i, flashlight_enabled_i, timewarp_enabled_i, relax_checks_od_i,
jump_window_i, replay_i, replay_aim_i, replay_keys_i, score_multiplier_i,
show_debug_i;
Expand All @@ -143,10 +143,10 @@ static void ConfigHandler_ReadLine(ImGuiContext *, ImGuiSettingsHandler *, void
else if (sscanf(line, "replay_keys=%d", &replay_keys_i) == 1) cfg_replay_keys = replay_keys_i;
else if (sscanf(line, "sm_lock=%d", &score_multiplier_i) == 1) cfg_score_multiplier_enabled = score_multiplier_i;
else if (sscanf(line, "sm_value=%f", &score_multiplier_value_f) == 1) cfg_score_multiplier_value = score_multiplier_value_f;
else if (sscanf(line, "drpc=%d", &discord_rich_presence_enabled_i) == 1) cfg_discord_rich_presence_enabled = discord_rich_presence_enabled_i;
else if (sscanf(line, "drpc_state=%511[^\n]", cfg_discord_rich_presence_state) == 1) {}
else if (sscanf(line, "drpc_large=%511[^\n]", cfg_discord_rich_presence_large_text) == 1) {}
else if (sscanf(line, "drpc_small=%511[^\n]", cfg_discord_rich_presence_small_text) == 1) {}
else if (sscanf(line, "drpc=%d", &drpc_enabled_i) == 1) cfg_drpc_enabled = drpc_enabled_i;
else if (sscanf(line, "drpc_state=%511[^\n]", cfg_drpc_state) == 1) {}
else if (sscanf(line, "drpc_large=%511[^\n]", cfg_drpc_large_text) == 1) {}
else if (sscanf(line, "drpc_small=%511[^\n]", cfg_drpc_small_text) == 1) {}
else if (sscanf(line, "fl=%d", &flashlight_enabled_i) == 1) cfg_flashlight_enabled = flashlight_enabled_i;
else if (sscanf(line, "hd=%d", &hidden_remover_enabled_i) == 1) cfg_hidden_remover_enabled = hidden_remover_enabled_i;
else if (sscanf(line, "tw_lock=%d", &timewarp_enabled_i) == 1) cfg_timewarp_enabled = timewarp_enabled_i;
Expand Down
27 changes: 13 additions & 14 deletions freedom/ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void update_ui()
selected_replay_offset ? update_tab("Replay", MenuTab::Replay, cfg_replay_enabled) : inactive_tab("Replay");

update_tab("Mods", MenuTab::Mods, cfg_flashlight_enabled || cfg_hidden_remover_enabled || cfg_score_multiplier_enabled);
update_tab("Misc", MenuTab::Misc, cfg_discord_rich_presence_enabled);
update_tab("Misc", MenuTab::Misc, cfg_drpc_enabled);
update_tab("About", MenuTab::About);

if (ImGui::Selectable("Debug", false, ImGuiSelectableFlags_DontClosePopups))
Expand Down Expand Up @@ -312,7 +312,6 @@ void update_ui()
ImGui::PopItemWidth();
if (ImGui::IsItemDeactivatedAfterEdit())
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
ImGui::SetCursorPosY(ImGui::GetWindowHeight() - ImGui::GetFrameHeightWithSpacing());
}
if (selected_tab == MenuTab::Timewarp)
{
Expand Down Expand Up @@ -422,36 +421,36 @@ void update_ui()
ImGui::Text("Discord RPC Settings");
ImGui::Dummy(ImVec2(.0f, 5.f));

if (ImGui::Checkbox("Enable", &cfg_discord_rich_presence_enabled))
if (ImGui::Checkbox("Enable", &cfg_drpc_enabled))
{
cfg_discord_rich_presence_enabled ? enable_discord_rich_presence_hooks() : disable_discord_rich_presence_hooks();
cfg_drpc_enabled ? enable_drpc_hooks() : disable_drpc_hooks();
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
}

ImGui::Dummy(ImVec2(.0f, 5.f));

if (!cfg_discord_rich_presence_enabled)
if (!cfg_drpc_enabled)
{
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleColor(ImGuiCol_Text, ITEM_DISABLED);
}
ImGui::PushItemWidth(ImGui::GetFontSize() * 16.f);
if (ImGui::InputTextEx("##rpc_state", "State", cfg_discord_rich_presence_state, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
if (ImGui::InputTextEx("##rpc_state", "State", cfg_drpc_state, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
set_discord_rpc_str(discord_rich_presence_state_wchar, cfg_discord_rich_presence_state, &discord_rich_presence_state_string_ptr);
set_discord_rpc_str(drpc_state_wchar, cfg_drpc_state, &drpc_state_string_ptr, &drpc_state_string_gc_handle);
}
if (ImGui::InputTextEx("##rpc_large_text", "Large Text", cfg_discord_rich_presence_large_text, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
if (ImGui::InputTextEx("##rpc_large_text", "Large Text", cfg_drpc_large_text, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
set_discord_rpc_str(discord_rich_presence_large_text_wchar, cfg_discord_rich_presence_large_text, &discord_rich_presence_large_text_string_ptr);
set_discord_rpc_str(drpc_large_text_wchar, cfg_drpc_large_text, &drpc_large_text_string_ptr, &drpc_large_text_string_gc_handle);
}
if (ImGui::InputTextEx("##rpc_small_text", "Small Text", cfg_discord_rich_presence_small_text, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
if (ImGui::InputTextEx("##rpc_small_text", "Small Text", cfg_drpc_small_text, 512, ImVec2(0, 0), ImGuiInputTextFlags_None))
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
set_discord_rpc_str(discord_rich_presence_small_text_wchar, cfg_discord_rich_presence_small_text, &discord_rich_presence_small_text_string_ptr);
set_discord_rpc_str(drpc_small_text_wchar, cfg_drpc_small_text, &drpc_small_text_string_ptr, &drpc_small_text_string_gc_handle);
}
if (!cfg_discord_rich_presence_enabled)
if (!cfg_drpc_enabled)
{
ImGui::PopStyleColor();
ImGui::PopItemFlag();
Expand Down Expand Up @@ -662,7 +661,7 @@ void draw_debug_log()
colored_if_null("Osu Username", osu_username_code_start);
colored_if_null("Window Manager", window_manager_code_start);
colored_if_null("Score Multiplier", score_multiplier_code_start);
colored_if_null("Discord RPC", discord_rich_presence_code_start);
colored_if_null("Discord RPC", drpc_code_start);
colored_if_null("Check Flashlight", check_flashlight_code_start);
colored_if_null("Update Flashlight", update_flashlight_code_start);
colored_if_null("Update Timing", update_timing_code_start);
Expand Down Expand Up @@ -731,7 +730,7 @@ void draw_debug_log()
colored_if_null("AR", ar_hook_jump_back);
colored_if_null("CS", cs_hook_jump_back);
colored_if_null("OD", od_hook_jump_back);
colored_if_null("Discord RPC", discord_rich_presence_jump_back);
colored_if_null("Discord RPC", drpc_jump_back);
colored_if_null("Beatmap Onload", beatmap_onload_hook_jump_back);
colored_if_null("Check Timewarp 1", check_timewarp_hook_1_jump_back);
colored_if_null("Check Timewarp 2", check_timewarp_hook_2_jump_back);
Expand Down
Loading

0 comments on commit 9a337a7

Please sign in to comment.