Skip to content

Commit

Permalink
free strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciremun committed Dec 31, 2023
1 parent 9f220b3 commit e46e1fc
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 194 deletions.
189 changes: 0 additions & 189 deletions freedom/baked_utils_dll.c

This file was deleted.

2 changes: 1 addition & 1 deletion freedom/clrhost.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "clrhost.h"
#include "baked_utils_dll.c"
#include "baked_utils_dll.h"

#import "mscorlib.tlb" auto_rename
using namespace mscorlib;
Expand Down
3 changes: 3 additions & 0 deletions freedom/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ void update_ui()
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
static wchar_t discord_rich_presence_state_wchar[512] = {0};
invoke_csharp_method(L"Freedom.Utils", L"FreeCSharpString", discord_rich_presence_state_wchar);
int wchars_count = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_state, -1, NULL, 0);
int bytes_written = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_state, -1, discord_rich_presence_state_wchar, wchars_count);
discord_rich_presence_state_wchar[bytes_written] = '\0';
Expand All @@ -387,6 +388,7 @@ void update_ui()
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
static wchar_t discord_rich_presence_large_text_wchar[512] = {0};
invoke_csharp_method(L"Freedom.Utils", L"FreeCSharpString", discord_rich_presence_large_text_wchar);
int wchars_count = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_large_text, -1, NULL, 0);
int bytes_written = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_large_text, -1, discord_rich_presence_large_text_wchar, wchars_count);
discord_rich_presence_large_text_wchar[bytes_written] = '\0';
Expand All @@ -400,6 +402,7 @@ void update_ui()
{
ImGui::SaveIniSettingsToDisk(ImGui::GetIO().IniFilename);
static wchar_t discord_rich_presence_small_text_wchar[512] = {0};
invoke_csharp_method(L"Freedom.Utils", L"FreeCSharpString", discord_rich_presence_small_text_wchar);
int wchars_count = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_small_text, -1, NULL, 0);
int bytes_written = MultiByteToWideChar(CP_UTF8, 0, discord_rich_presence_small_text, -1, discord_rich_presence_small_text_wchar, wchars_count);
discord_rich_presence_small_text_wchar[bytes_written] = '\0';
Expand Down
22 changes: 19 additions & 3 deletions freedom/utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Freedom
{
Expand Down Expand Up @@ -34,17 +35,33 @@ public struct ClassMethod
new ClassMethod {c = "#=zE0VDZfwJEH3z6D3XGqmTkDRFkwfG", m = "", t = ClassMethodType.UpdateVariables},
};

static Dictionary<String, GCHandle> CSharpStringHandles = new Dictionary<String, GCHandle>();

public static int GetSetPresencePtr()
{
return (int)Assembly.GetEntryAssembly().GetType("DiscordRPC.DiscordRpcClient").GetMethod("SetPresence").MethodHandle.GetFunctionPointer();
}

public static int GetCSharpStringPtr(String s)
{
if (CSharpStringHandles.ContainsKey(s))
return (int)(CSharpStringHandles[s].AddrOfPinnedObject() - 0x8);
GCHandle handle = GCHandle.Alloc(s, GCHandleType.Pinned);
CSharpStringHandles.Add(s, handle);
return (int)(handle.AddrOfPinnedObject() - 0x8);
}

public static int FreeCSharpString(String s)
{
if (CSharpStringHandles.ContainsKey(s))
{
CSharpStringHandles[s].Free();
CSharpStringHandles.Remove(s);
return 1;
}
return 0;
}

public static int SetClassMethod(String classmethod)
{
try
Expand All @@ -68,7 +85,6 @@ public static int SetClassMethod(String classmethod)
}
}
catch (Exception e) {
Console.WriteLine(e);
return 0;
}
return 1;
Expand Down Expand Up @@ -133,7 +149,7 @@ unsafe public static int ClassMethodsFromAddrs(Int32 cms_intptr, Int32 size)
}
}
}
catch (Exception e) { Console.WriteLine(e); }
catch (Exception e) {}
return 1;
}

Expand Down Expand Up @@ -166,7 +182,7 @@ public static int prepare_all_methods_fast()
if (m == null)
m = c.GetMethod(cm.m, BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(m.MethodHandle);
} catch (Exception e) { Console.WriteLine(e); ret = 0; }
} catch (Exception e) { ret = 0; }
}
return ret;
}
Expand Down
Loading

0 comments on commit e46e1fc

Please sign in to comment.