Skip to content

Commit

Permalink
Added support for IL2CPP coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
slxdy committed Jun 8, 2024
1 parent b1385d8 commit 4a36c5d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 79 deletions.
56 changes: 56 additions & 0 deletions TweaksLauncher.ModHandler/Il2Cpp/Il2CppEnumeratorWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#if IL2CPP

using Il2CppInterop.Runtime.Attributes;
using Il2CppInterop.Runtime.Injection;
using System;
using System.Collections;
using System.Drawing;

namespace TweaksLauncher.Il2Cpp;

[Il2CppImplements(typeof(Il2CppSystem.Collections.IEnumerator))]
internal class Il2CppEnumeratorWrapper : Il2CppSystem.Object
{
private readonly IEnumerator enumerator = null!;

static Il2CppEnumeratorWrapper()
{
ClassInjector.RegisterTypeInIl2Cpp<Il2CppEnumeratorWrapper>();
}

public Il2CppEnumeratorWrapper(IntPtr ptr) : base(ptr) { }
public Il2CppEnumeratorWrapper(IEnumerator _enumerator) : base(ClassInjector.DerivedConstructorPointer<Il2CppEnumeratorWrapper>())
{
ClassInjector.DerivedConstructorBody(this);
enumerator = _enumerator;
}

public Il2CppSystem.Object? Current
{
get => enumerator.Current switch
{
IEnumerator next => new Il2CppEnumeratorWrapper(next),
Il2CppSystem.Object il2cppObject => il2cppObject,
null => null,
_ => throw new NotSupportedException($"{enumerator.GetType()}: Unsupported type {enumerator.Current.GetType()}"),
};
}

public bool MoveNext()
{
try
{
return enumerator.MoveNext();
}
catch (Exception e)
{
ModHandler.Log(e.ToString(), Color.Red);

return false;
}
}

public void Reset() => enumerator.Reset();
}

#endif
16 changes: 16 additions & 0 deletions TweaksLauncher.ModHandler/Il2CppExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if IL2CPP

using System.Collections;
using TweaksLauncher.Il2Cpp;

namespace TweaksLauncher;

public static class Il2CppExtensions
{
public static UnityEngine.Coroutine StartCoroutine(this UnityEngine.MonoBehaviour obj, IEnumerator coroutine)
{
return obj.StartCoroutine(new Il2CppSystem.Collections.IEnumerator(new Il2CppEnumeratorWrapper(coroutine).Pointer));
}
}

#endif
21 changes: 13 additions & 8 deletions TweaksLauncher.ModHandler/LoadedMod.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using HarmonyLib;
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Reflection;


using UnityEngine;

#if IL2CPP
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime;
#endif

namespace TweaksLauncher;
Expand Down Expand Up @@ -70,12 +69,12 @@ public void Init()
}
catch (MissingMethodException)
{
ModHandler.Log($"Mod interface doesn't implement an 'Initialize' method: '{iMod.InterfaceType.FullName}'", Color.Red);
ModHandler.Log($"Mod interface doesn't implement an 'Initialize' method: '{iMod.InterfaceType.FullName}'", System.Drawing.Color.Red);
}
catch (Exception ex)
{
ModHandler.Log($"Mod interface failed to initialize: '{iMod.InterfaceType.FullName}'", Color.Red);
ModHandler.Log(ex.ToString(), Color.Red);
ModHandler.Log($"Mod interface failed to initialize: '{iMod.InterfaceType.FullName}'", System.Drawing.Color.Red);
ModHandler.Log(ex.ToString(), System.Drawing.Color.Red);
}
}
}
Expand All @@ -84,9 +83,15 @@ public void InitUnitySingletons()
{
foreach (var type in ModAssembly.GetTypes())
{
if (type.GetCustomAttributes(typeof(UnitySingletonAttribute), false).Length != 0 && UnityTools.MonoBehaviour.IsAssignableFrom(type))
if (type.GetCustomAttributes(typeof(UnitySingletonAttribute), false).Length != 0 && typeof(MonoBehaviour).IsAssignableFrom(type))
{
UnityTools.CreateComponentSingleton(type);
var obj = new GameObject();
UnityEngine.Object.DontDestroyOnLoad(obj);
#if IL2CPP
obj.AddComponent(Il2CppType.From(type));
#else
obj.AddComponent(type);
#endif
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions TweaksLauncher.ModHandler/ModHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Drawing;
using System.IO;
using System.Reflection;
using UnityEngine.SceneManagement;
using System.Runtime.CompilerServices;


#if IL2CPP
using TweaksLauncher.Il2Cpp;
Expand All @@ -14,7 +17,6 @@

#if MONO
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
#endif

namespace TweaksLauncher;
Expand Down Expand Up @@ -92,13 +94,18 @@ internal static void Start(string baseDir, string gameName, string gameDir
.Start();
#endif

UnityTools.Init();

LoadModsFromPath(GlobalModsDirectory);
LoadModsFromPath(ModsDirectory);

harmony = new Harmony("TweaksLauncher.ModHandler");
harmony.Patch(UnityTools.Internal_ActiveSceneChanged, prefix: new(OnInternalActiveSceneChanged));
WatchSceneChange();
}

// Need a separate method to prevent Start() from trying to resolve SceneManager early
[MethodImpl(MethodImplOptions.NoInlining)]
private static void WatchSceneChange()
{
harmony.Patch(typeof(SceneManager).GetMethod("Internal_ActiveSceneChanged", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), prefix: new(OnInternalActiveSceneChanged));
}

internal static void Log(string? message, Color baseColor = default, string? moduleName = null, Color moduleColor = default)
Expand Down
13 changes: 9 additions & 4 deletions TweaksLauncher.ModHandler/TweaksLauncher.ModHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
<ItemGroup Condition="$(DefineConstants.Contains(IL2CPP))">
<PackageReference Include="Il2CppInterop.HarmonySupport" Version="1.4.6-ci.394" />
<PackageReference Include="Il2CppInterop.Runtime" Version="1.4.6-ci.394" />
</ItemGroup>
<PackageReference Include="Il2CppInterop.ReferenceLibs" Version="1.0.0" IncludeAssets="compile" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="HarmonyX" Version="2.12.0" />
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.1.1" />
<ItemGroup Condition="$(DefineConstants.Contains(MONO))">
<PackageReference Include="UnityEngine.Modules" Version="2018.1.0" IncludeAssets="compile" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="HarmonyX" Version="2.12.0" />
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.1.1" />
</ItemGroup>

</Project>
63 changes: 0 additions & 63 deletions TweaksLauncher.ModHandler/UnityTools.cs

This file was deleted.

0 comments on commit 4a36c5d

Please sign in to comment.