ImageHooks.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Harmony;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using XUnity.AutoTranslator.Plugin.Core.Constants;
  11. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  12. namespace XUnity.AutoTranslator.Plugin.Core.Hooks
  13. {
  14. internal static class ImageHooks
  15. {
  16. public static readonly Type[] All = new[] {
  17. typeof( MaskableGraphic_OnEnable_Hook ),
  18. typeof( Image_sprite_Hook ),
  19. typeof( Image_overrideSprite_Hook ),
  20. typeof( Image_material_Hook ),
  21. typeof( RawImage_texture_Hook ),
  22. typeof( Cursor_SetCursor_Hook ),
  23. typeof( SpriteRenderer_sprite_Hook ),
  24. typeof( Sprite_texture_Hook ),
  25. // fallback hooks on material (Prefix hooks)
  26. typeof( Material_mainTexture_Hook ),
  27. // Live2D
  28. typeof( CubismRenderer_MainTexture_Hook ),
  29. typeof( CubismRenderer_TryInitialize_Hook ),
  30. // NGUI
  31. typeof( UIAtlas_spriteMaterial_Hook ),
  32. typeof( UISprite_OnInit_Hook ),
  33. typeof( UISprite_material_Hook ),
  34. typeof( UISprite_atlas_Hook ),
  35. typeof( UI2DSprite_sprite2D_Hook ),
  36. typeof( UI2DSprite_material_Hook ),
  37. typeof( UITexture_mainTexture_Hook ),
  38. typeof( UITexture_material_Hook ),
  39. typeof( UIPanel_clipTexture_Hook ),
  40. typeof( UIRect_OnInit_Hook ),
  41. // Utage
  42. typeof( DicingTextures_GetTexture_Hook ),
  43. };
  44. }
  45. [Harmony]
  46. internal static class DicingTextures_GetTexture_Hook
  47. {
  48. static bool Prepare( HarmonyInstance instance )
  49. {
  50. return ClrTypes.DicingTextures != null;
  51. }
  52. static MethodBase TargetMethod( HarmonyInstance instance )
  53. {
  54. return AccessTools.Method( ClrTypes.DicingTextures, "GetTexture", new[] { typeof( string ) } );
  55. }
  56. public static void Postfix( object __instance, Texture2D __result )
  57. {
  58. AutoTranslationPlugin.Current.Hook_ImageChanged( __result, false );
  59. }
  60. }
  61. [Harmony]
  62. internal static class Sprite_texture_Hook
  63. {
  64. static bool Prepare( HarmonyInstance instance )
  65. {
  66. return ClrTypes.Sprite != null;
  67. }
  68. static MethodBase TargetMethod( HarmonyInstance instance )
  69. {
  70. return AccessTools.Property( ClrTypes.Sprite, "texture" )?.GetGetMethod();
  71. }
  72. static void Postfix( Texture2D __result )
  73. {
  74. AutoTranslationPlugin.Current.Hook_ImageChanged( __result, true );
  75. }
  76. static bool RequireRuntimeHooker => true;
  77. }
  78. [Harmony]
  79. internal static class SpriteRenderer_sprite_Hook
  80. {
  81. static bool Prepare( HarmonyInstance instance )
  82. {
  83. return ClrTypes.SpriteRenderer != null;
  84. }
  85. static MethodBase TargetMethod( HarmonyInstance instance )
  86. {
  87. return AccessTools.Property( ClrTypes.SpriteRenderer, "sprite" )?.GetSetMethod();
  88. }
  89. public static void Postfix( object __instance )
  90. {
  91. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  92. }
  93. }
  94. [Harmony]
  95. internal static class CubismRenderer_MainTexture_Hook
  96. {
  97. static bool Prepare( HarmonyInstance instance )
  98. {
  99. return ClrTypes.CubismRenderer != null;
  100. }
  101. static MethodBase TargetMethod( HarmonyInstance instance )
  102. {
  103. return AccessTools.Property( ClrTypes.CubismRenderer, "MainTexture" )?.GetSetMethod();
  104. }
  105. public static void Prefix( object __instance, Texture2D value )
  106. {
  107. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, value, true , false);
  108. }
  109. }
  110. [Harmony]
  111. internal static class CubismRenderer_TryInitialize_Hook
  112. {
  113. static bool Prepare( HarmonyInstance instance )
  114. {
  115. return ClrTypes.CubismRenderer != null;
  116. }
  117. static MethodBase TargetMethod( HarmonyInstance instance )
  118. {
  119. return AccessTools.Method( ClrTypes.CubismRenderer, "TryInitialize" );
  120. }
  121. public static void Prefix( object __instance )
  122. {
  123. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true, true );
  124. }
  125. }
  126. [Harmony]
  127. internal static class Material_mainTexture_Hook
  128. {
  129. static bool Prepare( HarmonyInstance instance )
  130. {
  131. return true;
  132. }
  133. static MethodBase TargetMethod( HarmonyInstance instance )
  134. {
  135. return AccessTools.Property( typeof( Material ), "mainTexture" )?.GetSetMethod();
  136. }
  137. public static void Prefix( object __instance, Texture value )
  138. {
  139. if( value is Texture2D texture2d )
  140. {
  141. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2d, true, false );
  142. }
  143. }
  144. }
  145. [Harmony]
  146. internal static class MaskableGraphic_OnEnable_Hook
  147. {
  148. static bool Prepare( HarmonyInstance instance )
  149. {
  150. return true;
  151. }
  152. static MethodBase TargetMethod( HarmonyInstance instance )
  153. {
  154. return AccessTools.Method( typeof( MaskableGraphic ), "OnEnable" );
  155. }
  156. public static void Postfix( object __instance )
  157. {
  158. if( __instance is Image || __instance is RawImage )
  159. {
  160. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, true );
  161. }
  162. }
  163. }
  164. [Harmony]
  165. internal static class Image_sprite_Hook
  166. {
  167. static bool Prepare( HarmonyInstance instance )
  168. {
  169. return true;
  170. }
  171. static MethodBase TargetMethod( HarmonyInstance instance )
  172. {
  173. return AccessTools.Property( typeof( Image ), "sprite" )?.GetSetMethod();
  174. }
  175. public static void Postfix( object __instance )
  176. {
  177. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  178. }
  179. }
  180. [Harmony]
  181. internal static class Image_overrideSprite_Hook
  182. {
  183. static bool Prepare( HarmonyInstance instance )
  184. {
  185. return true;
  186. }
  187. static MethodBase TargetMethod( HarmonyInstance instance )
  188. {
  189. return AccessTools.Property( typeof( Image ), "overrideSprite" )?.GetSetMethod();
  190. }
  191. public static void Postfix( object __instance )
  192. {
  193. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  194. }
  195. }
  196. [Harmony]
  197. internal static class Image_material_Hook
  198. {
  199. static bool Prepare( HarmonyInstance instance )
  200. {
  201. return true;
  202. }
  203. static MethodBase TargetMethod( HarmonyInstance instance )
  204. {
  205. return AccessTools.Property( typeof( Image ), "material" )?.GetSetMethod();
  206. }
  207. public static void Postfix( object __instance )
  208. {
  209. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  210. }
  211. }
  212. [Harmony]
  213. internal static class RawImage_texture_Hook
  214. {
  215. static bool Prepare( HarmonyInstance instance )
  216. {
  217. return true;
  218. }
  219. static MethodBase TargetMethod( HarmonyInstance instance )
  220. {
  221. return AccessTools.Property( typeof( RawImage ), "texture" )?.GetSetMethod();
  222. }
  223. public static void Prefix( object __instance, Texture value )
  224. {
  225. if( value is Texture2D texture2d )
  226. {
  227. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2d, true, false );
  228. }
  229. }
  230. }
  231. [Harmony]
  232. internal static class Cursor_SetCursor_Hook
  233. {
  234. static bool Prepare( HarmonyInstance instance )
  235. {
  236. return true;
  237. }
  238. static MethodBase TargetMethod( HarmonyInstance instance )
  239. {
  240. return AccessTools.Method( typeof( Cursor ), "SetCursor", new[] { typeof( Texture2D ), typeof( Vector2 ), typeof( CursorMode ) } );
  241. }
  242. public static void Prefix( Texture2D texture )
  243. {
  244. AutoTranslationPlugin.Current.Hook_ImageChanged( texture, true );
  245. }
  246. }
  247. [Harmony]
  248. internal static class UIAtlas_spriteMaterial_Hook
  249. {
  250. static bool Prepare( HarmonyInstance instance )
  251. {
  252. return ClrTypes.UIAtlas != null;
  253. }
  254. static MethodBase TargetMethod( HarmonyInstance instance )
  255. {
  256. return AccessTools.Property( ClrTypes.UIAtlas, "spriteMaterial" )?.GetSetMethod();
  257. }
  258. public static void Postfix( object __instance )
  259. {
  260. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  261. }
  262. }
  263. [Harmony]
  264. internal static class UISprite_OnInit_Hook
  265. {
  266. static bool Prepare( HarmonyInstance instance )
  267. {
  268. return ClrTypes.UISprite != null;
  269. }
  270. static MethodBase TargetMethod( HarmonyInstance instance )
  271. {
  272. return AccessTools.Method( Constants.ClrTypes.UISprite, "OnInit" );
  273. }
  274. public static void Postfix( object __instance )
  275. {
  276. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, true );
  277. }
  278. }
  279. [Harmony]
  280. internal static class UISprite_material_Hook
  281. {
  282. static bool Prepare( HarmonyInstance instance )
  283. {
  284. return ClrTypes.UISprite != null;
  285. }
  286. static MethodBase TargetMethod( HarmonyInstance instance )
  287. {
  288. return AccessTools.Property( ClrTypes.UISprite, "material" )?.GetSetMethod();
  289. }
  290. public static void Postfix( object __instance )
  291. {
  292. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  293. }
  294. }
  295. [Harmony]
  296. internal static class UISprite_atlas_Hook
  297. {
  298. static bool Prepare( HarmonyInstance instance )
  299. {
  300. return ClrTypes.UISprite != null;
  301. }
  302. static MethodBase TargetMethod( HarmonyInstance instance )
  303. {
  304. return AccessTools.Property( ClrTypes.UISprite, "atlas" )?.GetSetMethod();
  305. }
  306. public static void Postfix( object __instance )
  307. {
  308. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  309. }
  310. }
  311. [Harmony]
  312. internal static class UITexture_mainTexture_Hook
  313. {
  314. static bool Prepare( HarmonyInstance instance )
  315. {
  316. return ClrTypes.UITexture != null;
  317. }
  318. static MethodBase TargetMethod( HarmonyInstance instance )
  319. {
  320. return AccessTools.Property( ClrTypes.UITexture, "mainTexture" )?.GetSetMethod();
  321. }
  322. public static void Postfix( object __instance )
  323. {
  324. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  325. }
  326. }
  327. [Harmony]
  328. internal static class UITexture_material_Hook
  329. {
  330. static bool Prepare( HarmonyInstance instance )
  331. {
  332. return ClrTypes.UITexture != null;
  333. }
  334. static MethodBase TargetMethod( HarmonyInstance instance )
  335. {
  336. return AccessTools.Property( ClrTypes.UITexture, "material" )?.GetSetMethod();
  337. }
  338. public static void Postfix( object __instance )
  339. {
  340. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  341. }
  342. }
  343. [Harmony]
  344. internal static class UIRect_OnInit_Hook
  345. {
  346. static bool Prepare( HarmonyInstance instance )
  347. {
  348. return ClrTypes.UIRect != null;
  349. }
  350. static MethodBase TargetMethod( HarmonyInstance instance )
  351. {
  352. return AccessTools.Method( ClrTypes.UIRect, "OnInit" );
  353. }
  354. public static void Postfix( object __instance )
  355. {
  356. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, true );
  357. }
  358. }
  359. [Harmony]
  360. internal static class UI2DSprite_sprite2D_Hook
  361. {
  362. static bool Prepare( HarmonyInstance instance )
  363. {
  364. return ClrTypes.UI2DSprite != null;
  365. }
  366. static MethodBase TargetMethod( HarmonyInstance instance )
  367. {
  368. return AccessTools.Property( ClrTypes.UI2DSprite, "sprite2D" )?.GetSetMethod();
  369. }
  370. public static void Postfix( object __instance )
  371. {
  372. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  373. }
  374. }
  375. [Harmony]
  376. internal static class UI2DSprite_material_Hook
  377. {
  378. static bool Prepare( HarmonyInstance instance )
  379. {
  380. return ClrTypes.UI2DSprite != null;
  381. }
  382. static MethodBase TargetMethod( HarmonyInstance instance )
  383. {
  384. return AccessTools.Property( ClrTypes.UI2DSprite, "material" )?.GetSetMethod();
  385. }
  386. public static void Postfix( object __instance )
  387. {
  388. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  389. }
  390. }
  391. [Harmony]
  392. internal static class UIPanel_clipTexture_Hook
  393. {
  394. static bool Prepare( HarmonyInstance instance )
  395. {
  396. return ClrTypes.UIPanel != null;
  397. }
  398. static MethodBase TargetMethod( HarmonyInstance instance )
  399. {
  400. return AccessTools.Property( ClrTypes.UIPanel, "clipTexture" )?.GetSetMethod();
  401. }
  402. public static void Postfix( object __instance )
  403. {
  404. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  405. }
  406. }
  407. [Harmony]
  408. internal static class UIFont_material_Hook
  409. {
  410. static bool Prepare( HarmonyInstance instance )
  411. {
  412. return ClrTypes.UIFont != null;
  413. }
  414. static MethodBase TargetMethod( HarmonyInstance instance )
  415. {
  416. return AccessTools.Property( ClrTypes.UIFont, "material" )?.GetSetMethod();
  417. }
  418. public static void Postfix( object __instance )
  419. {
  420. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  421. }
  422. }
  423. [Harmony]
  424. internal static class UIFont_dynamicFont_Hook
  425. {
  426. static bool Prepare( HarmonyInstance instance )
  427. {
  428. return ClrTypes.UIFont != null;
  429. }
  430. static MethodBase TargetMethod( HarmonyInstance instance )
  431. {
  432. return AccessTools.Property( ClrTypes.UIFont, "dynamicFont" )?.GetSetMethod();
  433. }
  434. public static void Postfix( object __instance )
  435. {
  436. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  437. }
  438. }
  439. [Harmony]
  440. internal static class UILabel_bitmapFont_Hook
  441. {
  442. static bool Prepare( HarmonyInstance instance )
  443. {
  444. return ClrTypes.UILabel != null;
  445. }
  446. static MethodBase TargetMethod( HarmonyInstance instance )
  447. {
  448. return AccessTools.Property( ClrTypes.UILabel, "bitmapFont" )?.GetSetMethod();
  449. }
  450. public static void Postfix( object __instance )
  451. {
  452. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  453. }
  454. }
  455. [Harmony]
  456. internal static class UILabel_trueTypeFont_Hook
  457. {
  458. static bool Prepare( HarmonyInstance instance )
  459. {
  460. return ClrTypes.UILabel != null;
  461. }
  462. static MethodBase TargetMethod( HarmonyInstance instance )
  463. {
  464. return AccessTools.Property( ClrTypes.UILabel, "trueTypeFont" )?.GetSetMethod();
  465. }
  466. public static void Postfix( object __instance )
  467. {
  468. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false, false );
  469. }
  470. }
  471. }