-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
95 lines (86 loc) · 4.35 KB
/
Plugin.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Exception = System.Exception;
namespace CrabGameUtils;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BasePlugin
{
public static SteamManager Steam { get; set; } = SteamManager.Instance;
public static Plugin Instance { get; set; } = null!;
public static ConfigFile StaticConfig { get; set; } = null!;
public static MonoBehaviourPublicCSDi2UIInstObUIloDiUnique GameModeManager { get; set; } = null!;
public static Modules.Config.Config Configuration { get; } = new($@"{Directory.GetCurrentDirectory()}\BepInEx\plugins\CrabGameUtilsData.json");
public static SystemCollections.List<Extension> ExtensionInstances { get; set; } = new();
public override void Load()
{
StaticConfig = Config;
Instance = this;
Harmony.CreateAndPatchAll(typeof(Plugin));
Harmony harmony = new Harmony("CrabGameUtils");
harmony.PatchAll();
harmony.PatchAll(typeof(BepinexDetectionPatch));
foreach (Type type in Assembly.GetAssembly(typeof(Extension)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(Extension))))
{
string name = type.GetCustomAttributesData().FirstOrDefault(a => a.AttributeType.Name == "ExtensionNameAttribute")?.ConstructorArguments.First().Value?.ToString() ?? type.Name;
Extension instance = (Extension)System.Activator.CreateInstance(type, null);
foreach (FieldInfo field in type.GetFields().Where(p => p.FieldType.Name.Contains("ExtensionConfig")))
field.GetValue(instance).GetType().GetMethod("InitConfig")!.Invoke(field.GetValue(instance), new object[] { name });
if (!instance.Enabled.Value) continue;
ExtensionInstances.Add(instance);
instance.Name = name;
Instance.Log.LogInfo($"{name}: loaded successfully");
}
}
[HarmonyPatch(typeof(GameUI), "Awake"), HarmonyPostfix]
public static void Awake(GameUI __instance)
{
Steam = SteamManager.Instance;
foreach (Extension extension in ExtensionInstances)
{
try { extension.Awake(); }
catch (Exception e)
{
extension.Enabled.Value = false;
ChatBox.Instance.ForceMessage($"<color=red>{extension.Name} errored (see logs for more details)</color>");
Instance.Log.LogError(e.ToString());
}
}
ExtensionInstances = ExtensionInstances.Where(e => e.Enabled.Value).ToList();
}
[HarmonyPatch(typeof(GameUI), "Start"), HarmonyPostfix]
public static void Start(GameUI __instance)
{
Steam = SteamManager.Instance;
foreach (Extension extension in ExtensionInstances)
{
try { extension.Start(); }
catch (Exception e)
{
ChatBox.Instance.ForceMessage($"<color=red>{extension.Name} errored (see logs for more details)</color>");
Instance.Log.LogError(e.ToString());
}
}
ExtensionInstances = ExtensionInstances.Where(e => e.Enabled.Value).ToList();
}
[HarmonyPatch(typeof(GameUI), "Update"), HarmonyPostfix]
public static void Update(GameUI __instance)
{
Steam = SteamManager.Instance;
foreach (Extension extension in ExtensionInstances)
{
try { extension.Update(); }
catch (Exception e)
{
ChatBox.Instance.ForceMessage($"<color=red>{extension.Name} errored (see logs for more details)</color>");
Instance.Log.LogError(e.ToString());
}
}
ExtensionInstances = ExtensionInstances.Where(e => e.Enabled.Value).ToList();
}
}
public class BepinexDetectionPatch {
[HarmonyPatch(typeof(MonoBehaviourPublicGataInefObInUnique), "Method_Private_Void_GameObject_Boolean_Vector3_Quaternion_0")]
[HarmonyPatch(typeof(MonoBehaviourPublicCSDi2UIInstObUIloDiUnique), "Method_Private_Void_0")]
[HarmonyPatch(typeof(MonoBehaviourPublicVesnUnique), "Method_Private_Void_0")]
[HarmonyPatch(typeof(MonoBehaviourPublicObjomaOblogaTMObseprUnique), "Method_Public_Void_PDM_2")]
[HarmonyPatch(typeof(MonoBehaviourPublicTeplUnique), "Method_Private_Void_PDM_32")]
[HarmonyPrefix] public static bool Prefix(MethodBase __originalMethod) => false;
}