Data.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using System;
  2. using UnityEngine;
  3. using System.Xml;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. public class Data
  9. {
  10. #region
  11. public static XmlNode PlayerNode
  12. {
  13. get
  14. {
  15. if (_PlayerNode == null)
  16. {
  17. _PlayerNode = PlayerDoc.SelectSingleNode("PlayerConfig");
  18. }
  19. return _PlayerNode;
  20. }
  21. set { _PlayerNode = value; }
  22. }
  23. public static XmlDocument PlayerDoc
  24. {
  25. get
  26. {
  27. if (_PlayerDoc == null)
  28. {
  29. FileInfo fileInfo = new FileInfo(Application.persistentDataPath + "/PlayerConfig.xml");
  30. if (fileInfo.Exists)
  31. {
  32. StreamReader sr = new StreamReader(Application.persistentDataPath + "/PlayerConfig.xml");
  33. _PlayerDoc = new XmlDocument();
  34. _PlayerDoc.LoadXml(sr.ReadToEnd());
  35. sr.Close();
  36. }
  37. else
  38. {
  39. TextAsset textAsset = (TextAsset)Resources.Load(@"XML\Config\PlayerConfig");
  40. _PlayerDoc = new XmlDocument();
  41. _PlayerDoc.LoadXml(textAsset.text);
  42. StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/PlayerConfig.xml");
  43. sw.Write(_PlayerDoc.OuterXml);
  44. sw.Close();
  45. }
  46. }
  47. return _PlayerDoc;
  48. }
  49. set { _PlayerDoc = value; }
  50. }
  51. public static XmlNode _PlayerNode;
  52. public static XmlDocument _PlayerDoc;
  53. #endregion
  54. private static void SaveSkillList()
  55. {
  56. XmlNode xmlNode;
  57. XmlAttribute xmlAttribute;
  58. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  59. xmlNode.RemoveAll();
  60. for (int i = 0; i < ManaData.SkillList.Count; i++)
  61. {
  62. if (ManaData.SkillList[i].SkillType == SkillType.Skill)
  63. {
  64. #region Skill
  65. Skill skill = (Skill)ManaData.SkillList[i];
  66. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, skill.Name, ""));
  67. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  68. xmlAttribute.Value = skill.SkillType.ToString();
  69. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  70. xmlAttribute.Value = skill.ItemStatus.ToString();
  71. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  72. xmlAttribute.Value = skill.Level.ToString();
  73. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  74. xmlAttribute.Value = skill.CoolTimer.ToString();
  75. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  76. xmlAttribute.Value = skill.UseTimer.ToString();
  77. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  78. #endregion
  79. }
  80. else if (ManaData.SkillList[i].SkillType == SkillType.Pack)
  81. {
  82. #region Pack
  83. Pack pack = (Pack)ManaData.SkillList[i];
  84. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, "礼包", ""));
  85. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  86. xmlAttribute.Value = pack.SkillType.ToString();
  87. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Name"));
  88. xmlAttribute.Value = pack.Name;
  89. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  90. xmlAttribute.Value = pack.ItemStatus.ToString();
  91. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  92. xmlAttribute.Value = pack.Level.ToString();
  93. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  94. #endregion
  95. }
  96. else if (ManaData.SkillList[i].SkillType == SkillType.Ability)
  97. {
  98. #region Ability
  99. Ability ability = (Ability)ManaData.SkillList[i];
  100. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, ability.Name, ""));
  101. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  102. xmlAttribute.Value = ability.SkillType.ToString();
  103. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  104. xmlAttribute.Value = ability.ItemStatus.ToString();
  105. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  106. xmlAttribute.Value = ability.Level.ToString();
  107. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  108. #endregion
  109. }
  110. else if (ManaData.SkillList[i].SkillType == SkillType.BigSkill)
  111. {
  112. #region BigSkill
  113. BigSkill bigSkill = (BigSkill)ManaData.SkillList[i];
  114. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, bigSkill.Name, ""));
  115. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  116. xmlAttribute.Value = bigSkill.SkillType.ToString();
  117. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  118. xmlAttribute.Value = bigSkill.ItemStatus.ToString();
  119. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("BarStatus"));
  120. xmlAttribute.Value = bigSkill.BarStatus.ToString();
  121. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  122. xmlAttribute.Value = bigSkill.Level.ToString();
  123. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  124. xmlAttribute.Value = bigSkill.CoolTimer.ToString();
  125. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  126. xmlAttribute.Value = bigSkill.UseTimer.ToString();
  127. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  128. #endregion
  129. }
  130. }
  131. }
  132. private static void SavePlantList()
  133. {
  134. XmlAttributeCollection attributes = PlayerNode.SelectSingleNode("PlantList").Attributes;
  135. attributes.RemoveAll();
  136. for (int i = 0; i < ManaGarden.PlantList.Count; i++)
  137. {
  138. attributes.Append(PlayerDoc.CreateAttribute(ManaGarden.PlantList[i].ParTra.name));
  139. attributes[i].Value = ManaGarden.PlantList[i].Id.ToString();
  140. }
  141. }
  142. private static void SaveCommon()
  143. {
  144. PlayerNode.SelectSingleNode("Sign").Attributes[0].Value = ManaData.Sign.ToString();
  145. PlayerNode.SelectSingleNode("Coin").Attributes[0].Value = ManaData.Coin.ToString("0");
  146. PlayerNode.SelectSingleNode("Level").Attributes[0].Value = ManaData.Level.ToString();
  147. PlayerNode.SelectSingleNode("Person").Attributes[0].Value = ManaData.Person.ToString();
  148. PlayerNode.SelectSingleNode("Diamond").Attributes[0].Value = ManaData.Diamond.ToString();
  149. PlayerNode.SelectSingleNode("QuitTime").Attributes[0].Value = DateTime.Now.ToString();
  150. PlayerNode.SelectSingleNode("CoinPerson").Attributes[0].Value = ManaData.CoinPerson.ToString();
  151. PlayerNode.SelectSingleNode("CircleTimer").Attributes[0].Value = ManaData.CircleTimer.ToString();
  152. }
  153. private static void SaveFlowerList()
  154. {
  155. XmlAttributeCollection attributes = PlayerNode.SelectSingleNode("FlowerList").Attributes;
  156. attributes[0].RemoveAll();
  157. XmlAttribute xmlAttribute = attributes.Append(PlayerDoc.CreateAttribute("ID"));
  158. foreach (var kv in ManaGarden.FlowerInfoDic)
  159. {
  160. if (kv.Value.Unlock)
  161. {
  162. xmlAttribute.Value += kv.Key + " ";
  163. }
  164. }
  165. xmlAttribute.Value = xmlAttribute.Value.Trim(' ');
  166. }
  167. public static void SavePlayerConfig()
  168. {
  169. SaveSkillList();
  170. SavePlantList();
  171. SaveCommon();
  172. SaveFlowerList();
  173. StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/PlayerConfig.xml");
  174. sw.Write(PlayerDoc.OuterXml);
  175. sw.Close();
  176. }
  177. private static void ResetSkillList()
  178. {
  179. XmlNode xmlNode;
  180. XmlAttribute xmlAttribute;
  181. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  182. xmlNode.RemoveAll();
  183. for (int i = 0; i < ManaData.SkillList.Count; i++)
  184. {
  185. if (ManaData.SkillList[i].SkillType == SkillType.Skill)
  186. {
  187. #region Skill
  188. Skill skill = (Skill)ManaData.SkillList[i];
  189. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, skill.Name, ""));
  190. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  191. xmlAttribute.Value = skill.SkillType.ToString();
  192. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  193. xmlAttribute.Value = SkillStatus.Lock.ToString();
  194. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  195. xmlAttribute.Value = 0.ToString();
  196. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  197. xmlAttribute.Value = 0.ToString();
  198. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  199. xmlAttribute.Value = 0.ToString();
  200. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  201. #endregion
  202. }
  203. else if (ManaData.SkillList[i].SkillType == SkillType.Pack)
  204. {
  205. #region Pack
  206. Pack pack = (Pack)ManaData.SkillList[i];
  207. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, "礼包", ""));
  208. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  209. xmlAttribute.Value = pack.SkillType.ToString();
  210. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Name"));
  211. xmlAttribute.Value = pack.Name;
  212. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  213. xmlAttribute.Value = SkillStatus.Lock.ToString();
  214. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  215. xmlAttribute.Value = 0.ToString();
  216. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  217. #endregion
  218. }
  219. else if (ManaData.SkillList[i].SkillType == SkillType.Ability)
  220. {
  221. #region Ability
  222. Ability ability = (Ability)ManaData.SkillList[i];
  223. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, ability.Name, ""));
  224. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  225. xmlAttribute.Value = ability.SkillType.ToString();
  226. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  227. xmlAttribute.Value = SkillStatus.Lock.ToString();
  228. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  229. xmlAttribute.Value = 0.ToString();
  230. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  231. #endregion
  232. }
  233. else if (ManaData.SkillList[i].SkillType == SkillType.BigSkill)
  234. {
  235. #region BigSkill
  236. BigSkill bigSkill = (BigSkill)ManaData.SkillList[i];
  237. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, bigSkill.Name, ""));
  238. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  239. xmlAttribute.Value = bigSkill.SkillType.ToString();
  240. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  241. xmlAttribute.Value = SkillStatus.Lock.ToString();
  242. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("BarStatus"));
  243. xmlAttribute.Value = SkillStatus.Buy.ToString();
  244. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  245. xmlAttribute.Value = 0.ToString();
  246. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  247. xmlAttribute.Value = 0.ToString();
  248. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  249. xmlAttribute.Value = 0.ToString();
  250. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  251. #endregion
  252. }
  253. }
  254. }
  255. private static void ResetPlantList()
  256. {
  257. XmlNode xmlNode = PlayerNode.SelectSingleNode("PlantList");
  258. xmlNode.RemoveAll();
  259. XmlAttribute xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA1"));
  260. xmlAttribute.Value = 1.ToString();
  261. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA2"));
  262. xmlAttribute.Value = 2.ToString();
  263. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA3"));
  264. xmlAttribute.Value = 3.ToString();
  265. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA4"));
  266. xmlAttribute.Value = 4.ToString();
  267. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA5"));
  268. xmlAttribute.Value = 5.ToString();
  269. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA6"));
  270. xmlAttribute.Value = 6.ToString();
  271. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA7"));
  272. xmlAttribute.Value = 7.ToString();
  273. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA8"));
  274. xmlAttribute.Value = 8.ToString();
  275. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("FlowerTraA9"));
  276. xmlAttribute.Value = 9.ToString();
  277. }
  278. private static void ResetCommon()
  279. {
  280. PlayerNode.SelectSingleNode("Sign").Attributes[0].Value = "0";
  281. PlayerNode.SelectSingleNode("Coin").Attributes[0].Value = "0";
  282. PlayerNode.SelectSingleNode("Level").Attributes[0].Value = "0";
  283. PlayerNode.SelectSingleNode("Person").Attributes[0].Value = "0";
  284. PlayerNode.SelectSingleNode("Diamond").Attributes[0].Value = "0";
  285. PlayerNode.SelectSingleNode("QuitTime").Attributes[0].Value = DateTime.Now.ToString();
  286. PlayerNode.SelectSingleNode("CoinPerson").Attributes[0].Value = "0";
  287. PlayerNode.SelectSingleNode("CircleTimer").Attributes[0].Value = "0";
  288. }
  289. private static void ResetFlowerList()
  290. {
  291. XmlAttributeCollection attributes = PlayerNode.SelectSingleNode("FlowerList").Attributes;
  292. attributes[0].RemoveAll();
  293. XmlAttribute xmlAttribute = attributes.Append(PlayerDoc.CreateAttribute("ID"));
  294. xmlAttribute.Value = "1 2 3 4 5 6 7 8 9 10";
  295. }
  296. public static void ResetPlayerConfig()
  297. {
  298. ResetSkillList();
  299. ResetPlantList();
  300. ResetCommon();
  301. ResetFlowerList();
  302. StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/PlayerConfig.xml");
  303. sw.Write(PlayerDoc.OuterXml);
  304. sw.Close();
  305. }
  306. public static XmlAttributeCollection GetMiniGameConfig()
  307. {
  308. TextAsset textAsset;
  309. XmlDocument xmlDoc = new XmlDocument();
  310. textAsset = (TextAsset)Resources.Load(@"XML\Config\minigame_config");
  311. xmlDoc.LoadXml(textAsset.text);
  312. XmlNode xmlNode = xmlDoc.SelectSingleNode("data").SelectSingleNode("item");
  313. return xmlNode.Attributes;
  314. }
  315. public static List<XmlAttributeCollection> GetSignConfig()
  316. {
  317. TextAsset textAsset;
  318. XmlDocument xmlDoc = new XmlDocument();
  319. List<XmlAttributeCollection> attributeList = new List<XmlAttributeCollection>();
  320. textAsset = (TextAsset)Resources.Load(@"XML\Config\signin_config");
  321. xmlDoc.LoadXml(textAsset.text);
  322. XmlNodeList xmlNodeList = xmlDoc.SelectSingleNode("data").SelectNodes("item");
  323. for (int i = 0; i < xmlNodeList.Count; i++)
  324. {
  325. attributeList.Add(xmlNodeList[i].Attributes);
  326. }
  327. return attributeList;
  328. }
  329. public static List<XmlAttributeCollection> GetSkillConfig()
  330. {
  331. TextAsset textAsset;
  332. XmlDocument xmlDoc = new XmlDocument();
  333. List<XmlNodeList> xmlNodeLists = new List<XmlNodeList>();
  334. List<XmlAttributeCollection> attributesList = new List<XmlAttributeCollection>();
  335. textAsset = (TextAsset)Resources.Load(@"XML\Config\pack_config");
  336. xmlDoc.LoadXml(textAsset.text);
  337. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  338. textAsset = (TextAsset)Resources.Load(@"XML\Config\skill_config");
  339. xmlDoc.LoadXml(textAsset.text);
  340. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  341. textAsset = (TextAsset)Resources.Load(@"XML\Config\ability_config");
  342. xmlDoc.LoadXml(textAsset.text);
  343. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  344. for (int i = 0; i < xmlNodeLists.Count; i++)
  345. {
  346. for (int j = 0; j < xmlNodeLists[i].Count; j++)
  347. {
  348. attributesList.Add(xmlNodeLists[i][j].Attributes);
  349. }
  350. }
  351. return attributesList;
  352. }
  353. public static List<XmlAttributeCollection> GetFlowerConfig()
  354. {
  355. XmlNodeList xmlNodeList;
  356. XmlDocument xmlDoc = new XmlDocument();
  357. List<XmlAttributeCollection> attributesList = new List<XmlAttributeCollection>();
  358. TextAsset textAsset = (TextAsset) Resources.Load(@"XML\Config\flower_config");
  359. xmlDoc.LoadXml(textAsset.text);
  360. xmlNodeList = xmlDoc.SelectSingleNode("data").SelectNodes("item");
  361. for (int i = 0; i < xmlNodeList.Count; i++)
  362. {
  363. attributesList.Add(xmlNodeList[i].Attributes);
  364. }
  365. return attributesList;
  366. }
  367. }