sinai-dev / UniverseLib

A library for making plugins which target IL2CPP and Mono Unity games.
GNU Lesser General Public License v2.1
116 stars 48 forks source link

Type with FullName `UniverseLib.UniversalBehaviour` is already injected #16

Closed SwiTool closed 1 year ago

SwiTool commented 1 year ago

I followed the guide here.

Though, here's my simple code:

        public override void OnApplicationStart()
        {
            Instance = this;
            UniverseLib.Universe.Init(LateInit, LogHandler);
        }

        private static void LateInit()
        {
        }

        private void LogHandler(string log, LogType level)
        {
            switch (level) {
                case LogType.Log:
                    this.LoggerInstance.Msg(log);
                    return;
                case LogType.Warning:
                case LogType.Assert:
                    this.LoggerInstance.Warning(log);
                    return;
                case LogType.Exception:
                case LogType.Error:
                    this.LoggerInstance.Error(log);
                    return;
            }
        }
And the error log ``` [21:56:57.690] ------------------------------ [21:56:57.690] ATestMod v0.0.1 [21:56:57.690] by SwiTool [21:56:57.691] Assembly: ATestMod.dll [21:56:57.692] ------------------------------ [21:56:57.693] ------------------------------ [21:56:57.693] MelonPreferencesManager v1.3.0 [21:56:57.694] by Sinai [21:56:57.695] Assembly: MelonPrefManager.IL2CPP.dll [21:56:57.695] ------------------------------ [21:56:57.697] ------------------------------ [21:56:57.697] UnityExplorer v4.9.0 [21:56:57.698] by Sinai [21:56:57.698] Assembly: UnityExplorer.ML.IL2CPP.dll [21:56:57.699] ------------------------------ [21:56:57.699] ------------------------------ [21:56:57.700] 3 Mods loaded. [21:56:57.863] Support Module Loaded: D:\Games\Il2cppTestGame\MelonLoader\Dependencies\SupportModules\Il2Cpp.dll [21:56:58.027] [ATestMod] [UniverseLib] UniverseLib 1.4.3 initializing... [21:56:58.433] [ATestMod] [WARNING] [UniverseLib] Expected Unhollowed folder path does not exist: ''. If you are using the standalone release, you can specify the Unhollowed modules path when you call CreateInstance(). [21:56:58.434] [ATestMod] [UniverseLib] Loaded Unhollowed modules in 0.001 seconds. [21:56:58.497] [ATestMod] [UniverseLib] Setup deobfuscation cache in 0.062 seconds. [21:56:58.500] [ATestMod] [UniverseLib] Finished UniverseLib initial setup. [21:56:58.503] [MelonPreferencesManager] [UniverseLib] UniverseLib 1.5.1 initializing... [21:56:58.507] [MelonPreferencesManager] [ERROR] System.ArgumentException: Type with FullName UniverseLib.UniversalBehaviour is already injected. Don't inject the same type twice, or use a different namespace at UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2CppImpl (System.Type type, System.Boolean logSuccess, UnhollowerBaseLib.Runtime.VersionSpecific.Class.INativeClassStruct[] interfaces) [0x001c7] in <6f5e4ff5bbd94258ab8bd773251f8e6c>:0 at UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp[T] () [0x00010] in <6f5e4ff5bbd94258ab8bd773251f8e6c>:0 at UniverseLib.UniversalBehaviour.Setup () [0x00001] in :0 at UniverseLib.Universe.Init (System.Single startupDelay, System.Action onInitialized, System.Action`2[T1,T2] logHandler, UniverseLib.Config.UniverseLibConfig config) [0x00071] in :0 at MelonPrefManager.PrefManagerMod.OnApplicationStart () [0x0007b] in <23e99f2c1426490cb4a4310b20ca572c>:0 at MelonLoader.MelonEvent+<>c.b__1_0 (MelonLoader.LemonAction x) [0x00000] in <6425afb6ac6a429aaf770255d2f7de57>:0 at MelonLoader.MelonEventBase`1[T].Invoke (System.Action`1[T] delegateInvoker) [0x00018] in <6425afb6ac6a429aaf770255d2f7de57>:0 [21:56:58.513] [UnityExplorer] UnityExplorer 4.9.0 initializing... [21:56:58.555] [UnityExplorer] Initialized UnityCrashPrevention for: Canvas.get_renderingDisplaySize [21:57:01.128] [ATestMod] LuaDLL.luaL_loadfile: embedded_LuaState_PCallCSFunction.ls [21:57:01.131] [ATestMod] LuaDLL.luaL_loadfile: embedded_LuaObject_init_newindexfun.ls [21:57:01.132] [ATestMod] LuaDLL.luaL_loadfile: embedded_LuaObject_init_indexfun.ls [21:57:01.217] [ATestMod] LuaDLL.luaL_loadfile: embedded_LuaState_yieldfunc.ls [21:57:01.217] [ATestMod] LuaDLL.luaL_loadfile: embedded_LuaState_class.ls [21:57:04.016] [ATestMod] [UniverseLib] Initialized Legacy Input support [21:57:04.106] [ERROR] Exception in IL2CPP-to-Managed trampoline, not passing it to il2cpp: System.NullReferenceException: Object reference not set to an instance of an object at UniverseLib.Universe.Patch (System.Type type, System.String methodName, HarmonyLib.MethodType methodType, System.Type[] arguments, System.Reflection.MethodInfo prefix, System.Reflection.MethodInfo postfix, System.Reflection.MethodInfo finalizer) [0x0012b] in <49afc313f5094db08cf47c312888dae9>:0 at UniverseLib.UI.UniversalUI.SetupAssetBundlePatches () [0x00028] in <49afc313f5094db08cf47c312888dae9>:0 at UniverseLib.UI.UniversalUI.LoadBundle () [0x00001] in <49afc313f5094db08cf47c312888dae9>:0 at UniverseLib.UI.UniversalUI.Init () [0x00001] in <49afc313f5094db08cf47c312888dae9>:0 at UniverseLib.Universe+d__23.MoveNext () [0x0009f] in <49afc313f5094db08cf47c312888dae9>:0 at UniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumerator.MoveNext () [0x00000] in <49afc313f5094db08cf47c312888dae9>:0 at (wrapper dynamic-method) UniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumerator.Trampoline_BooleanThisUniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumeratorMoveNext(intptr,UnhollowerBaseLib.Runtime.Il2CppMethodInfo*) ```

I'm using both UnityExplorer and MelonPreferencesManager. When I don't load my mod, they work just fine, but with my mod, nothing loads at all.

I've read the source code of MelonPreferencesManager but I can't see where i'm doing things wrong.

When I comment out the UniverseLib.Universe.Init() line, the error goes away.

Any idea if bug of misusage ?

SwiTool commented 1 year ago

I don't know exactly how I fixed this, but surely due to a Clean Build and a typo correction

<!-- IL2CPP References -->
<ItemGroup Condition="'$(Configuration)'=='IL2CPP'">
-    <Reference Include="UniverseLib.IL2CPP.Unhlolloer">
+    <Reference Include="UniverseLib.IL2CPP.Unhollower">
        <HintPath>packages\UniverseLib.IL2CPP.Unhollower.X.X.X\lib\net472\UniverseLib.IL2CPP.Unhollower.dll</HintPath>
        <Private>True</Private>
    </Reference>
</ItemGroup>
SwiTool commented 1 year ago

Though, I still have the second error in the log :

[21:57:04.106] [ERROR] Exception in IL2CPP-to-Managed trampoline, not passing it to il2cpp: System.NullReferenceException: Object reference not set to an instance of an object

  at UniverseLib.Universe.Patch (System.Type type, System.String methodName, HarmonyLib.MethodType methodType, System.Type[] arguments, System.Reflection.MethodInfo prefix, System.Reflection.MethodInfo postfix, System.Reflection.MethodInfo finalizer) [0x0012b] in <49afc313f5094db08cf47c312888dae9>:0 
  at UniverseLib.UI.UniversalUI.SetupAssetBundlePatches () [0x00028] in <49afc313f5094db08cf47c312888dae9>:0 
  at UniverseLib.UI.UniversalUI.LoadBundle () [0x00001] in <49afc313f5094db08cf47c312888dae9>:0 
  at UniverseLib.UI.UniversalUI.Init () [0x00001] in <49afc313f5094db08cf47c312888dae9>:0 
  at UniverseLib.Universe+<SetupCoroutine>d__23.MoveNext () [0x0009f] in <49afc313f5094db08cf47c312888dae9>:0 
  at UniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumerator.MoveNext () [0x00000] in <49afc313f5094db08cf47c312888dae9>:0 
  at (wrapper dynamic-method) UniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumerator.Trampoline_BooleanThisUniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumeratorMoveNext(intptr,UnhollowerBaseLib.Runtime.Il2CppMethodInfo*)

Seems like a patching error. I referenced 0Harmony.dll from Game_Data/MelonLoader/0Harmony.dll. I will try with the official Harmony package tonight and see if it solves the problem.

I must say though that I successfully patched some methods with 0Harmony.dll so this shouldn't be a problem in my guess.

I'll do more tests and keep this thread updated.

EDIT: Nah it's the same..