UGUIHooks.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using Harmony;
  7. using XUnity.AutoTranslator.Plugin.Core.Constants;
  8. namespace XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI
  9. {
  10. internal static class UGUIHooks
  11. {
  12. public static bool HooksOverriden = false;
  13. public static readonly Type[] All = new[] {
  14. typeof( Text_text_Hook ),
  15. typeof( Text_OnEnable_Hook ),
  16. };
  17. }
  18. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  19. internal static class Text_text_Hook
  20. {
  21. static bool Prepare( HarmonyInstance instance )
  22. {
  23. return ClrTypes.Text != null;
  24. }
  25. static MethodBase TargetMethod( HarmonyInstance instance )
  26. {
  27. var text = AccessTools.Property( ClrTypes.Text, "text" );
  28. return text.GetSetMethod();
  29. }
  30. static void Postfix( object __instance )
  31. {
  32. if( !UGUIHooks.HooksOverriden )
  33. {
  34. AutoTranslationPlugin.Current.Hook_TextChanged( __instance, false );
  35. }
  36. AutoTranslationPlugin.Current.Hook_HandleComponent( __instance );
  37. }
  38. }
  39. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  40. internal static class Text_OnEnable_Hook
  41. {
  42. static bool Prepare( HarmonyInstance instance )
  43. {
  44. return ClrTypes.Text != null;
  45. }
  46. static MethodBase TargetMethod( HarmonyInstance instance )
  47. {
  48. var OnEnable = AccessTools.Method( ClrTypes.Text, "OnEnable" );
  49. return OnEnable;
  50. }
  51. static void Postfix( object __instance )
  52. {
  53. if( !UGUIHooks.HooksOverriden )
  54. {
  55. AutoTranslationPlugin.Current.Hook_TextChanged( __instance, true );
  56. }
  57. AutoTranslationPlugin.Current.Hook_HandleComponent( __instance );
  58. }
  59. }
  60. }