Skip to content

Commit

Permalink
Added loader for lang files
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsong committed Feb 28, 2023
1 parent 0a5b1f3 commit 4afae54
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
52 changes: 52 additions & 0 deletions SpaceWarp/API/SpaceWarpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
34 changes: 34 additions & 0 deletions SpaceWarp/Patching/LoadingActions/LoadLocalizationAction.cs
Original file line number Diff line number Diff line change
@@ -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<string> reject)
{
ManagerLocator.TryGet(out SpaceWarpManager spaceWarpManager);

try
{
spaceWarpManager.LoadSingleModLocalization(_modID,_info);
resolve();
}
catch (Exception e)
{
reject(e.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string> reject)
{
ManagerLocator.TryGet(out SpaceWarpManager spaceWarpManager);

try
{
spaceWarpManager.LoadSpaceWarpLocalizations();
resolve();
}
catch (Exception e)
{
reject(e.ToString());
}
}
}
3 changes: 3 additions & 0 deletions SpaceWarp/Patching/LoadingScreenPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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));
}

Expand Down

0 comments on commit 4afae54

Please sign in to comment.