-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOperatorSerializer.cs
More file actions
157 lines (124 loc) · 4.18 KB
/
OperatorSerializer.cs
File metadata and controls
157 lines (124 loc) · 4.18 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
using UnityEngine;
using Forge.Extensions;
using System.Xml;
namespace Forge {
public static class OperatorSerializer {
public static JSONObject Serialize(this Operator op) {
var opJs = new JSONObject(JSONObject.Type.OBJECT);
opJs.AddField("Type", op.GetType().ToString());
opJs.AddField("GUID", op.GUID);
var posJs = new JSONObject(JSONObject.Type.ARRAY);
posJs.Add(op.EditorPosition.x, op.EditorPosition.y);
opJs.AddField("EditorPosition", posJs);
opJs.AddField("IsGeometryOutput", new JSONObject(op.IsGeometryOutput));
var paramsJs = new JSONObject(JSONObject.Type.OBJECT);
opJs.AddField("Params", paramsJs);
foreach (IOOutlet outlet in op.Inputs) {
// float
if (outlet.DataType == typeof(System.Single)) {
paramsJs.AddField(outlet.Name, op.GetValue<float>(outlet));
}
// bool
else if (outlet.DataType == typeof(System.Boolean)) {
paramsJs.AddField(outlet.Name, op.GetValue<bool>(outlet));
}
// int
else if (outlet.DataType == typeof(System.Int32)) {
paramsJs.AddField(outlet.Name, op.GetValue<int>(outlet));
}
// string
else if (outlet.DataType == typeof(System.String)) {
paramsJs.AddField(outlet.Name, op.GetValue<string>(outlet));
}
// Vector2
else if (outlet.DataType == typeof(Vector2)) {
var vJs = new JSONObject(JSONObject.Type.ARRAY);
var v = op.GetValue<Vector2>(outlet);
vJs.Add(v.x, v.y);
paramsJs.AddField(outlet.Name, vJs);
}
// Vector3
else if (outlet.DataType == typeof(Vector3)) {
var vJs = new JSONObject(JSONObject.Type.ARRAY);
var v = op.GetValue<Vector3>(outlet);
vJs.Add(v.x, v.y, v.z);
paramsJs.AddField(outlet.Name, vJs);
}
// Vector4
else if (outlet.DataType == typeof(Vector4)) {
var vJs = new JSONObject(JSONObject.Type.ARRAY);
var v = op.GetValue<Vector4>(outlet);
vJs.Add(v.x, v.y, v.z, v.w);
paramsJs.AddField(outlet.Name, vJs);
}
// Enum
else if (outlet.DataType.IsEnum) {
object objValue = ((System.Reflection.FieldInfo) outlet.Member).GetValue(op);
paramsJs.AddField(outlet.Name, objValue.ToString());
}
else {
paramsJs.AddField(outlet.Name, new JSONObject(JSONObject.Type.NULL));
}
}
return opJs;
}
public static void Deserialize(this Operator op, JSONObject opJs) {
op.GUID = opJs["GUID"].str;
op.EditorPosition = new Vector2(opJs["EditorPosition"][0].n, opJs["EditorPosition"][1].n);
op.IsGeometryOutput = opJs["IsGeometryOutput"].b;
var paramsJs = opJs["Params"];
for (int i = 0; i < op.Inputs.Length; i++) {
string param = op.Inputs[i].Name;
if (paramsJs[param] == null) {
Debug.LogWarningFormat("Serializer warning: `{0}.{1}` not found in serialized format", op.GetType().Name, param);
continue;
}
// float
if (op.Inputs[i].DataType == typeof(System.Single)) {
op.SetValue<float>(op.Inputs[i], paramsJs[param].n);
}
// bool
else if (op.Inputs[i].DataType == typeof(System.Boolean)) {
op.SetValue<bool>(op.Inputs[i], paramsJs[param].b);
}
// int
else if (op.Inputs[i].DataType == typeof(System.Int32)) {
op.SetValue<int>(op.Inputs[i], (int) paramsJs[param].n);
}
// string
else if (op.Inputs[i].DataType == typeof(System.String)) {
op.SetValue<string>(op.Inputs[i], (string) paramsJs[param].str);
}
// Vector2
else if (op.Inputs[i].DataType == typeof(Vector2)) {
op.SetValue<Vector2>(op.Inputs[i], new Vector2(
paramsJs[param][0].n,
paramsJs[param][1].n
));
}
// Vector3
else if (op.Inputs[i].DataType == typeof(Vector3)) {
op.SetValue<Vector3>(op.Inputs[i], new Vector3(
paramsJs[param][0].n,
paramsJs[param][1].n,
paramsJs[param][2].n
));
}
// Vector4
else if (op.Inputs[i].DataType == typeof(Vector4)) {
op.SetValue<Vector4>(op.Inputs[i], new Vector4(
paramsJs[param][0].n,
paramsJs[param][1].n,
paramsJs[param][2].n,
paramsJs[param][3].n
));
}
// Enum
else if (op.Inputs[i].DataType.IsEnum) {
object val = System.Enum.Parse(op.Inputs[i].DataType, paramsJs[param].str);
op.SetValue(op.Inputs[i], val);
}
}
}
}
}