Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.IO;
- using System.Xml;
- using System.Text.RegularExpressions;
- using Nekki.Yaml;
- using Nekki.Vector.Core.GameManagement;
- namespace Nekki.Vector.Core.User
- {
- public static class UserUpdater {
- const string _FolderName = "/user_update";
- static public void Run(int p_version) {
- int currVersion = VectorVersionController.UserVersion;
- for (int i = p_version; i < currVersion; i++)
- {
- string fileName = "from_" + i.ToString() + "_to_" + (i + 1).ToString() + "_update.yaml";
- Run(fileName);
- DataLocal.Current.Save();
- }
- }
- static void Run(string p_filename) {
- Generator.MainRandom.SetSeed(-1);
- YamlDocumentNekki doc = YamlUtils.OpenYamlFile(VectorPaths.Data + _FolderName, p_filename);
- List<Preset> _Presets = new List<Preset>();
- foreach (Mapping presetNode in doc.GetRoot())
- {
- switch (presetNode.key) {
- case "CountersUpdater":
- UpdateCounters(Preset.Create(presetNode));
- DataLocal.Current.Save();
- break;
- case "ItemsUpdater":
- UpdateItems(Preset.Create(presetNode));
- DataLocal.Current.Save();
- break;
- case "NewStashItems":
- NewItems(presetNode, true);
- DataLocal.Current.Save();
- break;
- case "NewEquippedItems":
- NewItems(presetNode, false);
- DataLocal.Current.Save();
- break;
- case "ReplaceRegularExpression":
- UserRegex(presetNode);
- DataLocal.Reload();
- break;
- default:
- DebugUtils.Dialog("unknown preset " + presetNode.key + " on " + p_filename, true);
- break;
- }
- }
- }
- static void UpdateCounters(Preset p_preset) {
- p_preset.SetPreset(null);
- }
- static void UpdateItems(Preset p_preset) {
- foreach(UserItem item in DataLocal.Current.AllItems) {
- StringBuffer.AddString("Upd_ItemName", item.Name);
- p_preset.SetPreset(item);
- StringBuffer.AddString("Upd_ItemName", "");
- }
- }
- static void NewItems(Mapping p_presetNodes, bool p_toStash) {
- foreach(Mapping presetnode in p_presetNodes) {
- Preset preset = Preset.Create(presetnode);
- if (preset == null) {
- continue;
- }
- UserItem item = null;
- for (int j = 0, itemsCount = preset.ItemsCount.ValueInt; j < itemsCount; ++j) {
- item = UserItem.CreateUserItem("");
- if (preset.SetPreset(item)) {
- if (p_toStash) {
- DataLocal.Current.AddToStash(item);
- }
- else {
- DataLocal.Current.AddToEquipped(item);
- }
- }
- }
- }
- }
- static void UserRegex(Mapping p_presetNodes)
- {
- string user = string.Empty;
- FileStream stream = null;
- try
- {
- using (stream = new FileStream(DataLocal.FilePath, FileMode.Open))
- {
- byte[] array = new byte[stream.Length];
- if (stream.CanRead)
- {
- stream.Read(array, 0, array.Length);
- }
- else
- {
- UnityEngine.Debug.Log("Reading user Error");
- }
- user = System.Text.Encoding.UTF8.GetString(array);
- }
- }
- catch (System.Exception ex)
- {
- UnityEngine.Debug.Log("Reading user Error");
- }
- finally
- {
- if (stream != null)
- stream.Close();
- }
- foreach (Mapping node in p_presetNodes.GetMapping("BoxContent"))
- {
- user = Regex.Replace(user, node.GetText("Regular").text, node.GetText("Replace").text);
- }
- byte[] userBytes = System.Text.Encoding.UTF8.GetBytes(user);
- try
- {
- using (stream = new FileStream(DataLocal.FilePath, FileMode.Truncate))
- {
- if (stream.CanWrite)
- {
- stream.Write(userBytes, 0, userBytes.Length);
- }
- else
- {
- UnityEngine.Debug.Log("Writing user Error");
- }
- }
- }
- catch (System.Exception ex)
- {
- UnityEngine.Debug.Log("Reading user Error");
- }
- finally
- {
- if (stream != null)
- stream.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement