MegaFlowXMLReader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 
  2. using UnityEngine;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public class MegaFlowXMLValue
  7. {
  8. public string name;
  9. public string value;
  10. }
  11. public class MegaFlowXMLNode
  12. {
  13. public String tagName;
  14. public MegaFlowXMLNode parentNode;
  15. public List<MegaFlowXMLNode> children;
  16. public List<MegaFlowXMLValue> values;
  17. public MegaFlowXMLNode()
  18. {
  19. tagName = "NONE";
  20. parentNode = null;
  21. children = new List<MegaFlowXMLNode>();
  22. values = new List<MegaFlowXMLValue>();
  23. }
  24. }
  25. public class MegaFlowXMLReader
  26. {
  27. private static char TAG_START = '<';
  28. private static char TAG_END = '>';
  29. private static char SPACE = ' ';
  30. private static char QUOTE = '"';
  31. private static char SLASH = '/';
  32. private static char EQUALS = '=';
  33. private static String BEGIN_QUOTE = "" + EQUALS + QUOTE;
  34. public MegaFlowXMLReader()
  35. {
  36. }
  37. public MegaFlowXMLNode read(String xml)
  38. {
  39. int index = 0;
  40. int lastIndex = 0;
  41. MegaFlowXMLNode rootNode = new MegaFlowXMLNode();
  42. MegaFlowXMLNode currentNode = rootNode;
  43. xml = xml.Replace(" \n", "");
  44. xml = xml.Replace("\n", "");
  45. while ( true )
  46. {
  47. index = xml.IndexOf(TAG_START, lastIndex);
  48. if ( index < 0 || index >= xml.Length )
  49. break;
  50. index++;
  51. lastIndex = xml.IndexOf(TAG_END, index);
  52. if ( lastIndex < 0 || lastIndex >= xml.Length )
  53. break;
  54. int tagLength = lastIndex - index;
  55. String xmlTag = xml.Substring(index, tagLength);
  56. if ( xmlTag[0] == SLASH )
  57. {
  58. currentNode = currentNode.parentNode;
  59. continue;
  60. }
  61. bool openTag = true;
  62. if ( xmlTag[tagLength - 1] == SLASH )
  63. {
  64. xmlTag = xmlTag.Substring(0, tagLength - 1);
  65. openTag = false;
  66. }
  67. MegaFlowXMLNode node = parseTag(xmlTag);
  68. node.parentNode = currentNode;
  69. currentNode.children.Add(node);
  70. if ( openTag )
  71. currentNode = node;
  72. }
  73. return rootNode;
  74. }
  75. public MegaFlowXMLNode parseTag(String xmlTag)
  76. {
  77. MegaFlowXMLNode node = new MegaFlowXMLNode();
  78. int nameEnd = xmlTag.IndexOf(SPACE, 0);
  79. if ( nameEnd < 0 )
  80. {
  81. node.tagName = xmlTag;
  82. return node;
  83. }
  84. String tagName = xmlTag.Substring(0, nameEnd);
  85. node.tagName = tagName;
  86. String attrString = xmlTag.Substring(nameEnd, xmlTag.Length - nameEnd);
  87. return parseAttributes(attrString, node);
  88. }
  89. public MegaFlowXMLNode parseAttributes(String xmlTag, MegaFlowXMLNode node)
  90. {
  91. int index = 0;
  92. int attrNameIndex = 0;
  93. int lastIndex = 0;
  94. while ( true )
  95. {
  96. index = xmlTag.IndexOf(BEGIN_QUOTE, lastIndex);
  97. if ( index < 0 || index > xmlTag.Length )
  98. break;
  99. attrNameIndex = xmlTag.LastIndexOf(SPACE, index);
  100. if ( attrNameIndex < 0 || attrNameIndex > xmlTag.Length )
  101. break;
  102. attrNameIndex++;
  103. String attrName = xmlTag.Substring(attrNameIndex, index - attrNameIndex);
  104. index += 2;
  105. lastIndex = xmlTag.IndexOf(QUOTE, index);
  106. if ( lastIndex < 0 || lastIndex > xmlTag.Length )
  107. {
  108. break;
  109. }
  110. int tagLength = lastIndex - index;
  111. String attrValue = xmlTag.Substring(index, tagLength);
  112. MegaFlowXMLValue val = new MegaFlowXMLValue();
  113. val.name = attrName;
  114. val.value = attrValue;
  115. node.values.Add(val);
  116. }
  117. return node;
  118. }
  119. static char[] commaspace = new char[] { ',', ' ' };
  120. static public Vector3 ParseV3Split(string str, int i)
  121. {
  122. return ParseV3(str.Split(commaspace, StringSplitOptions.RemoveEmptyEntries), i);
  123. }
  124. static public Vector3 ParseV3(string[] str, int i)
  125. {
  126. Vector3 p = Vector3.zero;
  127. p.x = float.Parse(str[i]);
  128. p.y = float.Parse(str[i + 1]);
  129. p.z = float.Parse(str[i + 2]);
  130. return p;
  131. }
  132. }