IMGUIHooks.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 UnityEngine;
  8. using static UnityEngine.GUI;
  9. namespace XUnity.AutoTranslator.Plugin.Core.Hooks.IMGUI
  10. {
  11. internal static class IMGUIHooks
  12. {
  13. public static bool HooksOverriden = false;
  14. public static readonly Type[] All = new[] {
  15. typeof( GUI_BeginGroup_Hook ),
  16. typeof( GUI_Box_Hook ),
  17. typeof( GUI_DoRepeatButton_Hook ),
  18. typeof( GUI_DoLabel_Hook ),
  19. typeof( GUI_DoButton_Hook ),
  20. typeof( GUI_DoModalWindow_Hook ),
  21. typeof( GUI_DoWindow_Hook ),
  22. typeof( GUI_DoTextField_Hook ),
  23. typeof( GUI_DoButtonGrid_Hook ),
  24. typeof( GUI_DoToggle_Hook ),
  25. };
  26. }
  27. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  28. internal static class GUI_BeginGroup_Hook
  29. {
  30. static bool Prepare( HarmonyInstance instance )
  31. {
  32. return Constants.ClrTypes.GUI != null;
  33. }
  34. static MethodBase TargetMethod( HarmonyInstance instance )
  35. {
  36. return AccessTools.Method( Constants.ClrTypes.GUI, "BeginGroup", new[] { typeof( Rect ), typeof( GUIContent ), typeof( GUIStyle ) } );
  37. }
  38. static void Prefix( GUIContent content )
  39. {
  40. if( !IMGUIHooks.HooksOverriden )
  41. {
  42. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  43. }
  44. }
  45. }
  46. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  47. internal static class GUI_Box_Hook
  48. {
  49. static bool Prepare( HarmonyInstance instance )
  50. {
  51. return Constants.ClrTypes.GUI != null;
  52. }
  53. static MethodBase TargetMethod( HarmonyInstance instance )
  54. {
  55. return AccessTools.Method( Constants.ClrTypes.GUI, "Box", new[] { typeof( Rect ), typeof( GUIContent ), typeof( GUIStyle ) } );
  56. }
  57. static void Prefix( GUIContent content )
  58. {
  59. if( !IMGUIHooks.HooksOverriden )
  60. {
  61. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  62. }
  63. }
  64. }
  65. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  66. internal static class GUI_DoRepeatButton_Hook
  67. {
  68. static bool Prepare( HarmonyInstance instance )
  69. {
  70. return Constants.ClrTypes.GUI != null;
  71. }
  72. static MethodBase TargetMethod( HarmonyInstance instance )
  73. {
  74. return AccessTools.Method( Constants.ClrTypes.GUI, "DoRepeatButton", new[] { typeof( Rect ), typeof( GUIContent ), typeof( GUIStyle ), typeof( FocusType ) } );
  75. }
  76. static void Prefix( GUIContent content )
  77. {
  78. if( !IMGUIHooks.HooksOverriden )
  79. {
  80. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  81. }
  82. }
  83. }
  84. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  85. internal static class GUI_DoLabel_Hook
  86. {
  87. static bool Prepare( HarmonyInstance instance )
  88. {
  89. return Constants.ClrTypes.GUI != null;
  90. }
  91. static MethodBase TargetMethod( HarmonyInstance instance )
  92. {
  93. return AccessTools.Method( Constants.ClrTypes.GUI, "DoLabel", new[] { typeof( Rect ), typeof( GUIContent ), typeof( IntPtr ) } );
  94. }
  95. static void Prefix( GUIContent content )
  96. {
  97. if( !IMGUIHooks.HooksOverriden )
  98. {
  99. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  100. }
  101. }
  102. }
  103. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  104. internal static class GUI_DoButton_Hook
  105. {
  106. static bool Prepare( HarmonyInstance instance )
  107. {
  108. return Constants.ClrTypes.GUI != null;
  109. }
  110. static MethodBase TargetMethod( HarmonyInstance instance )
  111. {
  112. return AccessTools.Method( Constants.ClrTypes.GUI, "DoButton", new[] { typeof( Rect ), typeof( GUIContent ), typeof( IntPtr ) } );
  113. }
  114. static void Prefix( GUIContent content )
  115. {
  116. if( !IMGUIHooks.HooksOverriden )
  117. {
  118. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  119. }
  120. }
  121. }
  122. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  123. internal static class GUI_DoModalWindow_Hook
  124. {
  125. static bool Prepare( HarmonyInstance instance )
  126. {
  127. return Constants.ClrTypes.GUI != null;
  128. }
  129. static MethodBase TargetMethod( HarmonyInstance instance )
  130. {
  131. return AccessTools.Method( Constants.ClrTypes.GUI, "DoModalWindow", new[] { typeof( int ), typeof( Rect ), typeof( WindowFunction ), typeof( GUIContent ), typeof( GUIStyle ), typeof( GUISkin ) } );
  132. }
  133. static void Prefix( GUIContent content )
  134. {
  135. if( !IMGUIHooks.HooksOverriden )
  136. {
  137. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  138. }
  139. }
  140. }
  141. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  142. internal static class GUI_DoWindow_Hook
  143. {
  144. static bool Prepare( HarmonyInstance instance )
  145. {
  146. return Constants.ClrTypes.GUI != null;
  147. }
  148. static MethodBase TargetMethod( HarmonyInstance instance )
  149. {
  150. return AccessTools.Method( Constants.ClrTypes.GUI, "DoWindow", new[] { typeof( int ), typeof( Rect ), typeof( WindowFunction ), typeof( GUIContent ), typeof( GUIStyle ), typeof( GUISkin ), typeof( bool ) } );
  151. }
  152. static void Prefix( GUIContent title )
  153. {
  154. if( !IMGUIHooks.HooksOverriden )
  155. {
  156. AutoTranslationPlugin.Current.Hook_TextChanged( title, false );
  157. }
  158. }
  159. }
  160. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  161. internal static class GUI_DoButtonGrid_Hook
  162. {
  163. static bool Prepare( HarmonyInstance instance )
  164. {
  165. return Constants.ClrTypes.GUI != null;
  166. }
  167. static MethodBase TargetMethod( HarmonyInstance instance )
  168. {
  169. return AccessTools.Method( Constants.ClrTypes.GUI, "DoButtonGrid", new[] { typeof( Rect ), typeof( int ), typeof( GUIContent[] ), typeof( int ), typeof( GUIStyle ), typeof( GUIStyle ), typeof( GUIStyle ), typeof( GUIStyle ) } );
  170. }
  171. static void Prefix( GUIContent[] contents )
  172. {
  173. if( !IMGUIHooks.HooksOverriden )
  174. {
  175. foreach( var content in contents )
  176. {
  177. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  178. }
  179. }
  180. }
  181. }
  182. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  183. internal static class GUI_DoTextField_Hook
  184. {
  185. static bool Prepare( HarmonyInstance instance )
  186. {
  187. return Constants.ClrTypes.GUI != null;
  188. }
  189. static MethodBase TargetMethod( HarmonyInstance instance )
  190. {
  191. return AccessTools.Method( Constants.ClrTypes.GUI, "DoTextField", new[] { typeof( Rect ), typeof( int ), typeof( GUIContent ), typeof( bool ), typeof( int ), typeof( GUIStyle ), typeof( string ) } );
  192. }
  193. static void Prefix( GUIContent content )
  194. {
  195. if( !IMGUIHooks.HooksOverriden )
  196. {
  197. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  198. }
  199. }
  200. }
  201. [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
  202. internal static class GUI_DoToggle_Hook
  203. {
  204. static bool Prepare( HarmonyInstance instance )
  205. {
  206. return Constants.ClrTypes.GUI != null;
  207. }
  208. static MethodBase TargetMethod( HarmonyInstance instance )
  209. {
  210. return AccessTools.Method( Constants.ClrTypes.GUI, "DoToggle", new[] { typeof( Rect ), typeof( int ), typeof( bool ), typeof( GUIContent ), typeof( IntPtr ) } );
  211. }
  212. static void Prefix( GUIContent content )
  213. {
  214. if( !IMGUIHooks.HooksOverriden )
  215. {
  216. AutoTranslationPlugin.Current.Hook_TextChanged( content, false );
  217. }
  218. }
  219. }
  220. }