Files
SaveWizard_rewritten/RegexHandler.cs
Lev Rusanov e633408665 Обновление
* Реализовано сохранение и восстановление бекапа
* Обновлен RegexHandler

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
2025-02-27 22:21:24 +07:00

145 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SaveWizard_rewritten
{
static class RegexHandler
{
private static List<string> lines;
private static List<string> backup;
public static void FillLines(List<string> _lines)
{
if (_lines == null)
return;
lines = new List<string>();
lines.AddRange(_lines);
backup = new List<string>();
backup.AddRange(_lines);
}
public static string GetLine(int index)
{
return lines[index];
}
public static List<string> GetAllLines()
{
return lines;
}
public static List<string> 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<int> SearchAllLines(string term)
{
List<int> matches = new List<int>();
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<string> GetArrayItems(int line)
{
List<string> items = new List<string>();
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);
//}
}
}