86 lines
No EOL
3 KiB
C#
86 lines
No EOL
3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using GamecraftModdingAPI;
|
|
using GamecraftModdingAPI.Commands;
|
|
using GamecraftModdingAPI.Utility;
|
|
using HarmonyLib;
|
|
using IllusionPlugin;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using ServiceLayer;
|
|
|
|
namespace Localization
|
|
{
|
|
public class LocalizationMod : IEnhancedPlugin
|
|
{
|
|
private readonly DirectoryInfo _pluginFolder = new DirectoryInfo(Path.Combine("Plugins", "Localization"));
|
|
public override void OnApplicationStart()
|
|
{
|
|
Main.Init();
|
|
if (!_pluginFolder.Exists)
|
|
_pluginFolder.Create();
|
|
string settingsPath = Path.Combine(_pluginFolder.FullName, "settings.json");
|
|
JObject settings;
|
|
if (File.Exists(settingsPath))
|
|
{
|
|
try
|
|
{
|
|
using (var stream = new JsonTextReader(File.OpenText(settingsPath)))
|
|
settings = JObject.Load(stream);
|
|
string lang = (string) (settings["lang"] ?? (settings["lang"] = "en-us"));
|
|
LoadTranslation(lang);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logging.LogError(e);
|
|
settings = new JObject();
|
|
}
|
|
}
|
|
else
|
|
settings = new JObject();
|
|
|
|
CommandBuilder.Builder("SetLanguage", "Sets the game's language")
|
|
.Action<string>(lang =>
|
|
{
|
|
settings["lang"] = lang;
|
|
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
|
|
settings.WriteTo(stream);
|
|
LoadTranslation(lang);
|
|
|
|
}).Build();
|
|
}
|
|
|
|
public override void OnApplicationQuit()
|
|
{
|
|
Main.Shutdown();
|
|
}
|
|
|
|
private void LoadTranslation(string lang)
|
|
{
|
|
Logging.CommandLog("Loading translation for " + lang + "...");
|
|
string langPath = Path.Combine(_pluginFolder.FullName, lang + ".json");
|
|
if (!File.Exists(langPath))
|
|
{
|
|
Logging.CommandLogError("Could not find json file!");
|
|
return;
|
|
}
|
|
|
|
var strings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(langPath));
|
|
var origStrings =
|
|
(Dictionary<string, string>) AccessTools.Field(typeof(LocalizationService), "LocalizedStrings")
|
|
.GetValue(null);
|
|
foreach (var kv in strings)
|
|
{
|
|
if (!origStrings.Remove(kv.Key))
|
|
Logging.CommandLogWarning(kv.Key + " wasn't in the original file.");
|
|
origStrings.Add(kv.Key, kv.Value);
|
|
}
|
|
|
|
Logging.CommandLog("Updated " + strings.Count + " strings (" + origStrings.Count + " strings in total)");
|
|
}
|
|
|
|
public override string Name { get; } = "LocalizationMod";
|
|
public override string Version { get; } = "1.0.0";
|
|
}
|
|
} |