-
Notifications
You must be signed in to change notification settings - Fork 9
/
ModLoader.cs
380 lines (352 loc) · 10.5 KB
/
ModLoader.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Assets.SFSML.Utility;
using SFSML.Attributes;
using SFSML.Enum;
using SFSML.Exceptions;
using SFSML.HookSystem.ReWork;
using SFSML.HookSystem.ReWork.BaseHooks.UtilHooks;
using SFSML.IO.Storable;
using SFSML.Translation;
using SFSML.Translation.Languages;
using UnityEngine;
namespace SFSML
{
[MyModEntryPoint]
public class ModLoader
{
public ModLoader()
{
bool flag = Application.platform == RuntimePlatform.WindowsPlayer;
if (flag)
{
this.myConsole = new MyConsole();
ModLoader.mainConsole = this.myConsole;
}
this.performDirCheck();
ModLoader.translation = new MyTranslationController<ModLoaderLang>(this.getMyDataDirectory(), "ModLoaderTranslations");
ModLoader.logTag = ModLoader.translation.autoFormat("@LogTag", new object[0]);
this.myConsole.tryLogCustom(ModLoader.translation.autoFormat("@EntryMessage", new object[0]), ModLoader.logTag, LogType.Generic);
SerializableList<string> serializableList = new SerializableList<string>();
new MyHookListener(delegate(MyHook hook)
{
MyKeyDownHook myKeyDownHook = (MyKeyDownHook)hook;
bool flag2 = myKeyDownHook.keyDown == KeyCode.F12;
if (flag2)
{
ModLoader.mainConsole.toggleConsole();
}
return hook;
}, typeof(MyKeyDownHook));
}
public void startLoadProcedure()
{
ModLoader.mainConsole.tryLogCustom("Initiating load procedure", ModLoader.logTag, LogType.Generic);
try
{
this.loadPriorityMods();
}
catch (Exception e)
{
this.myConsole.logError(e);
}
try
{
this.loadMods();
}
catch (Exception e2)
{
this.myConsole.logError(e2);
}
}
public string getMyBaseDirectory()
{
return Path.Combine(Application.dataPath, "SFSML");
}
public string getMyModDirectory()
{
return Path.Combine(Path.Combine(Application.dataPath, "SFSML"), "Mods");
}
public string getMyDataDirectory()
{
return Path.Combine(Path.Combine(Application.dataPath, "SFSML"), "Data");
}
public string getMyDirectory(PathFinder path)
{
string result;
switch (path)
{
case PathFinder.MyDataDir:
result = this.getMyDataDirectory();
break;
case PathFinder.MyModDir:
result = this.getMyModDirectory();
break;
case PathFinder.MyPriorityModDir:
result = Path.Combine(this.getMyModDirectory(), "priority");
break;
case PathFinder.MyNormalModDir:
result = Path.Combine(this.getMyModDirectory(), "normal");
break;
default:
result = null;
break;
}
return result;
}
private void performDirCheck()
{
bool flag = !Directory.Exists(this.getMyBaseDirectory());
if (flag)
{
Directory.CreateDirectory(this.getMyBaseDirectory());
ModLoader.mainConsole.tryLogCustom("Created SFSML directory.", "DirChecker", LogType.Generic);
}
bool flag2 = !Directory.Exists(this.getMyModDirectory());
if (flag2)
{
Directory.CreateDirectory(this.getMyModDirectory());
Directory.CreateDirectory(this.getMyDirectory(PathFinder.MyNormalModDir));
Directory.CreateDirectory(this.getMyDirectory(PathFinder.MyPriorityModDir));
ModLoader.mainConsole.tryLogCustom("Created Mods directory.", "DirChecker", LogType.Generic);
}
bool flag3 = !Directory.Exists(this.getMyDataDirectory());
if (flag3)
{
Directory.CreateDirectory(this.getMyDataDirectory());
ModLoader.mainConsole.tryLogCustom("Created Data directory.", "DirChecker", LogType.Generic);
}
}
private void loadPriorityMods()
{
try
{
string[] files = Directory.GetFiles(this.getMyDirectory(PathFinder.MyPriorityModDir));
foreach (string text in files)
{
bool flag = Path.GetExtension(text) != ".dll";
if (!flag)
{
this.loadModFromFile(text);
}
}
}
catch (MyCoreException ex)
{
ex.caller = new MyCoreException.MyCaller("loadPriorityMods", "ModLoader.cs");
}
catch (Exception e)
{
ModLoader.mainConsole.logError(e);
}
}
private void loadMods()
{
try
{
string[] files = Directory.GetFiles(this.getMyDirectory(PathFinder.MyNormalModDir));
foreach (string modFile in files)
{
this.loadModFromFile(modFile);
}
}
catch (MyCoreException ex)
{
ex.caller = new MyCoreException.MyCaller("loadMods", "ModLoader.cs");
}
catch (Exception e)
{
ModLoader.mainConsole.logError(e);
}
}
private void loadModFromFile(string modFile)
{
try
{
bool flag = Path.GetExtension(modFile) != ".dll";
if (!flag)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(modFile);
ModLoader.mainConsole.log("Loading mod: " + fileNameWithoutExtension, ModLoader.logTag);
Assembly assembly = Assembly.LoadFrom(modFile);
MyMod myMod = null;
foreach (Type type in assembly.GetTypes())
{
object[] customAttributes = type.GetCustomAttributes(typeof(MyModEntryPoint), true);
bool flag2 = customAttributes.Length == 1;
if (flag2)
{
myMod = (Activator.CreateInstance(type) as MyMod);
}
}
bool flag3 = myMod == null;
if (flag3)
{
ModLoader.mainConsole.tryLogCustom("Mod does not contain a ModLoader entry point.", ModLoader.logTag, LogType.Generic);
ModLoader.mainConsole.tryLogCustom("It is possible this mod is not compatible with ModLoader.", ModLoader.logTag, LogType.Generic);
}
else
{
List<string> list = new List<string>();
foreach (AssemblyName assemblyName in assembly.GetReferencedAssemblies())
{
list.Add(assemblyName.FullName);
}
ModLoader.mainConsole.tryLogCustom("Assemblies: [" + string.Join(",", list.ToArray()) + "]", ModLoader.logTag, LogType.Generic);
string path = Path.Combine(Path.GetDirectoryName(modFile), Path.GetFileNameWithoutExtension(modFile));
bool flag4 = myMod.LoadAssets(Path.Combine(path, "Assets"));
if (flag4)
{
ModLoader.mainConsole.tryLogCustom("Loaded assets.", ModLoader.logTag, LogType.Generic);
bool flag5 = myMod.fetchIcon();
if (flag5)
{
ModLoader.mainConsole.tryLogCustom("Loaded custom icon (Use UIE to display).", ModLoader.logTag, LogType.Generic);
}
}
bool flag6 = this.mods.ContainsKey(myMod.myName);
if (flag6)
{
ModLoader.mainConsole.tryLogCustom("Mod by the name " + myMod.myName + " already exists!", ModLoader.logTag, LogType.Generic);
}
else
{
string path2 = Path.Combine(this.getMyDataDirectory(), fileNameWithoutExtension);
myMod.assignDataPath(path2);
MethodInfo[] methods = myMod.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (MethodInfo methodInfo in methods)
{
MyListenerAttribute[] array2 = (MyListenerAttribute[])methodInfo.GetCustomAttributes(typeof(MyListenerAttribute), true);
bool flag7 = array2.Length == 1;
if (flag7)
{
new MyHookListener(methodInfo, myMod);
}
}
FieldInfo[] fields = myMod.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (FieldInfo fieldInfo in fields)
{
MyHookListenerContainer[] array4 = (MyHookListenerContainer[])fieldInfo.GetCustomAttributes(typeof(MyHookListenerContainer), true);
bool flag8 = array4.Length == 1;
if (flag8)
{
MethodInfo[] methods2 = fieldInfo.GetValue(myMod).GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (MethodInfo listenMethod in methods2)
{
new MyHookListener(listenMethod, myMod);
}
}
}
myMod.Load();
ModLoader.mainConsole.tryLogCustom(ModLoader.translation.autoFormat("@ModEntry", new object[]
{
myMod.myName,
myMod.myVersion,
myMod.myDescription
}), ModLoader.logTag, LogType.Generic);
this.loadedMods++;
this.mods[myMod.myName] = myMod;
MyHookSystem.executeHook<MyModLoadedHook>(new MyModLoadedHook(myMod));
}
}
}
}
catch (MyCoreException ex)
{
ex.caller = new MyCoreException.MyCaller("loadModFromFile", "ModLoader.cs");
}
catch (Exception e)
{
ModLoader.mainConsole.logError(e);
}
}
public void RunUpdate()
{
long num = General.getMillis() - this.lastUpdate;
bool flag = num < 100L;
if (!flag)
{
this.KeyUpdate();
this.lastUpdate = General.getMillis();
}
}
private void KeyUpdate()
{
foreach (object obj in this.codes)
{
KeyCode keyCode = (KeyCode)obj;
bool key = Input.GetKey(keyCode);
if (key)
{
bool flag = !this.keyDown.Contains(keyCode);
if (flag)
{
MyKeyDownHook myKeyDownHook = MyHookSystem.executeHook<MyKeyDownHook>(new MyKeyDownHook(keyCode));
bool flag2 = myKeyDownHook.isCanceled();
if (flag2)
{
bool register = myKeyDownHook.register;
if (register)
{
this.keyDown.Add(keyCode);
}
return;
}
this.keyDown.Add(keyCode);
}
}
}
List<KeyCode> list = new List<KeyCode>(this.keyDown);
foreach (KeyCode keyCode2 in list)
{
bool flag3 = !Input.GetKey(keyCode2);
if (flag3)
{
MyKeyUpHook baseHook = new MyKeyUpHook(keyCode2);
MyKeyUpHook myKeyUpHook = MyHookSystem.executeHook<MyKeyUpHook>(baseHook);
bool flag4 = myKeyUpHook.isCanceled();
if (flag4)
{
bool register2 = myKeyUpHook.register;
if (register2)
{
this.keyDown.Remove(keyCode2);
}
break;
}
this.keyDown.Remove(keyCode2);
}
}
}
public bool isModLoaded(string name)
{
return this.mods.ContainsKey(name);
}
public MyMod getMod(string name)
{
return this.mods[name];
}
public Dictionary<string, MyMod> getMods()
{
return new Dictionary<string, MyMod>(this.mods);
}
static ModLoader()
{
// Note: this type is marked as 'beforefieldinit'.
}
public static MyConsole mainConsole;
public int loadedMods = 0;
private Dictionary<string, MyMod> mods = new Dictionary<string, MyMod>();
private Canvas overlayObject = null;
public MyConsole myConsole;
public static readonly string version = "1.3.1";
private static string logTag = "ModLoader " + ModLoader.version;
public static MyTranslationController<ModLoaderLang> translation;
private GameObject overlay;
private Array codes = System.Enum.GetValues(typeof(KeyCode));
private List<KeyCode> keyDown = new List<KeyCode>();
private long lastUpdate = General.getMillis();
}
}