-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlackBoard.cs
633 lines (554 loc) · 24.6 KB
/
BlackBoard.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
using UnityEditor;
using System.Text.RegularExpressions;
[Serializable]
public class Blackboard : Dictionary<string, object>, ISerializationCallbackReceiver
{
[Serializable]
//struct represents the string key, and the serialized value from the dictionary.
private struct SaveItem
{
public string key;
public string value;
public int index;
public SaveItem(string key, string val, int index)
{
this.key = key;
this.value = val;
this.index = index;
}
}
//All serialized items except for objects in a scene, which have to be handled separately.
[SerializeField, HideInInspector]
private List<SaveItem> saveItems;
//We need a different struct and list for Object references in scene :(
[Serializable]
private struct NonAssetSaveItem
{
public string key;
public UnityEngine.Object obj;
public int index;
public NonAssetSaveItem(string key, UnityEngine.Object obj, int index)
{
this.key = key;
this.obj = obj;
this.index = index;
}
}
[SerializeField, HideInInspector]
private List<NonAssetSaveItem> sceneObjectSaveItems;
/// <summary>
/// Takes all of the keyvalue pairs from the Dictionary and stores them as Serializable lists.
/// </summary>
public void OnBeforeSerialize()
{
sceneObjectSaveItems = new List<NonAssetSaveItem>();
saveItems = new List<SaveItem>();
List<string> keys = this.Keys.ToList();
List<object> values = this.Values.ToList();
for (int i = 0; i < Count; i++)
{
object value = values[i];
string encode = "";
//Unhandled Enum Types
if (value is Enum)
{
Enum enumValue = (Enum)value;
Console.WriteLine("Enum Value: " + enumValue.ToString());
encode = $"({value.GetType().AssemblyQualifiedName}){enumValue}";
saveItems.Add(new SaveItem(keys[i], encode, i));
continue;
}
switch (value)
{
case null: encode = "(null)"; break;
case int: encode = "(int)" + ((int)value).ToString("F9"); break;
case float: encode = "(float)" + ((float)value).ToString("F9"); break;
case double: encode = "(double)" + ((double)value).ToString("F9"); break;
case long: encode = "(long)" + ((long)value).ToString(); break;
case string: encode = "(string)" + (string)value; break;
case bool: encode = "(bool)" + (((bool)value) == true ? "true" : "false"); break;
case Vector2Int: encode = "(Vector2Int)" + ((Vector2Int)value).ToString(); break;
case Vector3Int: encode = "(Vector3Int)" + ((Vector3Int)value).ToString(); break;
case Vector2: encode = "(Vector2)" + ((Vector2)value).ToString(); break;
case Vector3: encode = "(Vector3)" + ((Vector3)value).ToString(); break;
case Vector4: encode = "(Vector4)" + ((Vector4)value).ToString(); break;
case Bounds: encode = "(Bounds)" + ((Bounds)value).ToString(); break;
case Rect: encode = "(Rect)" + ((Rect)value).ToString("F9"); break;
case Color: encode = "(Color)" + JsonUtility.ToJson((Color)value); break;
case AnimationCurve: encode = "(AnimationCurve)" + Serializer.SerializeAnimationCurve((AnimationCurve)value); break;
case Gradient: encode = "(Gradient)" + Serializer.SerializeGradient((Gradient)value); break;
case UnityEngine.Object obj:
string assetPath = Application.isEditor ? AssetDatabase.GetAssetPath(obj) : null;
if (!string.IsNullOrEmpty(assetPath))
{
encode = "(UnityEngine.Object)" + assetPath;
}
else
{
sceneObjectSaveItems.Add(new NonAssetSaveItem(keys[i], obj, i));
}
break;
//Try to serialize to JSON. May be empty if type is not supported
default: encode = $"({value.GetType().AssemblyQualifiedName}){JsonUtility.ToJson(value)}"; break;
}
if (!string.IsNullOrEmpty(encode))
{
saveItems.Add(new SaveItem(keys[i], encode, i));
}
}
}
/// <summary>
/// Loads the two lists back into the Dictionary, using the Merge Linked Lists method.
/// </summary>
public void OnAfterDeserialize()
{
this.Clear();
int i = 0;
int j = 0;
//Ensure that the lists are not null to ensure no errors when accessing list.Count
saveItems = saveItems == null ? new List<SaveItem>() : saveItems;
sceneObjectSaveItems = sceneObjectSaveItems == null ? new List<NonAssetSaveItem>() : sceneObjectSaveItems;
while (i < saveItems.Count && j < sceneObjectSaveItems.Count)
{
if (saveItems[i].index < sceneObjectSaveItems[j].index)
{
string key = saveItems[i].key;
int openIndex = saveItems[i].value.IndexOf('(');
int closeIndex = saveItems[i].value.IndexOf(')');
string contentType = saveItems[i].value.Substring(openIndex + 1, closeIndex - openIndex - 1);
string encode = saveItems[i].value.Substring(closeIndex + 1);
DeserializeItem(contentType, key, encode);
i++;
}
else
{
Add(sceneObjectSaveItems[j].key, sceneObjectSaveItems[j].obj);
j++;
}
}
for(; i < saveItems.Count;i++)
{
string key = saveItems[i].key;
int openIndex = saveItems[i].value.IndexOf('(');
int closeIndex = saveItems[i].value.IndexOf(')');
string contentType = saveItems[i].value.Substring(openIndex + 1, closeIndex - openIndex - 1);
string encode = saveItems[i].value.Substring(closeIndex + 1);
DeserializeItem(contentType, key, encode);
}
for (; j < sceneObjectSaveItems.Count; j++)
{
Add(sceneObjectSaveItems[j].key, sceneObjectSaveItems[j].obj);
}
}
/// <summary>
/// Takes the key and encoded string from a serialized item and adds it back into the dictionary.
/// </summary>
private void DeserializeItem(string contentType, string key, string encodedValue)
{
switch (contentType)
{
case "null": Add(key, null); return;
case "int": Add(key, (int)int.Parse(encodedValue)); return;
case "float": Add(key, (float)float.Parse(encodedValue)); return;
case "double": Add(key, (double)double.Parse(encodedValue)); return;
case "long": Add(key, (long)long.Parse(encodedValue)); return;
case "string": Add(key, (string)encodedValue); return;
case "bool": Add(key, (bool)(encodedValue == "true" ? true : false)); return;
case "Vector2": Add(key, Serializer.ParseVector2(encodedValue)); return;
case "Vector3": Add(key, Serializer.ParseVector3(encodedValue)); return;
case "Vector2Int": Add(key, Serializer.ParseVector2Int(encodedValue)); return;
case "Vector3Int": Add(key, Serializer.ParseVector3Int(encodedValue)); return;
case "Vector4": Add(key, Serializer.ParseVector4(encodedValue)); return;
case "Bounds": Add(key, Serializer.ParseBounds(encodedValue)); return;
case "Rect": Add(key, Serializer.ParseRect(encodedValue)); return;
case "Color": Add(key, JsonUtility.FromJson<Color>(encodedValue)); return;
case "AnimationCurve": Add(key, Serializer.DeserializeAnimationCurve(encodedValue)); return;
case "Gradient": Add(key, Serializer.DeserializeGradient(encodedValue)); return;
case "UnityEngine.Object":
if(Application.isEditor)
{
EditorApplication.delayCall += () => Add(key, AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(encodedValue));
}
else
{
Add(key, Resources.Load(encodedValue));
}
return;
default: break;
}
//Different process for enums (of any type)
if(Serializer.EnumDeserialize(contentType, encodedValue, out object enumValue))
{
Add(key, enumValue);
}
//Tries to de-serialize a struct or class using JsonUtility.FromJson
if(Serializer.TryDeserializeJSON(contentType, encodedValue, out object result))
{
Add(key, result);
}
}
public void SetOrAdd(string key, object ob)
{
if (this.ContainsKey(key))
{
this[key] = ob;
}
else
{
this.Add(key, ob);
}
}
[System.Serializable]
private static class Serializer
{
#region GradientSerialization
[System.Serializable]
private class SerializableGradient
{
public SerializableColorKey[] colorKeys;
public SerializableAlphaKey[] alphaKeys;
public GradientMode mode;
public SerializableGradient(Gradient gradient)
{
colorKeys = new SerializableColorKey[gradient.colorKeys.Length];
for (int i = 0; i < gradient.colorKeys.Length; i++)
{
colorKeys[i] = new SerializableColorKey(gradient.colorKeys[i]);
}
alphaKeys = new SerializableAlphaKey[gradient.alphaKeys.Length];
for (int i = 0; i < gradient.alphaKeys.Length; i++)
{
alphaKeys[i] = new SerializableAlphaKey(gradient.alphaKeys[i]);
}
mode = gradient.mode;
}
public Gradient ToGradient()
{
Gradient gradient = new Gradient();
GradientColorKey[] gradientColorKeys = new GradientColorKey[colorKeys.Length];
for (int i = 0; i < colorKeys.Length; i++)
{
gradientColorKeys[i] = colorKeys[i].ToGradientColorKey();
}
GradientAlphaKey[] gradientAlphaKeys = new GradientAlphaKey[alphaKeys.Length];
for (int i = 0; i < alphaKeys.Length; i++)
{
gradientAlphaKeys[i] = alphaKeys[i].ToGradientAlphaKey();
}
gradient.SetKeys(gradientColorKeys, gradientAlphaKeys);
gradient.mode = mode;
return gradient;
}
}
[System.Serializable]
private class SerializableColorKey
{
public Color color;
public float time;
public SerializableColorKey(GradientColorKey colorKey)
{
color = colorKey.color;
time = colorKey.time;
}
public GradientColorKey ToGradientColorKey()
{
return new GradientColorKey(color, time);
}
}
[System.Serializable]
private class SerializableAlphaKey
{
public float alpha;
public float time;
public SerializableAlphaKey(GradientAlphaKey alphaKey)
{
alpha = alphaKey.alpha;
time = alphaKey.time;
}
public GradientAlphaKey ToGradientAlphaKey()
{
return new GradientAlphaKey(alpha, time);
}
}
public static string SerializeGradient(Gradient gradient)
{
SerializableGradient serializableGradient = new SerializableGradient(gradient);
return JsonUtility.ToJson(serializableGradient);
}
public static Gradient DeserializeGradient(string json)
{
SerializableGradient serializableGradient = JsonUtility.FromJson<SerializableGradient>(json);
if (serializableGradient != null)
{
return serializableGradient.ToGradient();
}
Debug.LogError("Failed to deserialize Gradient from JSON: " + json);
return new Gradient(); // Return a default Gradient or handle the error as needed
}
#endregion
#region AnimationCurveSerialization
[System.Serializable]
private struct SerializableKeyframe
{
public float time;
public float value;
public float inTangent;
public float outTangent;
public SerializableKeyframe(Keyframe keyframe)
{
time = keyframe.time;
value = keyframe.value;
inTangent = keyframe.inTangent;
outTangent = keyframe.outTangent;
}
}
[System.Serializable]
private struct SerializableAnimationCurve
{
public WrapMode preWrapMode;
public WrapMode postWrapMode;
public SerializableKeyframe[] keys;
public SerializableAnimationCurve(AnimationCurve curve)
{
preWrapMode = curve.preWrapMode;
postWrapMode = curve.postWrapMode;
keys = new SerializableKeyframe[curve.length];
for (int i = 0; i < curve.length; i++)
{
keys[i] = new SerializableKeyframe(curve[i]);
}
}
}
/// <summary>
/// Serializes an AnimationCurve to a JSON string.
/// </summary>
public static string SerializeAnimationCurve(AnimationCurve curve)
{
SerializableAnimationCurve serializableCurve = new SerializableAnimationCurve(curve);
string json = JsonUtility.ToJson(serializableCurve);
return json;
}
/// <summary>
/// Produces an AnimationCurve from a json string.
/// </summary>
public static AnimationCurve DeserializeAnimationCurve(string json)
{
SerializableAnimationCurve serializableCurve = JsonUtility.FromJson<SerializableAnimationCurve>(json);
Keyframe[] keyframes = new Keyframe[serializableCurve.keys.Length];
for (int i = 0; i < keyframes.Length; i++)
{
keyframes[i] = new Keyframe(
serializableCurve.keys[i].time,
serializableCurve.keys[i].value,
serializableCurve.keys[i].inTangent,
serializableCurve.keys[i].outTangent
);
}
AnimationCurve curve = new AnimationCurve(keyframes);
curve.postWrapMode = serializableCurve.postWrapMode;
curve.preWrapMode = serializableCurve.postWrapMode;
return curve;
}
#endregion
#region VectorSerialization
public static Vector2 ParseVector2(string vectorString)
{
vectorString = vectorString.Replace("(", "").Replace(")", "");
string[] components = vectorString.Split(',');
if (components.Length == 2 &&
float.TryParse(components[0], out float x) &&
float.TryParse(components[1], out float y))
{
return new Vector2(x, y);
}
Debug.LogError("Failed to parse Vector2 from string: " + vectorString);
return Vector2.zero;
}
public static Vector2Int ParseVector2Int(string vectorString)
{
vectorString = vectorString.Replace("(", "").Replace(")", "");
string[] components = vectorString.Split(',');
if (components.Length == 2 &&
int.TryParse(components[0], out int x) &&
int.TryParse(components[1], out int y))
{
return new Vector2Int(x, y);
}
Debug.LogError("Failed to parse Vector2 from string: " + vectorString);
return Vector2Int.zero;
}
public static Vector3 ParseVector3(string vectorString)
{
vectorString = vectorString.Replace("(", "").Replace(")", "");
string[] components = vectorString.Split(',');
if (components.Length == 3 &&
float.TryParse(components[0], out float x) &&
float.TryParse(components[1], out float y) &&
float.TryParse(components[2], out float z))
{
return new Vector3(x, y, z);
}
Debug.LogError("Failed to parse Vector3 from string: " + vectorString);
return Vector3.zero;
}
public static Vector3Int ParseVector3Int(string vectorString)
{
vectorString = vectorString.Replace("(", "").Replace(")", "");
string[] components = vectorString.Split(',');
if (components.Length == 3 &&
int.TryParse(components[0], out int x) &&
int.TryParse(components[1], out int y) &&
int.TryParse(components[2], out int z))
{
return new Vector3Int(x, y, z);
}
Debug.LogError("Failed to parse Vector3Int from string: " + vectorString);
return Vector3Int.zero;
}
public static Vector4 ParseVector4(string vectorString)
{
vectorString = vectorString.Replace("(", "").Replace(")", "");
string[] components = vectorString.Split(',');
if (components.Length == 4 &&
float.TryParse(components[0], out float x) &&
float.TryParse(components[1], out float y) &&
float.TryParse(components[2], out float z) &&
float.TryParse(components[3], out float w))
{
return new Vector4(x, y, z, w);
}
Debug.LogError("Failed to parse Vector4 from string: " + vectorString);
return Vector4.zero;
}
#endregion
#region BoundsSerialization
/// <summary>
/// Produces a Bounds object from the result of Bounds.ToString(). Returns a Bounds with all zero values if unable to parse.
/// </summary>
public static Bounds ParseBounds(string boundsString)
{
// Remove parentheses and labels from the string
boundsString = Regex.Replace(boundsString, @"[^\d\.\-,]", "");
string[] components = boundsString.Split(',');
if (components.Length == 6 &&
float.TryParse(components[0], out float center_x) &&
float.TryParse(components[1], out float center_y) &&
float.TryParse(components[2], out float center_z) &&
float.TryParse(components[3], out float extent_x) &&
float.TryParse(components[4], out float extent_y) &&
float.TryParse(components[5], out float extent_z))
{
Vector3 center = new Vector3(center_x, center_y, center_z);
Vector3 size = new Vector3(extent_x, extent_y, extent_z) * 2f;
return new Bounds(center, size);
}
Debug.LogWarning("Failed to parse Bounds from string: " + boundsString);
return new Bounds(Vector3.zero, Vector3.zero);
}
/// <summary>
/// Produces a BoundsInt object from the result of BoundsInt.ToString(). Returns a Bounds with all zero values if unable to parse.
/// </summary>
public static BoundsInt ParseBoundsInt(string boundsString)
{
// Remove parentheses and labels and any unwanted decimals from the string
boundsString = Regex.Replace(boundsString, @"[^\d\-,]", "");
string[] components = boundsString.Split(',');
if (components.Length == 6 &&
int.TryParse(components[0], out int center_x) &&
int.TryParse(components[1], out int center_y) &&
int.TryParse(components[2], out int center_z) &&
int.TryParse(components[3], out int extent_x) &&
int.TryParse(components[4], out int extent_y) &&
int.TryParse(components[5], out int extent_z))
{
Vector3Int center = new Vector3Int(center_x, center_y, center_z);
Vector3Int size = new Vector3Int(extent_x, extent_y, extent_z) * 2;
return new BoundsInt(center, size);
}
Debug.LogWarning("Failed to parse BoundsInt from string: " + boundsString);
return new BoundsInt(Vector3Int.zero, Vector3Int.zero);
}
#endregion
#region RectSerialization
/// <summary>
/// Takes the string result of Rect.ToString() and produces the original Rect. Returns a zero-rect if unable to parse.
/// </summary>
public static Rect ParseRect(string rectString)
{
// Remove parentheses and labels from the string
rectString = Regex.Replace(rectString, @"[^\d\.\-,]", "");
string[] components = rectString.Split(',');
if (components.Length == 4 &&
float.TryParse(components[0], out float x) &&
float.TryParse(components[1], out float y) &&
float.TryParse(components[2], out float width) &&
float.TryParse(components[3], out float height))
{
Rect rect = new Rect(x, y, width, height);
return rect;
}
Debug.LogWarning("Failed to parse Rect from string: " + rectString);
return new Rect(0, 0, 0, 0);
}
/// <summary>
/// Takes the string result of RectInt.ToString() and produces the original RectInt. Returns a zero-rect if unable to parse.
/// </summary>
public static RectInt ParseRectInt(string rectString)
{
// Remove parentheses and labels from the string
rectString = Regex.Replace(rectString, @"[^\d\-,]", "");
string[] components = rectString.Split(',');
if (components.Length == 4 &&
int.TryParse(components[0], out int x) &&
int.TryParse(components[1], out int y) &&
int.TryParse(components[2], out int width) &&
int.TryParse(components[3], out int height))
{
RectInt rect = new RectInt(x, y, width, height);
return rect;
}
Debug.LogWarning("Failed to parse RectInt from string: " + rectString);
return new RectInt(0, 0, 0, 0);
}
#endregion
/// <summary>
/// Takes the type, encoded as string, and the enum value and produces an Enum of the proper type.
/// </summary>
public static bool EnumDeserialize(string contentType, string encodedValue, out object enumValue)
{
Type type = Type.GetType(contentType);
if (type != null && type.IsEnum)
{
if (Enum.TryParse(type, encodedValue, out object enumIntermediateValue))
{
enumValue = Convert.ChangeType(enumIntermediateValue, type);
return true;
}
}
enumValue = null;
return false;
}
/// <summary>
/// Takes as input a string to be converted to a type, which is used to produce a Deserialized object.
/// </summary>
public static bool TryDeserializeJSON(string contentType, string json, out object result)
{
Type type = Type.GetType(contentType);
if (type != null)
{
result = JsonUtility.FromJson(json, type);
if (result != null)
{
return true;
}
}
result = null;
return false;
}
}
}