From c984c6324a4ac741891ff443f587a363f2182d51 Mon Sep 17 00:00:00 2001 From: js6pak Date: Thu, 14 Jan 2021 18:17:22 +0100 Subject: [PATCH] RegisterInIl2CppAttribute --- Reactor.Debugger/DebuggerPlugin.cs | 7 +++-- Reactor/Extensions/Extensions.cs | 7 +++++ Reactor/ReactorPlugin.cs | 7 +++-- Reactor/RegisterInIl2CppAttribute.cs | 41 ++++++++++++++++++++++++++++ Reactor/Unstrip/Patches.cs | 1 + 5 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 Reactor/RegisterInIl2CppAttribute.cs diff --git a/Reactor.Debugger/DebuggerPlugin.cs b/Reactor.Debugger/DebuggerPlugin.cs index 898c610..6c6d559 100644 --- a/Reactor.Debugger/DebuggerPlugin.cs +++ b/Reactor.Debugger/DebuggerPlugin.cs @@ -4,7 +4,7 @@ using BepInEx.IL2CPP; using HarmonyLib; using Reactor.Extensions; using UnhollowerBaseLib; -using UnhollowerRuntimeLib; +using UnhollowerBaseLib.Attributes; using UnityEngine; namespace Reactor.Debugger @@ -22,7 +22,7 @@ namespace Reactor.Debugger public override void Load() { - ClassInjector.RegisterTypeInIl2Cpp(); + RegisterInIl2CppAttribute.Register(); var gameObject = new GameObject(nameof(DebuggerPlugin)).DontDestroy(); Component = gameObject.AddComponent(); @@ -35,10 +35,13 @@ namespace Reactor.Debugger }); } + [RegisterInIl2Cpp] public class DebuggerComponent : MonoBehaviour { + [HideFromIl2Cpp] public bool DisableGameEnd { get; set; } + [HideFromIl2Cpp] public DragWindow TestWindow { get; } public DebuggerComponent(IntPtr ptr) : base(ptr) diff --git a/Reactor/Extensions/Extensions.cs b/Reactor/Extensions/Extensions.cs index 4c36611..5fd1b9e 100644 --- a/Reactor/Extensions/Extensions.cs +++ b/Reactor/Extensions/Extensions.cs @@ -28,6 +28,13 @@ namespace Reactor.Extensions return obj; } + + public static T DontUnload(this T obj) where T : Object + { + obj.hideFlags |= HideFlags.DontUnloadUnusedAsset; + + return obj; + } /// /// Adds a delegate to get one notification when a scene has loaded. diff --git a/Reactor/ReactorPlugin.cs b/Reactor/ReactorPlugin.cs index b7c87b0..afb52df 100644 --- a/Reactor/ReactorPlugin.cs +++ b/Reactor/ReactorPlugin.cs @@ -6,7 +6,7 @@ using HarmonyLib; using Reactor.Extensions; using Reactor.Patches; using Reactor.Unstrip; -using UnhollowerRuntimeLib; +using UnhollowerBaseLib.Attributes; using UnityEngine; namespace Reactor @@ -23,8 +23,7 @@ namespace Reactor public override void Load() { - ClassInjector.RegisterTypeInIl2Cpp(); - ClassInjector.RegisterTypeInIl2Cpp(); + RegisterInIl2CppAttribute.Register(); var gameObject = new GameObject(nameof(ReactorPlugin)).DontDestroy(); gameObject.AddComponent().Plugin = this; @@ -41,8 +40,10 @@ namespace Reactor return base.Unload(); } + [RegisterInIl2Cpp] public class ReactorComponent : MonoBehaviour { + [HideFromIl2Cpp] public ReactorPlugin Plugin { get; internal set; } public ReactorComponent(IntPtr ptr) : base(ptr) diff --git a/Reactor/RegisterInIl2CppAttribute.cs b/Reactor/RegisterInIl2CppAttribute.cs new file mode 100644 index 0000000..87ec55c --- /dev/null +++ b/Reactor/RegisterInIl2CppAttribute.cs @@ -0,0 +1,41 @@ +using System; +using System.Reflection; +using HarmonyLib; +using UnhollowerRuntimeLib; + +namespace Reactor +{ + /// + /// Utility attribute for automatically calling + /// + [AttributeUsage(AttributeTargets.Class)] + public class RegisterInIl2CppAttribute : Attribute + { + public static void Register() + { + Register(Assembly.GetCallingAssembly()); + } + + public static void Register(Assembly assembly) + { + foreach (var type in assembly.GetTypes()) + { + var attribute = type.GetCustomAttribute(); + + if (attribute != null) + { + try + { + typeof(ClassInjector).GetMethod(nameof(ClassInjector.RegisterTypeInIl2Cpp))! + .MakeGenericMethod(type) + .Invoke(null, new object[0]); + } + catch (Exception e) + { + PluginSingleton.Instance.Log.LogWarning($"Failed to register {type.FullDescription()}: {e}"); + } + } + } + } + } +} diff --git a/Reactor/Unstrip/Patches.cs b/Reactor/Unstrip/Patches.cs index 08488ae..ee19530 100644 --- a/Reactor/Unstrip/Patches.cs +++ b/Reactor/Unstrip/Patches.cs @@ -133,6 +133,7 @@ namespace Reactor.Unstrip } } + [RegisterInIl2Cpp] internal sealed class GUIWordWrapSizer : GUILayoutEntry { private readonly GUIContent m_Content;