-
Notifications
You must be signed in to change notification settings - Fork 80
/
EverestModule.cs
906 lines (766 loc) · 40.3 KB
/
EverestModule.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
using Celeste.Mod.Core;
using Celeste.Mod.Helpers;
using Celeste.Mod.UI;
using FMOD.Studio;
using Monocle;
using MonoMod;
using MonoMod.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Celeste.Mod {
/// <summary>
/// Your Everest main mod class inherits from this class.
/// </summary>
public abstract class EverestModule {
/// <summary>
/// Used by Everest itself to store any module metadata.
///
/// The metadata is usually parsed from meta.yaml in the archive.
///
/// You can override this property to provide dynamic metadata at runtime.
/// Doing so isn't advised though unless you absolutely know what you're doing.
/// Note that this doesn't affect mod loading.
/// </summary>
public virtual EverestModuleMetadata Metadata { get; set; }
/// <summary>
/// The type used for the settings object. Used for serialization, among other things.
/// </summary>
public virtual Type SettingsType => null;
/// <summary>
/// Any settings stored across runs. Everest loads this before Load gets invoked.
/// Define your custom property returning _Settings typecasted as your custom settings type.
/// </summary>
public virtual EverestModuleSettings _Settings { get; set; }
/// <summary>
/// The type used for the save data object. Used for serialization, among other things.
/// </summary>
public virtual Type SaveDataType => null;
/// <summary>
/// Any save data stored across runs.
/// Define your custom property returning _SaveData typecasted as your custom save data type.
/// </summary>
public virtual EverestModuleSaveData _SaveData { get; set; }
/// <summary>
/// Whether the save and session data can be saved asynchronously and separately by Everest.
/// Doing so will use [Read,Write,Deserialize,Serialize][SaveData,Session].
/// Otherwise, the obsolete and forcibly synchronous [Load,Save,Delete] methods will be used.
/// Defaults to true; automatically gets set to false if you override an old method without overriding any new one.
/// </summary>
public virtual bool SaveDataAsync { get; set; }
private bool ForceSaveDataAsync;
internal int ForceSaveDataFlush;
/// <summary>
/// The type used for the session object. Used for serialization, among other things.
/// </summary>
public virtual Type SessionType => null;
/// <summary>
/// Any save data stored for the current session.
/// Define your custom property returning _Session typecasted as your custom session type.
/// </summary>
public virtual EverestModuleSession _Session { get; set; }
public EverestModule() {
// Default to async as long as all old methods stay the same.
SaveDataAsync |=
typeof(EverestModule) == GetType().GetMethod(nameof(LoadSaveData)).DeclaringType &&
typeof(EverestModule) == GetType().GetMethod(nameof(SaveSaveData)).DeclaringType &&
typeof(EverestModule) == GetType().GetMethod(nameof(DeleteSaveData)).DeclaringType &&
typeof(EverestModule) == GetType().GetMethod(nameof(LoadSession)).DeclaringType &&
typeof(EverestModule) == GetType().GetMethod(nameof(SaveSession)).DeclaringType &&
typeof(EverestModule) == GetType().GetMethod(nameof(DeleteSession)).DeclaringType;
// Prefer async if the mod overrides any new method.
SaveDataAsync |=
typeof(EverestModule) != GetType().GetMethod(nameof(ReadSaveData)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(DeserializeSaveData)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(SerializeSaveData)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(WriteSaveData)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(ReadSession)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(DeserializeSession)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(SerializeSession)).DeclaringType ||
typeof(EverestModule) != GetType().GetMethod(nameof(WriteSession)).DeclaringType;
if (!SaveDataAsync)
Logger.Log(LogLevel.Warn, "EverestModule", $"{GetType().FullName} doesn't support save data async IO!");
}
/// <summary>
/// Load the mod settings. Loads the settings from {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.celeste by default.
/// </summary>
public virtual void LoadSettings() {
if (SettingsType == null)
return;
_Settings = (EverestModuleSettings) SettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);
// Temporary fallback to help migrate settings from their old location.
if (!File.Exists(path))
path = Path.Combine(Everest.PathEverest, "ModSettings-OBSOLETE", Metadata.Name + ".yaml");
if (!File.Exists(path))
return;
try {
using (Stream stream = File.OpenRead(path)) {
if (_Settings is EverestModuleBinarySettings) {
using (BinaryReader reader = new BinaryReader(stream))
((EverestModuleBinarySettings) _Settings).Read(reader);
} else {
using (StreamReader reader = new StreamReader(stream))
YamlHelper.DeserializerUsing(_Settings).Deserialize(reader, SettingsType);
}
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to load the settings of {Metadata.Name}!");
Logger.LogDetailed(e);
}
if (_Settings == null)
_Settings = (EverestModuleSettings) SettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
}
/// <summary>
/// Save the mod settings. Saves the settings to {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.yaml by default.
/// </summary>
public virtual void SaveSettings() {
bool forceFlush = ForceSaveDataFlush > 0;
if (forceFlush)
ForceSaveDataFlush--;
if (SettingsType == null || _Settings == null)
return;
string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);
if (File.Exists(path))
File.Delete(path);
Directory.CreateDirectory(Path.GetDirectoryName(path));
try {
using (FileStream stream = File.OpenWrite(path)) {
if (_Settings is EverestModuleBinarySettings) {
using (BinaryWriter writer = new BinaryWriter(stream)) {
((EverestModuleBinarySettings) _Settings).Write(writer);
if (forceFlush || ((CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
stream.Flush(true);
}
} else {
using (StreamWriter writer = new StreamWriter(stream)) {
YamlHelper.Serializer.Serialize(writer, _Settings, SettingsType);
if (forceFlush || ((CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
stream.Flush(true);
}
}
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to save the settings of {Metadata.Name}!");
Logger.LogDetailed(e);
}
}
/// <summary>
/// Load the mod save data. Loads the save data from {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
/// </summary>
[Obsolete("Override DeserializeSaveData and ReadSaveData instead.")]
public virtual void LoadSaveData(int index) {
ForceSaveDataAsync = true;
DeserializeSaveData(index, ReadSaveData(index));
ForceSaveDataAsync = false;
}
/// <summary>
/// Save the mod save data. Saves the save data to {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
/// </summary>
///
[Obsolete("Override SerializeSaveData and WriteSaveData instead.")]
public virtual void SaveSaveData(int index) {
ForceSaveDataAsync = true;
WriteSaveData(index, SerializeSaveData(index));
ForceSaveDataAsync = false;
}
/// <summary>
/// Delete the mod save data. Deletes the save data at {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
/// </summary>
[Obsolete("Override WriteSaveData and handle null data instead.")]
public virtual void DeleteSaveData(int index) {
ForceSaveDataAsync = true;
WriteSaveData(index, null);
ForceSaveDataAsync = false;
}
/// <summary>
/// Read the mod save data bytes from a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
/// </summary>
public virtual byte[] ReadSaveData(int index) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SaveDataType == null)
return null;
string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsave-" + Metadata.Name);
if (!File.Exists(path))
return null;
try {
return File.ReadAllBytes(path);
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to read the save data of {Metadata.Name}!");
Logger.LogDetailed(e);
return null;
}
}
/// <summary>
/// Write the mod save data bytes into a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsave-{Metadata.Name}.celeste by default.
/// </summary>
public virtual void WriteSaveData(int index, byte[] data) {
bool forceFlush = ForceSaveDataFlush > 0;
if (forceFlush)
ForceSaveDataFlush--;
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SaveDataType == null)
return;
string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsave-" + Metadata.Name);
if (File.Exists(path))
File.Delete(path);
if (data == null)
return;
Directory.CreateDirectory(Path.GetDirectoryName(path));
try {
using (FileStream stream = File.OpenWrite(path)) {
stream.Write(data, 0, data.Length);
if (forceFlush || (SaveDataAsync && (CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
stream.Flush(true);
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to write the save data of {Metadata.Name}!");
Logger.LogDetailed(e);
}
}
/// <summary>
/// Deserialize the mod save data from its raw bytes, fed with data from ReadSaveData either immediately or async.
/// </summary>
public virtual void DeserializeSaveData(int index, byte[] data) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SaveDataType == null)
return;
_SaveData = (EverestModuleSaveData) SaveDataType.GetConstructor(Type.EmptyTypes).Invoke(null);
_SaveData.Index = index;
if (data == null)
return;
try {
using (MemoryStream stream = new MemoryStream(data)) {
if (_SaveData is EverestModuleBinarySaveData bsd) {
using (BinaryReader reader = new BinaryReader(stream))
bsd.Read(reader);
} else {
using (StreamReader reader = new StreamReader(stream))
YamlHelper.DeserializerUsing(_SaveData).Deserialize(reader, SaveDataType);
}
}
_SaveData.Index = index;
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to deserialize the save data of {Metadata.Name}!");
Logger.LogDetailed(e);
}
}
/// <summary>
/// Serialize the mod save data into its raw bytes, to be fed into WriteSaveData immediately or async.
/// </summary>
public virtual byte[] SerializeSaveData(int index) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SaveDataType == null)
return null;
try {
using (MemoryStream stream = new MemoryStream()) {
if (_SaveData is EverestModuleBinarySaveData bsd) {
using (BinaryWriter writer = new BinaryWriter(new UndisposableStream(stream)))
bsd.Write(writer);
} else {
using (StreamWriter writer = new StreamWriter(new UndisposableStream(stream)))
YamlHelper.Serializer.Serialize(writer, _SaveData, SaveDataType);
}
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to serialize the save data of {Metadata.Name}!");
Logger.LogDetailed(e);
return null;
}
}
/// <summary>
/// Load the mod session. Loads the session from {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
/// </summary>
[Obsolete("Override DeserializeSession and ReadSession instead.")]
public virtual void LoadSession(int index, bool forceNew) {
ForceSaveDataAsync = true;
DeserializeSession(index, forceNew ? null : ReadSession(index));
ForceSaveDataAsync = false;
}
/// <summary>
/// Save the mod session. Saves the session to {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
/// </summary>
[Obsolete("Override SerializeSession and WriteSession instead.")]
public virtual void SaveSession(int index) {
ForceSaveDataAsync = true;
WriteSession(index, SerializeSession(index));
ForceSaveDataAsync = false;
}
/// <summary>
/// Delete the mod session. Deletes the session at {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
/// </summary>
[Obsolete("Override WriteSession and handle null data instead.")]
public virtual void DeleteSession(int index) {
ForceSaveDataAsync = true;
WriteSession(index, null);
ForceSaveDataAsync = false;
}
/// <summary>
/// Read the mod session bytes from a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
/// </summary>
public virtual byte[] ReadSession(int index) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SessionType == null)
return null;
string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);
if (!File.Exists(path))
return null;
try {
return File.ReadAllBytes(path);
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to read the session of {Metadata.Name}!");
Logger.LogDetailed(e);
return null;
}
}
/// <summary>
/// Write the mod session bytes into a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
/// </summary>
public virtual void WriteSession(int index, byte[] data) {
bool forceFlush = ForceSaveDataFlush > 0;
if (forceFlush)
ForceSaveDataFlush--;
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SessionType == null)
return;
string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);
if (File.Exists(path))
File.Delete(path);
if (data == null)
return;
Directory.CreateDirectory(Path.GetDirectoryName(path));
try {
using (FileStream stream = File.OpenWrite(path)) {
stream.Write(data, 0, data.Length);
if (forceFlush || (SaveDataAsync && (CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
stream.Flush(true);
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to write the session of {Metadata.Name}!");
Logger.LogDetailed(e);
}
}
/// <summary>
/// Deserialize the mod session from its raw bytes, fed with data from ReadSession either immediately or async.
/// </summary>
public virtual void DeserializeSession(int index, byte[] data) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SessionType == null)
return;
_Session = (EverestModuleSession) SessionType.GetConstructor(Type.EmptyTypes).Invoke(null);
_Session.Index = index;
if (data == null)
return;
try {
using (MemoryStream stream = new MemoryStream(data)) {
if (_Session is EverestModuleBinarySession bs) {
using (BinaryReader reader = new BinaryReader(stream))
bs.Read(reader);
} else {
using (StreamReader reader = new StreamReader(stream))
YamlHelper.DeserializerUsing(_Session).Deserialize(reader, SessionType);
}
}
_Session.Index = index;
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to deserialize the session of {Metadata.Name}!");
Logger.LogDetailed(e);
}
}
/// <summary>
/// Serialize the mod session into its raw bytes, to be fed into WriteSession immediately or async.
/// </summary>
public virtual byte[] SerializeSession(int index) {
if (!SaveDataAsync && !ForceSaveDataAsync)
throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
if (SessionType == null)
return null;
try {
using (MemoryStream stream = new MemoryStream()) {
if (_Session is EverestModuleBinarySession bs) {
using (BinaryWriter writer = new BinaryWriter(new UndisposableStream(stream)))
bs.Write(writer);
} else {
using (StreamWriter writer = new StreamWriter(new UndisposableStream(stream)))
YamlHelper.Serializer.Serialize(writer, _Session, SessionType);
}
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to serialize the session of {Metadata.Name}!");
Logger.LogDetailed(e);
return null;
}
}
/// <summary>
/// Perform any initializing actions after all mods have been loaded.
/// Do not depend on any specific order in which the mods get initialized.
/// </summary>
public abstract void Load();
/// <summary>
/// Perform any initializing actions after Celeste.Initialize has been called.
/// Do not depend on any specific order in which the mods get initialized.
/// </summary>
public virtual void Initialize() {
}
/// <summary>
/// Perform any content loading actions after Celeste.LoadContent has been called.
/// </summary>
[Obsolete("Override LoadContent(bool firstLoad) instead.")]
public virtual void LoadContent() {
}
/// <summary>
/// Perform any content loading actions after Celeste.LoadContent has been called.
/// </summary>
/// <param name="firstLoad">Is this the first load?</param>
public virtual void LoadContent(bool firstLoad) {
#pragma warning disable CS0618 // Type or member is obsolete
LoadContent();
#pragma warning restore CS0618 // Type or member is obsolete
}
/// <summary>
/// Unload any unmanaged resources allocated by the mod (f.e. textures) and
/// undo any changes performed by the mod.
/// </summary>
public abstract void Unload();
/// <summary>
/// Parse the current command-line argument and any follow-ups.
/// </summary>
/// <param name="arg">The current command line argument.</param>
/// <param name="args">Any further arguments the mod may want to dequeue and parse.</param>
/// <returns>True if the argument "belongs" to the mod, false otherwise.</returns>
public virtual bool ParseArg(string arg, Queue<string> args) {
return false;
}
public virtual void OnInputInitialize() {
if (SettingsType == null)
return;
object settings = _Settings;
if (settings == null)
return;
foreach (PropertyInfo prop in SettingsType.GetProperties()) {
if (!prop.CanRead)
continue;
if (typeof(ButtonBinding).IsAssignableFrom(prop.PropertyType)) {
InitializeButtonBinding(settings, prop);
} else if (false) {
// TODO: JoystickBindings
}
}
}
private void InitializeButtonBinding(object settings, PropertyInfo prop) {
if (!(prop.GetValue(settings) is ButtonBinding binding)) {
binding = new ButtonBinding();
DefaultButtonBindingAttribute defaults = prop.GetCustomAttribute<DefaultButtonBindingAttribute>();
if (defaults != null) {
if (defaults.Button != 0)
binding.Binding.Add(defaults.Button);
if (defaults.Key != 0)
binding.Binding.Add(defaults.Key);
if (defaults.Buttons != null)
binding.Binding.Add(defaults.Buttons.Where(b => b != 0).ToArray());
if (defaults.Keys != null)
binding.Binding.Add(defaults.Keys.Where(k => k != 0).ToArray());
}
prop.SetValue(settings, binding);
}
binding.Button = (patch_VirtualButton) new VirtualButton(binding.Binding, Input.Gamepad, 0.08f, 0.2f);
binding.Button.AutoConsumeBuffer = true;
}
public virtual void OnInputDeregister() {
if (SettingsType == null)
return;
object settings = _Settings;
foreach (PropertyInfo prop in SettingsType.GetProperties()) {
if (!prop.CanRead)
continue;
if (typeof(ButtonBinding).IsAssignableFrom(prop.PropertyType)) {
if (!(prop.GetValue(settings) is ButtonBinding binding))
continue;
binding.Button?.Deregister();
} else if (false) {
// TODO: JoystickBindings
}
}
}
protected virtual void CreateModMenuSectionHeader(TextMenu menu, bool inGame, EventInstance snapshot) {
Type type = SettingsType;
EverestModuleSettings settings = _Settings;
if (type == null || settings == null)
return;
string typeName = type.Name.ToLowerInvariant();
if (typeName.EndsWith("settings"))
typeName = typeName.Substring(0, typeName.Length - 8);
string nameDefaultPrefix = $"modoptions_{typeName}_";
string name; // We lazily reuse this field for the props later on.
name = type.GetCustomAttribute<SettingNameAttribute>()?.Name ?? $"{nameDefaultPrefix}title";
name = name.DialogCleanOrNull() ?? ModUpdaterHelper.FormatModName(Metadata.Name);
menu.Add(new patch_TextMenu.patch_SubHeader(name + " | v." + Metadata.VersionString));
}
protected virtual void CreateModMenuSectionKeyBindings(TextMenu menu, bool inGame, EventInstance snapshot) {
menu.Add(new TextMenu.Button(Dialog.Clean("options_keyconfig")).Pressed(() => {
menu.Focused = false;
Engine.Scene.Add(CreateKeyboardConfigUI(menu));
Engine.Scene.OnEndOfFrame += () => Engine.Scene.Entities.UpdateLists();
}));
menu.Add(new TextMenu.Button(Dialog.Clean("options_btnconfig")).Pressed(() => {
menu.Focused = false;
Engine.Scene.Add(CreateButtonConfigUI(menu));
Engine.Scene.OnEndOfFrame += () => Engine.Scene.Entities.UpdateLists();
}));
}
[MonoModReplace]
private Entity CreateKeyboardConfigUI(TextMenu menu) {
return new ModuleSettingsKeyboardConfigUI(this) {
OnClose = () => menu.Focused = true
};
}
[MonoModReplace]
private Entity CreateButtonConfigUI(TextMenu menu) {
return new ModuleSettingsButtonConfigUI(this) {
OnClose = () => menu.Focused = true
};
}
private Type _PrevSettingsType;
private PropertyInfo[] _PrevSettingsProps;
/// <summary>
/// Create the mod menu subsection including the section header in the given menu.
/// The default implementation uses reflection to attempt creating a menu.
/// </summary>
/// <param name="menu">Menu to add the section to.</param>
/// <param name="inGame">Whether we're in-game (paused) or in the main menu.</param>
/// <param name="snapshot">The Level.PauseSnapshot</param>
public virtual void CreateModMenuSection(patch_TextMenu menu, bool inGame, EventInstance snapshot) {
Type type = SettingsType;
EverestModuleSettings settings = _Settings;
if (type == null || settings == null)
return;
// The default name prefix.
string typeName = type.Name.ToLowerInvariant();
if (typeName.EndsWith("settings"))
typeName = typeName.Substring(0, typeName.Length - 8);
string nameDefaultPrefix = $"modoptions_{typeName}_";
// Any attributes we may want to get and read from later.
SettingInGameAttribute attribInGame;
SettingRangeAttribute attribRange;
SettingNumberInputAttribute attribNumber;
// If the settings type has got the InGame attrib, only show it in the matching situation.
if ((attribInGame = type.GetCustomAttribute<SettingInGameAttribute>()) != null &&
attribInGame.InGame != inGame)
return;
bool headerCreated = false;
if (GetType().GetMethod("CreateModMenuSection").DeclaringType != typeof(EverestModule)) {
CreateModMenuSectionHeader(menu, inGame, snapshot);
headerCreated = true;
}
PropertyInfo[] props;
if (type == _PrevSettingsType) {
props = _PrevSettingsProps;
} else {
_PrevSettingsProps = props = type.GetProperties();
_PrevSettingsType = type;
}
TextMenu.Item CreateItem(PropertyInfo prop, string name = null, object settingsObject = null) {
settingsObject ??= settings;
if ((attribInGame = prop.GetCustomAttribute<SettingInGameAttribute>()) != null &&
attribInGame.InGame != inGame)
return null;
if (prop.GetCustomAttribute<SettingIgnoreAttribute>() != null)
return null;
if (!prop.CanRead || !prop.CanWrite)
return null;
if (name == null) {
name = prop.GetCustomAttribute<SettingNameAttribute>()?.Name ?? $"{nameDefaultPrefix}{prop.Name.ToLowerInvariant()}";
name = name.DialogCleanOrNull() ?? prop.Name.SpacedPascalCase();
}
TextMenu.Item item = null;
Type propType = prop.PropertyType;
object value = prop.GetValue(settingsObject);
// Create the matching item based off of the type and attributes.
if (propType == typeof(bool)) {
item =
new TextMenu.OnOff(name, (bool) value)
.Change(v => prop.SetValue(settingsObject, v))
;
} else if (
propType == typeof(int) &&
(attribRange = prop.GetCustomAttribute<SettingRangeAttribute>()) != null
) {
if (attribRange.LargeRange) {
item =
new TextMenuExt.IntSlider(name, attribRange.Min, attribRange.Max, (int) value)
.Change(v => prop.SetValue(settingsObject, v))
;
} else {
item =
new TextMenu.Slider(name, i => i.ToString(), attribRange.Min, attribRange.Max, (int) value)
.Change(v => prop.SetValue(settingsObject, v))
;
}
} else if ((propType == typeof(int) || propType == typeof(float)) &&
(attribNumber = prop.GetCustomAttribute<SettingNumberInputAttribute>()) != null) {
float currentValue;
Action<float> valueSetter;
if (propType == typeof(int)) {
currentValue = (int) value;
valueSetter = v => prop.SetValue(settingsObject, (int) v);
} else {
currentValue = (float) value;
valueSetter = v => prop.SetValue(settingsObject, v);
}
int maxLength = attribNumber.MaxLength;
bool allowNegatives = attribNumber.AllowNegatives;
item =
new TextMenu.Button(name + ": " + currentValue.ToString($"F{maxLength}").TrimEnd('0').TrimEnd('.'))
.Pressed(() => {
Audio.Play(SFX.ui_main_savefile_rename_start);
menu.SceneAs<Overworld>().Goto<OuiNumberEntry>().Init<OuiModOptions>(
currentValue,
valueSetter,
maxLength,
propType == typeof(float),
allowNegatives
);
})
;
} else if (propType.IsEnum) {
Array enumValues = Enum.GetValues(propType);
Array.Sort((int[]) enumValues);
string enumNamePrefix = $"{nameDefaultPrefix}{prop.Name.ToLowerInvariant()}_";
item =
new TextMenu.Slider(name, (i) => {
string enumName = enumValues.GetValue(i).ToString();
return
$"{enumNamePrefix}{enumName.ToLowerInvariant()}".DialogCleanOrNull() ??
$"modoptions_{propType.Name.ToLowerInvariant()}_{enumName.ToLowerInvariant()}".DialogCleanOrNull() ??
enumName;
}, 0, enumValues.Length - 1, (int) value)
.Change(v => prop.SetValue(settingsObject, v))
;
} else if (!inGame && propType == typeof(string)) {
int maxValueLength = prop.GetCustomAttribute<SettingMaxLengthAttribute>()?.Max ?? 12;
int minValueLength = prop.GetCustomAttribute<SettingMinLengthAttribute>()?.Min ?? 1;
item =
new TextMenu.Button(name + ": " + value)
.Pressed(() => {
Audio.Play(SFX.ui_main_savefile_rename_start);
menu.SceneAs<Overworld>().Goto<OuiModOptionString>().Init<OuiModOptions>(
(string) value,
v => prop.SetValue(settingsObject, v),
maxValueLength,
minValueLength
);
})
;
}
return item;
}
foreach (PropertyInfo prop in props) {
string name = prop.GetCustomAttribute<SettingNameAttribute>()?.Name ?? $"{nameDefaultPrefix}{prop.Name.ToLowerInvariant()}";
name = name.DialogCleanOrNull() ?? prop.Name.SpacedPascalCase();
MethodInfo creator = type.GetMethod(
$"Create{prop.Name}Entry",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(TextMenu), typeof(bool) },
new ParameterModifier[0]
);
if (creator != null) {
if (!headerCreated) {
CreateModMenuSectionHeader(menu, inGame, snapshot);
headerCreated = true;
}
creator.CreateDelegate<Action<TextMenu, bool>>(settings)(menu, inGame);
continue;
}
TextMenu.Item item = CreateItem(prop, name);
if (item == null && prop.PropertyType.GetCustomAttribute<SettingSubMenuAttribute>() != null) {
object propObject = prop.GetValue(settings);
if (propObject == null)
prop.SetValue(settings, propObject = Activator.CreateInstance(prop.PropertyType));
TextMenuExt.SubMenu subMenu = new(name, false);
foreach (PropertyInfo subTypeProp in prop.PropertyType.GetProperties()) {
creator = prop.PropertyType.GetMethod(
$"Create{subTypeProp.Name}Entry",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(TextMenuExt.SubMenu), typeof(bool) },
new ParameterModifier[0]
);
if (creator != null) {
creator.CreateDelegate<Action<TextMenuExt.SubMenu, bool>>(propObject)(subMenu, inGame);
continue;
}
TextMenu.Item subMenuItem = CreateItem(subTypeProp, settingsObject: propObject);
if (subMenuItem == null)
continue;
string subsubheader = subTypeProp.GetCustomAttribute<SettingSubHeaderAttribute>()?.SubHeader;
if (subsubheader != null)
subMenu.Add(new TextMenu.SubHeader(subsubheader.DialogCleanOrNull() ?? subsubheader, false));
subMenu.Add(subMenuItem);
string subdescription = subTypeProp.GetCustomAttribute<SettingSubTextAttribute>()?.Description;
if (subdescription != null)
subMenuItem.AddDescription(subMenu, menu, subdescription.DialogCleanOrNull() ?? subdescription);
}
item = subMenu;
}
if (item == null)
continue;
if (!headerCreated) {
CreateModMenuSectionHeader(menu, inGame, snapshot);
headerCreated = true;
}
string subheader = prop.GetCustomAttribute<SettingSubHeaderAttribute>()?.SubHeader;
if (subheader != null)
menu.Add(new TextMenu.SubHeader(subheader.DialogCleanOrNull() ?? subheader, false));
menu.Add(item);
if (prop.GetCustomAttribute<SettingNeedsRelaunchAttribute>() != null)
item.NeedsRelaunch(menu);
string description = prop.GetCustomAttribute<SettingSubTextAttribute>()?.Description;
if (description != null)
item.AddDescription(menu, description.DialogCleanOrNull() ?? description);
}
foreach (PropertyInfo prop in type.GetProperties()) {
if ((attribInGame = prop.GetCustomAttribute<SettingInGameAttribute>()) != null &&
attribInGame.InGame != inGame)
continue;
if (prop.GetCustomAttribute<SettingIgnoreAttribute>() != null)
continue;
if (!prop.CanRead || !prop.CanWrite)
continue;
if (!typeof(ButtonBinding).IsAssignableFrom(prop.PropertyType))
continue;
if (!headerCreated) {
CreateModMenuSectionHeader(menu, inGame, snapshot);
headerCreated = true;
}
CreateModMenuSectionKeyBindings(menu, inGame, snapshot);
break;
}
}
/// <summary>
/// Create and add any map data processors to the given context, if any are needed.
/// </summary>
/// <param name="context">The context to add the processors to.</param>
public virtual void PrepareMapDataProcessors(MapDataFixup context) {
}
public virtual void LogRegistration() {
Logger.Log(LogLevel.Info, "core", $"Registered code module {GetType().FullName} for module {Metadata}.");
}
public virtual void LogUnregistration() {
Logger.Log(LogLevel.Info, "core", $"Unregistered code module {GetType().FullName} for module {Metadata}.");
}
}
}