-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetavizUnityFormat.cs
More file actions
475 lines (406 loc) · 13.1 KB
/
MetavizUnityFormat.cs
File metadata and controls
475 lines (406 loc) · 13.1 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
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
/**
* Metaviz Stacked XML format plugin for Unity3d
* v0.6.0
*/
namespace Metaviz
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
/**
* MetavizTransform
*/
public class Transform
{
public int x;
public int y;
public int w;
public int h;
public Transform(int in_x, int in_y, int in_w, int in_h)
{
x = in_x;
y = in_y;
w = in_w;
h = in_h;
}
}
/**
* MetavizNode
*/
public class Node
{
public string id;
public string type;
public Transform transform;
public Dictionary<string, object> param;
public List<Link> links;
public Node(string in_id, string in_type, int in_x, int in_y, int in_w, int in_h, Dictionary<string, object> in_params)
{
links = new List<Link>();
id = in_id;
type = in_type;
transform = new Transform(in_x, in_y, in_w, in_h);
param = in_params;
}
public Node[] GetChildren()
{
List<Node> children = new List<Node>();
foreach (Link link in links)
{
children.Add(link.end);
}
return children.ToArray();
}
public void TraverseTree(Action<Node> callback, int max = 1000, int level = 1)
{
foreach (Node node in GetChildren())
{
callback(node);
if (level < max) node.TraverseTree(callback, level + 1);
}
}
#if UNITY_EDITOR
public string DebugDump()
{
string buffer = "Node:\n";
buffer += " id: " + id + "\n";
buffer += " type: " + type + "\n";
buffer += " transform: (x = " + transform.x + ", y = " + transform.y + ", w = " + transform.w + ", h = " + transform.h + ")\n";
buffer += " params (" + param.Count + ")\n";
foreach (KeyValuePair<string, object> entry in param)
{
buffer += " " + entry.Key + " = " + (string)entry.Value + "\n";
}
buffer += " links (" + links.Count + ")\n";
foreach (Link link in links)
{
buffer += " -> " + link.end.id + "\n";
}
return buffer;
}
#endif
}
/**
* MetavizNodes
*/
public class Nodes
{
List<Node> list;
public Nodes()
{
list = new List<Node>();
}
public Node Add(string id, string type, int x, int y, int w, int h, Dictionary<string, object> data)
{
Node node = new Node(id, type, x, y, w, h, data);
list.Add(node);
return node;
}
public Node Get(string id)
{
foreach (Node node in list)
{
if (node.id == id) return node;
}
return null;
}
public Node Get(string type, string key, string val)
{
foreach (Node node in list)
{
if (node.type == type)
{
foreach (KeyValuePair<string, object> pair in node.param)
{
if (pair.Key.Equals(key) && pair.Value.Equals(val)) return node;
}
}
}
return null;
}
public Node[] GetAll(string ids)
{
List<Node> nodes = new List<Node>();
foreach (string id in ids.Split(','))
{
Node node = Get(id);
if (node != null) nodes.Add(node);
}
return nodes.ToArray();
}
public Node[] GetAll()
{
return list.ToArray();
}
public bool Del(string id)
{
Node node = Get(id);
if (node != null)
{
list.Remove(node);
return true;
}
return false;
}
#if UNITY_EDITOR
public string DebugDump()
{
string buffer = "Nodes (click to unfold):\n\n";
foreach (Node node in list)
{
buffer += node.DebugDump() + "\n";
}
return buffer;
}
#endif
}
/**
* MetavizLink
*/
public class Link
{
public string id;
public string type;
public Node start;
public Node end;
public Link(string in_id, string in_type, Node in_start, Node in_end)
{
id = in_id;
type = in_type;
start = in_start;
end = in_end;
}
#if UNITY_EDITOR
public string DebugDump()
{
string buffer = "Link:\n";
buffer += " id: " + id + "\n";
buffer += " type: " + type + "\n";
buffer += " start: " + start.id + "\n";
buffer += " end: " + end.id + "\n";
return buffer;
}
#endif
}
/**
* MetavizLinks
*/
public class Links
{
List<Link> list;
public Links()
{
list = new List<Link>();
}
public Link Add(string id, string type, Node start, Node end)
{
Link link = new Link(id, type, start, end);
list.Add(link);
return link;
}
public Link Get(string id)
{
foreach (Link link in list)
{
if (link.id == id) return link;
}
return null;
}
public Link[] GetAll(string ids)
{
List<Link> links = new List<Link>();
foreach (string id in ids.Split(','))
{
Link link = Get(id);
if (link != null) links.Add(link);
}
return links.ToArray();
}
public Link[] GetAll()
{
return list.ToArray();
}
public bool Del(string id)
{
Link link = Get(id);
if (link != null)
{
list.Remove(link);
return true;
}
return false;
}
#if UNITY_EDITOR
public string DebugDump()
{
string buffer = "Links (click to unfold):\n\n";
foreach (Link link in list)
{
buffer += link.DebugDump() + "\n";
}
return buffer;
}
#endif
}
/**
* MetavizRender
*/
public class Render
{
public Nodes nodes;
public Links links;
public Render()
{
nodes = new Nodes();
links = new Links();
}
}
/**
* MetavizUnityFormat
*/
public class UnityFormat
{
public Render render;
public UnityFormat()
{
render = new Render();
}
public void parse(string buffer)
{
// Parse
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(buffer);
// Header
string format = xmlDoc.SelectSingleNode("mv/format").InnerText;
int version = int.Parse(xmlDoc.SelectSingleNode("mv/version").InnerText);
if (format != "MetavizStack" || version < 4 || version > 6)
{
Debug.LogError("Unsupported or unknown Metaviz format version!");
return;
}
// All packets
List<XmlElement> packets = new List<XmlElement>();
// Flatten sessions to single packets list
foreach (XmlElement session in xmlDoc.SelectNodes("mv/history/session"))
{
foreach (XmlElement packet in session.ChildNodes) packets.Add(packet);
}
// Sort packets by timestamp
packets.Sort((a, b) => a.GetAttribute("timestamp").CompareTo(b.GetAttribute("timestamp")));
// Process packets for nodes
foreach (XmlElement packet in packets)
{
Node node = packet.HasAttribute("node") ? render.nodes.Get(packet.GetAttribute("node")) : null;
Node[] nodes = packet.HasAttribute("nodes") ? render.nodes.GetAll(packet.GetAttribute("nodes")) : null;
switch (packet.Name)
{
case "add":
// Node
if (packet.HasAttribute("node"))
{
render.nodes.Add(
packet.GetAttribute("node"),
packet.GetAttribute("type"),
int.Parse(packet.GetAttribute("x")),
int.Parse(packet.GetAttribute("y")),
int.Parse(packet.GetAttribute("w")),
int.Parse(packet.GetAttribute("h")),
DataCollect(packet.Attributes)
);
}
break;
case "del":
// Node
if (packet.HasAttribute("node"))
{
render.nodes.Del(packet.GetAttribute("node"));
}
break;
case "move":
foreach (Node n in nodes)
{
if (packet.HasAttribute("position-x"))
{
n.transform.x = int.Parse(packet.GetAttribute("position-x"));
n.transform.y = int.Parse(packet.GetAttribute("position-y"));
}
else if (packet.HasAttribute("offset"))
{
n.transform.x += int.Parse(packet.GetAttribute("offset-x"));
n.transform.y += int.Parse(packet.GetAttribute("offset-y"));
}
}
break;
case "resize":
foreach (Node n in nodes)
{
n.transform.w = int.Parse(packet.GetAttribute("w"));
n.transform.h = int.Parse(packet.GetAttribute("h"));
}
break;
case "param":
if (node != null)
{
foreach (KeyValuePair<string, object> entry in DataCollect(packet.Attributes))
{
node.param[entry.Key] = entry.Value;
}
}
break;
}
} // foreach
// Process packets for links (when all nodes already exists)
foreach (XmlElement packet in packets)
{
switch (packet.Name)
{
case "add":
// Link
if (packet.HasAttribute("link"))
{
Node start = render.nodes.Get(packet.GetAttribute("start"));
Node end = render.nodes.Get(packet.GetAttribute("end"));
if (start != null && end != null)
{
Link link = render.links.Add(
packet.GetAttribute("link"),
packet.GetAttribute("type"),
start,
end
);
start.links.Add(link);
}
}
break;
case "del":
// Link
if (packet.HasAttribute("link"))
{
render.links.Del(packet.GetAttribute("link"));
}
break;
}
} // foreach
}
Dictionary<string, object> DataCollect(XmlAttributeCollection attributes)
{
Dictionary<string, object> data = new Dictionary<string, object>();
foreach (XmlAttribute attribute in attributes)
{
// Get param
if (attribute.Name.StartsWith("param-"))
{
data[attribute.Name.Substring(6)] = System.Net.WebUtility.HtmlDecode(attribute.Value);
}
// Version < 6 compatibility
else if (attribute.Name.StartsWith("data-"))
{
data[attribute.Name.Substring(5)] = System.Net.WebUtility.HtmlDecode(attribute.Value);
}
}
return data;
}
}
}