From 4afae549d32235a0015275a7a53812866f350fcd Mon Sep 17 00:00:00 2001 From: adamsong Date: Tue, 28 Feb 2023 18:46:08 -0500 Subject: [PATCH] Added loader for lang files --- SpaceWarp/API/SpaceWarpManager.cs | 52 +++++++++++++++++++ .../LoadingActions/LoadLocalizationAction.cs | 34 ++++++++++++ .../LoadSpaceWarpLocalizationsAction.cs | 28 ++++++++++ SpaceWarp/Patching/LoadingScreenPatcher.cs | 3 ++ 4 files changed, 117 insertions(+) create mode 100644 SpaceWarp/Patching/LoadingActions/LoadLocalizationAction.cs create mode 100644 SpaceWarp/Patching/LoadingActions/LoadSpaceWarpLocalizationsAction.cs diff --git a/SpaceWarp/API/SpaceWarpManager.cs b/SpaceWarp/API/SpaceWarpManager.cs index 361c2015..110c68f1 100644 --- a/SpaceWarp/API/SpaceWarpManager.cs +++ b/SpaceWarp/API/SpaceWarpManager.cs @@ -715,4 +715,56 @@ public void LoadSingleModAddressables(string modID, ModInfo info) _modLogger.Info($"Did not find addressables for {modID}"); } } + + private void LoadLocalizationFromFolder(string folder) + { + _modLogger.Info($"Attempting to load localizations from {folder}"); + I2.Loc.LanguageSourceData languageSourceData = null; + if (!Directory.Exists(folder)) + { + _modLogger.Info($"{folder} does not exist, not loading localizations."); + return; + } + DirectoryInfo info = new DirectoryInfo(folder); + foreach (var csvFile in info.GetFiles("*.csv")) + { + languageSourceData ??= new I2.Loc.LanguageSourceData(); + var csvData = File.ReadAllText(csvFile.FullName); + languageSourceData.Import_CSV("", csvData, I2.Loc.eSpreadsheetUpdateMode.AddNewTerms); + } + + foreach (var i2csvFile in info.GetFiles("*.i2csv")) + { + languageSourceData ??= new I2.Loc.LanguageSourceData(); + var i2csvData = File.ReadAllText(i2csvFile.FullName); + languageSourceData.Import_I2CSV("", i2csvData, I2.Loc.eSpreadsheetUpdateMode.AddNewTerms); + } + + if (languageSourceData != null) + { + _modLogger.Info($"Loaded localizations from {folder}"); + I2.Loc.LocalizationManager.AddSource(languageSourceData); + } + else + { + _modLogger.Info($"No localizations found in {folder}"); + } + } + public void LoadSpaceWarpLocalizations() + { + if (I2.Loc.LocalizationManager.Sources.Count == 0) + { + I2.Loc.LocalizationManager.UpdateSources(); + } + + string localizationsPath = Path.Combine(SPACE_WARP_PATH, "localizations"); + LoadLocalizationFromFolder(localizationsPath); + } + + public void LoadSingleModLocalization(string modID, ModInfo info) + { + string modFolder = Path.Combine(MODS_FULL_PATH, modID); + string localizationsPath = Path.Combine(modFolder, "localizations"); + LoadLocalizationFromFolder(localizationsPath); + } } \ No newline at end of file diff --git a/SpaceWarp/Patching/LoadingActions/LoadLocalizationAction.cs b/SpaceWarp/Patching/LoadingActions/LoadLocalizationAction.cs new file mode 100644 index 00000000..0b6131bb --- /dev/null +++ b/SpaceWarp/Patching/LoadingActions/LoadLocalizationAction.cs @@ -0,0 +1,34 @@ +using System; +using KSP.Game.Flow; +using SpaceWarp.API; +using SpaceWarp.API.Managers; +using SpaceWarp.API.Mods.JSON; + +namespace SpaceWarp.Patching.LoadingActions; + +public class LoadLocalizationAction : FlowAction +{ + private string _modID; + private ModInfo _info; + + public LoadLocalizationAction(string name, string modID, ModInfo info) : base(name) + { + _modID = modID; + _info = info; + } + + public override void DoAction(Action resolve, Action reject) + { + ManagerLocator.TryGet(out SpaceWarpManager spaceWarpManager); + + try + { + spaceWarpManager.LoadSingleModLocalization(_modID,_info); + resolve(); + } + catch (Exception e) + { + reject(e.ToString()); + } + } +} \ No newline at end of file diff --git a/SpaceWarp/Patching/LoadingActions/LoadSpaceWarpLocalizationsAction.cs b/SpaceWarp/Patching/LoadingActions/LoadSpaceWarpLocalizationsAction.cs new file mode 100644 index 00000000..8f347067 --- /dev/null +++ b/SpaceWarp/Patching/LoadingActions/LoadSpaceWarpLocalizationsAction.cs @@ -0,0 +1,28 @@ +using System; +using KSP.Game.Flow; +using SpaceWarp.API; +using SpaceWarp.API.Managers; + +namespace SpaceWarp.Patching.LoadingActions; + +public class LoadSpaceWarpLocalizationsAction : FlowAction +{ + public LoadSpaceWarpLocalizationsAction(string name) : base(name) + { + } + + public override void DoAction(Action resolve, Action reject) + { + ManagerLocator.TryGet(out SpaceWarpManager spaceWarpManager); + + try + { + spaceWarpManager.LoadSpaceWarpLocalizations(); + resolve(); + } + catch (Exception e) + { + reject(e.ToString()); + } + } +} \ No newline at end of file diff --git a/SpaceWarp/Patching/LoadingScreenPatcher.cs b/SpaceWarp/Patching/LoadingScreenPatcher.cs index 8c5052ac..ad7ff4e4 100644 --- a/SpaceWarp/Patching/LoadingScreenPatcher.cs +++ b/SpaceWarp/Patching/LoadingScreenPatcher.cs @@ -19,6 +19,8 @@ public static void AddModLoadingScreens() new LoadSpaceWarpAddressablesAction("Initializing Space Warp Provided Addressables")); gameManager.LoadingFlow.AddAction( new SpaceWarpAssetInitializationAction("Initializing Space Warp Provided Assets")); + gameManager.LoadingFlow.AddAction( + new LoadSpaceWarpLocalizationsAction("Initializing Space Warp Localizations")); } public static void AddAllModLoadingSteps() @@ -31,6 +33,7 @@ public static void AddAllModLoadingSteps() mod.Item1, mod.Item2)); gameManager.LoadingFlow.AddAction(new LoadAssetAction($"Loading assets for {mod.Item1}", mod.Item1, mod.Item2)); + gameManager.LoadingFlow.AddAction(new LoadLocalizationAction($"Loading localizations for {mod.Item1}", mod.Item1, mod.Item2)); gameManager.LoadingFlow.AddAction(new LoadModAction($"Initializing {mod.Item1}", mod.Item1, mod.Item2)); }