浏览代码

Update to 2.15.3
* BUG FIX - Potential crash during startup where potentially 'illegal' property was being read.
* MISC - Support 'scrolling text' for immediate components such as IMGUI. Previously such behaviour would shut down the plugin.
* MISC - Changed behaviour of font overriding. All found Text instances will now have their fonts changed.

randoman 6 年之前
父节点
当前提交
ef69c56ed7

+ 6 - 1
CHANGELOG.md

@@ -1,4 +1,9 @@
-### 2.15.2
+### 2.15.3
+ * BUG FIX - Potential crash during startup where potentially 'illegal' property was being read.
+ * MISC - Support 'scrolling text' for immediate components such as IMGUI. Previously such behaviour would shut down the plugin.
+ * MISC - Changed behaviour of font overriding. All found Text instances will now have their fonts changed.
+
+### 2.15.2
  * BUG FIX - Fixed bug that could cause hooking not to work when hooks were overriden by external plugin
 
 ### 2.15.1

+ 1 - 0
README.md

@@ -92,6 +92,7 @@ The following key inputs are mapped:
  * ALT + D: Dump untranslated texts (if no endpoint is configured)
  * ALT + R: Reload translation files. Useful if you change the text files on the fly.
  * ALT + U: Manual hooking. The default hooks wont always pick up texts. This will attempt to make lookups manually.
+ * ALT + F: If OverrideFont is configured, will toggle between overridden and default font.
 
 ## Installation
 The plugin can be installed in following ways:

+ 1 - 1
src/XUnity.AutoTranslator.Patcher/Patcher.cs

@@ -29,7 +29,7 @@ namespace XUnity.AutoTranslator.Patcher
       {
          get
          {
-            return "2.15.2";
+            return "2.15.3";
          }
       }
 

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.BepIn/XUnity.AutoTranslator.Plugin.BepIn.csproj

@@ -2,7 +2,7 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <Version>2.15.2</Version>
+      <Version>2.15.3</Version>
    </PropertyGroup>
 
    <ItemGroup>

+ 168 - 23
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -66,7 +66,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
       /// </summary>
       private List<string> _textsToCopyToClipboardOrdered = new List<string>();
       private HashSet<string> _textsToCopyToClipboard = new HashSet<string>();
-      private float _clipboardUpdated = Time.realtimeSinceStartup;
+      private float _clipboardUpdated = 0.0f;
 
       /// <summary>
       /// The number of http translation errors that has occurred up until now.
@@ -84,6 +84,11 @@ namespace XUnity.AutoTranslator.Plugin.Core
       /// </summary>
       private Func<string, bool> _symbolCheck;
 
+      /// <summary>
+      /// Texts currently being scheduled for translation by 'immediate' components.
+      /// </summary>
+      private HashSet<string> _immediatelyTranslating = new HashSet<string>();
+
       private object _advEngine;
       private float? _nextAdvUpdate;
 
@@ -110,7 +115,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
       private int _secondForQueuedTranslation = -1;
       private int _consecutiveSecondsTranslated = 0;
 
-      private bool _changeFont = false;
+      private bool _hasOverrideFont = false;
+      private bool _overrideFont = false;
       private bool _initialized = false;
       private bool _temporarilyDisabled = false;
 
@@ -169,8 +175,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
             }
             else
             {
-               _changeFont = true;
+               _hasOverrideFont = true;
             }
+
+            _overrideFont = _hasOverrideFont;
          }
 
          LoadTranslations();
@@ -467,8 +475,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
             if( previouslyQueuedText != null )
             {
-               if( untranslatedText.StartsWith( previouslyQueuedText ) || previouslyQueuedText.StartsWith( untranslatedText )
-                  || untranslatedText.EndsWith( previouslyQueuedText ) || previouslyQueuedText.EndsWith( untranslatedText ) )
+               if( untranslatedText.RemindsOf( previouslyQueuedText ) )
                {
                   wasProblematic = true;
                   break;
@@ -693,6 +700,21 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      public void Hook_HandleFont( object ui )
+      {
+         if( _hasOverrideFont )
+         {
+            var info = ui.GetTranslationInfo();
+            if( _overrideFont )
+            {
+               info?.ChangeFont( ui );
+            }
+            else
+            {
+               info?.UnchangeFont( ui );
+            }
+         }
+      }
 
       /// <summary>
       /// Sets the text of a UI  text, while ensuring this will not fire a text changed event.
@@ -703,7 +725,6 @@ namespace XUnity.AutoTranslator.Plugin.Core
          {
             try
             {
-               // TODO: Disable ANY Hook
                _hooksEnabled = false;
 
                if( info != null )
@@ -711,18 +732,6 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   info.IsCurrentlySettingText = true;
                }
 
-               if( _changeFont )
-               {
-                  if( isTranslated )
-                  {
-                     info?.ChangeFont( ui );
-                  }
-                  else
-                  {
-                     info?.UnchangeFont( ui );
-                  }
-               }
-
                if( Settings.EnableUIResizing )
                {
                   if( isTranslated )
@@ -1024,18 +1033,49 @@ namespace XUnity.AutoTranslator.Plugin.Core
                }
                else if( !isSpammer || ( isSpammer && IsShortText( text ) ) )
                {
-                  // Lets try not to spam a service that might not be there...
-                  if( _endpoint != null )
+                  if( context != null )
                   {
-                     if( !Settings.IsShutdown )
+                     // if there is a context, this is a part-translation, which means it is not a candidate for scrolling-in text
+                     if( _endpoint != null )
+                     {
+                        if( !Settings.IsShutdown )
+                        {
+                           // once the text has stabilized, attempt to look it up
+                           var job = GetOrCreateTranslationJobFor( ui, textKey, context );
+                        }
+                     }
+                     else
                      {
-                        var job = GetOrCreateTranslationJobFor( ui, textKey, context );
+                        QueueNewUntranslatedForDisk( textKey );
                      }
                   }
                   else
                   {
-                     QueueNewUntranslatedForDisk( textKey );
+                     StartCoroutine(
+                        WaitForTextStablization(
+                           textKey: textKey,
+                           delay: 1.0f,
+                           onTextStabilized: () =>
+                           {
+                              // Lets try not to spam a service that might not be there...
+                              if( _endpoint != null )
+                              {
+                                 // once the text has stabilized, attempt to look it up
+                                 if( !Settings.IsShutdown )
+                                 {
+                                    if( !TryGetTranslation( textKey, out translation ) )
+                                    {
+                                       var job = GetOrCreateTranslationJobFor( ui, textKey, context );
+                                    }
+                                 }
+                              }
+                              else
+                              {
+                                 QueueNewUntranslatedForDisk( textKey );
+                              }
+                           } ) );
                   }
+
                }
             }
          }
@@ -1115,6 +1155,53 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      /// <summary>
+      /// Utility method that allows me to wait to call an action, until
+      /// the text has stopped changing. This is important for 'story'
+      /// mode text, which 'scrolls' into place slowly. This version is
+      /// for global text, where the component cannot tell us if the text
+      /// has changed itself.
+      /// </summary>
+      public IEnumerator WaitForTextStablization( TranslationKey textKey, float delay, Action onTextStabilized, Action onFailed = null )
+      {
+         var text = textKey.GetDictionaryLookupKey();
+
+         if( !_immediatelyTranslating.Contains( text ) )
+         {
+            _immediatelyTranslating.Add( text );
+            try
+            {
+               yield return new WaitForSeconds( delay );
+
+               bool succeeded = true;
+               foreach( var otherImmediatelyTranslating in _immediatelyTranslating )
+               {
+                  if( text != otherImmediatelyTranslating )
+                  {
+                     if( text.RemindsOf( otherImmediatelyTranslating ) )
+                     {
+                        succeeded = false;
+                        break;
+                     }
+                  }
+               }
+
+               if( succeeded )
+               {
+                  onTextStabilized();
+               }
+               else
+               {
+                  onFailed?.Invoke();
+               }
+            }
+            finally
+            {
+               _immediatelyTranslating.Remove( text );
+            }
+         }
+      }
+
       public IEnumerator DelayForSeconds( float delay, Action onContinue )
       {
          yield return new WaitForSeconds( delay );
@@ -1179,6 +1266,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
                {
                   ToggleTranslation();
                }
+               else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.F ) )
+               {
+                  ToggleFont();
+               }
                else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.D ) )
                {
                   DumpUntranslated();
@@ -1542,6 +1633,60 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      private void ToggleFont()
+      {
+         if( _hasOverrideFont )
+         {
+            _overrideFont = !_overrideFont;
+
+            var objects = ObjectExtensions.GetAllRegisteredObjects();
+            Logger.Current.Info( $"Toggling fonts of {objects.Count} objects." );
+
+            if( _overrideFont )
+            {
+               // make sure we use the translated version of all texts
+               foreach( var kvp in objects )
+               {
+                  var ui = kvp.Key;
+                  try
+                  {
+                     if( ( ui as Component )?.gameObject?.activeSelf ?? false )
+                     {
+                        var info = (TranslationInfo)kvp.Value;
+                        info?.ChangeFont( ui );
+                     }
+                  }
+                  catch( Exception )
+                  {
+                     // not super pretty, no...
+                     ObjectExtensions.Remove( ui );
+                  }
+               }
+            }
+            else
+            {
+               // make sure we use the original version of all texts
+               foreach( var kvp in objects )
+               {
+                  var ui = kvp.Key;
+                  try
+                  {
+                     if( ( ui as Component )?.gameObject?.activeSelf ?? false )
+                     {
+                        var info = (TranslationInfo)kvp.Value;
+                        info?.UnchangeFont( ui );
+                     }
+                  }
+                  catch( Exception )
+                  {
+                     // not super pretty, no...
+                     ObjectExtensions.Remove( ui );
+                  }
+               }
+            }
+         }
+      }
+
       private void ToggleTranslation()
       {
          _isInTranslatedMode = !_isInTranslatedMode;

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.Core/Constants/PluginData.cs

@@ -11,6 +11,6 @@ namespace XUnity.AutoTranslator.Plugin.Core.Constants
 
       public const string Name = "XUnity Auto Translator";
 
-      public const string Version = "2.15.2";
+      public const string Version = "2.15.3";
    }
 }

+ 5 - 0
src/XUnity.AutoTranslator.Plugin.Core/Extensions/StringExtensions.cs

@@ -393,5 +393,10 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
          }
          return String.Empty;
       }
+
+      public static bool RemindsOf( this string that, string other )
+      {
+         return that.StartsWith( other ) || other.StartsWith( that ) || that.EndsWith( other ) || other.EndsWith( that );
+      }
    }
 }

+ 2 - 5
src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs

@@ -28,11 +28,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          {
             if( Settings.EnableUGUI || Settings.EnableUtage )
             {
-               success = SetupHook( KnownEvents.OnUnableToTranslateUGUI, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
-               if( !success )
-               {
-                  harmony.PatchAll( UGUIHooks.All );
-               }
+               UGUIHooks.HooksOverriden = SetupHook( KnownEvents.OnUnableToTranslateUGUI, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
+               harmony.PatchAll( UGUIHooks.All );
             }
          }
          catch( Exception e )

+ 12 - 2
src/XUnity.AutoTranslator.Plugin.Core/Hooks/UGUIHooks.cs

@@ -10,6 +10,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI
 {
    public static class UGUIHooks
    {
+      public static bool HooksOverriden = false;
+
       public static readonly Type[] All = new[] {
          typeof( TextPropertyHook ),
          typeof( OnEnableHook ),
@@ -32,7 +34,11 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI
 
       static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
+         if( !UGUIHooks.HooksOverriden )
+         {
+            AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
+         }
+         AutoTranslationPlugin.Current.Hook_HandleFont( __instance );
       }
    }
 
@@ -52,7 +58,11 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI
 
       static void Postfix( object __instance )
       {
-         AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
+         if( !UGUIHooks.HooksOverriden )
+         {
+            AutoTranslationPlugin.Current.Hook_TextChanged( __instance );
+         }
+         AutoTranslationPlugin.Current.Hook_HandleFont( __instance );
       }
    }
 }

+ 2 - 2
src/XUnity.AutoTranslator.Plugin.Core/TranslationInfo.cs

@@ -68,9 +68,9 @@ namespace XUnity.AutoTranslator.Plugin.Core
             var previousFont = ui.font;
             var newFont = FontCache.GetOrCreate( previousFont.fontSize );
 
-            ui.font = newFont;
-            if( _unfont == null )
+            if( newFont != previousFont )
             {
+               ui.font = newFont;
                _unfont = obj =>
                {
                   ( (Text)obj ).font = previousFont;

+ 2 - 3
src/XUnity.AutoTranslator.Plugin.Core/Web/GoogleTranslateEndpoint.cs

@@ -39,8 +39,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
       private bool _hasSetup = false;
       //private bool _hasSetupCustomUserAgent = false;
       //private string _popularUserAgent;
-      private long m = 425635;
-      private long s = 1953544246;
+      private long m = 427761;
+      private long s = 1179739010;
 
       public GoogleTranslateEndpoint()
       {
@@ -99,7 +99,6 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
             {
                yield return enumerator.Current;
             }
-
          }
       }
 

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.Core/XUnity.AutoTranslator.Plugin.Core.csproj

@@ -2,7 +2,7 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <Version>2.15.2</Version>
+      <Version>2.15.3</Version>
    </PropertyGroup>
 
    <ItemGroup>

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.IPA/XUnity.AutoTranslator.Plugin.IPA.csproj

@@ -2,7 +2,7 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <Version>2.15.2</Version>
+      <Version>2.15.3</Version>
    </PropertyGroup>
 
    <ItemGroup>

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.UnityInjector/XUnity.AutoTranslator.Plugin.UnityInjector.csproj

@@ -2,7 +2,7 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <Version>2.15.2</Version>
+      <Version>2.15.3</Version>
    </PropertyGroup>
 
    <ItemGroup>

+ 1 - 1
src/XUnity.AutoTranslator.Setup/XUnity.AutoTranslator.Setup.csproj

@@ -4,7 +4,7 @@
       <OutputType>Exe</OutputType>
       <TargetFramework>net40</TargetFramework>
       <AssemblyName>SetupReiPatcherAndAutoTranslator</AssemblyName>
-      <Version>2.15.2</Version>
+      <Version>2.15.3</Version>
    </PropertyGroup>
 
    <ItemGroup>