From e59dfec8b50b1489c3451bdc65e7b26628605006 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 7 Apr 2023 21:08:28 +0600 Subject: [PATCH] Fixed Localisation functions --- paradox-mod-merger/Localisation.cs | 115 ++++++++++++++++++----------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/paradox-mod-merger/Localisation.cs b/paradox-mod-merger/Localisation.cs index ab9edd4..11aace2 100644 --- a/paradox-mod-merger/Localisation.cs +++ b/paradox-mod-merger/Localisation.cs @@ -2,81 +2,108 @@ static class Localisation { - static ConsoleLogger logger = new($"logs", "autoloc"); + static ConsoleLogger logger = new("logs", "autoloc"); static void Log(params string[] msg) => logger.Log(msg); - public static void GenerateRussian(IOPath engDir, IOPath rusDir) + public static void GenerateRussian(IOPath _engDir, IOPath _rusDir) { int counter = 0; - foreach (var fileName in Directory.GetAllFiles(engDir)) - { - if (!fileName.EndsWith("l_english.yml")) - continue; - - IOPath rusFileName = fileName - .ReplaceBase(engDir, rusDir) - .Replace("l_english", "l_russian"); - - if (!File.Exists(rusFileName)) - { - Log("gray", $"skipped file {rusFileName.RemoveBase(rusDir)}"); - continue; - } - string text = File.ReadAllText(fileName) - .Replace("l_english:", "l_russian: "); - byte[] bytes = StringConverter.UTF8BOM.GetBytes(text); - File.WriteAllBytes(rusFileName, bytes); - Log("g", $"file {rusFileName} created"); - counter++; - } + ProcessDir(_engDir, _rusDir); Log("g",$"created {counter} localisation files"); + + void ProcessDir(IOPath engDir, IOPath rusDir) + { + foreach (var fileName in Directory.GetFiles(engDir)) + { + if (!fileName.EndsWith("l_english.yml")) + continue; + + IOPath rusFileName = fileName + .ReplaceBase(engDir, rusDir) + .Replace("l_english", "l_russian"); + + if (File.Exists(rusFileName)) + { + // Log("w", $"skipped {rusFileName.RemoveBase(rusDir)}"); + continue; + } + + string text = File.ReadAllText(fileName) + .Replace("l_english:", "l_russian: "); + byte[] bytes = StringConverter.UTF8BOM.GetBytes(text); + File.WriteAllBytes(rusFileName, bytes); + Log("g", $"created {rusFileName}"); + counter++; + } + + void ProcessSubdir(string subdirEngName, string subdirRusName) + { + var subdirEng = Path.Concat(engDir,subdirEngName); + var subdirRus = Path.Concat(rusDir,subdirRusName); + if (Directory.Exists(subdirEng)) + ProcessDir(subdirEng, subdirRus); + } + + ProcessSubdir("english", "russian"); + ProcessSubdir("replace", "replace"); + ProcessSubdir("name_lists", "name_lists"); + ProcessSubdir("random_names", "random_names"); + } } // deletes all localisations except l_russian and l_english public static void Clean(IOPath _loc_dir) { - Log("g", $"deleted {RemoveUnneededDirs(_loc_dir)} dirs"); - Log("g", $"deleted {RemoveUnneededFiles(_loc_dir)} files"); + int deleted_files_count=0, deleted_dirs_count=0; + DeleteUnneededDirs(_loc_dir); + DeleteUnneededFiles(_loc_dir); + Log("g", $"deleted {deleted_files_count} files"); + Log("g", $"deleted {deleted_dirs_count} dirs"); - - int RemoveUnneededDirs(IOPath loc_dir) + void DeleteUnneededDirs(IOPath loc_dir) { - int count = 0; foreach (var subdir in Directory.GetDirectories(loc_dir)) { string dir_basename = subdir.LastName().Str; - if (dir_basename == "russian" || dir_basename == "english") - continue; - - if (dir_basename == "replace") + if (dir_basename is "russian" or "english") { - RemoveUnneededDirs(subdir); - RemoveUnneededFiles(subdir); + // Log("w",$"skipped {subdir}"); continue; } - if (dir_basename.Contains("rus")) + if (dir_basename is "replace" or "name_lists" or "random_names") + { + DeleteUnneededDirs(subdir); + DeleteUnneededFiles(subdir); + continue; + } + + // incorrect dirs, for example l_russian/ + if (dir_basename.ToLower().Contains("russian") || dir_basename.ToLower().Contains("english")) Log("y", $"unexpected dir: {subdir}"); - Directory.Delete(subdir); - count++; - } - return count; + Directory.Delete(subdir); + Log("m", $"deleted {subdir}"); + deleted_dirs_count++; + } } - int RemoveUnneededFiles(IOPath loc_dir) + void DeleteUnneededFiles(IOPath loc_dir) { - int count = 0; foreach (var file in Directory.GetFiles(loc_dir)) { - if(file.EndsWith("l_russian") || file.EndsWith("l_enghish")) + if(file.EndsWith("l_russian.yml") || file.EndsWith("l_enghish.yml")) + { + // Log("w",$"skipped {file}"); continue; + } + if (!file.Contains("_l_") || !file.EndsWith(".yml")) Log("y",$"unexpected file: {file}"); File.Delete(file); - count++; + Log("m",$"deleted {file}"); + deleted_files_count++; } - return count; } } } \ No newline at end of file