forked from kreaddy/MagicTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
236 lines (220 loc) · 8.68 KB
/
Main.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
using HarmonyLib;
using ModKit;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityModManagerNet;
namespace MagicTime
{
internal static class Main
{
public static bool Enabled;
public static UnityModManager.ModEntry Mod;
public static Settings SettingsContainer = new Settings();
public static void Log(string msg)
{
Mod.Logger.Log(msg);
}
public static void OnSaveGUI(UnityModManager.ModEntry obj)
{
SettingsContainer.Save();
}
private static bool Load(UnityModManager.ModEntry modEntry)
{
Mod = modEntry;
SettingsContainer.Load();
modEntry.OnToggle = OnToggle;
modEntry.OnGUI = OnGUI;
modEntry.OnSaveGUI = OnSaveGUI;
var harmony = new Harmony(modEntry.Info.Id);
Resources.Initializer();
harmony.PatchAll(Assembly.GetExecutingAssembly());
return true;
}
private static void OnGUI(UnityModManager.ModEntry obj)
{
UI.AutoWidth();
using (UI.VerticalScope())
{
UI.Label("You need to restart the game for changes to take effect!".yellow().bold().size(20));
UI.Space(15);
for (int i = 0; i < SettingsContainer.groups.Count; i++)
{
var key = SettingsContainer.groups.Keys.ElementAt(i);
var group = SettingsContainer.groups[key];
UI.Div(0, 30);
if (i == 0)
{
UI.Toggle(group.name.pink().bold().size(15), ref group.enabled);
UI.Label(group.description);
}
if (i > 0 && group.settings != null)
{
UI.HStack(null, 0, () =>
{
UI.Toggle(group.name.pink().bold().size(15), ref group.enabled);
});
foreach (var setting in group.settings.Values)
{
if (setting.homebrew)
{
UI.Toggle(setting.name.bold().green(), ref setting.enabled);
}
else
{
UI.Toggle(setting.name.bold(), ref setting.enabled);
}
UI.Label(setting.description);
}
}
}
}
}
private static bool OnToggle(UnityModManager.ModEntry modEntry, bool value)
{
Enabled = value;
return true;
}
public class Setting
{
public string description;
public bool enabled;
public bool homebrew;
public string id;
public string name;
public Setting(string g_id, string g_name, bool g_enabled, string desc, bool bad_bubble = false)
{
id = g_id;
name = g_name;
enabled = g_enabled;
description = desc;
homebrew = bad_bubble;
}
}
public class SettingGroup
{
public string description;
public bool enabled;
public string id;
public string name;
public Setting[] content;
[JsonIgnore]
public Dictionary<string, Setting> settings = new Dictionary<string, Setting>();
public SettingGroup(string g_id, string g_name, bool g_enabled, string desc = null, JArray g_content = null)
{
id = g_id;
name = g_name;
enabled = g_enabled;
description = desc;
if (g_content != null)
{
foreach (var item in g_content)
{
var i_id = item["id"].ToString();
settings.Add(i_id, new Setting(i_id, item["name"].ToString(), (bool)item["enabled"],
item["description"].ToString(), (bool)item["homebrew"]));
}
}
}
public void PrepareSerialization()
{
content = settings.Values.ToArray();
}
}
public class Settings : UnityModManager.ModSettings
{
public Dictionary<string, SettingGroup> groups = new Dictionary<string, SettingGroup>();
public string mod_path;
private JArray raw_data;
public void Load()
{
mod_path = Path.Combine(Mod.Path, "Settings.json");
var serialize_settings = new JsonSerializerSettings()
{
CheckAdditionalContent = true,
NullValueHandling = NullValueHandling.Include,
Formatting = Formatting.Indented
};
var serializer = JsonSerializer.Create(serialize_settings);
CreateOrUpdateSettingsFile(serializer);
DeserializeSettings(serializer);
CreateSettingGroups();
raw_data.Clear();
}
public void Save()
{
var serialize_settings = new JsonSerializerSettings()
{
CheckAdditionalContent = true,
NullValueHandling = NullValueHandling.Include,
Formatting = Formatting.Indented
};
var serializer = JsonSerializer.Create(serialize_settings);
var groups_to_serialize = new List<SettingGroup>();
foreach (var group in groups)
{
group.Value.PrepareSerialization();
groups_to_serialize.Add(group.Value);
}
using (StreamWriter file = new StreamWriter(mod_path))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
serializer.Serialize(writer, groups_to_serialize.ToArray());
}
}
private void CreateOrUpdateSettingsFile(JsonSerializer serializer)
{
if (!File.Exists(mod_path))
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MagicTime.DefaultSettings.json"))
using (FileStream file = File.Create(mod_path))
{
stream.CopyTo(file);
}
}
else
{
JArray default_settings;
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MagicTime.DefaultSettings.json"))
using (StreamReader reader = new StreamReader(stream))
{
default_settings = JArray.Parse(reader.ReadToEnd());
}
using (StreamReader reader = File.OpenText(mod_path))
{
var user_settings = JArray.Parse(reader.ReadToEnd());
default_settings.Merge(user_settings, new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Merge
});
}
using (StreamWriter file = new StreamWriter(mod_path))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
serializer.Serialize(writer, default_settings);
}
}
}
private void CreateSettingGroups()
{
foreach (var item in raw_data)
{
var id = item["id"].ToString();
groups.Add(id, new SettingGroup(id, item["name"].ToString(), (bool)item["enabled"],
item["description"].ToString(), item["content"].ToObject<JArray>()));
}
}
private void DeserializeSettings(JsonSerializer serializer)
{
using (StreamReader file = File.OpenText(mod_path))
using (JsonTextReader reader = new JsonTextReader(file))
{
raw_data = serializer.Deserialize<JArray>(reader);
}
}
}
}
}