-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModInstance.cs
More file actions
236 lines (228 loc) · 8.62 KB
/
ModInstance.cs
File metadata and controls
236 lines (228 loc) · 8.62 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
namespace GlassLoader
{
public class ModConfig
{
public bool RuntimeCompileEnabled = false;
public List<string> Dependencies = new List<string>();
public List<string> DependenciesShared = new List<string>();
public int Priority = 1;
public string Author = "Unknown";
public bool Enabled = true;
public string TargetGameVersion = null;
public string TargetGameDirectory = null;
public bool GameVersionStrictMode = false;
public bool GameDirectoryStrictMode = false;
public ModConfig()
{
}
}
public class ModListener
{
public MethodInfo Listener;
public string Name;
public int ParamLength;
public ModListener(MethodInfo listener, string Name = null)
{
if (Name == null) Name = Guid.NewGuid().ToString();
Listener = listener;
ParamLength = listener.GetParameters().Length;
}
}
public class ModPatchInfo
{
public string ID;
public MethodBase Origin;
public List<MethodInfo> Patches = new List<MethodInfo>();
public ModPatchInfo(string ID, MethodBase origin, List<MethodInfo> patches)
{
this.ID = ID;
this.Origin = origin;
this.Patches = patches;
}
}
public class ModInstance : IDisposable
{
public Assembly assembly;
public Harmony harmony;
public string UUID;
public Type EventListeners;
public ModConfig Config;
public List<FileInfo> Dll = new List<FileInfo>();
public List<FileInfo> Cs = new List<FileInfo>();
public DirectoryInfo ModDirectory;
public Dictionary<string, List<ModListener>> Listeners = new Dictionary<string, List<ModListener>>();
public Dictionary<string, ModPatchInfo> ModPatches = new Dictionary<string, ModPatchInfo>();
public void AddListener(string identifier, ModListener listener)
{
if (Listeners.ContainsKey(identifier)) Listeners[identifier].Add(listener);
else Listeners.Add(identifier, new List<ModListener>() { listener });
}
public void RemoveListener(string identifier, ModListener listener)
{
foreach (var item in Listeners)
{
foreach (ModListener value in item.Value)
{
if (value.Equals(listener)) item.Value.Remove(value);
}
if (item.Value.Count == 0) Listeners.Remove(item.Key);
}
}
public void RemoveListener(string identifier, MethodInfo listener)
{
foreach (var item in Listeners)
{
foreach (ModListener value in item.Value)
{
if (value.Listener.Equals(listener)) item.Value.Remove(value);
}
if (item.Value.Count == 0) Listeners.Remove(item.Key);
}
}
public void RemoveListener(string identifier, string modListenerName)
{
foreach (var item in Listeners)
{
foreach (ModListener value in item.Value)
{
if (value.Name == modListenerName) item.Value.Remove(value);
}
if (item.Value.Count == 0) Listeners.Remove(item.Key);
}
}
public ModInstance(Assembly assembly = null, string UUID = null)
{
if (UUID == null) this.UUID = Guid.NewGuid().ToString();
this.assembly = assembly;
harmony = new Harmony($"com.GlassMod.{UUID}");
//LoadListeners();
}
public void LoadListeners()
{
if (assembly == null)
{
var ex = new Exception("Mod Assembly equals null! Listeners loading failure");
GLog.Warn(ex);
return;
throw ex;
}
EventListeners = assembly.GetTypes().Where(x => x.Name == "EventListeners").FirstOrDefault();
if (EventListeners == null) GLog.Warn("EventListeners Not Found in Mod: " + assembly.GetName());
else
{
var methods = EventListeners.GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo method in methods)
{
try
{
if (Listeners.Keys.Contains(method.Name)) Listeners[method.Name].Add(new ModListener(method));
else Listeners.Add(method.Name, new List<ModListener> { new ModListener(method) });
}
catch (Exception ex)
{
GLog.Warn(ex);
continue;
}
}
}
}
public void RemoveGlobalPatch(string HarmonyID)
{
Harmony.UnpatchID(HarmonyID);
}
public void UnPatch(MethodBase original, MethodInfo patch)
{
harmony.Unpatch(original: original, patch: patch);
}
public void UnpatchSelfAll()
{
harmony.UnpatchSelf();
}
public void UnPatch(ModPatchInfo patch)
{
foreach (var methods in patch.Patches)
{
harmony.Unpatch(patch.Origin, methods);
}
if(ModPatches.Keys.Contains(patch.ID)) ModPatches.Remove(patch.ID);
}
public void UnPatch(string PatchID)
{
ModPatchInfo patch;
if (ModPatches.Keys.Contains(PatchID))
{
patch = ModPatches[PatchID];
}
else
{
throw new Exception($"PatchID [{PatchID}] Not Found!");
}
foreach (var methods in patch.Patches)
{
harmony.Unpatch(patch.Origin, methods);
}
ModPatches.Remove(PatchID);
}
public ModPatchInfo? Patch(MethodBase original, MethodInfo prefix = null, MethodInfo postfix = null, MethodInfo transpiler = null, MethodInfo finalizer = null,
MethodInfo ilmanipulator = null, bool ThrowOnError = true)
{
HarmonyMethod harmonyPrefix = prefix != null ? new HarmonyMethod(prefix) : null;
HarmonyMethod harmonyPostfix = postfix != null ? new HarmonyMethod(postfix) : null;
HarmonyMethod harmonyTranspiler = transpiler != null ? new HarmonyMethod(transpiler) : null;
HarmonyMethod harmonyFinalizer = finalizer != null ? new HarmonyMethod(finalizer) : null;
HarmonyMethod harmonyILManipulator = ilmanipulator != null ? new HarmonyMethod(ilmanipulator) : null;
try
{
harmony.Patch(original, harmonyPrefix, harmonyPostfix, harmonyTranspiler, harmonyFinalizer, harmonyILManipulator);
string PatchID = Guid.NewGuid().ToString();
List<MethodInfo> ModPatches = new List<MethodInfo>();
if (prefix != null) ModPatches.Add(prefix);
if (postfix != null) ModPatches.Add(postfix);
if (transpiler != null) ModPatches.Add(transpiler);
if (finalizer != null) ModPatches.Add(finalizer);
if (ilmanipulator != null) ModPatches.Add(ilmanipulator);
ModPatchInfo modPatchInfo = new ModPatchInfo(PatchID, original, ModPatches);
return modPatchInfo;
}
catch (Exception ex)
{
if (ThrowOnError)
{
throw ex;
}
try
{
var unpatch = prefix ?? postfix ?? transpiler ?? finalizer ?? ilmanipulator;
harmony.Unpatch(original, unpatch);
}
catch (Exception ex2)
{
GLog.Error(new Exception($"Automatic Unpatch [{original.Name}] Failed."));
//GUtil.ErrorTerminate();
try
{
harmony.UnpatchSelf();
}
catch (Exception ex3)
{
GLog.Fatal("Unpatch Self Failed! Unexpected Error may occurr!");
throw ex3;
}
}
}
return null;
}
public void Dispose()
{
harmony?.UnpatchSelf();
}
}
}