My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Squared.Game.Serialization;

namespace Squared.Game.Graph {
public interface INode {
IEnumerable<INode> GetChildren ();
void AddChild (INode child);
}

public interface IGraphWriter {
void BeginWrite (INode root);
void WriteNodeHeader (INode node);
void WriteNodeFooter (INode node);
void EndWrite ();
}

public struct NodeInfo {
public INode Node;
public INode Parent;

public NodeInfo (INode node, INode parent) {
Node = node;
Parent = parent;
}

public NodeInfo (INode node) {
Node = node;
Parent = null;
}

public override string ToString () {
return String.Format("NodeInfo({0}, {1})", Node, Parent);
}
}

#if !XBOX
// Whoops, we removed a bunch of core classes and APIs without updating the documentation.
// Sorry, our bad. - Microsoft

public class XmlGraphReader {
struct StackEntry {
public XmlNode Child;
public INode Parent;
}

private XmlReader _Reader;
private ITypeResolver _TypeResolver;

public XmlGraphReader (XmlReader reader)
: this(reader, SerializationExtensions.DefaultTypeResolver) {
}

public XmlGraphReader (XmlReader reader, ITypeResolver typeResolver) {
_Reader = reader;
_TypeResolver = typeResolver;
}

private XmlDocument ReadXmlFragment (string elementName) {
if (_Reader.IsEmptyElement)
return null;

var result = new XmlDocument();
if (!_Reader.Read())
return null;

while (_Reader.NodeType != XmlNodeType.EndElement || _Reader.Name != elementName) {
var node = result.ReadNode(_Reader);

if (node != null)
result.AppendChild(node);
else
throw new ArgumentException("Invalid XML fragment");
}

return result;
}

private INode ResolveNode (XmlNode node, StringValueDictionary<INode> nodes) {
return nodes[node.Attributes.GetNamedItem("key").Value];
}

private INode BuildGraph (XmlDocument graph, StringValueDictionary<INode> nodes) {
INode root = null;
var xmlStack = new LinkedList<StackEntry>();
foreach (var child in graph.ChildNodes.Cast<XmlNode>()) {
if (child.NodeType == XmlNodeType.Element) {
xmlStack.AddLast(new StackEntry { Child = child, Parent = null });
break;
}
}

while (xmlStack.Count > 0) {
var current = xmlStack.First.Value;
xmlStack.RemoveFirst();

var node = ResolveNode(current.Child, nodes);
if (current.Parent == null)
root = node;
else
current.Parent.AddChild(node);

foreach (var child in current.Child.ChildNodes.Cast<XmlNode>()) {
if (child.NodeType == XmlNodeType.Element)
xmlStack.AddLast(new StackEntry { Child = child, Parent = node });
}
}

return root;
}

public INode Read () {
_Reader.ReadToDescendant("graph");
var graph = ReadXmlFragment("graph");

_Reader.ReadToFollowing("nodes");
var nodes = _Reader.ReadDictionary<INode>(_TypeResolver);

return BuildGraph(graph, nodes);
}
}
#endif

public class XmlGraphWriter : IGraphWriter {
private XmlWriter _Writer;
private ITypeResolver _TypeResolver;
private int _NextNodeID;
private Dictionary<INode, string> _NodeIDs;
private StringValueDictionary<INode> _Nodes;

public XmlGraphWriter (XmlWriter writer)
: this (writer, SerializationExtensions.DefaultTypeResolver) {
}

public XmlGraphWriter (XmlWriter writer, ITypeResolver typeResolver) {
_Writer = writer;
_TypeResolver = typeResolver;
}

public void BeginWrite (INode root) {
_NextNodeID = 1;
_Nodes = new StringValueDictionary<INode>();
_NodeIDs = new Dictionary<INode, string>();
_Writer.WriteStartElement("graph");
}

internal string GetNodeID (INode node) {
string id = null;

if (_NodeIDs.TryGetValue(node, out id))
return id;

if (node is INamedObject)
id = (node as INamedObject).Name;

if ((id != null) && _Nodes.ContainsKey(id)) {
if (_Nodes[id] == node)
return id;
}

if (id == null)
id = _NextNodeID.ToString();

_NextNodeID += 1;
_NodeIDs[node] = id;
_Nodes[id] = node;
return id;
}

public void WriteNodeHeader (INode node) {
string id = GetNodeID(node);

_Writer.WriteStartElement("node");
_Writer.WriteAttributeString("key", id.ToString());
}

public void WriteNodeFooter (INode node) {
_Writer.WriteEndElement();
}

public void EndWrite () {
_Writer.WriteEndElement();
_Writer.WriteStartElement("nodes");
_Writer.WriteDictionary(_Nodes, _TypeResolver);
_Writer.WriteEndElement();
_Writer.Flush();
}
}

public static class GraphExtensionMethods {
public static IEnumerable<NodeInfo> TraverseDepthFirst (this INode root) {
var visitedNodes = new Dictionary<INode, bool>();
var list = new LinkedList<NodeInfo>();
var result = new NodeInfo { Node = root, Parent = null };
list.AddLast(result);

while (list.Count > 0) {
var current = list.First;
result.Parent = current.Value.Node;
if (!visitedNodes.ContainsKey(result.Parent)) {
visitedNodes.Add(result.Parent, true);
foreach (var leaf in current.Value.Node.GetChildren()) {
result.Node = leaf;
list.AddBefore(current, result);
}
}

list.Remove(current);
yield return current.Value;
}
}

public static IEnumerable<NodeInfo> TraverseBreadthFirst (this INode root) {
var visitedNodes = new Dictionary<INode, bool>();
var list = new LinkedList<NodeInfo>();
var result = new NodeInfo { Node = root, Parent = null };
list.AddLast(result);

while (list.Count > 0) {
var current = list.First;
result.Parent = current.Value.Node;
if (!visitedNodes.ContainsKey(result.Parent)) {
visitedNodes.Add(result.Parent, true);
foreach (var leaf in current.Value.Node.GetChildren()) {
result.Node = leaf;
list.AddLast(result);
}
}

list.Remove(current);
yield return current.Value;
}
}

public static void Serialize (this INode root, IGraphWriter writer) {
var stack = new Stack<INode>();

writer.BeginWrite(root);

foreach (var info in root.TraverseDepthFirst()) {
while ((stack.Count > 0) && (stack.Peek() != info.Parent)) {
writer.WriteNodeFooter(stack.Pop());
}

writer.WriteNodeHeader(info.Node);
stack.Push(info.Node);
}

while (stack.Count > 0)
writer.WriteNodeFooter(stack.Pop());

writer.EndWrite();
}

#if !XBOX
public static INode ReadGraph (this XmlReader reader) {
return ReadGraph(reader, SerializationExtensions.DefaultTypeResolver);
}

public static INode ReadGraph (this XmlReader reader, ITypeResolver typeResolver) {
var greader = new XmlGraphReader(reader, typeResolver);
return greader.Read();
}
#endif
}
}

Change log

r392 by kevin.gadd on Dec 30, 2010   Diff
Fixed most portability issues with the XNA
3.1 -> 4.0 port on the XBox 360. XmlNode
and XmlDocument are still totally f--king
gone, though, because Microsoft is
retarded. Wee!
Go to: 
Project members, sign in to write a code review

Older revisions

r329 by kevin.gadd on Jan 23, 2010   Diff
Fix a bug I introduced into Web
Fix a bug in how Graph parses
malformed XML
r267 by kevin.gadd on May 16, 2009   Diff
Correctly handle reentrant
SetAnimation calls from within
animations
Add WhenFinished and WatchPlayState
animation extension methods
...
r246 by kevin.gadd on Feb 19, 2009   Diff
Add basic handling of graph cycles to
TraverseBreadthFirst and
TraverseDepthFirst, and add a test
All revisions of this file

File info

Size: 8849 bytes, 269 lines
Powered by Google Project Hosting