-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationElement.cs
More file actions
193 lines (153 loc) · 6.09 KB
/
SerializationElement.cs
File metadata and controls
193 lines (153 loc) · 6.09 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace BinarySerializationEditor
{
public class SerializationElement // Organized way to keep track of elements
{
public enum OriginType // If represented thing is a field, dictionary entry or list item
{
Field,
DictEntry,
ListItem,
TopLevel
}
public enum Classification // If represented thing is an object, dictionary, list or primitive type (string, int, float, etc...)
{
Object,
Dictionary,
List,
Primitive,
Enum
}
public Dictionary<string, SerializationElement> children = new Dictionary<string, SerializationElement>(); // All children, depending on classification, will be created in different ways
public SerializationElement parent;
public bool topLevel;
public dynamic value; // Represented object in raw form
public Type valueType; // Type of value
public string name; // Key in parent's children dictionary
public OriginType origin; // Origin of element
public Classification classification; // Classification of element
public bool strToObjValid = true;
public string valueStr;
public dynamic location;
public dynamic originalValue;
public Display.ElementGUI gui;
public SerializationElement(string name, dynamic obj, OriginType originType, SerializationElement parent, dynamic locationData) // Recursive constructor
{
// Set argument stuff
origin = originType;
value = obj;
valueStr = Convert.ToString(value);
originalValue = Utils.DeepClone<dynamic>(value);
valueType = value.GetType();
this.name = name;
this.parent = parent;
location = locationData;
// [TODO] Add proper functionality for Enums
if (value == null || Utils.IsPrimitive(valueType)) // Is primitive
{
classification = Classification.Primitive;
return; // Primitive types have no children
}
else if (valueType.IsEnum)
{
classification = Classification.Enum;
return; // Enums have no children
}
else if (value is IEnumerable) // Is enumerable
{
if (value is IDictionary) // Has two values for each item
{
classification = Classification.Dictionary;
}
else // Has only one value for each item
{
classification = Classification.List;
}
}
else // Is object
{
classification = Classification.Object;
}
AddAllChildren(); // Add Children
gui = new Display.ElementGUI(this, Display.instance);
}
public void StringEdited()
{
if (classification == Classification.Primitive && !(value is bool))
{
dynamic t = Utils.TryParse(value, valueStr, out strToObjValid);
if (strToObjValid)
{
value = t;
switch (origin)
{
case OriginType.Field:
location.SetValue(parent.value, value);
return;
case OriginType.DictEntry:
parent.value[location] = value;
return;
case OriginType.ListItem:
parent.value[location] = value;
return;
case OriginType.TopLevel:
return;
}
}
}
}
// Child Adders
public void AddAllChildren()
{
children.Clear(); // Remove all pre-existing children first
switch (classification)
{
case Classification.Dictionary:
foreach (dynamic keyValue in value) // Iterate as Dictionary
{
AddChildFromKeyValuePair(keyValue.Key, keyValue.Value);
}
return;
case Classification.List:
int iter = 0;
foreach (dynamic i in value) // Iterate as list
{
AddChildFromListItem(iter, i);
iter++;
}
return;
case Classification.Object:
foreach (FieldInfo field in valueType.GetFields()) // Iterate through all fields
{
AddChildFromField(field);
}
return;
}
}
// Individual child adders
public SerializationElement AddChildFromField(FieldInfo field)
{
var child = new SerializationElement(field.Name, field.GetValue(value), OriginType.Field, this, field); // Create field child
children.Add(field.Name, child);
return child;
}
public SerializationElement AddChildFromKeyValuePair(dynamic key, dynamic value)
{
var child = new SerializationElement(Convert.ToString(key), value, OriginType.DictEntry, this, key); // Create dictionary entry child
children.Add(Convert.ToString(key), child);
return child;
}
public SerializationElement AddChildFromListItem(int index, dynamic value)
{
var child = new SerializationElement(Convert.ToString(index), value, OriginType.ListItem, this, index); // Create list item child
children.Add(Convert.ToString(index), child);
return child;
}
}
}