Data.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Collections.Generic;
  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. private static XmlNode _PlayerNode;
  52. private 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].name));
  139. attributes[i].Value = ManaGarden.PlantList[i].Flower.FlowerInfo.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. for (int i = 0; i < ManaGarden.FlowerInfoList.Count; i++)
  159. {
  160. if (ManaGarden.FlowerInfoList[i].Unlock)
  161. {
  162. xmlAttribute.Value += ManaGarden.FlowerInfoList[i].Id - 1;
  163. if (i != ManaGarden.FlowerInfoList.Count - 1)
  164. {
  165. xmlAttribute.Value += " ";
  166. }
  167. }
  168. }
  169. }
  170. public static void SavePlayerConfig()
  171. {
  172. SaveSkillList();
  173. SavePlantList();
  174. SaveCommon();
  175. SaveFlowerList();
  176. StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/PlayerConfig.xml");
  177. sw.Write(PlayerDoc.OuterXml);
  178. sw.Close();
  179. }
  180. private static void ResetSkillList()
  181. {
  182. XmlNode xmlNode;
  183. XmlAttribute xmlAttribute;
  184. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  185. xmlNode.RemoveAll();
  186. for (int i = 0; i < ManaData.SkillList.Count; i++)
  187. {
  188. if (ManaData.SkillList[i].SkillType == SkillType.Skill)
  189. {
  190. #region Skill
  191. Skill skill = (Skill)ManaData.SkillList[i];
  192. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, skill.Name, ""));
  193. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  194. xmlAttribute.Value = skill.SkillType.ToString();
  195. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  196. xmlAttribute.Value = SkillStatus.Lock.ToString();
  197. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  198. xmlAttribute.Value = 0.ToString();
  199. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  200. xmlAttribute.Value = 0.ToString();
  201. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  202. xmlAttribute.Value = 0.ToString();
  203. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  204. #endregion
  205. }
  206. else if (ManaData.SkillList[i].SkillType == SkillType.Pack)
  207. {
  208. #region Pack
  209. Pack pack = (Pack)ManaData.SkillList[i];
  210. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, "礼包", ""));
  211. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  212. xmlAttribute.Value = pack.SkillType.ToString();
  213. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Name"));
  214. xmlAttribute.Value = pack.Name;
  215. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  216. xmlAttribute.Value = SkillStatus.Lock.ToString();
  217. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  218. xmlAttribute.Value = 0.ToString();
  219. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  220. #endregion
  221. }
  222. else if (ManaData.SkillList[i].SkillType == SkillType.Ability)
  223. {
  224. #region Ability
  225. Ability ability = (Ability)ManaData.SkillList[i];
  226. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, ability.Name, ""));
  227. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  228. xmlAttribute.Value = ability.SkillType.ToString();
  229. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  230. xmlAttribute.Value = SkillStatus.Lock.ToString();
  231. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  232. xmlAttribute.Value = 0.ToString();
  233. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  234. #endregion
  235. }
  236. else if (ManaData.SkillList[i].SkillType == SkillType.BigSkill)
  237. {
  238. #region BigSkill
  239. BigSkill bigSkill = (BigSkill)ManaData.SkillList[i];
  240. xmlNode = xmlNode.AppendChild(PlayerDoc.CreateNode(XmlNodeType.Element, bigSkill.Name, ""));
  241. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SkillType"));
  242. xmlAttribute.Value = bigSkill.SkillType.ToString();
  243. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("ItemStatus"));
  244. xmlAttribute.Value = SkillStatus.Lock.ToString();
  245. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("BarStatus"));
  246. xmlAttribute.Value = SkillStatus.Buy.ToString();
  247. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("Level"));
  248. xmlAttribute.Value = 0.ToString();
  249. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("CdTimer"));
  250. xmlAttribute.Value = 0.ToString();
  251. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("EffectTimer"));
  252. xmlAttribute.Value = 0.ToString();
  253. xmlNode = PlayerNode.SelectSingleNode("SkillList");
  254. #endregion
  255. }
  256. }
  257. }
  258. private static void ResetPlantList()
  259. {
  260. XmlNode xmlNode = PlayerNode.SelectSingleNode("PlantList");
  261. xmlNode.RemoveAll();
  262. XmlAttribute xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA1"));
  263. xmlAttribute.Value = 0.ToString();
  264. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA2"));
  265. xmlAttribute.Value = 1.ToString();
  266. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA3"));
  267. xmlAttribute.Value = 2.ToString();
  268. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA4"));
  269. xmlAttribute.Value = 3.ToString();
  270. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA5"));
  271. xmlAttribute.Value = 4.ToString();
  272. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA6"));
  273. xmlAttribute.Value = 5.ToString();
  274. xmlAttribute = xmlNode.Attributes.Append(PlayerDoc.CreateAttribute("SlotA7"));
  275. xmlAttribute.Value = 6.ToString();
  276. }
  277. private static void ResetCommon()
  278. {
  279. PlayerNode.SelectSingleNode("Sign").Attributes[0].Value = "0";
  280. PlayerNode.SelectSingleNode("Coin").Attributes[0].Value = "0";
  281. PlayerNode.SelectSingleNode("Level").Attributes[0].Value = "0";
  282. PlayerNode.SelectSingleNode("Person").Attributes[0].Value = "0";
  283. PlayerNode.SelectSingleNode("Diamond").Attributes[0].Value = "0";
  284. PlayerNode.SelectSingleNode("QuitTime").Attributes[0].Value = DateTime.Now.ToString();
  285. PlayerNode.SelectSingleNode("CoinPerson").Attributes[0].Value = "0";
  286. PlayerNode.SelectSingleNode("CircleTimer").Attributes[0].Value = "0";
  287. }
  288. private static void ResetFlowerList()
  289. {
  290. XmlAttributeCollection attributes = PlayerNode.SelectSingleNode("FlowerList").Attributes;
  291. attributes[0].RemoveAll();
  292. XmlAttribute xmlAttribute = attributes.Append(PlayerDoc.CreateAttribute("ID"));
  293. xmlAttribute.Value = "0 1 2 3 4 5 6 7 8 9";
  294. }
  295. public static void ResetPlayerConfig()
  296. {
  297. ResetSkillList();
  298. ResetPlantList();
  299. ResetCommon();
  300. ResetFlowerList();
  301. StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/PlayerConfig.xml");
  302. sw.Write(PlayerDoc.OuterXml);
  303. sw.Close();
  304. }
  305. public static XmlAttributeCollection GetAwardConfig()
  306. {
  307. TextAsset textAsset;
  308. XmlDocument xmlDoc = new XmlDocument();
  309. textAsset = (TextAsset)Resources.Load(@"XML\Config\award_config");
  310. xmlDoc.LoadXml(textAsset.text);
  311. XmlNode xmlNode = xmlDoc.SelectSingleNode("data").SelectSingleNode("item");
  312. return xmlNode.Attributes;
  313. }
  314. public static List<XmlAttributeCollection> GetSkillConfig()
  315. {
  316. TextAsset textAsset;
  317. XmlDocument xmlDoc = new XmlDocument();
  318. List<XmlNodeList> xmlNodeLists = new List<XmlNodeList>();
  319. List<XmlAttributeCollection> attributesList = new List<XmlAttributeCollection>();
  320. textAsset = (TextAsset)Resources.Load(@"XML\Config\pack_config");
  321. xmlDoc.LoadXml(textAsset.text);
  322. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  323. textAsset = (TextAsset)Resources.Load(@"XML\Config\skill_config");
  324. xmlDoc.LoadXml(textAsset.text);
  325. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  326. textAsset = (TextAsset)Resources.Load(@"XML\Config\ability_config");
  327. xmlDoc.LoadXml(textAsset.text);
  328. xmlNodeLists.Add(xmlDoc.SelectSingleNode("data").SelectNodes("item"));
  329. for (int i = 0; i < xmlNodeLists.Count; i++)
  330. {
  331. for (int j = 0; j < xmlNodeLists[i].Count; j++)
  332. {
  333. attributesList.Add(xmlNodeLists[i][j].Attributes);
  334. }
  335. }
  336. return attributesList;
  337. }
  338. public static List<XmlAttributeCollection> GetSignConfig()
  339. {
  340. TextAsset textAsset;
  341. XmlNodeList xmlNodeList;
  342. XmlDocument xmlDoc = new XmlDocument();
  343. List<XmlAttributeCollection> attributeList = new List<XmlAttributeCollection>();
  344. textAsset = (TextAsset)Resources.Load(@"XML\Config\signin_config");
  345. xmlDoc.LoadXml(textAsset.text);
  346. xmlNodeList = xmlDoc.SelectSingleNode("data").SelectNodes("item");
  347. for (int i = 0; i < xmlNodeList.Count; i++)
  348. {
  349. attributeList.Add(xmlNodeList[i].Attributes);
  350. }
  351. return attributeList;
  352. }
  353. public static List<XmlAttributeCollection> GetFlowerConfig()
  354. {
  355. TextAsset textAsset;
  356. XmlNodeList xmlNodeList;
  357. XmlDocument xmlDoc = new XmlDocument();
  358. List<XmlAttributeCollection> attributesList = new List<XmlAttributeCollection>();
  359. textAsset = (TextAsset) Resources.Load(@"XML\Config\flower_config");
  360. xmlDoc.LoadXml(textAsset.text);
  361. xmlNodeList = xmlDoc.SelectSingleNode("data").SelectNodes("item");
  362. for (int i = 0; i < xmlNodeList.Count; i++)
  363. {
  364. attributesList.Add(xmlNodeList[i].Attributes);
  365. }
  366. return attributesList;
  367. }
  368. }