Localization mod for Gamecraft.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Commands;
  7. using GamecraftModdingAPI.Utility;
  8. using HarmonyLib;
  9. using IllusionPlugin;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. using ServiceLayer;
  13. namespace Localization
  14. {
  15. public class LocalizationMod : IEnhancedPlugin
  16. {
  17. private readonly DirectoryInfo _pluginFolder = new DirectoryInfo(Path.Combine("Plugins", "Localization"));
  18. private Dictionary<string, string> _origStrings;
  19. public override void OnApplicationStart()
  20. {
  21. Main.Init();
  22. if (!_pluginFolder.Exists)
  23. _pluginFolder.Create();
  24. string settingsPath = Path.Combine(_pluginFolder.FullName, "settings.json");
  25. JObject settings;
  26. if (File.Exists(settingsPath))
  27. {
  28. try
  29. {
  30. using (var stream = new JsonTextReader(File.OpenText(settingsPath)))
  31. settings = JObject.Load(stream);
  32. string lang = (string) (settings["lang"] ?? (settings["lang"] = "en"));
  33. lang = lang.ToLower();
  34. if (lang != "en")
  35. LoadTranslation(lang);
  36. }
  37. catch (Exception e)
  38. {
  39. Logging.LogError(e);
  40. settings = new JObject();
  41. }
  42. }
  43. else
  44. settings = new JObject();
  45. CommandBuilder.Builder("SetLanguage", "Sets the game's language")
  46. .Action<string>(lang =>
  47. {
  48. lang = lang.ToLower();
  49. settings["lang"] = lang;
  50. using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
  51. settings.WriteTo(stream);
  52. LoadTranslation(lang);
  53. }).Build();
  54. CommandBuilder.Builder("ListLanguages", "Lists available languages")
  55. .Action(() =>
  56. {
  57. Logging.CommandLog(new[] {"en"}.Concat(_pluginFolder.EnumerateFiles()
  58. .Where(file => file.Extension == ".json" && file.Name.Length < 8)
  59. .Select(file => file.Name.Replace(file.Extension, ""))).Distinct()
  60. .Aggregate((a, b) => a + ", " + b));
  61. }).Build();
  62. CommandBuilder.Builder("DumpLangStrings", "Dumps the current strings. Set language to en-us first.")
  63. .Action(() =>
  64. {
  65. File.WriteAllText(Path.Combine(_pluginFolder.FullName, "dumped.json"),
  66. JsonConvert.SerializeObject(_origStrings));
  67. }).Build();
  68. }
  69. public override void OnApplicationQuit()
  70. {
  71. Main.Shutdown();
  72. }
  73. private void LoadTranslation(string lang)
  74. {
  75. Logging.CommandLog("Loading translation for " + lang + "...");
  76. LoadOriginalStrings();
  77. string langPath = Path.Combine(_pluginFolder.FullName, lang + ".json");
  78. if (!File.Exists(langPath))
  79. {
  80. if (lang == "en")
  81. {
  82. AccessTools.Method(typeof(LocalizationService), "Init").Invoke(null, new object[0]);
  83. _origStrings = null; //Reset as the object changed
  84. LoadOriginalStrings();
  85. Logging.CommandLog("Strings reset to default. (" + _origStrings.Count + " strings in total)");
  86. return;
  87. }
  88. Logging.CommandLogError("Could not find json file!");
  89. return;
  90. }
  91. var strings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(langPath));
  92. foreach (var kv in strings)
  93. {
  94. if (!_origStrings.Remove(kv.Key))
  95. Logging.CommandLogWarning(kv.Key + " wasn't in the original file.");
  96. _origStrings.Add(kv.Key, kv.Value);
  97. }
  98. Logging.CommandLog("Updated " + strings.Count + " strings (" + _origStrings.Count + " strings in total)");
  99. }
  100. private void LoadOriginalStrings()
  101. {
  102. if (_origStrings != null) return;
  103. _origStrings =
  104. (Dictionary<string, string>) AccessTools.Field(typeof(LocalizationService), "LocalizedStrings")
  105. .GetValue(null);
  106. }
  107. public override string Name { get; } = "LocalizationMod";
  108. public override string Version { get; } = "1.0.0";
  109. }
  110. }