Skip to content

Commit

Permalink
Fixed UTF16 Mono strings (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
slxdy committed Jun 11, 2024
1 parent ff5e72a commit 1a6cb1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
17 changes: 13 additions & 4 deletions TweaksLauncher.Game/Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace TweaksLauncher;

internal static unsafe class Mono
{
public static void Init()
public static nint Domain { get; private set; }

public static void Init(nint domainPtr)
{
var lib = NativeLibrary.Load(Program.runtimePath);

Expand All @@ -13,9 +15,16 @@ public static void Init()
mono_class_from_name = Marshal.GetDelegateForFunctionPointer<mono_class_from_name_sig>(NativeLibrary.GetExport(lib, "mono_class_from_name"));
mono_class_get_method_from_name = Marshal.GetDelegateForFunctionPointer<mono_class_get_method_from_name_sig>(NativeLibrary.GetExport(lib, "mono_class_get_method_from_name"));
mono_runtime_invoke = Marshal.GetDelegateForFunctionPointer<mono_runtime_invoke_sig>(NativeLibrary.GetExport(lib, "mono_runtime_invoke"));
mono_string_new = Marshal.GetDelegateForFunctionPointer<mono_string_new_sig>(NativeLibrary.GetExport(lib, "mono_string_new"));
mono_string_new_utf16 = Marshal.GetDelegateForFunctionPointer<mono_string_new_utf16_sig>(NativeLibrary.GetExport(lib, "mono_string_new_utf16"));
mono_add_internal_call = Marshal.GetDelegateForFunctionPointer<mono_add_internal_call_sig>(NativeLibrary.GetExport(lib, "mono_add_internal_call"));
mono_string_to_utf16 = Marshal.GetDelegateForFunctionPointer<mono_string_to_utf16_sig>(NativeLibrary.GetExport(lib, "mono_string_to_utf16"));

Domain = domainPtr;
}

public static nint NewString(string ourString)
{
return mono_string_new_utf16(Domain, ourString, ourString.Length);
}

#pragma warning disable IDE1006 // Naming Styles
Expand All @@ -24,7 +33,7 @@ public static void Init()
internal static mono_class_from_name_sig mono_class_from_name { get; private set; } = null!;
internal static mono_class_get_method_from_name_sig mono_class_get_method_from_name { get; private set; } = null!;
internal static mono_runtime_invoke_sig mono_runtime_invoke { get; private set; } = null!;
internal static mono_string_new_sig mono_string_new { get; private set; } = null!;
internal static mono_string_new_utf16_sig mono_string_new_utf16 { get; private set; } = null!;
internal static mono_add_internal_call_sig mono_add_internal_call { get; private set; } = null!;
internal static mono_string_to_utf16_sig mono_string_to_utf16 { get; private set; } = null!;
#pragma warning restore IDE1006 // Naming Styles
Expand All @@ -45,7 +54,7 @@ public static void Init()
internal delegate nint mono_runtime_invoke_sig(nint method, nint obj, nint* args, nint* exception);

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
internal delegate nint mono_string_new_sig(nint domain, string value);
internal delegate nint mono_string_new_utf16_sig(nint domain, string value, int length);

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
internal delegate nint mono_add_internal_call_sig(string methodName, nint func);
Expand Down
14 changes: 7 additions & 7 deletions TweaksLauncher.Game/MonoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static int Start()
return GameManager.Start();
}

private static void LoadModHandler(nint domain)
private static void LoadModHandler()
{
var handlerPath = Path.Combine(Program.baseDir, "bin", "Mono", "TweaksLauncher.dll");
if (!File.Exists(handlerPath))
Expand All @@ -26,9 +26,8 @@ private static void LoadModHandler(nint domain)
return;
}

Mono.Init();

var modHandlerAsm = Mono.mono_domain_assembly_open(domain, handlerPath);
var modHandlerAsm = Mono.mono_domain_assembly_open(Mono.Domain, handlerPath);
if (modHandlerAsm == 0)
{
Logger.Log($"Failed to load Mod Handler into the Mono domain.", Color.Red);
Expand All @@ -49,9 +48,9 @@ private static void LoadModHandler(nint domain)
var logImplPtr = Marshal.GetFunctionPointerForDelegate(logImpl);
Mono.mono_add_internal_call("TweaksLauncher.ModHandler::LogInternal", logImplPtr);

var baseDir = Mono.mono_string_new(domain, Program.baseDir);
var gameName = Mono.mono_string_new(domain, Program.gameName);
var gameDir = Mono.mono_string_new(domain, Program.gamePath);
var baseDir = Mono.NewString(Program.baseDir);
var gameName = Mono.NewString(Program.gameName);
var gameDir = Mono.NewString(Program.gamePath);

var args = stackalloc nint[3];
args[0] = baseDir;
Expand All @@ -73,8 +72,9 @@ private static nint OnDomainInit(nint domainName, nint a)
Logger.Log("Creating Mono Domain");

var domain = monoJitInit.Original(domainName, a);
Mono.Init(domain);

LoadModHandler(domain);
LoadModHandler();

return domain;
}
Expand Down

0 comments on commit 1a6cb1c

Please sign in to comment.