Skip to content

Commit

Permalink
Reimplemented Unity Mono Support
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Feb 7, 2025
1 parent 24f6d7b commit 77013be
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ public abstract class MelonCoroutineInterop

public static class MelonCoroutines
{
private static List<IEnumerator> QueuedCoroutines = new List<IEnumerator>();
private static bool HasProcessedQueue;
public static List<IEnumerator> QueuedCoroutines { get; private set; } = new List<IEnumerator>();
public static MelonCoroutineInterop Interop;

public static void ProcessQueue()
{
if (HasProcessedQueue)
if (QueuedCoroutines == null)
return;
HasProcessedQueue = true;
foreach (var queuedCoroutine in QueuedCoroutines)
Start(queuedCoroutine);
QueuedCoroutines.Clear();
QueuedCoroutines = null;
}

/// <summary>
Expand All @@ -46,6 +45,8 @@ public static object Start(IEnumerator routine)
/// <returns>An object that can be passed to Stop to stop this coroutine</returns>
public static object Queue(IEnumerator routine)
{
if (QueuedCoroutines == null)
return routine;
QueuedCoroutines.Add(routine);
return routine;
}
Expand All @@ -63,6 +64,8 @@ public static void Stop(object coroutineToken)

public static void Dequeue(object coroutineToken)
{
if (QueuedCoroutines == null)
return;
IEnumerator routine = coroutineToken as IEnumerator;
if (QueuedCoroutines.Contains(routine))
QueuedCoroutines.Remove(routine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public override object Start(IEnumerator coroutine)
{
if (Component != null)
return Component.StartCoroutine(new Il2CppSystem.Collections.IEnumerator(new Il2CppEnumeratorWrapper(coroutine).Pointer));

MelonCoroutines.Queue(coroutine);
return coroutine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ internal class Il2CppSupportComponent : MonoBehaviour

public Il2CppSupportComponent(IntPtr value) : base(value) { }

Il2CppSupportComponent()
{
try
{
SetAsLastSiblingMethod = typeof(Transform).GetMethod("SetAsLastSibling", BindingFlags.Public | BindingFlags.Instance);
if (SetAsLastSiblingMethod != null)
return;

useGeneratedAssembly = false;
SetAsLastSiblingDelegateField = IL2CPP.ResolveICall<SetAsLastSiblingDelegate>("UnityEngine.Transform::SetAsLastSibling");
if (SetAsLastSiblingDelegateField == null)
throw new Exception("Unable to find Internal Call for UnityEngine.Transform::SetAsLastSibling");
}
catch (Exception ex) { LogError("Getting UnityEngine.Transform::SetAsLastSibling", ex); }
}

[HideFromIl2Cpp]
private void LogError(string cat, Exception ex)
{
Expand Down Expand Up @@ -80,6 +64,19 @@ internal void SiblingFix()

void Start()
{
try
{
SetAsLastSiblingMethod = typeof(Transform).GetMethod("SetAsLastSibling", BindingFlags.Public | BindingFlags.Instance);
if (SetAsLastSiblingMethod != null)
return;

useGeneratedAssembly = false;
SetAsLastSiblingDelegateField = IL2CPP.ResolveICall<SetAsLastSiblingDelegate>("UnityEngine.Transform::SetAsLastSibling");
if (SetAsLastSiblingDelegateField == null)
throw new Exception("Unable to find Internal Call for UnityEngine.Transform::SetAsLastSibling");
}
catch (Exception ex) { LogError("Getting UnityEngine.Transform::SetAsLastSibling", ex); }

if ((ModuleInterop.Support == null) || (((Il2CppSupportModule)ModuleInterop.Support).component != this))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ public override object Start(IEnumerator coroutine)
{
if (Component != null)
return Component.StartCoroutine(coroutine);
return MelonCoroutines.Start(coroutine);
MelonCoroutines.Queue(coroutine);
return coroutine;
}

public override void Stop(object coroutineToken)
{
if (Component != null)
Component.StopCoroutine(coroutineToken as Coroutine);
else
MelonCoroutines.Stop(coroutineToken);
MelonCoroutines.Dequeue(coroutineToken as IEnumerator);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ internal class MonoSupportComponent : MonoBehaviour

private MethodInfo SetAsLastSiblingMethod;

MonoSupportComponent()
{
try
{
SetAsLastSiblingMethod = typeof(Transform).GetMethod("SetAsLastSibling", BindingFlags.Public | BindingFlags.Instance);
if (SetAsLastSiblingMethod == null)
throw new Exception("Unable to find UnityEngine.Transform::SetAsLastSibling");
}
catch (Exception ex) { LogError("Getting UnityEngine.Transform::SetAsLastSibling", ex); }
}

private void LogError(string cat, Exception ex)
{
hadError = true;
Expand All @@ -48,6 +37,14 @@ internal void SiblingFix()

void Start()
{
try
{
SetAsLastSiblingMethod = typeof(Transform).GetMethod("SetAsLastSibling", BindingFlags.Public | BindingFlags.Instance);
if (SetAsLastSiblingMethod == null)
throw new Exception("Unable to find UnityEngine.Transform::SetAsLastSibling");
}
catch (Exception ex) { LogError("Getting UnityEngine.Transform::SetAsLastSibling", ex); }

if ((ModuleInterop.Support == null) || (((MonoSupportModule)ModuleInterop.Support).component != this))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal void CreateGameObject()
obj.hideFlags = HideFlags.DontSave;

// Create Support Component
component = obj.AddComponent<MonoSupportComponent>();
component = (MonoSupportComponent)obj.AddComponent(typeof(MonoSupportComponent));
component.SiblingFix();

// Create Interop for Coroutine Management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ private static IntPtr h_mono_runtime_invoke(IntPtr method, IntPtr obj, void** pa
// Check for Trigger Method
foreach (string triggerMethod in RuntimeInfo.TriggerMethods)
{
if (!methodName.Contains(triggerMethod))
if (string.IsNullOrEmpty(triggerMethod)
|| !methodName.Contains(triggerMethod))
continue;

// Detach mono_runtime_invoke Detour
Expand Down
6 changes: 0 additions & 6 deletions MelonLoader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mono", "Mono", "{5B41B0FE-3
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mono", "Mono", "{3B132718-3D60-44B5-AAF8-BDA2E8BBEDBE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CoreCLR", "CoreCLR", "{3A54C1B8-344B-4079-971C-7CF7BF1F7295}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Godot", "Godot", "{AEB11019-A160-4A8C-AB56-06E890AF1BA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runtime.Mono", "Dependencies\Modules\Runtimes\Mono\Runtime.Mono\Runtime.Mono.csproj", "{CF071430-B106-4D7E-9AC5-D3E892479BD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runtime.Mono.Shared", "Dependencies\Modules\Runtimes\Mono\Runtime.Mono.Shared\Runtime.Mono.Shared.csproj", "{AF553B0E-A74D-4F1F-857C-18741FB255E8}"
Expand Down Expand Up @@ -129,8 +125,6 @@ Global
{51405AB6-110B-4279-8D73-177B7B74537C} = {1A31A1FE-5F97-4D02-B622-8D62F971EB67}
{5B41B0FE-3B90-4F82-B1B0-061E39C27EBB} = {339D0985-0570-4C26-B09F-40E6460646EB}
{3B132718-3D60-44B5-AAF8-BDA2E8BBEDBE} = {5939AA02-721D-4011-9B89-358446C3C818}
{3A54C1B8-344B-4079-971C-7CF7BF1F7295} = {339D0985-0570-4C26-B09F-40E6460646EB}
{AEB11019-A160-4A8C-AB56-06E890AF1BA4} = {EFD62F17-3C48-4D77-8F19-1D4BCD62CEFB}
{CF071430-B106-4D7E-9AC5-D3E892479BD2} = {5B41B0FE-3B90-4F82-B1B0-061E39C27EBB}
{AF553B0E-A74D-4F1F-857C-18741FB255E8} = {5B41B0FE-3B90-4F82-B1B0-061E39C27EBB}
{27AD78CF-2EC4-4E02-8CEA-D2533D821C33} = {3B132718-3D60-44B5-AAF8-BDA2E8BBEDBE}
Expand Down

0 comments on commit 77013be

Please sign in to comment.