RealWorldTerrainXML.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using InfinityCode.RealWorldTerrain.Utils;
  9. using UnityEngine;
  10. using Object = UnityEngine.Object;
  11. #if NETFX_CORE
  12. using Windows.Data.Xml.Dom;
  13. #else
  14. using System.Xml;
  15. #endif
  16. namespace InfinityCode.RealWorldTerrain.XML
  17. {
  18. /// <summary>
  19. /// Wrapper for XML.
  20. /// </summary>
  21. public class RealWorldTerrainXML : IEnumerable
  22. {
  23. private XmlDocument _document;
  24. private XmlElement _element;
  25. /// <summary>
  26. /// Name of the node.
  27. /// </summary>
  28. public string name
  29. {
  30. #if !NETFX_CORE
  31. get { return _element != null ? _element.Name : null; }
  32. #else
  33. get { return _element != null ? _element.TagName: null; }
  34. #endif
  35. }
  36. /// <summary>
  37. /// The number of child nodes.
  38. /// </summary>
  39. public int count
  40. {
  41. get { return hasChildNodes ? _element.ChildNodes.Count : 0; }
  42. }
  43. /// <summary>
  44. /// Checks whether the contents of the node.
  45. /// </summary>
  46. public bool isNull
  47. {
  48. get { return _document == null || _element == null; }
  49. }
  50. /// <summary>
  51. /// Reference to XmlDocument.
  52. /// </summary>
  53. public XmlDocument document
  54. {
  55. get { return _document; }
  56. }
  57. /// <summary>
  58. /// Reference to XmlElement.
  59. /// </summary>
  60. public XmlElement element
  61. {
  62. get { return _element; }
  63. }
  64. /// <summary>
  65. /// Gets a value indicating whether the current node has any attributes.
  66. /// </summary>
  67. public bool hasAttributes
  68. {
  69. get { return _element != null && _element.Attributes.Count > 0; }
  70. }
  71. /// <summary>
  72. /// Gets a value indicating the presence of the child nodes from the current node.
  73. /// </summary>
  74. public bool hasChildNodes
  75. {
  76. get
  77. {
  78. #if !NETFX_CORE
  79. return _element != null && _element.HasChildNodes;
  80. #else
  81. return _element != null && _element.HasChildNodes();
  82. #endif
  83. }
  84. }
  85. /// <summary>
  86. /// Content of node as string.
  87. /// </summary>
  88. public string outerXml
  89. {
  90. get
  91. {
  92. #if !NETFX_CORE
  93. return _element != null ? _element.OuterXml : null;
  94. #else
  95. return _element != null? _element.GetXml(): null;
  96. #endif
  97. }
  98. }
  99. /// <summary>
  100. /// Get the child element by index.
  101. /// </summary>
  102. /// <param name="index">Index of child element.</param>
  103. /// <returns>Child element.</returns>
  104. public RealWorldTerrainXML this[int index]
  105. {
  106. get
  107. {
  108. if (!hasChildNodes) return new RealWorldTerrainXML();
  109. if (index < 0 || index >= _element.ChildNodes.Count) return new RealWorldTerrainXML();
  110. return new RealWorldTerrainXML(_element.ChildNodes[index] as XmlElement);
  111. }
  112. }
  113. /// <summary>
  114. /// Get the child element by name.
  115. /// </summary>
  116. /// <param name="childName">Name of child element.</param>
  117. /// <returns>Child element.</returns>
  118. public RealWorldTerrainXML this[string childName]
  119. {
  120. get
  121. {
  122. if (!hasChildNodes) return new RealWorldTerrainXML();
  123. #if !NETFX_CORE
  124. return new RealWorldTerrainXML(_element[childName]);
  125. #else
  126. return new RealWorldTerrainXML(GetFirstChild(_element, childName));
  127. #endif
  128. }
  129. }
  130. /// <summary>
  131. /// Creates an empty element.
  132. /// </summary>
  133. public RealWorldTerrainXML()
  134. {
  135. }
  136. /// <summary>
  137. /// Creates a new element with the specified name.
  138. /// </summary>
  139. /// <param name="nodeName">Name of element.</param>
  140. public RealWorldTerrainXML(string nodeName)
  141. {
  142. try
  143. {
  144. _document = new XmlDocument();
  145. _element = _document.CreateElement(nodeName);
  146. _document.AppendChild(_element);
  147. }
  148. catch (Exception)
  149. {
  150. _document = null;
  151. _element = null;
  152. }
  153. }
  154. /// <summary>
  155. /// Creates a new element based on the XmlElement.
  156. /// </summary>
  157. /// <param name="xmlElement">XmlElement for which will create the wrapper.</param>
  158. public RealWorldTerrainXML(XmlElement xmlElement)
  159. {
  160. if (xmlElement == null) return;
  161. _element = xmlElement;
  162. _document = _element.OwnerDocument;
  163. }
  164. /// <summary>
  165. /// Get an attribute by name.
  166. /// </summary>
  167. /// <param name="attributeName">Name of attribute.</param>
  168. /// <returns>Value of attribute as string.</returns>
  169. public string A(string attributeName)
  170. {
  171. return A<string>(attributeName);
  172. }
  173. /// <summary>
  174. /// Get an attribute by name, and return as the specified type.
  175. /// </summary>
  176. /// <typeparam name="T">Type of attribute.</typeparam>
  177. /// <param name="attributeName">Name of attribute.</param>
  178. /// <returns>Value of attribute as specified type.</returns>
  179. public T A<T>(string attributeName)
  180. {
  181. if (!hasAttributes) return default(T);
  182. #if !NETFX_CORE
  183. XmlAttribute el = _element.Attributes[attributeName];
  184. #else
  185. XmlAttribute el = _element.Attributes.GetNamedItem(attributeName) as XmlAttribute;
  186. #endif
  187. if (el == null) return default(T);
  188. string value = el.Value;
  189. if (string.IsNullOrEmpty(value)) return default(T);
  190. Type type = typeof(T);
  191. if (type == typeof(string)) return (T) Convert.ChangeType(value, type);
  192. T obj = default(T);
  193. PropertyInfo[] properties = RealWorldTerrainReflectionHelper.GetProperties(type);
  194. Type underlyingType = type;
  195. #if !UNITY_WSA
  196. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.InvariantCultureIgnoreCase)) underlyingType = properties[1].PropertyType;
  197. #else
  198. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.OrdinalIgnoreCase)) underlyingType = properties[1].PropertyType;
  199. #endif
  200. MethodInfo method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string), typeof(IFormatProvider) });
  201. if (method != null) return (T)method.Invoke(null, new object[] { value, RealWorldTerrainCultureInfo.numberFormat });
  202. method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string) });
  203. if (method != null) return (T)method.Invoke(null, new[] { value });
  204. return obj;
  205. }
  206. /// <summary>
  207. /// Set an named attribute.
  208. /// </summary>
  209. /// <param name="attributeName">Name of attribute.</param>
  210. /// <param name="value">Value of attribute.</param>
  211. public void A(string attributeName, object value)
  212. {
  213. if (_element == null) return;
  214. _element.SetAttribute(attributeName, value.ToString());
  215. }
  216. /// <summary>
  217. /// Sets the color attribute as hex value.
  218. /// </summary>
  219. /// <param name="attributeName">Name of attribute.</param>
  220. /// <param name="value">Color</param>
  221. public void A(string attributeName, Color32 value)
  222. {
  223. A(attributeName, value.r.ToString("X2") + value.g.ToString("X2") + value.b.ToString("X2"));
  224. }
  225. /// <summary>
  226. /// Append a child element.
  227. /// </summary>
  228. /// <param name="newChild">Element.</param>
  229. public void AppendChild(XmlElement newChild)
  230. {
  231. if (_element == null || newChild == null) return;
  232. if (_element.OwnerDocument != newChild.OwnerDocument) newChild = _element.OwnerDocument.ImportNode(newChild, true) as XmlElement;
  233. _element.AppendChild(newChild);
  234. }
  235. /// <summary>
  236. /// Append a child element.
  237. /// </summary>
  238. /// <param name="newChild">Element.</param>
  239. public void AppendChild(RealWorldTerrainXML newChild)
  240. {
  241. if (newChild == null) return;
  242. AppendChild(newChild._element);
  243. }
  244. /// <summary>
  245. /// Append a child elements.
  246. /// </summary>
  247. /// <param name="list">List of elements.</param>
  248. #if !NETFX_CORE
  249. public void AppendChilds(IEnumerable<XmlNode> list)
  250. #else
  251. public void AppendChilds(IEnumerable<IXmlNode> list)
  252. #endif
  253. {
  254. if (_element == null) return;
  255. foreach (var node in list) _element.AppendChild(node);
  256. }
  257. /// <summary>
  258. /// Append a child elements.
  259. /// </summary>
  260. /// <param name="list">List of elements.</param>
  261. public void AppendChilds(IEnumerable<RealWorldTerrainXML> list)
  262. {
  263. if (_element == null) return;
  264. foreach (RealWorldTerrainXML node in list)
  265. {
  266. if (node._element != null) _element.AppendChild(node._element);
  267. }
  268. }
  269. /// <summary>
  270. /// Append a child elements.
  271. /// </summary>
  272. /// <param name="list">List of elements.</param>
  273. public void AppendChilds(XmlNodeList list)
  274. {
  275. if (_element == null) return;
  276. #if !NETFX_CORE
  277. foreach (XmlNode node in list) _element.AppendChild(node);
  278. #else
  279. foreach (IXmlNode node in list) _element.AppendChild(node);
  280. #endif
  281. }
  282. /// <summary>
  283. /// Append a child elements.
  284. /// </summary>
  285. /// <param name="list">List of elements.</param>
  286. public void AppendChilds(RealWorldTerrainXMLList list)
  287. {
  288. if (_element == null) return;
  289. foreach (RealWorldTerrainXML node in list)
  290. {
  291. if (node._element != null) _element.AppendChild(node._element);
  292. }
  293. }
  294. /// <summary>
  295. /// Creates a child element with the specified name.
  296. /// </summary>
  297. /// <param name="nodeName">Name of child element.</param>
  298. /// <returns>Child element.</returns>
  299. public RealWorldTerrainXML Create(string nodeName)
  300. {
  301. if (_document == null || _element == null) return new RealWorldTerrainXML();
  302. XmlElement xmlElement = _document.CreateElement(nodeName);
  303. _element.AppendChild(xmlElement);
  304. return new RealWorldTerrainXML(xmlElement);
  305. }
  306. /// <summary>
  307. /// Creates a child element with the specified name and value.
  308. /// </summary>
  309. /// <param name="nodeName">Name of child element.</param>
  310. /// <param name="value">Value of child element.</param>
  311. /// <returns>Child element.</returns>
  312. public RealWorldTerrainXML Create(string nodeName, bool value)
  313. {
  314. return Create(nodeName, value ? "True" : "False");
  315. }
  316. /// <summary>
  317. /// Creates a child element with the specified name and value.
  318. /// </summary>
  319. /// <param name="nodeName">Name of child element.</param>
  320. /// <param name="value">Value of child element.</param>
  321. /// <returns>Child element.</returns>
  322. public RealWorldTerrainXML Create(string nodeName, Color32 value)
  323. {
  324. return Create(nodeName, value.r.ToString("X2") + value.g.ToString("X2") + value.b.ToString("X2"));
  325. }
  326. /// <summary>
  327. /// Creates a child element with the specified name and value.
  328. /// </summary>
  329. /// <param name="nodeName">Name of child element.</param>
  330. /// <param name="value">Value of child element.</param>
  331. /// <returns>Child element.</returns>
  332. public RealWorldTerrainXML Create(string nodeName, float value)
  333. {
  334. return Create(nodeName, value.ToString());
  335. }
  336. /// <summary>
  337. /// Creates a child element with the specified name and value.
  338. /// </summary>
  339. /// <param name="nodeName">Name of child element.</param>
  340. /// <param name="value">Value of child element.</param>
  341. /// <returns>Child element.</returns>
  342. public RealWorldTerrainXML Create(string nodeName, double value)
  343. {
  344. return Create(nodeName, value.ToString());
  345. }
  346. /// <summary>
  347. /// Creates a child element with the specified name and value.
  348. /// </summary>
  349. /// <param name="nodeName">Name of child element.</param>
  350. /// <param name="value">Value of child element.</param>
  351. /// <returns>Child element.</returns>
  352. public RealWorldTerrainXML Create(string nodeName, int value)
  353. {
  354. return Create(nodeName, value.ToString());
  355. }
  356. /// <summary>
  357. /// Creates a child element with the specified name and value.
  358. /// </summary>
  359. /// <param name="nodeName">Name of child element.</param>
  360. /// <param name="value">Value of child element.</param>
  361. /// <returns>Child element.</returns>
  362. public RealWorldTerrainXML Create(string nodeName, Object value)
  363. {
  364. return Create(nodeName, value != null ? value.GetInstanceID() : 0);
  365. }
  366. /// <summary>
  367. /// Creates a child element with the specified name and value.
  368. /// </summary>
  369. /// <param name="nodeName">Name of child element.</param>
  370. /// <param name="value">Value of child element.</param>
  371. /// <returns>Child element.</returns>
  372. public RealWorldTerrainXML Create(string nodeName, string value)
  373. {
  374. RealWorldTerrainXML node = Create(nodeName);
  375. node.SetChild(value);
  376. return node;
  377. }
  378. /// <summary>
  379. /// Creates a child element with the specified name and value.
  380. /// </summary>
  381. /// <param name="nodeName">Name of child element.</param>
  382. /// <param name="value">Value of child element.</param>
  383. /// <returns>Child element.</returns>
  384. public RealWorldTerrainXML Create(string nodeName, Vector2 value)
  385. {
  386. RealWorldTerrainXML node = Create(nodeName);
  387. node.Create("X", value.x);
  388. node.Create("Y", value.y);
  389. return node;
  390. }
  391. /// <summary>
  392. /// Creates a child element with the specified name and value.
  393. /// </summary>
  394. /// <param name="nodeName">Name of child element.</param>
  395. /// <param name="value">Value of child element.</param>
  396. /// <returns>Child element.</returns>
  397. public RealWorldTerrainXML Create(string nodeName, Vector3 value)
  398. {
  399. RealWorldTerrainXML node = Create(nodeName);
  400. node.Create("X", value.x);
  401. node.Create("Y", value.y);
  402. node.Create("Z", value.z);
  403. return node;
  404. }
  405. /// <summary>
  406. /// Find a child at the specified XPath.
  407. /// </summary>
  408. /// <param name="xpath">XPath string.</param>
  409. /// <param name="nsmgr">An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression. </param>
  410. /// <returns>Child element.</returns>
  411. public RealWorldTerrainXML Find(string xpath, System.Xml.XmlNamespaceManager nsmgr = null)
  412. {
  413. if (!hasChildNodes) return new RealWorldTerrainXML();
  414. #if !NETFX_CORE
  415. XmlElement xmlElement = _element.SelectSingleNode(xpath, nsmgr) as XmlElement;
  416. #else
  417. string ns = null;
  418. if (nsmgr != null)
  419. {
  420. var nss = nsmgr.GetNamespacesInScope(System.Xml.XmlNamespaceScope.ExcludeXml);
  421. if (nss.Keys.Count > 0)
  422. {
  423. var key = nss.Keys.First();
  424. ns = String.Format("xmlns:{0}='{1}'", key, nsmgr.LookupNamespace(key));
  425. }
  426. }
  427. XmlElement xmlElement = (ns == null ? _element.SelectSingleNode(xpath) : _element.SelectSingleNodeNS(xpath, ns)) as XmlElement;
  428. #endif
  429. if (xmlElement != null) return new RealWorldTerrainXML(xmlElement);
  430. return new RealWorldTerrainXML();
  431. }
  432. /// <summary>
  433. /// Find a child at the specified XPath, and return value as the specified type.
  434. /// </summary>
  435. /// <typeparam name="T">Type of child element.</typeparam>
  436. /// <param name="xpath">XPath string.</param>
  437. /// <param name="nsmgr">An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression. </param>
  438. /// <returns>Value of child element as the specified type.</returns>
  439. public T Find<T>(string xpath, System.Xml.XmlNamespaceManager nsmgr = null)
  440. {
  441. if (!hasChildNodes) return default(T);
  442. #if !NETFX_CORE
  443. return Get<T>(_element.SelectSingleNode(xpath, nsmgr) as XmlElement);
  444. #else
  445. string ns = null;
  446. if (nsmgr != null)
  447. {
  448. var nss = nsmgr.GetNamespacesInScope(System.Xml.XmlNamespaceScope.ExcludeXml);
  449. if (nss.Keys.Count > 0)
  450. {
  451. var key = nss.Keys.First();
  452. ns = String.Format("xmlns:{0}='{1}'", key, nsmgr.LookupNamespace(key));
  453. }
  454. }
  455. return Get<T>((ns == null ? _element.SelectSingleNode(xpath) : _element.SelectSingleNodeNS(xpath, ns)) as XmlElement);
  456. #endif
  457. }
  458. /// <summary>
  459. /// Finds all childs at the specified XPath.
  460. /// </summary>
  461. /// <param name="xpath">XPath string.</param>
  462. /// <param name="nsmgr">An XmlNamespaceManager to use for resolving namespaces for prefixes in the XPath expression. </param>
  463. /// <returns>List of the elements.</returns>
  464. public RealWorldTerrainXMLList FindAll(string xpath, System.Xml.XmlNamespaceManager nsmgr = null)
  465. {
  466. if (!hasChildNodes) return new RealWorldTerrainXMLList();
  467. #if !NETFX_CORE
  468. return new RealWorldTerrainXMLList(_element.SelectNodes(xpath, nsmgr));
  469. #else
  470. string ns = null;
  471. if (nsmgr != null)
  472. {
  473. var nss = nsmgr.GetNamespacesInScope(System.Xml.XmlNamespaceScope.ExcludeXml);
  474. if (nss.Keys.Count > 0)
  475. {
  476. var key = nss.Keys.First();
  477. ns = String.Format("xmlns:{0}='{1}'", key, nsmgr.LookupNamespace(key));
  478. }
  479. }
  480. return new RealWorldTerrainXMLList(ns == null ? _element.SelectNodes(xpath) : _element.SelectNodesNS(xpath, ns));
  481. #endif
  482. }
  483. /// <summary>
  484. /// Get the value of element as string.
  485. /// </summary>
  486. /// <param name="childName">Name of child.</param>
  487. /// <returns>Value of element as string.</returns>
  488. public string Get(string childName)
  489. {
  490. return Get<string>(childName);
  491. }
  492. #if NETFX_CORE
  493. private static XmlElement GetFirstChild(XmlElement element, string childName)
  494. {
  495. if (element == null) return null;
  496. var nodeList = element.GetElementsByTagName(childName);
  497. if (nodeList.Count == 0) return null;
  498. return nodeList[0] as XmlElement;
  499. }
  500. #endif
  501. /// <summary>
  502. /// Get the value of element as the specified type.
  503. /// </summary>
  504. /// <typeparam name="T">Type of element</typeparam>
  505. /// <param name="el">Element</param>
  506. /// <returns>Value of element as the specified type.</returns>
  507. public T Get<T>(XmlElement el)
  508. {
  509. if (el == null) return default(T);
  510. #if !NETFX_CORE
  511. string value = el.InnerXml;
  512. #else
  513. string value = el.InnerText;
  514. #endif
  515. if (string.IsNullOrEmpty(value)) return default(T);
  516. Type type = typeof(T);
  517. if (type == typeof(string)) return (T) Convert.ChangeType(value, type);
  518. if (type == typeof(Color) || type == typeof(Color32)) return (T) Convert.ChangeType(RealWorldTerrainUtils.HexToColor(value), type);
  519. #if !NETFX_CORE
  520. if (type == typeof(Vector2)) return (T) Convert.ChangeType(new Vector2(Get<float>(el["X"]), Get<float>(el["Y"])), type);
  521. if (type == typeof(Vector3)) return (T) Convert.ChangeType(new Vector3(Get<float>(el["X"]), Get<float>(el["Y"]), Get<float>(el["Z"])), type);
  522. #else
  523. if (type == typeof(Vector2)) return (T)Convert.ChangeType(new Vector2(Get<float>(GetFirstChild(el, "X")), Get<float>(GetFirstChild(el, "Y"))), type);
  524. if (type == typeof(Vector3)) return (T)Convert.ChangeType(new Vector3(Get<float>(GetFirstChild(el, "X")), Get<float>(GetFirstChild(el, "Y")), Get<float>(GetFirstChild(el, "Z"))), type);
  525. #endif
  526. T obj = default(T);
  527. PropertyInfo[] properties = RealWorldTerrainReflectionHelper.GetProperties(type);
  528. Type underlyingType = type;
  529. #if !UNITY_WSA
  530. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.InvariantCultureIgnoreCase)) underlyingType = properties[1].PropertyType;
  531. #else
  532. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.OrdinalIgnoreCase)) underlyingType = properties[1].PropertyType;
  533. #endif
  534. try
  535. {
  536. MethodInfo method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string), typeof(IFormatProvider) });
  537. if (method != null) obj = (T)method.Invoke(null, new object[] { value, RealWorldTerrainCultureInfo.numberFormat });
  538. else
  539. {
  540. method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string) });
  541. obj = (T)method.Invoke(null, new[] { value });
  542. }
  543. }
  544. catch (Exception exception)
  545. {
  546. Debug.Log(exception.Message + "\n" + exception.StackTrace);
  547. throw;
  548. }
  549. return obj;
  550. }
  551. /// <summary>
  552. /// Get the value of element as the specified type or default value if the child is not found.
  553. /// </summary>
  554. /// <typeparam name="T">Type of element</typeparam>
  555. /// <param name="el">Element</param>
  556. /// <param name="defaultValue">Default value</param>
  557. /// <returns>Value of element as the specified type or default value.</returns>
  558. public T Get<T>(XmlElement el, T defaultValue)
  559. {
  560. if (el == null) return defaultValue;
  561. #if !NETFX_CORE
  562. string value = el.InnerXml;
  563. #else
  564. string value = el.InnerText;
  565. #endif
  566. if (string.IsNullOrEmpty(value)) return defaultValue;
  567. Type type = typeof(T);
  568. if (type == typeof(string)) return (T) Convert.ChangeType(value, type);
  569. if (type == typeof(Color) || type == typeof(Color32)) return (T) Convert.ChangeType(RealWorldTerrainUtils.HexToColor(value), type);
  570. #if !NETFX_CORE
  571. if (type == typeof(Vector2)) return (T) Convert.ChangeType(new Vector2(Get<float>(el["X"]), Get<float>(el["Y"])), type);
  572. if (type == typeof(Vector3)) return (T) Convert.ChangeType(new Vector3(Get<float>(el["X"]), Get<float>(el["Y"]), Get<float>(el["Z"])), type);
  573. #else
  574. if (type == typeof(Vector2)) return (T)Convert.ChangeType(new Vector2(Get<float>(GetFirstChild(el, "X")), Get<float>(GetFirstChild(el, "Y"))), type);
  575. if (type == typeof(Vector3)) return (T)Convert.ChangeType(new Vector3(Get<float>(GetFirstChild(el, "X")), Get<float>(GetFirstChild(el, "Y")), Get<float>(GetFirstChild(el, "Z"))), type);
  576. #endif
  577. T obj = defaultValue;
  578. PropertyInfo[] properties = RealWorldTerrainReflectionHelper.GetProperties(type);
  579. Type underlyingType = type;
  580. #if !UNITY_WSA
  581. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.InvariantCultureIgnoreCase)) underlyingType = properties[1].PropertyType;
  582. #else
  583. if (properties.Length == 2 && string.Equals(properties[0].Name, "HasValue", StringComparison.OrdinalIgnoreCase)) underlyingType = properties[1].PropertyType;
  584. #endif
  585. try
  586. {
  587. MethodInfo method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string), typeof(IFormatProvider) });
  588. if (method != null) obj = (T)method.Invoke(null, new object[] { value, RealWorldTerrainCultureInfo.numberFormat });
  589. else
  590. {
  591. method = RealWorldTerrainReflectionHelper.GetMethod(underlyingType, "Parse", new[] { typeof(string) });
  592. obj = (T)method.Invoke(null, new[] { value });
  593. }
  594. }
  595. catch (Exception exception)
  596. {
  597. Debug.Log(exception.Message + "\n" + exception.StackTrace);
  598. throw;
  599. }
  600. return obj;
  601. }
  602. /// <summary>
  603. /// Get the value of child element as the specified type.
  604. /// </summary>
  605. /// <typeparam name="T">Type of child element.</typeparam>
  606. /// <param name="childName">Name of child.</param>
  607. /// <returns>Value of element as the specified type.</returns>
  608. public T Get<T>(string childName)
  609. {
  610. if (!hasChildNodes) return default(T);
  611. #if !NETFX_CORE
  612. return Get<T>(_element[childName]);
  613. #else
  614. return Get<T>(GetFirstChild(_element, childName));
  615. #endif
  616. }
  617. /// <summary>
  618. /// Get the value of child element as the specified type or default value if the child is not found.
  619. /// </summary>
  620. /// <typeparam name="T">Type of child element.</typeparam>
  621. /// <param name="childName">Name of child.</param>
  622. /// <param name="defaultValue">Default value.</param>
  623. /// <returns>Value of element as the specified type or default value.</returns>
  624. public T Get<T>(string childName, T defaultValue)
  625. {
  626. if (!hasChildNodes) return defaultValue;
  627. #if !NETFX_CORE
  628. return Get(_element[childName], defaultValue);
  629. #else
  630. return Get(GetFirstChild(_element, childName), defaultValue);
  631. #endif
  632. }
  633. public IEnumerator GetEnumerator()
  634. {
  635. for (int i = 0; i < count; i++)
  636. {
  637. yield return this[i];
  638. }
  639. }
  640. public Vector2 GetLatLng(string subNodeName)
  641. {
  642. RealWorldTerrainXML subNode = this[subNodeName];
  643. return new Vector2(subNode.Get<float>("lng"), subNode.Get<float>("lat"));
  644. }
  645. /// <summary>
  646. /// Get NamespaceManager for current xml node.
  647. /// </summary>
  648. /// <param name="prefix">Namespace prefix.</param>
  649. /// <returns>NamespaceManager</returns>
  650. public RealWorldTerrainXMLNamespaceManager GetNamespaceManager(string prefix = null)
  651. {
  652. #if !NETFX_CORE
  653. RealWorldTerrainXMLNamespaceManager nsmgr = new RealWorldTerrainXMLNamespaceManager(document.NameTable);
  654. if (prefix == null) prefix = element.GetPrefixOfNamespace(element.NamespaceURI);
  655. nsmgr.AddNamespace(prefix, element.NamespaceURI);
  656. #else
  657. RealWorldTerrainXMLNamespaceManager nsmgr = new RealWorldTerrainXMLNamespaceManager(new System.Xml.NameTable());
  658. if (prefix == null) prefix = element.Prefix.ToString();
  659. nsmgr.AddNamespace(prefix, element.NamespaceUri.ToString());
  660. #endif
  661. return nsmgr;
  662. }
  663. public bool HasChild(string childName)
  664. {
  665. if (!hasChildNodes) return false;
  666. #if !NETFX_CORE
  667. return _element[childName] != null;
  668. #else
  669. return GetFirstChild(_element, childName) != null;
  670. #endif
  671. }
  672. /// <summary>
  673. /// Converts XMLNode coordinates from Google Maps into Vector2.
  674. /// </summary>
  675. /// <param name="node">XMLNode coordinates from Google Maps.</param>
  676. /// <returns>Coordinates as Vector2.</returns>
  677. public static Vector2 GetVector2FromNode(RealWorldTerrainXML node)
  678. {
  679. float lng = node.Get<float>("lng");
  680. float lat = node.Get<float>("lat");
  681. return new Vector2(lng, lat);
  682. }
  683. /// <summary>
  684. /// Loads the XML from a string.
  685. /// </summary>
  686. /// <param name="xmlString">XML string.</param>
  687. /// <returns>First element.</returns>
  688. public static RealWorldTerrainXML Load(string xmlString)
  689. {
  690. try
  691. {
  692. XmlDocument document = new XmlDocument();
  693. document.LoadXml(xmlString);
  694. return new RealWorldTerrainXML(document.DocumentElement);
  695. }
  696. catch
  697. {
  698. Debug.Log("Can not load XML from string:\n" + xmlString);
  699. return new RealWorldTerrainXML();
  700. }
  701. }
  702. /// <summary>
  703. /// Removes this element from the XML.
  704. /// </summary>
  705. public void Remove()
  706. {
  707. if (_element == null || _element.ParentNode == null) return;
  708. _element.ParentNode.RemoveChild(_element);
  709. }
  710. /// <summary>
  711. /// Removes child element from the XML.
  712. /// </summary>
  713. /// <param name="childName">Name of child element.</param>
  714. public void Remove(string childName)
  715. {
  716. if (!hasChildNodes) return;
  717. #if !NETFX_CORE
  718. _element.RemoveChild(_element[childName]);
  719. #else
  720. _element.RemoveChild(GetFirstChild(_element, childName));
  721. #endif
  722. }
  723. /// <summary>
  724. /// Removes child element from the XML.
  725. /// </summary>
  726. /// <param name="childIndex">Index of child element.</param>
  727. public void Remove(int childIndex)
  728. {
  729. if (!hasChildNodes) return;
  730. if (childIndex < 0 || childIndex >= _element.ChildNodes.Count) return;
  731. _element.RemoveChild(_element.ChildNodes[childIndex]);
  732. }
  733. /// <summary>
  734. /// Sets the value of the element.
  735. /// </summary>
  736. /// <param name="value">Value of element.</param>
  737. private void SetChild(string value)
  738. {
  739. if (_element == null || _document == null) return;
  740. _element.AppendChild(_document.CreateTextNode(value));
  741. }
  742. /// <summary>
  743. /// Gets the value of the element as string.
  744. /// </summary>
  745. /// <returns>Value of the element as string.</returns>
  746. public string Value()
  747. {
  748. return Value<string>();
  749. }
  750. /// <summary>
  751. /// Gets the value of the element as the specified type.
  752. /// </summary>
  753. /// <typeparam name="T">Type of element.</typeparam>
  754. /// <returns>Value as the specified type.</returns>
  755. public T Value<T>()
  756. {
  757. return Get<T>(_element);
  758. }
  759. }
  760. }