Kaynağa Gözat

dynamic headers and fix to error handling

Scrublord1336 6 yıl önce
ebeveyn
işleme
280a5e0a86

+ 16 - 30
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -938,7 +938,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                                     // Lets try not to spam a service that might not be there...
                                     if( _endpoint != null )
                                     {
-                                       if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
+                                       if( !Settings.IsShutdown )
                                        {
                                           var job = GetOrCreateTranslationJobFor( ui, stabilizedTextKey, context );
                                           job.Components.Add( ui );
@@ -963,7 +963,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   // Lets try not to spam a service that might not be there...
                   if( _endpoint != null )
                   {
-                     if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
+                     if( !Settings.IsShutdown )
                      {
                         var job = GetOrCreateTranslationJobFor( ui, textKey, context );
                      }
@@ -1276,6 +1276,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       private void OnTranslationFailed( TranslationJob job )
       {
+         Settings.TranslationCount++; // counts as a translation
          _consecutiveErrors++;
 
          job.State = TranslationJobState.Failed;
@@ -1283,28 +1284,21 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
          if( !Settings.IsShutdown )
          {
-            if( _consecutiveErrors > Settings.MaxErrors )
+            if( _consecutiveErrors >= Settings.MaxErrors )
             {
-               if( _endpoint.ShouldGetSecondChanceAfterFailure() )
-               {
-                  Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
-                  _consecutiveErrors = Settings.MaxErrors;
-               }
-               else
-               {
-                  Settings.IsShutdown = true;
-                  Logger.Current.Error( $"More than {Settings.MaxErrors} consecutive errors occurred. Shutting down plugin." );
+               Settings.IsShutdown = true;
+               Logger.Current.Error( $"{Settings.MaxErrors} or more consecutive errors occurred. Shutting down plugin." );
 
-                  _unstartedJobs.Clear();
-                  _completedJobs.Clear();
-                  _ongoingJobs.Clear();
-               }
+               _unstartedJobs.Clear();
+               _completedJobs.Clear();
+               _ongoingJobs.Clear();
             }
          }
       }
 
       private void OnTranslationFailed( TranslationBatch batch )
       {
+         Settings.TranslationCount++; // counts as a translation
          _consecutiveErrors++;
 
          foreach( var tracker in batch.Trackers )
@@ -1315,22 +1309,14 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
          if( !Settings.IsShutdown )
          {
-            if( _consecutiveErrors > Settings.MaxErrors )
+            if( _consecutiveErrors >= Settings.MaxErrors )
             {
-               if( _endpoint.ShouldGetSecondChanceAfterFailure() )
-               {
-                  Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
-                  _consecutiveErrors = Settings.MaxErrors;
-               }
-               else
-               {
-                  Settings.IsShutdown = true;
-                  Logger.Current.Error( $"More than {Settings.MaxErrors} consecutive errors occurred. Shutting down plugin." );
+               Settings.IsShutdown = true;
+               Logger.Current.Error( $"{Settings.MaxErrors} or more consecutive errors occurred. Shutting down plugin." );
 
-                  _unstartedJobs.Clear();
-                  _completedJobs.Clear();
-                  _ongoingJobs.Clear();
-               }
+               _unstartedJobs.Clear();
+               _completedJobs.Clear();
+               _ongoingJobs.Clear();
             }
          }
       }

+ 0 - 7
src/XUnity.AutoTranslator.Plugin.Core/IKnownEndpoint.cs

@@ -19,13 +19,6 @@ namespace XUnity.AutoTranslator.Plugin.Core
       /// </summary>
       bool IsBusy { get; }
 
-      /// <summary>
-      /// Called before plugin shutdown and can return true to prevent plugin shutdown, if the plugin
-      /// can provide a secondary strategy for translation.
-      /// </summary>
-      /// <returns></returns>
-      bool ShouldGetSecondChanceAfterFailure();
-
       /// <summary>
       /// "Update" game loop method.
       /// </summary>

+ 29 - 24
src/XUnity.AutoTranslator.Plugin.Core/Web/GoogleTranslateEndpoint.cs

@@ -20,13 +20,22 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
    {
       //protected static readonly ConstructorInfo WwwConstructor = Constants.Types.WWW?.GetConstructor( new[] { typeof( string ), typeof( byte[] ), typeof( Dictionary<string, string> ) } );
       private static readonly string HttpsServicePointTemplateUrl = "https://translate.googleapis.com/translate_a/single?client=t&dt=t&sl={0}&tl={1}&ie=UTF-8&oe=UTF-8&tk={2}&q={3}";
-      private static readonly string FallbackHttpsServicePointTemplateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}";
       private static readonly string HttpsTranslateUserSite = "https://translate.google.com";
       private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
       private static readonly string UserAgentRepository = "https://techblog.willshouse.com/2012/01/03/most-common-user-agents/";
+      private static readonly System.Random RandomNumbers = new System.Random();
+
+      private static readonly string[] Accepts = new string[] { null, "*/*", "application/json" };
+      private static readonly string[] AcceptLanguages = new string[] { null, "en-US,en;q=0.9", "en-US,en", "en-US", "en" };
+      private static readonly string[] Referers = new string[] { null, "https://translate.google.com/" };
+      private static readonly string[] AcceptCharsets = new string[] { null, Encoding.UTF8.WebName };
+
+      private static readonly string Accept = Accepts[ RandomNumbers.Next( Accepts.Length ) ];
+      private static readonly string AcceptLanguage = AcceptLanguages[ RandomNumbers.Next( AcceptLanguages.Length ) ];
+      private static readonly string Referer = Referers[ RandomNumbers.Next( Referers.Length ) ];
+      private static readonly string AcceptCharset = AcceptCharsets[ RandomNumbers.Next( AcceptCharsets.Length ) ];
 
       private CookieContainer _cookieContainer;
-      private bool _hasFallenBack = false;
       private bool _hasSetup = false;
       private bool _hasSetupCustomUserAgent = false;
       //private string _popularUserAgent;
@@ -47,9 +56,23 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
       public override void ApplyHeaders( WebHeaderCollection headers )
       {
          headers[ HttpRequestHeader.UserAgent ] = Settings.GetUserAgent( DefaultUserAgent );
-         headers[ HttpRequestHeader.AcceptLanguage ] = "en-US,en;q=0.9";
-         headers[ HttpRequestHeader.Accept ] = "*/*";
-         headers[ HttpRequestHeader.Referer ] = "https://translate.google.com/";
+
+         if( AcceptLanguage != null )
+         {
+            headers[ HttpRequestHeader.AcceptLanguage ] = AcceptLanguage;
+         }
+         if( Accept != null )
+         {
+            headers[ HttpRequestHeader.Accept ] = Accept;
+         }
+         if( Referer != null )
+         {
+            headers[ HttpRequestHeader.Referer ] = Referer;
+         }
+         if( AcceptCharset != null )
+         {
+            headers[ HttpRequestHeader.AcceptCharset ] = AcceptCharset;
+         }
       }
 
       public override IEnumerator OnBeforeTranslate( int translationCount )
@@ -292,25 +315,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
 
       public override string GetServiceUrl( string untranslatedText, string from, string to )
       {
-         if( _hasFallenBack )
-         {
-            return string.Format( FallbackHttpsServicePointTemplateUrl, from, to, WWW.EscapeURL( untranslatedText ) );
-         }
-         else
-         {
-            return string.Format( HttpsServicePointTemplateUrl, from, to, Tk( untranslatedText ), WWW.EscapeURL( untranslatedText ) );
-         }
-      }
-
-      public override bool ShouldGetSecondChanceAfterFailure()
-      {
-         if( !_hasFallenBack )
-         {
-            _hasFallenBack = true;
-            return true;
-         }
-
-         return false;
+         return string.Format( HttpsServicePointTemplateUrl, from, to, Tk( untranslatedText ), WWW.EscapeURL( untranslatedText ) );
       }
 
       public override void WriteCookies( HttpWebResponse response )