Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Text.RegularExpressions;
  5. using Nekki.Yaml;
  6. using Nekki.Vector.Core.GameManagement;
  7.  
  8. namespace Nekki.Vector.Core.User
  9. {
  10. public static class UserUpdater {
  11. const string _FolderName = "/user_update";
  12.  
  13. static public void Run(int p_version) {
  14. int currVersion = VectorVersionController.UserVersion;
  15. for (int i = p_version; i < currVersion; i++)
  16. {
  17. string fileName = "from_" + i.ToString() + "_to_" + (i + 1).ToString() + "_update.yaml";
  18. Run(fileName);
  19. DataLocal.Current.Save();
  20. }
  21. }
  22.  
  23. static void Run(string p_filename) {
  24. Generator.MainRandom.SetSeed(-1);
  25. YamlDocumentNekki doc = YamlUtils.OpenYamlFile(VectorPaths.Data + _FolderName, p_filename);
  26.  
  27. List<Preset> _Presets = new List<Preset>();
  28.  
  29. foreach (Mapping presetNode in doc.GetRoot())
  30. {
  31. switch (presetNode.key) {
  32. case "CountersUpdater":
  33. UpdateCounters(Preset.Create(presetNode));
  34. DataLocal.Current.Save();
  35. break;
  36. case "ItemsUpdater":
  37. UpdateItems(Preset.Create(presetNode));
  38. DataLocal.Current.Save();
  39. break;
  40. case "NewStashItems":
  41. NewItems(presetNode, true);
  42. DataLocal.Current.Save();
  43. break;
  44. case "NewEquippedItems":
  45. NewItems(presetNode, false);
  46. DataLocal.Current.Save();
  47. break;
  48. case "ReplaceRegularExpression":
  49. UserRegex(presetNode);
  50. DataLocal.Reload();
  51.  
  52. break;
  53. default:
  54. DebugUtils.Dialog("unknown preset " + presetNode.key + " on " + p_filename, true);
  55. break;
  56.  
  57. }
  58. }
  59. }
  60.  
  61. static void UpdateCounters(Preset p_preset) {
  62. p_preset.SetPreset(null);
  63. }
  64.  
  65. static void UpdateItems(Preset p_preset) {
  66.  
  67. foreach(UserItem item in DataLocal.Current.AllItems) {
  68. StringBuffer.AddString("Upd_ItemName", item.Name);
  69. p_preset.SetPreset(item);
  70. StringBuffer.AddString("Upd_ItemName", "");
  71. }
  72. }
  73.  
  74. static void NewItems(Mapping p_presetNodes, bool p_toStash) {
  75. foreach(Mapping presetnode in p_presetNodes) {
  76. Preset preset = Preset.Create(presetnode);
  77. if (preset == null) {
  78. continue;
  79. }
  80. UserItem item = null;
  81. for (int j = 0, itemsCount = preset.ItemsCount.ValueInt; j < itemsCount; ++j) {
  82. item = UserItem.CreateUserItem("");
  83. if (preset.SetPreset(item)) {
  84. if (p_toStash) {
  85. DataLocal.Current.AddToStash(item);
  86. }
  87. else {
  88. DataLocal.Current.AddToEquipped(item);
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. static void UserRegex(Mapping p_presetNodes)
  96. {
  97.  
  98. string user = string.Empty;
  99. FileStream stream = null;
  100. try
  101. {
  102. using (stream = new FileStream(DataLocal.FilePath, FileMode.Open))
  103. {
  104. byte[] array = new byte[stream.Length];
  105. if (stream.CanRead)
  106. {
  107. stream.Read(array, 0, array.Length);
  108. }
  109. else
  110. {
  111. UnityEngine.Debug.Log("Reading user Error");
  112. }
  113. user = System.Text.Encoding.UTF8.GetString(array);
  114.  
  115. }
  116. }
  117. catch (System.Exception ex)
  118. {
  119. UnityEngine.Debug.Log("Reading user Error");
  120. }
  121. finally
  122. {
  123. if (stream != null)
  124. stream.Close();
  125. }
  126. foreach (Mapping node in p_presetNodes.GetMapping("BoxContent"))
  127. {
  128. user = Regex.Replace(user, node.GetText("Regular").text, node.GetText("Replace").text);
  129. }
  130. byte[] userBytes = System.Text.Encoding.UTF8.GetBytes(user);
  131. try
  132. {
  133. using (stream = new FileStream(DataLocal.FilePath, FileMode.Truncate))
  134. {
  135.  
  136. if (stream.CanWrite)
  137. {
  138. stream.Write(userBytes, 0, userBytes.Length);
  139. }
  140. else
  141. {
  142. UnityEngine.Debug.Log("Writing user Error");
  143. }
  144. }
  145. }
  146. catch (System.Exception ex)
  147. {
  148. UnityEngine.Debug.Log("Reading user Error");
  149. }
  150. finally
  151. {
  152. if (stream != null)
  153. stream.Close();
  154. }
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement