Hooks.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Harmony;
  2. using System.Reflection;
  3. using TMPro;
  4. namespace DynamicTranslationLoader
  5. {
  6. public static class Hooks
  7. {
  8. public static void InstallHooks()
  9. {
  10. var harmony = HarmonyInstance.Create("com.bepis.bepinex.dynamictranslationloader");
  11. MethodInfo original = AccessTools.Property(typeof(TMP_Text), "text").GetSetMethod();
  12. HarmonyMethod prefix = new HarmonyMethod(typeof(Hooks).GetMethod("TextPropertyHook"));
  13. harmony.Patch(original, prefix, null);
  14. original = AccessTools.Method(typeof(TMP_Text), "SetText", new[] { typeof(string) });
  15. prefix = new HarmonyMethod(typeof(Hooks).GetMethod("SetTextHook"));
  16. harmony.Patch(original, prefix, null);
  17. original = AccessTools.Property(typeof(UnityEngine.UI.Text), "text").GetSetMethod();
  18. prefix = new HarmonyMethod(typeof(Hooks).GetMethod("TextPropertyHook"));
  19. harmony.Patch(original, prefix, null);
  20. }
  21. public static void TextPropertyHook(ref string value)
  22. {
  23. value = DynamicTranslator.Translate(value);
  24. }
  25. public static void SetTextHook(ref string text)
  26. {
  27. text = DynamicTranslator.Translate(text);
  28. }
  29. }
  30. }