-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModHooks.cs
470 lines (415 loc) · 14 KB
/
ModHooks.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Pepperoni
{
public class ModHooks
{
// Currently unused
internal static bool IsInitialized;
/// <summary>
/// Contains the seperator for path's, useful for handling Mac vs Windows vs Linux
/// </summary>
public static char PathSeperator = SystemInfo.operatingSystem.Contains("Windows") ? '\\' : '/';
// Currently unused
private static readonly string SettingsPath = Application.persistentDataPath + @"\" + "ModdingApi.nop";
/// <summary>
/// ModHooks singleton that persists throughout the game process lifetime
/// </summary>
private static ModHooks _instance;
public List<string> LoadedMods = new List<string>();
/// <summary>
/// Log Console instance
/// </summary>
private Console _console;
/// <summary>
/// Mod API Version Major Number
/// </summary>
private const int _modVersionMajor = 3;
/// <summary>
/// Mod API Version Minor Number
/// </summary>
private const int _modVersionMinor = 2;
/// <summary>
/// Mod API Version string in "vX.Y" format
/// </summary>
public string ModVersion
{
get
{
return $"v{_modVersionMajor}.{_modVersionMinor}";
}
}
private ModHooks()
{
IsInitialized = true;
}
/// <summary>
/// Property for ModHooks Singleton. This getter should be used by mods to subscribe to events.
/// </summary>
public static ModHooks Instance
{
get
{
if (_instance != null) return _instance;
_instance = new ModHooks();
_instance.LogConsole("Pepperoni Hook API is live!\n");
return _instance;
}
}
/// <summary>
/// Internal function for displaying messages on console window
/// </summary>
/// <param name="message">Message to be added to console output</param>
[System.Diagnostics.Conditional("DEBUG")]
public void LogConsole(string message)
{
if (_console == null)
{
GameObject go = new GameObject();
UnityEngine.Object.DontDestroyOnLoad(go);
_console = go.AddComponent<Console>();
}
_console.AddText(message);
}
#region HookApi
private event CollectibleCallback _GetCollectibleHook;
/// <summary>
/// Event Subscriber for collectible collision
/// </summary>
public event CollectibleCallback GetCollectibleHook
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding GetCollectibleHook");
_GetCollectibleHook += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing GetCollectibleHook");
_GetCollectibleHook -= value;
}
}
internal void OnGetCollectible(int currentCol, Collider other)
{
// Too much chatter
// Logger.LogDebug($"[API] - OnGetCollectible invoked");
if (_GetCollectibleHook == null) return;
Delegate[] invocationList = _GetCollectibleHook.GetInvocationList();
foreach (CollectibleCallback i in invocationList)
{
try
{
i.Invoke(currentCol, other);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event BeforeSceneLoadCallback _BeforeSceneLoad;
/// <summary>
/// Event Subscriber for Before Scene Load
/// </summary>
public event BeforeSceneLoadCallback BeforeSceneLoad
{
add
{
_BeforeSceneLoad += value;
}
remove
{
_BeforeSceneLoad -= value;
}
}
internal void OnBeforeSceneLoad(string scene)
{
if (_BeforeSceneLoad == null) return;
Delegate[] invocationList = _BeforeSceneLoad.GetInvocationList();
foreach (BeforeSceneLoadCallback i in invocationList)
{
try
{
i.Invoke(scene);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event AfterSceneLoadCallback _AfterSceneLoad;
/// <summary>
/// Event Subscriber for Scene Load
/// </summary>
public event AfterSceneLoadCallback AfterSceneLoad
{
add
{
_AfterSceneLoad += value;
}
remove
{
_AfterSceneLoad -= value;
}
}
internal void OnAfterSceneLoad(string scene)
{
if (_AfterSceneLoad == null) return;
Delegate[] invocationList = _AfterSceneLoad.GetInvocationList();
foreach (AfterSceneLoadCallback i in invocationList)
{
try
{
i.Invoke(scene);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event NewGameStartedCallback _NewGameStart;
/// <summary>
/// Event Subscriber for Scene Load
/// </summary>
public event NewGameStartedCallback NewGameStart
{
add
{
_NewGameStart += value;
}
remove
{
_NewGameStart -= value;
}
}
internal void OnNewGameStart()
{
if (_NewGameStart == null) return;
Delegate[] invocationList = _NewGameStart.GetInvocationList();
foreach (NewGameStartedCallback i in invocationList)
{
try
{
i.Invoke();
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event PlayerUnLoadedCallback _PlayerUnLoaded;
public event PlayerUnLoadedCallback PlayerUnLoaded
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding PlayerUnLoaded");
_PlayerUnLoaded += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing PlayerUnLoaded");
_PlayerUnLoaded -= value;
}
}
/// <summary>
/// Invoked once the player object state is marked as loaded
/// </summary>
internal void OnPlayerUnLoad()
{
Logger.LogFine($"[API] - OnPlayerUnLoad invoked");
if (_PlayerUnLoaded == null) return;
Delegate[] invocationList = _PlayerUnLoaded.GetInvocationList();
foreach (PlayerUnLoadedCallback i in invocationList)
{
try
{
i.Invoke();
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event SaveGameBeforeSaveCallback _OnPreSaveGameHook;
public event SaveGameBeforeSaveCallback OnPreSaveGameHook
{
add { _OnPreSaveGameHook += value; }
remove { _OnPreSaveGameHook -= value; }
}
internal void OnSaveGameBeforeSave()
{
Logger.LogFine($"[API] - OnSaveGameBeforeSave invoked");
if (_OnPreSaveGameHook == null) return;
Delegate[] invocationList = _OnPreSaveGameHook.GetInvocationList();
foreach (SaveGameBeforeSaveCallback i in invocationList)
{
try
{
i.Invoke();
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event SaveGameAfterSaveCallback _OnPostSaveGameHook;
public event SaveGameAfterSaveCallback OnPostSaveGameHook
{
add { _OnPostSaveGameHook += value; }
remove { _OnPostSaveGameHook -= value; }
}
internal void OnSaveGameAfterSave()
{
Logger.LogFine($"[API] - OnSaveGameAfterSave invoked");
if (_OnPostSaveGameHook == null) return;
Delegate[] invocationList = _OnPostSaveGameHook.GetInvocationList();
foreach (SaveGameAfterSaveCallback i in invocationList)
{
try
{
i.Invoke();
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event ParseScriptProxy _OnParseScriptHook;
public event ParseScriptProxy OnParseScriptHook
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding OnParseScriptHook");
_OnParseScriptHook += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing OnParseScriptHook");
_OnParseScriptHook -= value;
}
}
internal string OnParseScript(string text)
{
Logger.LogFine($"[API] - OnParseScript invoked");
if (_OnParseScriptHook == null) return text;
string retVal = text;
Delegate[] invocationList = _OnParseScriptHook.GetInvocationList();
foreach (ParseScriptProxy i in invocationList)
{
try
{
retVal = i.Invoke(retVal);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
return retVal;
}
private event PlayerSetCostumeCallback _OnPlayerSetCostumeHook;
public event PlayerSetCostumeCallback OnPlayerSetCostumeHook
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding OnPlayerSetCostume");
_OnPlayerSetCostumeHook += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing OnPlayerSetCostumeHook");
_OnPlayerSetCostumeHook -= value;
}
}
internal void OnPlayerSetCostume(SkinnedMeshRenderer skinnedMeshRenderer)
{
Logger.LogFine($"[API] - OnPlayerSetCostume invoked");
if (_OnPlayerSetCostumeHook == null) return;
Delegate[] invocationList = _OnPlayerSetCostumeHook.GetInvocationList();
foreach (PlayerSetCostumeCallback i in invocationList)
{
try
{
i.Invoke(skinnedMeshRenderer);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event PlayerEarlyUpdateCallback _OnPlayerEarlyUpdateHook;
public event PlayerEarlyUpdateCallback OnPlayerEarlyUpdateHook
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding OnPlayerEarlyUpdateHook");
_OnPlayerEarlyUpdateHook += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing OnPlayerEarlyUpdateHook");
_OnPlayerEarlyUpdateHook -= value;
}
}
internal void OnPlayerEarlyUpdate(PlayerMachine playerMachine)
{
// Too much chatter
// Logger.LogFine($"[API] - OnPlayerEarlyUpdate invoked");
if (_OnPlayerEarlyUpdateHook == null) return;
Delegate[] invocationList = _OnPlayerEarlyUpdateHook.GetInvocationList();
foreach (PlayerEarlyUpdateCallback i in invocationList)
{
try
{
i.Invoke(playerMachine);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
}
private event SpeakerPlaySoundProxy _OnSpeakerPlayHook;
public event SpeakerPlaySoundProxy OnSpeakerPlayHook
{
add
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Adding OnSpeakerPlayHook");
_OnSpeakerPlayHook += value;
}
remove
{
Logger.LogDebug($"[{value.Method.DeclaringType?.Name}] - Removing OnSpeakerPlayHook");
_OnSpeakerPlayHook -= value;
}
}
internal AudioClip OnSpeakerPlay(AudioClip clip)
{
//Logger.LogFine($"[API] - OnSpeakerPlay invoked");
if (_OnSpeakerPlayHook == null) return clip;
AudioClip retVal = null;
Delegate[] invocationList = _OnSpeakerPlayHook.GetInvocationList();
foreach (SpeakerPlaySoundProxy i in invocationList)
{
try
{
retVal = i.Invoke(clip);
}
catch (Exception ex)
{
Logger.LogError("[API] - " + ex);
}
}
return retVal;
}
#endregion
}
}