Просмотр исходного кода

load order fix, hook override fix

Scrublord1336 6 лет назад
Родитель
Сommit
5aea004e6f

+ 2 - 0
CHANGELOG.md

@@ -2,7 +2,9 @@
  * FEATURE - General support for rich text in relation to UGUI and Utage
  * FEATURE - Experimental support for custom fonts for UGUI
  * CHANGE - Support only source languages with predefined character checks - for now (ja, zh-CN, zh-TW, ru, ko, en)
+ * CHANGE - Slightly different translation load priority from files
  * BUG FIX - Dramatically improved resize behaviour for NGUI
+ * BUG FIX - Fixed a bug where hook overrides would not always be honored depending on mod load order
  * MISC - 3 additional spam prevention checks
  * MISC - Uses BepInLogger for BepInEx implementation (requires 4.0.0+)
 

+ 48 - 34
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -33,6 +33,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
 {
    public class AutoTranslationPlugin : MonoBehaviour
    {
+      private static readonly char[][] TranslationSplitters = new char[][] { new char[] { '\t' }, new char[] { '=' } };
+
       /// <summary>
       /// Allow the instance to be accessed statically, as only one will exist.
       /// </summary>
@@ -108,6 +110,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
       private int _consecutiveSecondsTranslated = 0;
 
       private bool _changeFont = false;
+      private bool _initialized = false;
 
       public void Initialize()
       {
@@ -170,14 +173,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
          t2.Start();
       }
 
-      private string[] GetTranslationFiles()
+      private IEnumerable<string> GetTranslationFiles()
       {
-         return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ), $"*.txt", SearchOption.AllDirectories ) // FIXME: Add $"*{Language}.txt"
-            .Union( new[] { Settings.AutoTranslationsFilePath } )
-            .Select( x => x.Replace( "/", "\\" ) )
-            .Distinct()
-            .OrderBy( x => x )
-            .ToArray();
+         return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ), $"*.txt", SearchOption.AllDirectories )
+            .Select( x => x.Replace( "/", "\\" ) );
       }
 
       private void MaintenanceLoop( object state )
@@ -245,35 +244,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
             {
                Directory.CreateDirectory( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ) );
                Directory.CreateDirectory( Path.GetDirectoryName( Path.Combine( Config.Current.DataPath, Settings.OutputFile ) ) );
-               var tab = new char[] { '\t' };
-               var equals = new char[] { '=' };
-               var splitters = new char[][] { tab, equals };
 
-               foreach( var fullFileName in GetTranslationFiles() )
+               var mainTranslationFile = Settings.AutoTranslationsFilePath.Replace( "/", "\\" );
+               LoadTranslationsInFile( mainTranslationFile );
+               foreach( var fullFileName in GetTranslationFiles().Reverse().Except( new[] { mainTranslationFile } ) )
                {
-                  if( File.Exists( fullFileName ) )
-                  {
-                     string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
-                     foreach( string translation in translations )
-                     {
-                        for( int i = 0 ; i < splitters.Length ; i++ )
-                        {
-                           var splitter = splitters[ i ];
-                           string[] kvp = translation.Split( splitter, StringSplitOptions.None );
-                           if( kvp.Length >= 2 )
-                           {
-                              string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
-                              string value = TextHelper.Decode( kvp[ 1 ].TrimIfConfigured() );
-
-                              if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
-                              {
-                                 AddTranslation( key, value );
-                                 break;
-                              }
-                           }
-                        }
-                     }
-                  }
+                  LoadTranslationsInFile( fullFileName );
                }
             }
          }
@@ -283,6 +259,35 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      private void LoadTranslationsInFile( string fullFileName )
+      {
+         if( File.Exists( fullFileName ) )
+         {
+            Logger.Current.Debug( $"Loading translations from {fullFileName}." );
+
+            string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
+            foreach( string translation in translations )
+            {
+               for( int i = 0 ; i < TranslationSplitters.Length ; i++ )
+               {
+                  var splitter = TranslationSplitters[ i ];
+                  string[] kvp = translation.Split( splitter, StringSplitOptions.None );
+                  if( kvp.Length == 2 )
+                  {
+                     string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
+                     string value = TextHelper.Decode( kvp[ 1 ].TrimIfConfigured() );
+
+                     if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) && IsTranslatable( key ) )
+                     {
+                        AddTranslation( key, value );
+                        break;
+                     }
+                  }
+               }
+            }
+         }
+      }
+
       private void LoadStaticTranslations()
       {
          if( Settings.UseStaticTranslations && Settings.FromLanguage == Settings.DefaultFromLanguage && Settings.Language == Settings.DefaultLanguage )
@@ -1039,6 +1044,15 @@ namespace XUnity.AutoTranslator.Plugin.Core
          onContinue();
       }
 
+      public void Start()
+      {
+         if( !_initialized )
+         {
+            _initialized = true;
+            Initialize();
+         }
+      }
+
       public void Update()
       {
          try

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

@@ -37,7 +37,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
          }
          catch( Exception e )
          {
-            Logger.Current.Error( e, "An error occurred while setting up hooks for UGUI."  );
+            Logger.Current.Error( e, "An error occurred while setting up hooks for UGUI." );
          }
 
          try
@@ -139,6 +139,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Hooks
                                  addMethod.Invoke( component, new object[] { callback } );
                               }
 
+                              Logger.Current.Info( eventName + " was hooked by external plugin." );
                               return true;
                            }
                            catch { }

+ 0 - 1
src/XUnity.AutoTranslator.Plugin.Core/PluginLoader.cs

@@ -23,7 +23,6 @@ namespace XUnity.AutoTranslator.Plugin.Core
             var obj = new GameObject( "Auto Translator" );
             var instance = obj.AddComponent<AutoTranslationPlugin>();
             GameObject.DontDestroyOnLoad( obj );
-            instance.Initialize();
          }
       }