UGUIImageHooks.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 UnityEngine.UI;
  9. namespace XUnity.AutoTranslator.Plugin.Core.Hooks
  10. {
  11. public static class UGUIImageHooks
  12. {
  13. public static readonly Type[] All = new[] {
  14. typeof( MaskableGraphic_OnEnable_Hook ),
  15. typeof( Image_sprite_Hook ),
  16. typeof( Image_overrideSprite_Hook ),
  17. typeof( RawImage_texture_Hook ),
  18. typeof( Cursor_SetCursor_Hook ),
  19. //// fallback hooks on material (Prefix hooks)
  20. //typeof( Material_mainTexture_Hook ),
  21. };
  22. }
  23. //[Harmony]
  24. //public static class Material_mainTexture_Hook
  25. //{
  26. // static bool Prepare( HarmonyInstance instance )
  27. // {
  28. // return true;
  29. // }
  30. // static MethodBase TargetMethod( HarmonyInstance instance )
  31. // {
  32. // return AccessTools.Property( typeof( Material ), "mainTexture" ).GetSetMethod();
  33. // }
  34. // public static void Prefix( object __instance, Texture value )
  35. // {
  36. // if( value is Texture2D texture2D )
  37. // {
  38. // AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2D, true );
  39. // }
  40. // }
  41. //}
  42. [Harmony]
  43. public static class MaskableGraphic_OnEnable_Hook
  44. {
  45. static bool Prepare( HarmonyInstance instance )
  46. {
  47. return true;
  48. }
  49. static MethodBase TargetMethod( HarmonyInstance instance )
  50. {
  51. return AccessTools.Method( typeof( MaskableGraphic ), "OnEnable" );
  52. }
  53. public static void Postfix( object __instance )
  54. {
  55. if( __instance is Image || __instance is RawImage )
  56. {
  57. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance );
  58. }
  59. }
  60. }
  61. [Harmony]
  62. public static class Image_sprite_Hook
  63. {
  64. static bool Prepare( HarmonyInstance instance )
  65. {
  66. return true;
  67. }
  68. static MethodBase TargetMethod( HarmonyInstance instance )
  69. {
  70. return AccessTools.Property( typeof( Image ), "sprite" ).GetSetMethod();
  71. }
  72. public static void Postfix( object __instance )
  73. {
  74. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance );
  75. }
  76. }
  77. [Harmony]
  78. public static class Image_overrideSprite_Hook
  79. {
  80. static bool Prepare( HarmonyInstance instance )
  81. {
  82. return true;
  83. }
  84. static MethodBase TargetMethod( HarmonyInstance instance )
  85. {
  86. return AccessTools.Property( typeof( Image ), "overrideSprite" ).GetSetMethod();
  87. }
  88. public static void Postfix( object __instance )
  89. {
  90. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance );
  91. }
  92. }
  93. [Harmony]
  94. public static class RawImage_texture_Hook
  95. {
  96. static bool Prepare( HarmonyInstance instance )
  97. {
  98. return true;
  99. }
  100. static MethodBase TargetMethod( HarmonyInstance instance )
  101. {
  102. return AccessTools.Property( typeof( RawImage ), "texture" ).GetSetMethod();
  103. }
  104. public static void Postfix( object __instance )
  105. {
  106. AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance );
  107. }
  108. }
  109. [Harmony]
  110. public static class Cursor_SetCursor_Hook
  111. {
  112. static bool Prepare( HarmonyInstance instance )
  113. {
  114. return true;
  115. }
  116. static MethodBase TargetMethod( HarmonyInstance instance )
  117. {
  118. return AccessTools.Method( typeof( Cursor ), "SetCursor", new[] { typeof( Texture2D ), typeof( Vector2 ), typeof( CursorMode ) } );
  119. }
  120. public static void Postfix( Texture2D texture )
  121. {
  122. AutoTranslationPlugin.Current.Hook_ImageChanged( texture );
  123. }
  124. }
  125. }