|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using System.Globalization; |
| 3 | +using System.Windows.Forms; |
| 4 | +using System.Xml; |
| 5 | +using System.Xml.Linq; |
| 6 | + |
| 7 | +namespace RimLangKit |
| 8 | +{ |
| 9 | + internal sealed class FindChanges |
| 10 | + { |
| 11 | + static Dictionary<string, string> TranslationData = new(); |
| 12 | + static Dictionary<string, string> ModData = new(); |
| 13 | + static Dictionary<string, string> ChangedData = new(); |
| 14 | + |
| 15 | + internal static bool GetTranslationData(string currentFile, TextBox textBox) |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + XDocument.Load(currentFile, LoadOptions.PreserveWhitespace); |
| 20 | + } |
| 21 | + catch |
| 22 | + { |
| 23 | + textBox.AppendText($"{Environment.NewLine}Не удалось загрузить файл {currentFile}"); |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + XDocument xDoc = XDocument.Load(currentFile, LoadOptions.PreserveWhitespace); |
| 28 | + |
| 29 | + if (xDoc.Element("LanguageData") is null) |
| 30 | + { |
| 31 | + textBox.AppendText($"{Environment.NewLine}Не удалось найти LanguageData {currentFile}"); |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + XElement? root = xDoc.Element("LanguageData"); |
| 36 | + if (root?.Elements() is null) |
| 37 | + { |
| 38 | + textBox.AppendText($"{Environment.NewLine}Тег LanguageData пуст {currentFile}"); |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + XmlReaderSettings settings = new(){ DtdProcessing = DtdProcessing.Parse }; |
| 43 | + XmlReader reader = XmlReader.Create(currentFile, settings); |
| 44 | + reader.MoveToContent(); |
| 45 | + |
| 46 | + string key = string.Empty; |
| 47 | + string value = string.Empty; |
| 48 | + bool hasValue = false; |
| 49 | + while (reader.Read()) |
| 50 | + { |
| 51 | + switch (reader.NodeType) |
| 52 | + { |
| 53 | + case XmlNodeType.Element: |
| 54 | + key = reader.Name.ToString(); |
| 55 | + if (hasValue) |
| 56 | + { |
| 57 | + bool unique = TranslationData.TryAdd(key, value); |
| 58 | + if (!unique) |
| 59 | + { |
| 60 | + DateTime time = DateTime.Now; |
| 61 | + string result = time.ToString("ffff", CultureInfo.InvariantCulture); |
| 62 | + TranslationData.TryAdd(key + result, value); |
| 63 | + } |
| 64 | + } |
| 65 | + hasValue = false; |
| 66 | + break; |
| 67 | + case XmlNodeType.Comment: |
| 68 | + value = reader.Value.ToString()[4..].Trim(); |
| 69 | + hasValue = true; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + internal static bool GetModData(string currentFile, TextBox textBox) |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + XDocument.Load(currentFile, LoadOptions.PreserveWhitespace); |
| 81 | + } |
| 82 | + catch |
| 83 | + { |
| 84 | + textBox.AppendText($"{Environment.NewLine}Не удалось загрузить файл {currentFile}"); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + XDocument xDoc = XDocument.Load(currentFile, LoadOptions.PreserveWhitespace); |
| 89 | + |
| 90 | + if (xDoc.Element("LanguageData") is null) |
| 91 | + { |
| 92 | + textBox.AppendText($"{Environment.NewLine}Не удалось найти LanguageData {currentFile}"); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + XElement? root = xDoc.Element("LanguageData"); |
| 97 | + if (root?.Elements() is null) |
| 98 | + { |
| 99 | + textBox.AppendText($"{Environment.NewLine}Тег LanguageData пуст {currentFile}"); |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + foreach (XElement node in root.Elements()) |
| 104 | + { |
| 105 | + bool unique = ModData.TryAdd(node.Name.ToString(), node.Value); |
| 106 | + if (!unique) |
| 107 | + { |
| 108 | + DateTime time = DateTime.Now; |
| 109 | + string result = time.ToString("ffff", CultureInfo.InvariantCulture); |
| 110 | + TranslationData.TryAdd(node.Name.ToString() + result, node.Value); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + internal static void FindChangesInFiles() |
| 118 | + { |
| 119 | + foreach (var line in TranslationData) |
| 120 | + { |
| 121 | + if (ModData.TryGetValue(line.Key, out string? value)) |
| 122 | + { |
| 123 | + if (!line.Value.Equals(value, StringComparison.Ordinal)) |
| 124 | + { |
| 125 | + ChangedData.Add(line.Key, value); |
| 126 | + } |
| 127 | + ModData.Remove(line.Key); |
| 128 | + TranslationData.Remove(line.Key); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + internal static void WriteChanges(TextBox textBox) |
| 135 | + { |
| 136 | + if (ChangedData.Count == 0) |
| 137 | + { |
| 138 | + textBox.AppendText($"{Environment.NewLine}Не найдено сломанных файлов."); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + StreamWriter sw = new(Path.Combine(Directory.GetCurrentDirectory() + "\\ChangedData.txt"), false); |
| 143 | + sw.WriteLine("==Различающиеся строки=="); |
| 144 | + foreach (var line in ChangedData) |
| 145 | + { |
| 146 | + sw.WriteLine($"{line.Key}: {line.Value}"); |
| 147 | + } |
| 148 | + sw.Close(); |
| 149 | + textBox.AppendText($"{Environment.NewLine}Найдено {ChangedData.Count} изменений. Результат записан в файл ChangedData.txt папки {Directory.GetCurrentDirectory()}"); |
| 150 | + ChangedData.Clear(); |
| 151 | + } |
| 152 | + |
| 153 | + if (ModData.Count != 0) |
| 154 | + { |
| 155 | + StreamWriter sw = new(Path.Combine(Directory.GetCurrentDirectory() + "\\ModData.txt"), false); |
| 156 | + sw.WriteLine("==Строки только в моде (не обязательно ошибка)=="); |
| 157 | + foreach (var line in ModData) |
| 158 | + { |
| 159 | + sw.WriteLine($"{line.Key}: {line.Value}"); |
| 160 | + } |
| 161 | + sw.Close(); |
| 162 | + textBox.AppendText($"{Environment.NewLine}Найдено {ModData.Count} изменений. Результат записан в файл ModData.txt папки {Directory.GetCurrentDirectory()}"); |
| 163 | + ModData.Clear(); |
| 164 | + } |
| 165 | + |
| 166 | + if (TranslationData.Count != 0) |
| 167 | + { |
| 168 | + StreamWriter sw = new(Path.Combine(Directory.GetCurrentDirectory() + "\\TranslationData.txt"), false); |
| 169 | + sw.WriteLine("==Строки только в переводе (не обязательно ошибка)=="); |
| 170 | + foreach (var line in TranslationData) |
| 171 | + { |
| 172 | + sw.WriteLine($"{line.Key}: {line.Value}"); |
| 173 | + } |
| 174 | + sw.Close(); |
| 175 | + textBox.AppendText($"{Environment.NewLine}Найдено {TranslationData.Count} изменений. Результат записан в файл TranslationData.txt папки {Directory.GetCurrentDirectory()}"); |
| 176 | + TranslationData.Clear(); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments