using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace SaveWizard_rewritten { static class RegexHandler { private static List lines; private static List backup; public static void FillLines(List _lines) { if (_lines == null) return; lines = new List(); lines.AddRange(_lines); backup = new List(); backup.AddRange(_lines); } public static string GetLine(int index) { return lines[index]; } public static List GetAllLines() { return lines; } public static List GetBackup() { return backup; } public static int SearchLine(string term, int start = 0, string cancel = "this_string_must_not_exist") { if (lines[start].Contains(term)) return start; start += 1; while (start <= (lines.Count-1)) { if (lines[start].Contains(term)) return start; if (lines[start].Contains(cancel)) return 0; start += 1; } return 0; } public static int SearchLineInUnit(string term, string unit) { return SearchLine(term, start: SearchLine(" : " + unit + " {"), cancel: "}"); } public static List SearchAllLines(string term) { List matches = new List(); int line = 0; while (SearchLine(term, start: line + 1) != 0) { line = SearchLine(term, start: line + 1); matches.Add(line); } if (matches.Count == 0) return null; return matches; } public static string GetValue(int line) { return Regex.Match(lines[line], ": (.+)$").Groups[1].ToString(); } public static void SetValue(int line, string value) { string name = Regex.Match(lines[line], "(.+):").Groups[1].ToString(); lines[line] = $"{name}: {value}"; } //public static string GetUnitName(int line) //{ // return Regex.Match(lines[line], " : (.+) {$").Groups[1].ToString(); //} public static int GetArrayLength(int line) { return Convert.ToInt32(GetValue(line)); } //public static string GetArrayValueByIndex(int line, int index) //{ // return Regex.Match(lines[line + index + 1], ": (.+)$").Groups[1].ToString(); //} //public static int GetArrayIndexByValue(int line, string value) //{ // int count = 0; // for (int i = 0; i < GetArrayLength(line); i++) // { // if (GetValue(line + count + 1) == value) // return count; // count += 1; // } // return 0; //} public static List GetArrayItems(string search_term) { List items = new List(); int line = SearchLine(search_term); for (int i = 0; i < GetArrayLength(line); i++) items.Add(Regex.Match(lines[line + i + 1], ": (.+)$").Groups[1].ToString()); if (items.Count == 0) return null; return items; } public static void AddArrayValue(int line, string value) { string name = Regex.Match(lines[line], "(.+):").Groups[1].ToString(); int length = GetArrayLength(line); lines[line] = $"{name}: {length + 1}"; lines.Insert(line + length + 1, $"{name}[{length}]: {value}"); } //public static void RemoveArrayValue(int line, string value) //{ // string name = Regex.Match(lines[line], "(.+):").Groups[1].ToString(); // lines.Remove(lines[line + 1 + GetArrayIndexByValue(line, value)]); // int count = GetArrayLength(line); // lines[line] = $"{name}: {count - 1}"; // for (int i = 0; i < count; i++) // //lines[line + i + 1] = ("") // lines[line + i + 1].Replace("[[0-9]+]", $"{i}"); //} //public static void ChangeArrayValue(int line, int index, string value) //{ // line += index + 1; // SetValue(line, value); //} } }