-
Notifications
You must be signed in to change notification settings - Fork 1
/
Options.cs
203 lines (162 loc) · 5.35 KB
/
Options.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
namespace QList;
using System.Reflection;
using MelonLoader;
using QList.OptionTypes;
public static class Options
{
internal static Dictionary<string, ModOptionContainer> CurrentModOptions = new();
private static List<BaseOption> OnUpdateListeners = new();
private static bool initialized;
public static void RegisterMod(MelonMod mod)
{
if (mod == null)
{
Log.LogOutput($"Unable to register mod: ensure mod is not null", Log.ELevel.Error);
return;
}
var key = $"{mod.Info.Author}.{mod.Info.Name}";
if (CurrentModOptions.ContainsKey(key))
{
Log.LogOutput(
$"Unable to register '{mod.Info.Name}': mod already registered",
Log.ELevel.Warning
);
return;
}
CurrentModOptions.Add(
key,
new ModOptionContainer(mod, System.Reflection.Assembly.GetCallingAssembly())
);
if (initialized) // Only sort after LateInitialize
SortModList();
Log.LogOutput($"Registered mod: '{mod.Info.Name}'", Log.ELevel.Message);
}
public static void DeregisterMod(MelonMod mod)
{
if (mod == null)
{
Log.LogOutput($"Unable to deregister mod: ensure mod is not null", Log.ELevel.Error);
return;
}
var key = $"{mod.Info.Author}.{mod.Info.Name}";
if (!CurrentModOptions.ContainsKey(key))
{
Log.LogOutput(
$"Unable to deregister '{mod.Info.Name}': mod not registered",
Log.ELevel.Warning
);
return;
}
CurrentModOptions.Remove(key);
SortModList();
Log.LogOutput($"Deregistered mod: '{mod.Info.Name}'", Log.ELevel.Message);
}
/// <summary>
/// Adds an option for a registered mod.
/// </summary>
/// <param name="name">Used if no MelonPreference exists in the given option</param>
/// <param name="description">Used if no MelonPreference exists in the given option</param>
/// <param name="category">Used if no MelonPreference exists in the given option</param>
/// <returns>False if unable to add option</returns>
public static bool AddOption(
BaseOption option,
string? name = "",
string? description = "",
string? category = ""
)
{
if (option == null)
{
Log.LogOutput($"Unable to add option: option is null", Log.ELevel.Warning);
return false;
}
var assembly = System.Reflection.Assembly.GetCallingAssembly();
var search = CurrentModOptions.Where(x => x.Value.assembly.Equals(assembly));
if (search == null || search.Count() == 0)
{
Log.LogOutput(
$"Unable to add option '{option.name}': Mod {assembly.GetName()} not registered. Call Options.RegisterMod first. ({assembly})",
Log.ELevel.Warning
);
return false;
}
var container = search?.First();
if (container == null || !container.HasValue)
{
Log.LogOutput(
$"Unable to add option '{option.name}': Mod {assembly.GetName()} not registered. Call Options.RegisterMod first. ({assembly}",
Log.ELevel.Warning
);
return false;
}
container.Value.Value.AddOption(option);
if (category != null && category.Length > 0)
option.category = category;
if (name != null && name.Length > 0)
option.name = name;
if (description != null && description.Length > 0)
option.description = description;
return true;
}
internal static void SortModList()
{
var sortedKeys = CurrentModOptions.Keys.ToList();
sortedKeys.Sort();
var sortedDictionary = new Dictionary<string, ModOptionContainer>();
foreach (var key in sortedKeys)
sortedDictionary.Add(key, CurrentModOptions[key]);
CurrentModOptions = sortedDictionary;
initialized = true;
}
internal static void SubscribeOnUpdate(BaseOption option)
{
if (OnUpdateListeners.Contains(option))
return;
OnUpdateListeners.Add(option);
}
public static void UnsubscribeOnUpdate(BaseOption option)
{
if (!OnUpdateListeners.Contains(option))
return;
OnUpdateListeners.Remove(option);
}
internal static void OnUpdate()
{
foreach (var option in OnUpdateListeners)
if (option != null)
option.OnUpdate();
}
internal static void OnLateUpdate()
{
}
}
internal class ModOptionContainer
{
#region Variables & Constructor
public bool IsDirty
{
get { return dirty; }
}
public Assembly assembly;
public MelonMod mod;
private bool dirty;
private List<BaseOption> options = new();
public ModOptionContainer(MelonMod mod, Assembly assembly)
{
this.mod = mod;
this.assembly = assembly;
}
#endregion
#region Options
public void AddOption(BaseOption option)
{
options.Add(option);
dirty = true;
}
public System.Collections.ObjectModel.ReadOnlyCollection<BaseOption> GetOptions()
{
dirty = false;
return options.AsReadOnly();
}
#endregion
}