-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBulletPattern.cs
153 lines (135 loc) · 4.74 KB
/
BulletPattern.cs
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
using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Schema;
namespace BulletMLLib
{
/// <summary>
/// This is a complete document that describes a bullet pattern.
/// </summary>
public class BulletPattern
{
public BulletPattern(string path)
{
var settings = new XmlReaderSettings { ProhibitDtd = false };
var reader = XmlReader.Create(path, settings);
ParseXML(reader, null);
}
public BulletPattern(XmlReader reader)
{
ParseXML(reader, null);
}
/// <summary>
/// The root node of a tree structure that describes the bullet pattern
/// </summary>
public BulletMLNode RootNode { get; private set; }
public string Filename { get; set; }
/// <summary>
/// the orientation of this bullet pattern: horizontal or veritcal
/// this is read in from the xml
/// </summary>
/// <value>The orientation.</value>
public PatternType Orientation { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="BulletMLLib.BulletPattern"/> class.
/// </summary>
public BulletPattern()
{
RootNode = null;
}
/// <summary>
/// convert a string to a pattern type enum
/// </summary>
/// <returns>The type to name.</returns>
/// <param name="str">String.</param>
private static PatternType StringToPatternType(string str)
{
return (PatternType)Enum.Parse(typeof(PatternType), str, true);
}
/// <summary>
/// Parses a bullet pattern from a BulletML Xml file
/// </summary>
/// <param name="reader">The XmlReader that contains the xmlfile. Note that if you open it as a local file the web player will not work.
/// You can use WWW-requests instead.</param>
/// <param name="callback">A callback function for when the fileparsing is completed</param>
public void ParseXML(XmlReader reader, Action callback)
{
try
{
//Open the file.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
XmlNode rootXmlNode = xmlDoc.DocumentElement;
//make sure it is actually an xml node
if (rootXmlNode != null && rootXmlNode.NodeType == XmlNodeType.Element)
{
//eat up the name of that xml node
string strElementName = rootXmlNode.Name;
if ("bulletml" != strElementName)
{
//The first node HAS to be bulletml
UnityEngine.Debug.Log("Error reading \"" + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead");
throw new Exception("Error reading \"" + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead");
}
//Create the root node of the bulletml tree
RootNode = new BulletMLNode(NodeName.Bulletml);
//Read in the whole bulletml tree
RootNode.Parse(rootXmlNode, null);
//Find what kind of pattern this is: horizontal or vertical
XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes;
for (int i = 0; i < mapAttributes.Count; i++)
{
//will only have the name attribute
string strName = mapAttributes.Item(i).Name;
string strValue = mapAttributes.Item(i).Value;
if ("type" == strName)
{
//if this is a top level node, "type" will be veritcal or horizontal
Orientation = StringToPatternType(strValue);
}
}
}
}
catch (Exception ex)
{
//an error ocurred reading in the tree
UnityEngine.Debug.Log("Error reading." + ex.Message);
throw new Exception("Error reading \"" + "\"", ex);
}
reader.Close();
//validate that the bullet nodes are all valid
try
{
RootNode.ValidateNode();
}
catch (Exception ex)
{
//an error ocurred reading in the tree
UnityEngine.Debug.Log("Error reading \"" + "\"" + ex.Message);
throw new Exception("Error reading \"" + "\"", ex);
}
try
{
UnityEngine.Debug.Log("Loading done.");
}
catch (Exception)
{
// will throw if not running unity
}
if (callback != null)
callback();
}
/// <summary>
/// delegate method that gets called when a validation error occurs
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="args">Arguments.</param>
public static void MyValidationEventHandler(object sender, ValidationEventArgs args)
{
throw new XmlSchemaException("Error validating bulletml document: " + args.Message,
args.Exception,
args.Exception.LineNumber,
args.Exception.LinePosition);
}
}
}