DynamicTranslator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using BepInEx;
  2. using BepInEx.Common;
  3. using Harmony;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using TMPro;
  12. using UnityEngine;
  13. using UnityEngine.SceneManagement;
  14. namespace DynamicTranslationLoader
  15. {
  16. public class DynamicTranslator : BaseUnityPlugin
  17. {
  18. private static Dictionary<string, string> translations = new Dictionary<string, string>();
  19. private static List<string> untranslated = new List<string>();
  20. public override string Name => "Dynamic Translator";
  21. public DynamicTranslator()
  22. {
  23. string[] translation = File.ReadAllLines(Utility.CombinePaths(Utility.PluginsDirectory, "translation", "translation.txt"));
  24. for (int i = 0; i < translation.Length; i++)
  25. {
  26. string line = translation[i];
  27. if (!line.Contains('='))
  28. continue;
  29. string[] split = line.Split('=');
  30. translations[split[0]] = split[1];
  31. }
  32. var harmony = HarmonyInstance.Create("com.bepis.bepinex.dynamictranslationloader");
  33. MethodInfo original = AccessTools.Property(typeof(TMP_Text), "text").GetSetMethod();
  34. HarmonyMethod prefix = new HarmonyMethod(typeof(Hooks).GetMethod("LabelTextHook"));
  35. harmony.Patch(original, prefix, null);
  36. original = AccessTools.Method(typeof(TMP_Text), "SetText", new[] { typeof(string) });
  37. prefix = new HarmonyMethod(typeof(Hooks).GetMethod("SetTextHook"));
  38. harmony.Patch(original, prefix, null);
  39. }
  40. protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  41. {
  42. TranslateAll();
  43. }
  44. void Update()
  45. {
  46. if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.F10))
  47. {
  48. Dump();
  49. Chainloader.Log($"Text dumped to \"{Path.GetFullPath("dumped-tl.txt")}\"", true);
  50. }
  51. }
  52. void TranslateAll()
  53. {
  54. foreach (TextMeshProUGUI gameObject in Resources.FindObjectsOfTypeAll<TextMeshProUGUI>())
  55. {
  56. //gameObject.text = "Harsh is shit";
  57. if (translations.ContainsKey(gameObject.text))
  58. gameObject.text = translations[gameObject.text];
  59. else if (!untranslated.Contains(gameObject.text) &&
  60. !translations.ContainsValue(gameObject.text.Trim()))
  61. {
  62. untranslated.Add(gameObject.text);
  63. }
  64. }
  65. }
  66. void Dump()
  67. {
  68. string output = "";
  69. foreach (var kv in translations)
  70. output += $"{kv.Key.Trim()}={kv.Value.Trim()}\r\n";
  71. foreach (var text in untranslated)
  72. if(!Regex.Replace(text, @"[\d-]", string.Empty).IsNullOrWhiteSpace()
  73. && !text.Contains("Reset"))
  74. output += $"{text.Trim()}=\r\n";
  75. File.WriteAllText("dumped-tl.txt", output);
  76. }
  77. public static string Translate(string input)
  78. {
  79. if (translations.ContainsKey(input))
  80. return translations[input];
  81. if (!untranslated.Contains(input))
  82. untranslated.Add(input);
  83. return input;
  84. }
  85. }
  86. }