Skip to content

Commit

Permalink
allow reloading translations at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Sep 27, 2023
1 parent 952e63a commit 4b26a95
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions Runtime/DVLangHelper.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Reference Include="I2.Localization" />
<Reference Include="UnityEngine" />
<Reference Include="UnityEngine.CoreModule" />
<Reference Include="UnityEngine.IMGUIModule" />
<Reference Include="UnityEngine.UnityWebRequestModule" />
<Reference Include="UnityModManager" />
</ItemGroup>
Expand Down
24 changes: 23 additions & 1 deletion Runtime/LangHelperMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityModManagerNet;

namespace DVLangHelper.Runtime
Expand All @@ -20,6 +21,7 @@ public static void Load(UnityModManager.ModEntry modEntry)
Settings = UnityModManager.ModSettings.Load<LangHelperSettings>(modEntry);
modEntry.OnGUI = DrawGUI;
modEntry.OnSaveGUI = SaveGUI;
modEntry.OnHideGUI = HideGUI;

string cacheDir = CacheDirPath;
if (!Settings.UseCache && Directory.Exists(cacheDir))
Expand All @@ -35,16 +37,28 @@ public static void Load(UnityModManager.ModEntry modEntry)
harmony.PatchAll(Assembly.GetExecutingAssembly());
}

public static string GuiMessage = string.Empty;

private static void DrawGUI(UnityModManager.ModEntry entry)
{
Settings.Draw(entry);

if (!string.IsNullOrEmpty(GuiMessage))
{
GUILayout.Label(GuiMessage);
}
}

private static void SaveGUI(UnityModManager.ModEntry entry)
{
Settings.Save(entry);
}

private static void HideGUI(UnityModManager.ModEntry entry)
{
GuiMessage = string.Empty;
}

public static void Log(string message)
{
Instance!.Logger.Log(message);
Expand All @@ -70,9 +84,17 @@ public class LangHelperSettings : UnityModManager.ModSettings, IDrawable
[Draw("Enable Caching of Web CSVs (debug)")]
public bool UseCache = true;

[Draw("Reload translations files")]
public bool Reload = false;

public void OnChange()
{

if (Reload)
{
Reload = false;
int nReloaded = TranslationInjector.ReloadTranslationFiles();
LangHelperMain.GuiMessage = $"Reloaded {nReloaded} translation files";
}
}

public override void Save(UnityModManager.ModEntry modEntry)
Expand Down
54 changes: 54 additions & 0 deletions Runtime/TranslationInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
Expand All @@ -19,11 +20,36 @@ public class TranslationInjector

public static bool InjectionStarted = false;

public static int ReloadTranslationFiles()
{
int totalReloaded = 0;
foreach (var instance in _instances)
{
foreach (var file in instance._csvFiles)
{
switch (file.Type)
{
case CsvFileInfo.SourceType.Local:
instance.AddTranslationsFromCsv(file.Path);
totalReloaded++;
break;
case CsvFileInfo.SourceType.URL:
instance.AddTranslationsFromWebCsv(file.Path);
totalReloaded++;
break;
}
}
}
return totalReloaded;
}

public readonly string Id;
private readonly GameObject _sourceHolder;
private readonly LanguageSource _source;
private readonly LanguageSourceData _langData;

private readonly List<CsvFileInfo> _csvFiles = new List<CsvFileInfo>(1);

public TranslationInjector(string sourceId)
{
Id = sourceId;
Expand Down Expand Up @@ -53,6 +79,11 @@ public void AddTranslationsFromCsv(string csvPath)
{
string csvText = LocalizationReader.ReadCSVfile(csvPath, Encoding.UTF8);
_langData.Import_CSV(string.Empty, csvText, eSpreadsheetUpdateMode.Merge);

if (!_csvFiles.Any(f => f.Path == csvPath))
{
_csvFiles.Add(new CsvFileInfo(CsvFileInfo.SourceType.Local, csvPath));
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -88,6 +119,11 @@ private IEnumerator AddWebCsvCoro(string url)
string downloaded = request.downloadHandler.text;
_langData.Import_CSV(string.Empty, downloaded, eSpreadsheetUpdateMode.Merge);

if (!_csvFiles.Any(f => f.Path == url))
{
_csvFiles.Add(new CsvFileInfo(CsvFileInfo.SourceType.URL, url));
}

LangHelperMain.Log($"Successfully fetched web csv translations from {url}");

if (LangHelperMain.Settings.UseCache)
Expand Down Expand Up @@ -145,5 +181,23 @@ public void PerformInjection()
LangHelperMain.Log($"Injecting language source {Id}");
_addSourceMethod.Invoke(null, new object[] { _langData });
}

private readonly struct CsvFileInfo
{
public readonly SourceType Type;
public readonly string Path;

public CsvFileInfo(SourceType type, string path)
{
Type = type;
Path = path;
}

public enum SourceType
{
Local,
URL,
}
}
}
}

0 comments on commit 4b26a95

Please sign in to comment.