Newer
Older
Marco Zimmer
committed
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
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
using System;
using Newtonsoft.Json;
using UnityEngine;
using static CommunicationEvents;
// I would go for static virtual methods, but C#9 does not allow me...
// static methods cannot be overwritten -> virtual
public interface IJSONsavable<T> where T : IJSONsavable<T>, new()
{
// stand-in for non static methods
public static readonly IJSONsavable<T> Instance = new T();
public static readonly FieldInfo[] JsonSaveableFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonAutoPreProcessFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JSONsavable.JsonAutoPreProcessAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonAutoPostProcessFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JSONsavable.JsonAutoPostProcessAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonSeperateFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JSONsavable.JsonSeparateAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
// TODO: this?
public string name { get; set; }
public string path { get; set; }
protected static List<Directories>
hierarchie = new List<Directories> { Directories.misc };
#region OverridableMethods
public virtual string _IJGetName(string name) => name;
public virtual List<Directories> _IJGetHierarchie(List<Directories> hierarchie_base)
{
hierarchie_base ??= new List<Directories>();
return hierarchie_base.Concat(hierarchie).ToList();
}
public virtual bool _IJGetRawObject(out T payload, string path) => JSONsavable.ReadFromJsonFile<T>(out payload, path);
public virtual T _IJPreProcess(T payload) => payload;
public virtual T _IJPostProcess(T payload) => payload;
#endregion OverridableMethods
#region MethodTemplates
public bool store(List<Directories> hierarchie, string name, bool use_install_folder = false, bool overwrite = true, bool deep_store = true)
=> store(hierarchie, name, (T)this, use_install_folder, overwrite, deep_store);
public static bool store(List<Directories> hierarchie, string name, T payload, bool use_install_folder = false, bool overwrite = true, bool deep_store = true)
{
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
string path = CreatePathToFile(out bool exists, new_name, "JSON", new_hierarchie, use_install_folder);
if (exists && !overwrite)
return false;
// store fields decorated with JsonSeparateAttribute and implementing IJSONsavable<> separately
if (deep_store
&& !store_children(hierarchie, name, payload, use_install_folder: false, overwrite, deep_store: true))
return false;
// store this
string path_o = payload.path;
payload.path = path;
var new_payload =
preprocess(payload);
payload.path = path_o;
JSONsavable.WriteToJsonFile(path, new_payload);
return true;
}
public bool store_children(List<Directories> hierarchie, string name, bool use_install_folder = false, bool overwrite = true, bool deep_store = true)
=> store_children(hierarchie, name, (T)this, use_install_folder, overwrite, deep_store);
public static bool store_children(List<Directories> hierarchie, string name, T payload, bool use_install_folder = false, bool overwrite = true, bool deep_store = true)
{
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
for ((int max_i, bool success) = (0, true); max_i < JsonSeperateFields.Count(); max_i++)
{
var field = JsonSeperateFields[max_i];
dynamic save_me = field.GetValue(payload); // is S:IJSONsavable<S>
Type interface_type = typeof(IJSONsavable<>).MakeGenericType(field.FieldType);
Type[] store_args_type = new Type[] { typeof(List<Directories>), typeof(string), field.FieldType, typeof(bool), typeof(bool), typeof(bool) };
object[] store_args = new object[] { new_hierarchie, new_name, save_me, use_install_folder, overwrite, deep_store };
var method = interface_type.GetMethod("store", store_args_type);
success &= (bool)method.Invoke(null, store_args);
// in case of no success: delete it again
if (!success)
{
delete_children(hierarchie, name, use_install_folder, JsonSeperateFields.Count() - max_i);
return false;
}
}
return true;
}
public static bool load_children(List<Directories> hierarchie, string name, ref T raw_payload, bool use_install_folder = false, bool deep_load = true, bool post_process = true)
{
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
bool success = true;
for (int max_i = 0; max_i < JsonSeperateFields.Count(); max_i++)
{
var field = JsonSeperateFields[max_i];
Type interface_type = typeof(IJSONsavable<>).MakeGenericType(field.FieldType);
Type[] load_args_type = new Type[] { typeof(List<Directories>), typeof(string), field.FieldType.MakeByRefType(), typeof(bool), typeof(bool), typeof(bool) };
object[] load_args = new object[] { new_hierarchie, new_name, null, use_install_folder, deep_load, post_process };
var method = interface_type.GetMethod("load", BindingFlags.Public | BindingFlags.Static, null, load_args_type, null);
bool success_i = (bool)method.Invoke(null, load_args);
field.SetValue(raw_payload, success_i ? load_args[2] : Activator.CreateInstance(field.FieldType));
success &= success_i;
}
return success;
}
public static bool load(List<Directories> hierarchie, string name, out T payload, bool use_install_folder = false, bool deep_load = true, bool post_process = true)
{
payload = default(T);
bool success = true;
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
string path = CreatePathToFile(out bool loadable, new_name, "JSON", new_hierarchie, use_install_folder);
if (!loadable)
return false;
if (!Instance._IJGetRawObject(out T raw_payload, path))
return false;
raw_payload.name = new_name;
// load fields decorated with JsonSeparateAttribute and implementing IJSONsavable<> separately
if (deep_load
&& !load_children(hierarchie, name, ref raw_payload, false /*use_install_folder*/))
success = false;
payload = post_process
? postprocess(raw_payload)
: raw_payload;
return success;
}
public static T postprocess(T payload)
{
if (payload == null)
return Instance._IJPostProcess(payload);
for (int i = 0; i < JsonAutoPostProcessFields.Count(); i++)
{
var field = JsonAutoPostProcessFields[i];
dynamic process_me = field.GetValue(payload); // is S:IJSONsavable<S>
Type interface_type = typeof(IJSONsavable<>).MakeGenericType(field.FieldType);
Type[] process_args_type = new Type[] { field.FieldType };
object[] process_args = new object[] { process_me };
var method = interface_type.GetMethod("postprocess", process_args_type);
var processed = method.Invoke(null, process_args);
field.SetValue(payload, processed);
}
return Instance._IJPostProcess(payload);
}
public static T preprocess(T payload)
{
if (payload == null)
return Instance._IJPreProcess(payload);
for (int i = 0; i < JsonAutoPreProcessFields.Count(); i++)
{
var field = JsonAutoPreProcessFields[i];
dynamic process_me = field.GetValue(payload); // is S:IJSONsavable<S>
Type interface_type = typeof(IJSONsavable<>).MakeGenericType(field.FieldType);
Type[] process_args_type = new Type[] { field.FieldType };
object[] process_args = new object[] { process_me };
var method = interface_type.GetMethod("preprocess", process_args_type);
var processed = method.Invoke(null, process_args);
field.SetValue(payload, processed);
}
return Instance._IJPreProcess(payload);
}
public static void delete_children(List<Directories> hierarchie, string name, bool use_install_folder = false, int skip_last_children = 0)
{
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
for (int i = 0; i < JsonSeperateFields.Count() - skip_last_children; i++)
{
var field = JsonSeperateFields[i];
Type interface_type = typeof(IJSONsavable<>).MakeGenericType(field.FieldType);
Type[] delete_args_type = new Type[] { typeof(List<Directories>), typeof(string), typeof(bool) };
object[] delete_args = new object[] { new_hierarchie, new_name, use_install_folder };
var method = interface_type.GetMethod("delete", delete_args_type);
method.Invoke(null, delete_args);
}
}
public static bool delete(List<Directories> hierarchie, string name, bool use_install_folder = false)
{
var new_hierarchie =
Instance._IJGetHierarchie(hierarchie);
var new_name =
Instance._IJGetName(name);
string path = CreatePathToFile(out bool _, new_name, "JSON", new_hierarchie, use_install_folder);
if (!delete(path))
return false;
delete_children(hierarchie, name, use_install_folder);
return true;
}
// does not delete children!
private static bool delete(string path)
{
if (!File.Exists(path))
return false;
File.Delete(path);
return true;
}
// public bool delete() => delete(hierarchie, name);
#endregion MethodTemplates
}
public static class JSONsavable
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class JsonSeparateAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class JsonAutoPostProcessAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class JsonAutoPreProcessAttribute : Attribute { }
/// <summary>
/// Writes the given object instance to a Json file, recursively, including public members, excluding [JsonIgnore].
/// <para>Object type must have a parameterless constructor.</para>
/// <para>Only public properties and variables will be written to the file. These can be any type though, even other non-abstract classes.</para>
/// </summary>
/// <param name="filePath">The file path to write the object instance to.</param>
/// <param name="objectToWrite">The object instance to write to the file.</param>
public static bool WriteToJsonFile(string filePath, object objectToWrite)
{
// This tells your serializer that multiple references are okay.
var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
TextWriter writer = null;
try
{
string payload = JsonConvert.SerializeObject(objectToWrite, settings);
writer = new StreamWriter(filePath);
writer.Write(payload);
return true;
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
finally
{
if (writer != null)
writer.Close();
}
}
/// <summary>
/// Reads an object instance from an Json file.
/// <para>Object type must have a parameterless constructor.</para>
/// </summary>
/// <typeparam name="T">The type of object to read from the file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <returns>Returns a new instance of the object read from the Json file.</returns>
public static bool ReadFromJsonFile<T>(out T payload, string filePath) where T : new()
{
payload = default(T);
TextReader reader = null;
try
{
reader = new StreamReader(filePath);
var fileContents = reader.ReadToEnd();
payload = JsonConvert.DeserializeObject<T>(fileContents);
return true;
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
finally
{
if (reader != null)
reader.Close();
}
}
}