Browse Source

fix some hooks, fix toggle translations

randoman 6 years ago
parent
commit
aef81a1bb1

+ 21 - 15
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -1098,6 +1098,18 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      private void TranslateTexture( object ui, bool forceReload )
+      {
+         if( ui is Texture2D texture2d )
+         {
+            TranslateTexture( null, texture2d, false, forceReload );
+         }
+         else
+         {
+            TranslateTexture( ui, null, false, forceReload );
+         }
+      }
+
       private void TranslateTexture( object source, Texture2D texture, bool isPrefixHooked, bool forceReload )
       {
          try
@@ -2064,12 +2076,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
                      }
                   }
                }
-               else
+
+               if( Settings.EnableTextureTranslation )
                {
-                  if( Settings.EnableTextureTranslation )
-                  {
-                     TranslateTexture( ui, null, false, true );
-                  }
+                  TranslateTexture( ui, true );
                }
             }
             catch( Exception )
@@ -2197,12 +2207,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
                         }
                      }
                   }
-                  else
+
+                  if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
                   {
-                     if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
-                     {
-                        TranslateTexture( ui, null, false, false );
-                     }
+                     TranslateTexture( ui, false );
                   }
                }
                catch( Exception )
@@ -2231,12 +2239,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
                         }
                      }
                   }
-                  else
+
+                  if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
                   {
-                     if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
-                     {
-                        TranslateTexture( ui, null, false, false );
-                     }
+                     TranslateTexture( ui, false );
                   }
                }
                catch( Exception )

+ 2 - 0
src/XUnity.AutoTranslator.Plugin.Core/Extensions/ComponentExtensions.cs

@@ -68,6 +68,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
 
       public static Texture2D GetTexture( this object ui )
       {
+         if( ui == null ) return null;
+
          if( ui is Image image )
          {
             return image.mainTexture as Texture2D;

+ 23 - 23
src/XUnity.AutoTranslator.Plugin.Core/Hooks/NGUIImageHooks.cs

@@ -41,9 +41,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UIAtlas, "spriteMaterial" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
    
@@ -79,9 +79,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UISprite, "material" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
 
@@ -98,9 +98,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UISprite, "atlas" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
    
@@ -117,9 +117,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UITexture, "mainTexture" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
 
@@ -136,9 +136,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UITexture, "material" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
 
@@ -174,9 +174,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UI2DSprite, "sprite2D" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
 
@@ -193,24 +193,23 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          return AccessTools.Property( ClrTypes.UI2DSprite, "material" ).GetSetMethod();
       }
 
-      public static void Prefix( object __instance )
+      public static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, true );
+         AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, null, false );
       }
    }
 
-
    [Harmony]
-   public static class UIFont_material_Hook
+   public static class UIPanel_clipTexture_Hook
    {
       static bool Prepare( HarmonyInstance instance )
       {
-         return ClrTypes.UIFont != null;
+         return ClrTypes.UIPanel != null;
       }
 
       static MethodBase TargetMethod( HarmonyInstance instance )
       {
-         return AccessTools.Property( ClrTypes.UIFont, "material" ).GetSetMethod();
+         return AccessTools.Property( ClrTypes.UIPanel, "clipTexture" ).GetSetMethod();
       }
 
       public static void Postfix( object __instance )
@@ -219,8 +218,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
       }
    }
 
+
    [Harmony]
-   public static class UIFont_dynamicFont_Hook
+   public static class UIFont_material_Hook
    {
       static bool Prepare( HarmonyInstance instance )
       {
@@ -229,7 +229,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
 
       static MethodBase TargetMethod( HarmonyInstance instance )
       {
-         return AccessTools.Property( ClrTypes.UIFont, "dynamicFont" ).GetSetMethod();
+         return AccessTools.Property( ClrTypes.UIFont, "material" ).GetSetMethod();
       }
 
       public static void Postfix( object __instance )
@@ -239,16 +239,16 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
    }
 
    [Harmony]
-   public static class UIPanel_clipTexture_Hook
+   public static class UIFont_dynamicFont_Hook
    {
       static bool Prepare( HarmonyInstance instance )
       {
-         return ClrTypes.UIPanel != null;
+         return ClrTypes.UIFont != null;
       }
 
       static MethodBase TargetMethod( HarmonyInstance instance )
       {
-         return AccessTools.Property( ClrTypes.UIPanel, "clipTexture" ).GetSetMethod();
+         return AccessTools.Property( ClrTypes.UIFont, "dynamicFont" ).GetSetMethod();
       }
 
       public static void Postfix( object __instance )

+ 29 - 6
src/XUnity.AutoTranslator.Plugin.Core/Hooks/UGUIImageHooks.cs

@@ -15,6 +15,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          typeof( MaskableGraphic_OnEnable_Hook ),
          typeof( Image_sprite_Hook ),
          typeof( Image_overrideSprite_Hook ),
+         typeof( Image_material_Hook ),
          typeof( RawImage_texture_Hook ),
          typeof( Cursor_SetCursor_Hook ),
 
@@ -27,9 +28,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
    {
       public static void Prefix( object __instance, object value )
       {
-         if( value is Texture2D texture2D )
+         if( value is Texture2D texture2d )
          {
-            AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2D, true );
+            AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2d, true );
          }
       }
    }
@@ -49,9 +50,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
 
       public static void Prefix( object __instance, Texture value )
       {
-         if( value is Texture2D texture2D )
+         if( value is Texture2D texture2d )
          {
-            AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2D, true );
+            AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2d, true );
          }
       }
    }
@@ -117,7 +118,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
    }
 
    [Harmony]
-   public static class RawImage_texture_Hook
+   public static class Image_material_Hook
    {
       static bool Prepare( HarmonyInstance instance )
       {
@@ -126,7 +127,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
 
       static MethodBase TargetMethod( HarmonyInstance instance )
       {
-         return AccessTools.Property( typeof( RawImage ), "texture" ).GetSetMethod();
+         return AccessTools.Property( typeof( Image ), "material" ).GetSetMethod();
       }
 
       public static void Postfix( object __instance )
@@ -135,6 +136,28 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
       }
    }
 
+   [Harmony]
+   public static class RawImage_texture_Hook
+   {
+      static bool Prepare( HarmonyInstance instance )
+      {
+         return true;
+      }
+
+      static MethodBase TargetMethod( HarmonyInstance instance )
+      {
+         return AccessTools.Property( typeof( RawImage ), "texture" ).GetSetMethod();
+      }
+
+      public static void Prefix( object __instance, Texture value )
+      {
+         if( value is Texture2D texture2d )
+         {
+            AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, texture2d, true );
+         }
+      }
+   }
+
    [Harmony]
    public static class Cursor_SetCursor_Hook
    {