//static void Main(string[] args)
//{
// Random r = new Random();
// BulletMLSystem.Init(r);
// BulletMLParser parser = new BulletMLParser();
// parser.ParseXML("test.xml");
// BulletMLSrc mover = new BulletMLSrc(parser.tree);
// for (int i = 0; i < 200; i++)
// {
// mover.Update();
// }
// Debug.Write("\n--end--\n");
// Debug.Read();
//}
public void ParseXml(string xmlFileName)
{
//Debug.WriteLine(" ----- " + xmlFileName + " ----- ");
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;
XmlReader reader = XmlReader.Create(xmlFileName, settings);
BulletMLParser parser = new BulletMLParser();
try
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//Debug.Write("<" + reader.Name + ">\n");
BulletMLTree element = new BulletMLTree();
element.Name = parser.StringToName(reader.Name);
if (reader.HasAttributes)
{
element.Type = parser.StringToType(reader.GetAttribute("type"));
element.Label = reader.GetAttribute("label");
#if ExpandedBulletML
element.visible = reader.GetAttribute("visible") == "false" ? false : true;
element.bulletName = reader.GetAttribute("name");
#endif
}
if (Tree == null)
{
Tree = element;
}
else
{
Tree.Children.Add(element);
if (Tree.Children.Count > 1)
{
Tree.Children[Tree.Children.Count - 2].Next = Tree.Children[Tree.Children.Count - 1];
}
element.Parent = Tree;
if (!reader.IsEmptyElement)
{
Tree = element;
}
}
break;
case XmlNodeType.Text: //Display the text in each element.
//Debug.WriteLine(reader.Value +"\n");
string line = reader.Value;
string word = "";
for (int i = 0; i < line.Length; i++)
{
if (('0' <= line[i] && line[i] <= '9') || line[i] == '.')
{
word = word + line[i];
if (i < line.Length - 1) //まだ続きがあれば
{
continue;
}
}
if (word != "")
{
float num;
if (float.TryParse(word, out num))
{
Tree.Values.Add(new BulletValue(BLValueType.Number, num));
word = "";
//Debug.WriteLine("数値を代入" + num);
}
else
{
//Debug.WriteLine("構文にエラーがあります : " + line[i]);
}
}
if (line[i] == '$')
{
if (line[i + 1] >= '0' && line[i + 1] <= '9')
{
Tree.Values.Add(new BulletValue(BLValueType.Param, Convert.ToInt32(line[i + 1].ToString())));
i++;
//Debug.WriteLine("パラメータを代入");
}
else if (line.Substring(i, 5) == "$rank")
{
//Debug.WriteLine("ランクを代入");
i += 4;
Tree.Values.Add(new BulletValue(BLValueType.Rank, 0));
}
else if (line.Substring(i, 5) == "$rand")
{
//Debug.WriteLine("Randを代入");
i += 4;
Tree.Values.Add(new BulletValue(BLValueType.Rand, 0));
}
}
else if (line[i] == '*' || line[i] == '/' || line[i] == '+' || line[i] == '-' || line[i] == '(' || line[i] == ')')
{
Tree.Values.Add(new BulletValue(BLValueType.Operator, line[i]));
//Debug.WriteLine("演算子を代入 " + line[i]);
}
else if (line[i] == ' ' || line[i] == '\n')
{
}
else
{
//Debug.WriteLine("構文にエラーがあります : " + line[i]);
}
}
break;
case XmlNodeType.EndElement: //Display the end of the element.
if (Tree.Parent != null)
{
Tree = Tree.Parent;
}
//Debug.Write("</" + reader.Name + ">\n");
break;
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
reader.Close();
}
//Debug.WriteLine("\n-------------end-----------------");
}