TextMeshProHooks.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.TextMeshPro
  9. {
  10. public static class TextMeshProHooks
  11. {
  12. public static readonly Type[] All = new[] {
  13. typeof( TeshMeshProUGUIOnEnableHook ),
  14. typeof( TeshMeshProOnEnableHook ),
  15. typeof( TextPropertyHook ),
  16. typeof( SetTextHook1 ),
  17. typeof( SetTextHook2 ),
  18. typeof( SetTextHook3 ),
  19. typeof( SetCharArrayHook1 ),
  20. typeof( SetCharArrayHook2 ),
  21. typeof( SetCharArrayHook3 ),
  22. };
  23. }
  24. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  25. public static class TeshMeshProUGUIOnEnableHook
  26. {
  27. static bool Prepare( HarmonyInstance instance )
  28. {
  29. return Types.TextMeshProUGUI != null;
  30. }
  31. static MethodBase TargetMethod( HarmonyInstance instance )
  32. {
  33. return AccessTools.Method( Types.TextMeshProUGUI, "OnEnable" );
  34. }
  35. static void Postfix( object __instance )
  36. {
  37. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  38. }
  39. }
  40. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  41. public static class TeshMeshProOnEnableHook
  42. {
  43. static bool Prepare( HarmonyInstance instance )
  44. {
  45. return Types.TextMeshPro != null;
  46. }
  47. static MethodBase TargetMethod( HarmonyInstance instance )
  48. {
  49. return AccessTools.Method( Types.TextMeshPro, "OnEnable" );
  50. }
  51. static void Postfix( object __instance )
  52. {
  53. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  54. }
  55. }
  56. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  57. public static class TextPropertyHook
  58. {
  59. static bool Prepare( HarmonyInstance instance )
  60. {
  61. return Types.TMP_Text != null;
  62. }
  63. static MethodBase TargetMethod( HarmonyInstance instance )
  64. {
  65. return AccessTools.Property( Types.TMP_Text, "text" ).GetSetMethod();
  66. }
  67. static void Postfix( object __instance )
  68. {
  69. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  70. }
  71. }
  72. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  73. public static class SetTextHook1
  74. {
  75. static bool Prepare( HarmonyInstance instance )
  76. {
  77. return Types.TMP_Text != null;
  78. }
  79. static MethodBase TargetMethod( HarmonyInstance instance )
  80. {
  81. return AccessTools.Method( Types.TMP_Text, "SetText", new[] { typeof( StringBuilder ) } );
  82. }
  83. static void Postfix( object __instance )
  84. {
  85. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  86. }
  87. }
  88. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  89. public static class SetTextHook2
  90. {
  91. static bool Prepare( HarmonyInstance instance )
  92. {
  93. return Types.TMP_Text != null;
  94. }
  95. static MethodBase TargetMethod( HarmonyInstance instance )
  96. {
  97. return AccessTools.Method( Types.TMP_Text, "SetText", new[] { typeof( string ), typeof( bool ) } );
  98. }
  99. static void Postfix( object __instance )
  100. {
  101. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  102. }
  103. }
  104. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  105. public static class SetTextHook3
  106. {
  107. static bool Prepare( HarmonyInstance instance )
  108. {
  109. return Types.TMP_Text != null;
  110. }
  111. static MethodBase TargetMethod( HarmonyInstance instance )
  112. {
  113. return AccessTools.Method( Types.TMP_Text, "SetText", new[] { typeof( string ), typeof( float ), typeof( float ), typeof( float ) } );
  114. }
  115. static void Postfix( object __instance )
  116. {
  117. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  118. }
  119. }
  120. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  121. public static class SetCharArrayHook1
  122. {
  123. static bool Prepare( HarmonyInstance instance )
  124. {
  125. return Types.TMP_Text != null;
  126. }
  127. static MethodBase TargetMethod( HarmonyInstance instance )
  128. {
  129. return AccessTools.Method( Types.TMP_Text, "SetCharArray", new[] { typeof( char[] ) } );
  130. }
  131. static void Postfix( object __instance )
  132. {
  133. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  134. }
  135. }
  136. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  137. public static class SetCharArrayHook2
  138. {
  139. static bool Prepare( HarmonyInstance instance )
  140. {
  141. return Types.TMP_Text != null;
  142. }
  143. static MethodBase TargetMethod( HarmonyInstance instance )
  144. {
  145. return AccessTools.Method( Types.TMP_Text, "SetCharArray", new[] { typeof( char[] ), typeof( int ), typeof( int ) } );
  146. }
  147. static void Postfix( object __instance )
  148. {
  149. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  150. }
  151. }
  152. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  153. public static class SetCharArrayHook3
  154. {
  155. static bool Prepare( HarmonyInstance instance )
  156. {
  157. return Types.TMP_Text != null;
  158. }
  159. static MethodBase TargetMethod( HarmonyInstance instance )
  160. {
  161. return AccessTools.Method( Types.TMP_Text, "SetCharArray", new[] { typeof( int[] ), typeof( int ), typeof( int ) } );
  162. }
  163. static void Postfix( object __instance )
  164. {
  165. AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
  166. }
  167. }
  168. }