-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModManagerAPI.cs
More file actions
396 lines (335 loc) · 15.6 KB
/
ModManagerAPI.cs
File metadata and controls
396 lines (335 loc) · 15.6 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
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
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace CustomModManager.API
{
public static class ModManagerAPI
{
private static string CORE_ASSEMBLY_ID = "ModManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
private static bool initialized = false;
private static Assembly CORE_ASSEMBLY;
static ModManagerAPI()
{
TryDetectAssembly();
}
private static void TryDetectAssembly()
{
if (initialized)
return;
CORE_ASSEMBLY = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(assembly => assembly.FullName == CORE_ASSEMBLY_ID);
initialized = true;
}
public static bool IsModManagerLoaded()
{
return CORE_ASSEMBLY != null;
}
public static ModSettings GetModSettings(global::Mod modInstance)
{
if (!IsModManagerLoaded())
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Attempted to create mod settings while mod manager is not installed.");
return new ModSettings(modInstance, null);
}
try
{
return new ModSettings(modInstance, CORE_ASSEMBLY.CreateInstance("CustomModManager.ModManagerModSettings", true, 0, null, new object[] { modInstance }, null, null));
}
catch
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Failed to locate ModSettings instance in Mod Manager. Perhaps an out-of-date API version is being used?");
return new ModSettings(modInstance, null);
}
}
public class ModSettings
{
private readonly global::Mod modInstance;
private readonly object instance;
public ModSettings(global::Mod modInstance, object instance)
{
this.modInstance = modInstance;
this.instance = instance;
}
public ModSetting<T> Hook<T>(string key, string nameUnlocalized, Action<T> setCallback, Func<T> getCallback, Func<T, (string unformatted, string formatted)> toString, Func<string, (T, bool)> fromString)
{
try
{
MethodInfo method = CORE_ASSEMBLY.GetType("CustomModManager.API.IModSettings").GetMethods().Single(m => m.Name == "Hook" && m.IsGenericMethod && m.IsVirtual).MakeGenericMethod(typeof(T));
object settingInstance = method.Invoke(instance, new object[] { key, nameUnlocalized, setCallback, getCallback, toString, fromString });
return new ModSetting<T>(this, key, settingInstance);
}
catch
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Failed to create Mod Setting instance. Perhaps an out-of-date API version is being used?");
}
return new ModSetting<T>(this, key, null);
}
public ModSetting<string> Category(string key, string nameUnlocalized)
{
try
{
MethodInfo method = CORE_ASSEMBLY.GetType("CustomModManager.API.IModSettings").GetMethods().Single(m => m.Name == "Category");
object settingInstance = method.Invoke(instance, new object[] { key, nameUnlocalized });
return new ModSetting<string>(this, key, settingInstance);
}
catch
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Failed to create Mod Setting instance. Perhaps an out-of-date API version is being used?");
}
return new ModSetting<string>(this, key, null);
}
public ModSetting<string> Button(string key, string nameUnlocalized, Action clickCallback, Func<string> buttonText)
{
try
{
MethodInfo method = CORE_ASSEMBLY.GetType("CustomModManager.API.IModSettings").GetMethods().Single(m => m.Name == "Button");
object settingInstance = method.Invoke(instance, new object[] { key, nameUnlocalized, clickCallback, buttonText });
return new ModSetting<string>(this, key, settingInstance);
}
catch
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Failed to create Mod Setting instance. Perhaps an out-of-date API version is being used?");
}
return new ModSetting<string>(this, key, null);
}
public ModSetting<T> Hook<T>(string key, string nameUnlocalized, Action<T> setCallback, Func<T> getCallback, Func<T, string> toString, Func<string, (T, bool)> fromString)
{
return Hook(key, nameUnlocalized, setCallback, getCallback, (value) =>
{
string valueAsString = toString(value);
return (valueAsString, valueAsString);
}, fromString);
}
public void CreateTab(string key, string nameUnlocalized)
{
try
{
MethodInfo method = CORE_ASSEMBLY.GetType("CustomModManager.API.IModSettings").GetMethods().Single(m => m.Name == "CreateTab" && m.IsVirtual);
method.Invoke(instance, new object[] { key, nameUnlocalized });
}
catch
{
Log.Warning($"[{modInstance.Name}] [Mod Manager API] Failed to create Mod Setting tab. Perhaps an out-of-date API version is being used?");
}
}
public class ModSetting<T>
{
private readonly ModSettings settingsInstance;
private readonly string key;
private readonly object instance;
internal ModSetting(ModSettings settings, string key, object instance)
{
this.settingsInstance = settings;
this.key = key;
this.instance = instance;
}
public ModSetting<T> SetAllowedValues(T[] allowedValues)
{
try
{
TryInvokeMethod("SetAllowedValues", allowedValues);
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to set allowed values for mod setting {this.key}");
}
return this;
}
public ModSetting<T> SetTab(string tabKey)
{
try
{
TryInvokeMethod("SetTab", tabKey);
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to set tab key {tabKey} for mod setting {this.key}");
}
return this;
}
public ModSetting<T> SetMinimumMaximumAndIncrementValues(T minimumValue, T maximumValue, T incrementValue)
{
try
{
TryInvokeMethod("SetMinimumMaximumAndIncrementValues", minimumValue, maximumValue, incrementValue);
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to set minimum/maximum/increment values for mod setting {this.key}");
}
return this;
}
public ModSetting<T> SetWrap(bool wrap)
{
try
{
TryInvokeMethod("SetWrap", wrap);
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to set wrap flag for mod setting {this.key}");
}
return this;
}
public void Update()
{
try
{
TryInvokeMethod("Update", new object[] { });
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to update mod setting {this.key}");
}
}
public ModSetting<T> SetEnabled(Func<bool> enabled)
{
try
{
TryInvokeMethod("SetEnabled", enabled);
}
catch
{
Log.Warning($"[{settingsInstance.modInstance.Name}] [Mod Manager API] [Mod Settings] Failed to set enabled selector for mod setting {this.key}");
}
return this;
}
private void TryInvokeMethod(string name, params object[] parameters)
{
if (instance == null)
return;
MethodInfo setAllowedValuesMethod = CORE_ASSEMBLY.GetType("CustomModManager.API.IModSetting`1[[" + typeof(T).AssemblyQualifiedName + "]]").GetMethods().Single(m => m.Name == name && m.IsVirtual);
setAllowedValuesMethod.Invoke(instance, parameters);
}
}
}
#if MM_API_EXTENSIONS
public static class Extensions
{
public static void AddStartupMessage(string title, string message)
{
StartupMessageBox.startupMessages.Add(new StartupMessageBox.StartupMessage(title, message));
}
private static class StartupMessageBox
{
public class StartupMessage
{
public readonly string Title;
public readonly string Message;
public StartupMessage(string title, string message)
{
this.Title = title;
this.Message = message;
}
}
public static List<StartupMessage> startupMessages = new System.Collections.Generic.List<StartupMessage>();
[HarmonyPatch(typeof(XUiC_MainMenu))]
[HarmonyPatch("Open")]
class XUiC_MainMenu_Open_Extension
{
static bool mainMenuLoaded = false;
static void Postfix(XUi _xuiInstance)
{
if (mainMenuLoaded)
return;
mainMenuLoaded = true;
if (startupMessages.Count > 0)
{
ConstructNextMessageAction(_xuiInstance, 0).Invoke();
}
}
private static Action ConstructNextMessageAction(XUi _xuiInstance, int currentReadCount)
{
return () =>
{
StartupMessage message = startupMessages[currentReadCount];
XUiC_MessageBoxWindowGroup.ShowMessageBox(_xuiInstance, message.Title, message.Message, () =>
{
if (++currentReadCount < startupMessages.Count)
{
ConstructNextMessageAction(_xuiInstance, currentReadCount);
}
}, ++currentReadCount >= startupMessages.Count);
};
}
}
}
public static void AddLoadingScreenTip(string title, string message)
{
LoadingScreenTips.tips.Add(new LoadingScreenTips.LoadingScreenTip(title, message));
}
private static class LoadingScreenTips
{
private static readonly FieldInfo tipsField = AccessTools.DeclaredField(typeof(XUiC_LoadingScreen), "tips");
private static readonly FieldInfo currentTipIndexField = AccessTools.DeclaredField(typeof(XUiC_LoadingScreen), "currentTipIndex");
public static readonly List<LoadingScreenTip> tips = new List<LoadingScreenTip>();
private static List<string> GetTips()
{
return (List<string>)tipsField.GetValue(null);
}
public class LoadingScreenTip
{
public readonly string Title;
public readonly string Message;
public LoadingScreenTip(string title, string message)
{
this.Title = title;
this.Message = message;
}
}
[HarmonyPatch(typeof(XUiC_LoadingScreen))]
[HarmonyPatch("OnOpen")]
class XUiC_LoadingScreen_OnOpen_Extension
{
static void Prefix(XUiC_LoadingScreen __instance)
{
for(int i = 0; i < tips.Count; i++)
GetTips().Add("mm-loadingscreen-tip" + i);
}
}
[HarmonyPatch(typeof(XUiC_LoadingScreen))]
[HarmonyPatch("OnClose")]
class XUiC_LoadingScreen_OnClose_Extension
{
static void Prefix(XUiC_LoadingScreen __instance)
{
for(int i = 0; i < tips.Count; i++)
GetTips().RemoveAt(GetTips().Count - 1);
}
}
[HarmonyPatch(typeof(XUiC_LoadingScreen))]
[HarmonyPatch("GetBindingValue")]
class XUiC_LoadingScreen_GetBindingValue_Extension
{
static bool Prefix(XUiC_LoadingScreen __instance, ref string _value, string _bindingName, ref bool __result)
{
int currentTipIndex = (int)currentTipIndexField.GetValue(__instance);
if (currentTipIndex >= 0)
{
string currentTip = GetTips()[currentTipIndex];
if (currentTip.StartsWith("mm-loadingscreen-tip"))
{
if (int.TryParse(currentTip.Substring(20), out int index))
{
switch (_bindingName)
{
case "title":
_value = tips[index].Title;
break;
case "text":
_value = tips[index].Message;
break;
}
return false;
}
}
}
return true;
}
}
}
}
#endif
}
}