DynamicTranslator.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using BepInEx;
  2. using BepInEx.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using TMPro;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. namespace DynamicTranslationLoader
  13. {
  14. public class DynamicTranslator : BaseUnityPlugin
  15. {
  16. Dictionary<string, string> translations = new Dictionary<string, string>();
  17. List<string> untranslated = new List<string>();
  18. public override string Name => "Dynamic Translator";
  19. public DynamicTranslator()
  20. {
  21. string[] translation = File.ReadAllLines(Utility.CombinePaths(Utility.ExecutingDirectory, "translation", "translation.txt"));
  22. for (int i = 0; i < translation.Length; i++)
  23. {
  24. string line = translation[i];
  25. if (!line.Contains('='))
  26. continue;
  27. string[] split = line.Split('=');
  28. translations[split[0]] = split[1];
  29. }
  30. }
  31. protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  32. {
  33. Translate();
  34. }
  35. void OnUpdate()
  36. {
  37. if (UnityEngine.Event.current.Equals(Event.KeyboardEvent("f9")))
  38. {
  39. Translate();
  40. }
  41. else if (UnityEngine.Event.current.Equals(Event.KeyboardEvent("f10")))
  42. {
  43. Dump();
  44. Console.WriteLine($"Text dumped to \"{Path.GetFullPath("dumped-tl.txt")}\"");
  45. }
  46. }
  47. void Translate()
  48. {
  49. foreach (TextMeshProUGUI gameObject in GameObject.FindObjectsOfType<TextMeshProUGUI>())
  50. {
  51. //gameObject.text = "Harsh is shit";
  52. if (translations.ContainsKey(gameObject.text))
  53. gameObject.text = translations[gameObject.text];
  54. else
  55. if (!untranslated.Contains(gameObject.text))
  56. untranslated.Add(gameObject.text);
  57. }
  58. }
  59. void Dump()
  60. {
  61. string output = "";
  62. foreach (var kv in translations)
  63. output += $"{kv.Key.Trim()}={kv.Value.Trim()}\r\n";
  64. foreach (var text in untranslated)
  65. if (!text.IsNullOrWhiteSpace()
  66. && !text.Contains("Reset")
  67. && !Regex.Replace(text, @"[\d-]", string.Empty).IsNullOrWhiteSpace()
  68. && !translations.ContainsValue(text.Trim()))
  69. output += $"{text.Trim()}=\r\n";
  70. File.WriteAllText("dumped-tl.txt", output);
  71. }
  72. public bool TryTranslate(string input, out string output)
  73. {
  74. if (translations.ContainsKey(input))
  75. {
  76. output = translations[input];
  77. return true;
  78. }
  79. output = null;
  80. if (!untranslated.Contains(input))
  81. untranslated.Add(input);
  82. return false;
  83. }
  84. }
  85. }