Skip to content

Commit 4cded93

Browse files
committed
Версия 3.3
1 parent 3ee6961 commit 4cded93

26 files changed

Lines changed: 1040 additions & 262 deletions

EncodingFix.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RimLangKit
55
{
66
internal sealed class EncodingFix
77
{
8-
internal static void EncodingFixProcessing(string currentFile, TextBox textBox)
8+
internal static bool EncodingFixProcessing(string currentFile)
99
{
1010
string text = File.ReadAllText(currentFile);
1111
// Замена табов на пробелы
@@ -21,6 +21,8 @@ internal static void EncodingFixProcessing(string currentFile, TextBox textBox)
2121
text = encoder.GetString(result);
2222

2323
File.WriteAllText(currentFile, text);
24+
25+
return true;
2426
}
2527
}
2628
}

FileFix.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Xml.Linq;
2+
3+
namespace RimLangKit
4+
{
5+
internal sealed class FileFix
6+
{
7+
static Dictionary<string, string> BrokenFiles = new();
8+
9+
internal static bool FileFixProcessing(string currentFile)
10+
{
11+
// Позволяет избежать падения при загрузке сломанного .xml файла
12+
try
13+
{
14+
XDocument.Load(currentFile, LoadOptions.PreserveWhitespace);
15+
}
16+
catch
17+
{
18+
BrokenFiles.Add(currentFile, "Ошибка загрузки файла");
19+
return false;
20+
}
21+
22+
XDocument xDoc = XDocument.Load(currentFile, LoadOptions.PreserveWhitespace);
23+
24+
// Позволяет проверка декларации
25+
string? Declaration = xDoc.Declaration?.ToString();
26+
if (Declaration is null)
27+
{
28+
BrokenFiles.Add(currentFile, "Отсутствует декларация");
29+
}
30+
else if (!Declaration.StartsWith("<?xml version=\"1.0\" encoding=\"utf-8\"", StringComparison.OrdinalIgnoreCase))
31+
{
32+
BrokenFiles.Add(currentFile, "Ошибка декларации");
33+
return false;
34+
}
35+
36+
// Технические файлы
37+
if (currentFile.EndsWith("About.xml", StringComparison.OrdinalIgnoreCase) || currentFile.EndsWith("LoadFolders.xml", StringComparison.OrdinalIgnoreCase))
38+
{
39+
return true;
40+
}
41+
42+
// Позволяет избежать обработки пустого или не содержащего нужных тегов файла
43+
if (xDoc.Element("LanguageData") is null)
44+
{
45+
BrokenFiles.Add(currentFile, "Отсутствует тег LanguageData");
46+
return false;
47+
}
48+
49+
//Перевод контекста в содержимое тега LanguageData
50+
XElement? root = xDoc.Element("LanguageData");
51+
if (root?.Elements() is null)
52+
{
53+
BrokenFiles.Add(currentFile, "Пустой тег LanguageData");
54+
return false;
55+
}
56+
return true;
57+
}
58+
59+
internal static void WriteBrokenFiles(TextBox textBox)
60+
{
61+
if (BrokenFiles.Count == 0)
62+
{
63+
textBox.AppendText($"{Environment.NewLine}Не найдено сломанных файлов.");
64+
return;
65+
}
66+
67+
StreamWriter sw = new(Path.Combine(Directory.GetCurrentDirectory() + "\\BrokenFiles.txt"), false);
68+
foreach (var file in BrokenFiles)
69+
{
70+
sw.WriteLine($"{file.Value}: {file.Key}");
71+
}
72+
sw.Close();
73+
textBox.AppendText($"{Environment.NewLine}Найдено {BrokenFiles.Count} сломанных файлов. Результат записан в файл BrokenFiles.txt папки {Directory.GetCurrentDirectory()}");
74+
BrokenFiles.Clear();
75+
}
76+
}
77+
}

FindChanges.cs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}

HttpClient.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json;
2+
3+
namespace RimLangKit
4+
{
5+
public class HttpClient
6+
{
7+
private static readonly System.Net.Http.HttpClient Client = new();
8+
static HttpClient()
9+
{
10+
Client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:110.0) Gecko/20100101 Firefox/110.0");
11+
}
12+
13+
public static Root? GetGithubJson()
14+
{
15+
HttpResponseMessage response;
16+
try
17+
{
18+
response = Client.GetAsync("https://api.github.com/repos/OneCodeUnit/RimLangKit/releases/latest").Result;
19+
}
20+
catch
21+
{
22+
return null;
23+
}
24+
string? text = response.Content.ReadAsStringAsync().Result;
25+
Root? json = JsonSerializer.Deserialize<Root?>(text);
26+
return json;
27+
}
28+
}
29+
30+
public class Root
31+
{
32+
#pragma warning disable CA1707, CS8618, IDE1006
33+
public string tag_name { get; set; }
34+
#pragma warning restore CA1707, CS8618, IDE1006
35+
}
36+
}

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [2023] [OliveWizard]
189+
Copyright [2024] [OliveWizard]
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)