-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameExtensionsManager.cs
65 lines (53 loc) · 2.54 KB
/
GameExtensionsManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Harmony;
using MelonLoader;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnhollowerRuntimeLib;
using UnityEngine;
using Wirelet.Components.Extensions;
namespace Wirelet
{
internal static class GameExtensionsManager
{
private static Dictionary<string, (Type type, int childDepth)> extensionComponents = new Dictionary<string, (Type, int)>();
internal static void Init()
{
WireletMod.Instance.Harmony.Patch(
typeof(PuppetMasta.BehaviourBase).GetMethod("OnEnable"),
postfix: new HarmonyMethod(typeof(GameExtensionsManager).GetMethod("TrySetupGameobject", BindingFlags.NonPublic | BindingFlags.Static)));
WireletMod.Instance.Harmony.Patch(
typeof(StressLevelZero.Interaction.ButtonToggle).GetMethod("Awake"),
prefix: new HarmonyMethod(typeof(GameExtensionsManager).GetMethod("TrySetupGameobject", BindingFlags.NonPublic | BindingFlags.Static)));
extensionComponents.Add(Il2CppType.Of<PuppetMasta.BehaviourCrablet>().FullName, (typeof(CrabletComponent), 2));
extensionComponents.Add(Il2CppType.Of<StressLevelZero.Interaction.ButtonToggle>().FullName, (typeof(ButtonToggleComponent), 0));
}
#region BehaviourBaseNav
private static void TrySetupGameobject(MonoBehaviour __instance)
{
MelonLogger.Msg("OnEnable called on " + __instance.GetIl2CppType().Name);
if (__instance.gameObject.GetComponentInParent<WireletBehaviour>() != null)
return;
if (extensionComponents.TryGetValue(__instance.GetIl2CppType().FullName, out (Type type, int childDepth) wireletType)) {
GameObject go = GetParentRecursive(__instance.transform, wireletType.childDepth).gameObject;
if (go.GetComponentInParent<WireletBehaviour>() != null)
return;
MelonLogger.Msg("Adding wirelet extension to instance of " + __instance.GetIl2CppType().Name);
WireletBehaviour wb = go.AddComponent<WireletBehaviour>();
wb.Setup(wireletType.type);
}
else
MelonLogger.Msg("Could not find wirelet extension for " + __instance.GetIl2CppType().Name);
}
#endregion
private static Transform GetParentRecursive(Transform t, int depth)
{
while (t != null && depth > 0)
{
t = t.parent;
--depth;
}
return t;
}
}
}