A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.Linq;
6using UnityEngine;
7using UnityObject = UnityEngine.Object;
8
9namespace Unity.VisualScripting
10{
11 [Serializable]
12 public struct SerializationData
13 {
14 [SerializeField]
15 private string _json;
16
17 public string json => _json;
18
19 [SerializeField]
20 private UnityObject[] _objectReferences;
21
22 public UnityObject[] objectReferences => _objectReferences;
23
24#if DEBUG_SERIALIZATION
25 [SerializeField]
26 private string _guid;
27
28 public string guid => _guid;
29#endif
30
31 public SerializationData(string json, IEnumerable<UnityObject> objectReferences)
32 {
33 _json = json;
34 _objectReferences = objectReferences?.ToArray() ?? Empty<UnityObject>.array;
35
36#if DEBUG_SERIALIZATION
37 _guid = Guid.NewGuid().ToString();
38#endif
39 }
40
41 public SerializationData(string json, params UnityObject[] objectReferences) : this(json, ((IEnumerable<UnityObject>)objectReferences)) { }
42
43 internal void Clear()
44 {
45 _json = null;
46 _objectReferences = null;
47 }
48
49 public string ToString(string title)
50 {
51 using (var writer = new StringWriter())
52 {
53 if (!string.IsNullOrEmpty(title))
54 {
55#if DEBUG_SERIALIZATION
56 writer.WriteLine(title + $" ({guid})");
57#else
58
59 writer.WriteLine(title);
60#endif
61 writer.WriteLine();
62 }
63#if DEBUG_SERIALIZATION
64 else
65 {
66 writer.WriteLine(guid);
67 writer.WriteLine();
68 }
69#endif
70
71 writer.WriteLine("Object References: ");
72
73 if (objectReferences.Length == 0)
74 {
75 writer.WriteLine("(None)");
76 }
77 else
78 {
79 var index = 0;
80
81 foreach (var objectReference in objectReferences)
82 {
83 if (objectReference.IsUnityNull())
84 {
85 writer.WriteLine($"{index}: null");
86 }
87 else if (UnityThread.allowsAPI)
88 {
89 writer.WriteLine($"{index}: {objectReference.GetType().FullName} [{objectReference.GetHashCode()}] \"{objectReference.name}\"");
90 }
91 else
92 {
93 writer.WriteLine($"{index}: {objectReference.GetType().FullName} [{objectReference.GetHashCode()}]");
94 }
95
96 index++;
97 }
98 }
99
100 writer.WriteLine();
101 writer.WriteLine("JSON: ");
102 writer.WriteLine(Serialization.PrettyPrint(json));
103
104 return writer.ToString();
105 }
106 }
107
108 public override string ToString()
109 {
110 return ToString(null);
111 }
112
113 public void ShowString(string title = null)
114 {
115 var dataPath = Path.GetTempPath() + Guid.NewGuid() + ".json";
116 File.WriteAllText(dataPath, ToString(title));
117 Process.Start(dataPath);
118 }
119 }
120}