Scrublord1336 6 éve
szülő
commit
725f9af643

+ 40 - 11
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -25,6 +25,7 @@ using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
 using UnityEngine.SceneManagement;
 using XUnity.AutoTranslator.Plugin.Core.Constants;
 using XUnity.AutoTranslator.Plugin.Core.Debugging;
+using Harmony;
 
 namespace XUnity.AutoTranslator.Plugin.Core
 {
@@ -78,6 +79,9 @@ namespace XUnity.AutoTranslator.Plugin.Core
       /// </summary>
       private Func<string, bool> _symbolCheck;
 
+      private object _advEngine;
+      private float? _nextAdvUpdate;
+
       private IKnownEndpoint _endpoint;
 
       private int[] _currentTranslationsQueuedPerSecondRollingWindow = new int[ Settings.TranslationQueueWatchWindow ];
@@ -181,7 +185,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
          catch( Exception e )
          {
-            Logger.Current.Error( e, "An error occurred while saving translations to disk."  );
+            Logger.Current.Error( e, "An error occurred while saving translations to disk." );
          }
       }
 
@@ -234,7 +238,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
-      private TranslationJob GetOrCreateTranslationJobFor( TranslationKeys key )
+      private TranslationJob GetOrCreateTranslationJobFor( object ui, TranslationKeys key )
       {
          if( _unstartedJobs.TryGetValue( key.GetDictionaryLookupKey(), out TranslationJob job ) )
          {
@@ -252,6 +256,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
          Logger.Current.Debug( "Queued translation for: " + key.GetDictionaryLookupKey() );
 
          job = new TranslationJob( key );
+         job.OriginalSources.Add( ui );
+
          _unstartedJobs.Add( key.GetDictionaryLookupKey(), job );
 
          CheckThresholds();
@@ -364,7 +370,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          return _translations.TryGetValue( key.GetDictionaryLookupKey(), out value );
       }
 
-      private string Override_TextChanged( object ui, string text )
+      public string Override_TextChanged( object ui, string text )
       {
          if( _hooksEnabled )
          {
@@ -491,7 +497,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
             return null;
          }
 
-         var supportsStabilization = SupportsStabilization( ui );
+         var supportsStabilization = ui.SupportsStabilization();
          if( Settings.Delay == 0 || !supportsStabilization )
          {
             return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization );
@@ -598,7 +604,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                                     {
                                        if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
                                        {
-                                          var job = GetOrCreateTranslationJobFor( stabilizedTextKey );
+                                          var job = GetOrCreateTranslationJobFor( ui, stabilizedTextKey );
                                           job.Components.Add( ui );
                                        }
                                     }
@@ -629,7 +635,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                      {
                         if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
                         {
-                           GetOrCreateTranslationJobFor( textKey );
+                           GetOrCreateTranslationJobFor( ui, textKey );
                         }
                      }
                      else
@@ -644,11 +650,6 @@ namespace XUnity.AutoTranslator.Plugin.Core
          return null;
       }
 
-      public bool SupportsStabilization( object ui )
-      {
-         return !( ui is GUIContent );
-      }
-
       /// <summary>
       /// Utility method that allows me to wait to call an action, until
       /// the text has stopped changing. This is important for 'story'
@@ -700,6 +701,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
                ResetThresholdTimerIfRequired();
                KickoffTranslations();
                FinishTranslations();
+
+               if( _nextAdvUpdate.HasValue && Time.time > _nextAdvUpdate )
+               {
+                  _nextAdvUpdate = null;
+                  UpdateUtageText();
+               }
             }
 
             if( Input.anyKey )
@@ -826,11 +833,33 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   }
                }
 
+               //Logger.Current.Debug( "FINISH: " + job.TranslatedText + ", " + string.Join( ", ", job.OriginalSources.Select( x => x.GetType().Name ).ToArray() ) );
+
+               // Utage support
+               if( Constants.Types.AdvCommand != null && Constants.Types.AdvEngine != null
+                  && job.OriginalSources.Any( x => Constants.Types.AdvCommand.IsAssignableFrom( x.GetType() ) ) )
+               {
+                  _nextAdvUpdate = Time.time + 0.5f;
+               }
+
                AddTranslation( job.Keys, job.TranslatedText );
             }
          }
       }
 
+      private void UpdateUtageText()
+      {
+         if( _advEngine == null )
+         {
+            _advEngine = GameObject.FindObjectOfType( Constants.Types.AdvEngine );
+         }
+
+         if( _advEngine != null )
+         {
+            AccessTools.Method( Constants.Types.AdvEngine, "ChangeLanguage" )?.Invoke( _advEngine, new object[ 0 ] );
+         }
+      }
+
       private void ReloadTranslations()
       {
          LoadTranslations();

+ 6 - 0
src/XUnity.AutoTranslator.Plugin.Core/Constants/Types.cs

@@ -22,6 +22,12 @@ namespace XUnity.AutoTranslator.Plugin.Core.Constants
 
       public static readonly Type UguiNovelText = FindType( "Utage.UguiNovelText" );
 
+      public static readonly Type AdvCommand = FindType( "Utage.AdvCommand" );
+
+      public static readonly Type AdvEngine = FindType( "Utage.AdvEngine" );
+
+      public static readonly Type AdvUguiSelection = FindType( "Utage.AdvUguiSelection" );
+
       private static Type FindType( string name )
       {
          return AppDomain.CurrentDomain.GetAssemblies()

+ 8 - 5
src/XUnity.AutoTranslator.Plugin.Core/Extensions/ComponentExtensions.cs

@@ -16,7 +16,12 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
          string text = null;
          var type = ui.GetType();
 
-         if( ui is Text )
+         if( type == Constants.Types.UguiNovelText && ( (Component)ui ).gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.AdvUguiSelection ) != null )
+         {
+            // these texts are handled by AdvCommand, unless it is a selection
+            text = ( (Text)ui ).text;
+         }
+         else if( ui is Text )
          {
             text = ( (Text)ui ).text;
          }
@@ -37,12 +42,10 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
       {
          var type = ui.GetType();
 
-         if( type == Constants.Types.UguiNovelText )
+         if( type == Constants.Types.UguiNovelText && ( ( Component ) ui ).gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.AdvUguiSelection ) != null )
          {
-            Logger.Current.Info( "Setting NovelText: " + text );
-            ( (Text)ui ).text = string.Empty;
+            // these texts are handled by AdvCommand, unless it is a selection
             ( (Text)ui ).text = text;
-            type.GetProperty( "LengthOfView" )?.GetSetMethod()?.Invoke( ui, new object[] { text.Length } );
          }
          else if( ui is Text )
          {

+ 7 - 1
src/XUnity.AutoTranslator.Plugin.Core/Extensions/ObjectExtensions.cs

@@ -12,10 +12,16 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
    {
       private static readonly object Sync = new object();
       private static readonly WeakDictionary<object, object> DynamicFields = new WeakDictionary<object, object>();
+      
+      public static bool SupportsStabilization( this object ui )
+      {
+         var type = ui.GetType();
+         return !( ui is GUIContent || Constants.Types.AdvCommand.IsAssignableFrom( type ) ) ;
+      }
 
       public static TranslationInfo GetTranslationInfo( this object obj, bool isAwakening )
       {
-         if( obj is GUIContent ) return null;
+         if( !obj.SupportsStabilization() ) return null;
 
          var info = obj.Get<TranslationInfo>();
 

+ 16 - 0
src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs

@@ -94,6 +94,22 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          {
             Logger.Current.Error( e, "An error occurred while setting up hooks for IMGUI." );
          }
+
+         try
+         {
+            if( true )
+            {
+               success = SetupHook( "NANIDESU", defaultHook );
+               if( !success )
+               {
+                  harmony.PatchAll( UtageHooks.All );
+               }
+            }
+         }
+         catch( Exception e )
+         {
+            Logger.Current.Error( e, "An error occurred while setting up hooks for Utage." );
+         }
       }
 
       public static bool SetupHook( string eventName, Func<object, string, string> callback )

+ 39 - 0
src/XUnity.AutoTranslator.Plugin.Core/Hooks/UtageHooks.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using Harmony;
+
+namespace XUnity.AutoTranslator.Plugin.Core.Hooks
+{
+   public static class UtageHooks
+   {
+      public static readonly Type[] All = new[] {
+         typeof( AdvCommand_ParseCellLocalizedTextHook )
+      };
+   }
+
+   [Harmony, HarmonyAfter( Constants.KnownPlugins.DynamicTranslationLoader )]
+   public static class AdvCommand_ParseCellLocalizedTextHook
+   {
+      static bool Prepare( HarmonyInstance instance )
+      {
+         return Constants.Types.AdvCommand != null;
+      }
+
+      static MethodBase TargetMethod( HarmonyInstance instance )
+      {
+         return AccessTools.Method( Constants.Types.AdvCommand, "ParseCellLocalizedText", new Type[] { } );
+      }
+
+      static void Postfix( object __instance, ref string __result )
+      {
+         var result = AutoTranslationPlugin.Current.Override_TextChanged( __instance, __result );
+         if( !string.IsNullOrEmpty( result ) )
+         {
+            __result = result;
+         }
+      }
+   }
+}

+ 3 - 0
src/XUnity.AutoTranslator.Plugin.Core/TranslationJob.cs

@@ -15,9 +15,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
          Keys = key;
 
          Components = new List<object>();
+         OriginalSources = new HashSet<object>();
       }
 
       public List<object> Components { get; private set; }
+
+      public HashSet<object> OriginalSources { get; private set; }
       
       public TranslationKeys Keys { get; private set; }