-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrailineModule.cs
99 lines (78 loc) · 3.11 KB
/
TrailineModule.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
using System;
using System.IO;
using Celeste.Mod.Core;
using Celeste.Mod.Trailine.Modules;
using Celeste.Mod.Trailine.ThirdParty;
using Celeste.Mod.Trailine.UI;
using Celeste.Mod.Trailine.Utils;
using Microsoft.Xna.Framework;
using MonoMod.ModInterop;
using YamlDotNet.Serialization;
namespace Celeste.Mod.Trailine {
public class TrailineModule : EverestModule {
public TrailineModule() {
Instance = this;
}
public static TrailineModule Instance { get; private set; }
public override Type SettingsType => typeof(TrailineSettings);
public static TrailineSettings Settings => Instance._Settings as TrailineSettings;
public static bool Loaded = false;
public override void Load() {
typeof(GravityHelperImports).ModInterop();
if (Loaded || !Settings.Enabled) {
return;
}
CustomTrail.Load();
PatternPreview.Load();
SubMenuFix.Load();
Debug.Load();
Loaded = true;
}
public override void Unload() {
if (!Loaded) {
return;
}
CustomTrail.Unload();
PatternPreview.Unload();
SubMenuFix.Unload();
Debug.Unload();
Loaded = false;
}
public override void SaveSettings() {
DDW_EverestModule wrapper = new DDW_EverestModule(this);
bool forceFlush = wrapper.ForceSaveDataFlush > 0;
if (forceFlush) {
wrapper.ForceSaveDataFlush--;
}
if (SettingsType == null || _Settings == null) {
return;
}
Settings.SettingsVersion = 1;
string path = UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);
if (File.Exists(path)) {
File.Delete(path);
}
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
try {
using (FileStream stream = File.OpenWrite(path)) {
using (StreamWriter writer = new StreamWriter(stream)) {
// disable anchors in serialized yaml
// https://github.com/EverestAPI/Everest/pull/404
ISerializer serializer = new SerializerBuilder()
.ConfigureDefaultValuesHandling(DefaultValuesHandling.Preserve)
.DisableAliases()
.WithAttributeOverride<Color>(c => c.PackedValue, new YamlIgnoreAttribute())
.Build();
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);
}
}
}
}