Sfoglia il codice sorgente

respect bepin logger config, fixed issue with ancient unity engine Closes #1

Scrublord1336 6 anni fa
parent
commit
d6551b3977

+ 5 - 0
src/XUnity.AutoTranslator.Plugin.BepIn/BepInLogger.cs

@@ -8,6 +8,11 @@ namespace XUnity.AutoTranslator.Plugin.BepIn
 {
    public class BepInLogger : Logger
    {
+      public BepInLogger()
+      {
+         RespectSettings = false;
+      }
+
       protected override void Log( LogLevel level, string message )
       {
          BepInEx.Logger.CurrentLogger.Log( Convert( level ), "[XUnity.AutoTranslator] " + message );

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

@@ -6,6 +6,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Constants
 {
    public static class Types
    {
+      public static readonly Type TextEditor = FindType( "UnityEngine.TextEditor" );
+      public static readonly Type CustomYieldInstruction = FindType( "UnityEngine.CustomYieldInstruction" );
+
       public static readonly Type TMP_InputField = FindType( "TMPro.TMP_InputField" );
       public static readonly Type TMP_Text = FindType( "TMPro.TMP_Text" );
       public static readonly Type TextMeshProUGUI = FindType( "TMPro.TextMeshProUGUI" );

+ 13 - 2
src/XUnity.AutoTranslator.Plugin.Core/Features.cs

@@ -2,7 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using UnityEngine;
+using XUnity.AutoTranslator.Plugin.Core.Constants;
 
 namespace XUnity.AutoTranslator.Plugin.Core
 {
@@ -10,11 +10,22 @@ namespace XUnity.AutoTranslator.Plugin.Core
    {
       public static readonly bool SupportsClipboard = false;
 
+      public static readonly bool SupportsCustomYieldInstruction = false;
+
       static Features()
       {
          try
          {
-            SupportsClipboard = typeof( TextEditor )?.GetProperty( "text" )?.GetSetMethod() != null;
+            SupportsClipboard = Types.TextEditor?.GetProperty( "text" )?.GetSetMethod() != null;
+         }
+         catch( Exception )
+         {
+            
+         }
+
+         try
+         {
+            SupportsCustomYieldInstruction = Types.CustomYieldInstruction != null;
          }
          catch( Exception )
          {

+ 9 - 2
src/XUnity.AutoTranslator.Plugin.Core/Logger.cs

@@ -10,6 +10,13 @@ namespace XUnity.AutoTranslator.Plugin.Core
    {
       public static Logger Current;
 
+      public Logger()
+      {
+         RespectSettings = true;
+      }
+
+      public bool RespectSettings { get; protected set; }
+
       public void Error( Exception e, string message )
       {
          Log( LogLevel.Error, message + Environment.NewLine + e );
@@ -42,7 +49,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Debug( Exception e, string message )
       {
-         if( Settings.EnableDebugLogs )
+         if( Settings.EnableDebugLogs || !RespectSettings )
          {
             Log( LogLevel.Debug, message + Environment.NewLine + e );
          }
@@ -50,7 +57,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Debug( string message )
       {
-         if( Settings.EnableDebugLogs )
+         if( Settings.EnableDebugLogs || !RespectSettings )
          {
             Log( LogLevel.Debug, message );
          }

+ 38 - 0
src/XUnity.AutoTranslator.Plugin.Core/Shim/CustomYieldInstructionShim.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace XUnity.AutoTranslator.Plugin.Core.Shim
+{
+   public abstract class CustomYieldInstructionShim : IEnumerator
+   {
+      // Methods
+      protected CustomYieldInstructionShim()
+      {
+      }
+
+      public bool MoveNext()
+      {
+         return keepWaiting;
+      }
+
+      public void Reset()
+      {
+      }
+
+      // Properties
+      public object Current
+      {
+         get
+         {
+            return null;
+         }
+      }
+
+      public abstract bool keepWaiting { get; }
+   }
+
+
+}

+ 14 - 6
src/XUnity.AutoTranslator.Plugin.Core/Web/GoogleTranslateEndpoint.cs

@@ -85,8 +85,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
          //   var enumerator = SetupDynamicUserAgent();
          //   while( enumerator.MoveNext() )
          //   {
-         //      var current = enumerator.Current;
-         //      if( current != null ) yield return current;
+         //      yield return enumerator.Current;
          //   }
          //}
 
@@ -98,8 +97,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
             var enumerator = SetupTKK();
             while( enumerator.MoveNext() )
             {
-               var current = enumerator.Current;
-               if( current != null ) yield return current;
+               yield return enumerator.Current;
             }
 
          }
@@ -183,10 +181,20 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
          if( downloadResult != null )
          {
-            yield return downloadResult;
+            if( Features.SupportsCustomYieldInstruction )
+            {
+               yield return downloadResult;
+            }
+            else
+            {
+               while( !downloadResult.IsCompleted )
+               {
+                  yield return new WaitForSeconds( 0.2f );
+               }
+            }
 
             error = downloadResult.Error;
-            if( downloadResult.Succeeded )
+            if( downloadResult.Succeeded && downloadResult.Result != null )
             {
                try
                {

+ 19 - 8
src/XUnity.AutoTranslator.Plugin.Core/Web/KnownHttpEndpoint.cs

@@ -2,6 +2,7 @@
 using System.Collections;
 using System.Net;
 using System.Threading;
+using UnityEngine;
 using XUnity.AutoTranslator.Plugin.Core.Configuration;
 
 namespace XUnity.AutoTranslator.Plugin.Core.Web
@@ -55,7 +56,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
                }
             }
             Logger.Current.Debug( "Starting translation for: " + untranslatedText );
-            DownloadResult result = null;
+            DownloadResult downloadResult = null;
             try
             {
                var client = GetClient();
@@ -65,11 +66,11 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
                if( request != null )
                {
-                  result = client.GetDownloadResult( new Uri( url ), request );
+                  downloadResult = client.GetDownloadResult( new Uri( url ), request );
                }
                else
                {
-                  result = client.GetDownloadResult( new Uri( url ) );
+                  downloadResult = client.GetDownloadResult( new Uri( url ) );
                }
             }
             catch( Exception e )
@@ -77,15 +78,25 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
                Logger.Current.Error( e, "Error occurred while setting up translation request." );
             }
 
-            if( result != null )
+            if( downloadResult != null )
             {
-               yield return result;
+               if( Features.SupportsCustomYieldInstruction )
+               {
+                  yield return downloadResult;
+               }
+               else
+               {
+                  while( !downloadResult.IsCompleted )
+                  {
+                     yield return new WaitForSeconds( 0.2f );
+                  }
+               }
 
                try
                {
-                  if( result.Succeeded )
+                  if( downloadResult.Succeeded && downloadResult.Result != null )
                   {
-                     if( TryExtractTranslated( result.Result, out var translatedText ) )
+                     if( TryExtractTranslated( downloadResult.Result, out var translatedText ) )
                      {
                         Logger.Current.Debug( $"Translation for '{untranslatedText}' succeded. Result: {translatedText}" );
 
@@ -100,7 +111,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
                   }
                   else
                   {
-                     Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + result.Error );
+                     Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + downloadResult.Error );
                      failure();
                   }
                }

+ 4 - 1
src/XUnity.AutoTranslator.Plugin.Core/Web/UnityWebClient.cs

@@ -9,6 +9,7 @@ using System.Reflection;
 using System.Text;
 using Harmony;
 using UnityEngine;
+using XUnity.AutoTranslator.Plugin.Core.Shim;
 
 namespace XUnity.AutoTranslator.Plugin.Core.Web
 {
@@ -128,7 +129,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
       }
    }
 
-   public class DownloadResult : CustomYieldInstruction
+   public class DownloadResult : CustomYieldInstructionShim
    {
       private bool _isCompleted = false;
 
@@ -145,6 +146,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public string Error { get; set; }
 
+      public bool IsCompleted => _isCompleted;
+
       public bool Succeeded => Error == null;
    }
 }