From 4d436bbfcfd54f979904f4adf26ab95f02b3feb8 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 5 Oct 2020 15:33:14 +0200 Subject: [PATCH] Load translation, SetLanguage command --- .gitignore | 5 ++ Localization.sln | 16 +++++ Localization/Localization.csproj | 73 +++++++++++++++++++++ Localization/LocalizationMod.cs | 86 +++++++++++++++++++++++++ Localization/Properties/AssemblyInfo.cs | 35 ++++++++++ 5 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 Localization.sln create mode 100644 Localization/Localization.csproj create mode 100644 Localization/LocalizationMod.cs create mode 100644 Localization/Properties/AssemblyInfo.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Localization.sln b/Localization.sln new file mode 100644 index 0000000..4b2eeaf --- /dev/null +++ b/Localization.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Localization", "Localization\Localization.csproj", "{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Localization/Localization.csproj b/Localization/Localization.csproj new file mode 100644 index 0000000..0136f30 --- /dev/null +++ b/Localization/Localization.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {E2169518-26AB-4DA6-ACB2-6BCFB35D6953} + Library + Properties + Localization + Localization + v4.8 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Lib.Harmony.2.0.2\lib\net48\0Harmony.dll + True + + + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll + + + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll + + + ..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll + + + ..\..\ref\Gamecraft_Data\Managed\RobocraftX.Common.dll + + + + + + + + + + + + + + + + + + diff --git a/Localization/LocalizationMod.cs b/Localization/LocalizationMod.cs new file mode 100644 index 0000000..6d8a6a1 --- /dev/null +++ b/Localization/LocalizationMod.cs @@ -0,0 +1,86 @@ +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(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>(File.ReadAllText(langPath)); + var origStrings = + (Dictionary) 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"; + } +} \ No newline at end of file diff --git a/Localization/Properties/AssemblyInfo.cs b/Localization/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c0a2425 --- /dev/null +++ b/Localization/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Localization")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Localization")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("E2169518-26AB-4DA6-ACB2-6BCFB35D6953")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file