forked from RedBigz/Citruslib-FixedUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSettings.cs
More file actions
324 lines (231 loc) · 9.05 KB
/
GameSettings.cs
File metadata and controls
324 lines (231 loc) · 9.05 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using UnityEngine;
namespace WobbleBridge
{
public static partial class Wobble
{
public static SettingsFile<GameSetting> ExtraSettings = new SettingsFile<GameSetting>("ExtraSettings");
}
/// <summary>
/// A serializable setting, used when making custom settings files.
/// </summary>
[Serializable]
public class SettingObject
{
/// <summary>
/// the name of the setting
/// </summary>
[JsonProperty(Order = -2)]
public string name;
/// <summary>
/// a description of the setting
/// </summary>
[JsonProperty(Order = -1)]
public string description;
}
/// <summary>
/// A Settings file, containing a list of SettingObjects.
/// </summary>
/// <typeparam name="T"></typeparam>
public class SettingsFile<T> where T: SettingObject
{
static Logger setLog = new Logger("WobbleBridge-Settings", ConsoleColor.Cyan);
//public static List<SettingCategory> settings = new List<SettingCategory>();
//public static List<SettingCategory> defaults = new List<SettingCategory>();
internal List<T> settings = new List<T>();
internal List<T> defaults = new List<T>();
//static SettingFile settings;
/// <summary>
/// Sets a setting and then writes all settings. use this for mid-match setting changes, via commands for example.
/// </summary>
/// <param name="name">the name of a setting to change</param>
/// <param name="value">the value to set it to</param>
public bool SetSetting(string name, string value)
{
T g =
settings.Find(p => p.name == name);
if (g == null) return false;
WriteSettings();
return true;
}
//when setting a setting to json
internal static string SubJson(object o)
{
return JsonConvert.SerializeObject(o, Formatting.Indented).Replace("\"","'");
}
string path;
/// <summary>
/// creates a settings file at the specified path.
/// </summary>
/// <param name="path">The filepath. if absolute is set to false, the filepath starts in the application's directory</param>
/// <param name="absolute">if true, the filepath is relative to the application's install location</param>
public SettingsFile(string path, bool absolute = false)
{
this.path = path+".json";
string text = path + ".json";
if (!absolute)
{
DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath);
text = Path.Combine(directoryInfo.Parent.FullName, path + ".json");
}
path = text;
}
/// <summary>
/// reads the settings from it's file. if it doesn't exist, a file with default values is created instead.
/// If the file exists, but some settings are missing, those missing settings are set to their defaults and added to the file
/// </summary>
public void ReadSettings()
{
//settings.Add("test", "test");
settings = new List<T>();
setLog.Log("Reading Settings from "+path);
if (!File.Exists(path))
{
setLog.Log("...The settings there were missing... writing up a default file");
File.WriteAllText(path, JsonConvert.SerializeObject(defaults,Formatting.Indented));
}
string json = File.ReadAllText(path);
bool needsWrite = false;
//JsonSerializerSettings js = new JsonSerializerSettings();
try
{
settings = JsonConvert.DeserializeObject<List<T>>(json);
setLog.Log(string.Format("settings file at {0} was read successfully", path));
}
catch(Exception e)
{
setLog.LogError(string.Format("settings file at {0} could not be read!!! resetting to defaults",path));
File.WriteAllText(path, JsonConvert.SerializeObject(defaults, Formatting.Indented));
settings = defaults;
needsWrite = true;
setLog.LogError(e.Message +'\n' +e.StackTrace);
}
foreach (T g in defaults)
{
if (settings.Find(p=>p.name == g.name)==null)
{
//setting is missing from text file... need to rewrite the whole text file!
setLog.Log(string.Format("settings file was missing default setting {0}", g.name));
needsWrite = true;
settings.Add(g);
}
}
if (needsWrite)
{
WriteSettings();
}
}
/// <summary>
/// writes all the settings current values to it's file.
/// </summary>
public void WriteSettings()
{
setLog.Log("writing extra settings!");
string write = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(path, write);
}
//i forget what i was doing with this, ignore for now
internal string GetString(object o)
{
if (o.GetType() == typeof(List<Vector3>))
{
string ret = "";
List<Vector3> l = (List<Vector3>)o;
if (l.Count() == 0) return "";
ret = l.First().ToString();
if (l.Count() == 1) return ret;
bool first = true;
foreach (Vector3 vec in l)
{
if (first)
{
first = false;
continue;
}
ret += "," + vec.ToString();
}
return ret;
}
return o.ToString();
}
/// <summary>
/// adds a SettingObject to the DEFAULT settings list. use when initializing the default settings, BEFORE ever reading!
/// </summary>
/// <param name="g">The SettingObject to add</param>
/// <returns></returns>
public bool AddSetting(T g)
{
g.name = g.name.ToLower();
if (defaults.Find(p=>p.name == g.name)!=null)
{
setLog.LogError(string.Format("Tried adding setting \"{0}\" but it already exists?", g.name));
return false;
}
//setLog.Log(string.Format("added default setting: {0}",g.name));
defaults.Add(g);
return true;
}
/// <summary>
/// looks for a setting with name and returns true if found.
///
/// if a setting isnt found in the main settings, returns the default value and re-'reads' the settings file, adding ALL missing entries.
///
/// if a setting isnt found, returns false!
/// </summary>
/// <param name="name">the name of the setting</param>
/// <param name="g">a SettingObject out parameter</param>
/// <returns></returns>
public bool TryGetSetting(string name, out T g)
{
name = name.ToLower();
g = null;
T kvp = settings.Find(p => p.name.ToLower() == name);
if (kvp != null)
{
g = kvp;
return true;
}
kvp = defaults.Find(p => p.name.ToLower() == name);
if (kvp != null)
{
ReadSettings();
T kvp2 = settings.Find(p => p.name.ToLower() == name);
if (kvp2 != null)
{
//setLog.Log(string.Format("late-entry setting '{0}' was recovered", name));
g = kvp2;
return true;
}
//setLog.Log(string.Format("late-entry setting '{0}' was NOT found, using default value", name));
g = kvp;
return true;
}
setLog.LogError(string.Format("could not find setting {0}", name));
return false;
}
}
/// <summary>
/// a common purpose SettingObject. Has a string value. it's reccomended to use this when making settings
/// </summary>
[Serializable]
public class GameSetting : SettingObject
{
/// <summary>
/// the value of the setting
/// </summary>
[JsonProperty(Order = 1)]
public string value; //current value
/*
public GameSetting(string n, object v, string d = "")
{
name = n.ToLower();
value = v.ToString();
description = d;
}
*/
}
}