Преглед изворни кода

bumped version, more anti-spam safeguards

gravydevsupreme пре 7 година
родитељ
комит
f8eed715a7

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

@@ -2,9 +2,9 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <AssemblyVersion>2.4.1.0</AssemblyVersion>
-      <FileVersion>2.4.1.0</FileVersion>
-      <Version>2.4.1</Version>
+      <AssemblyVersion>2.5.0.0</AssemblyVersion>
+      <FileVersion>2.5.0.0</FileVersion>
+      <Version>2.5.0</Version>
    </PropertyGroup>
 
    <ItemGroup>

+ 68 - 6
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -77,8 +77,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
       /// </summary>
       private Func<string, bool> _symbolCheck;
 
+      private int[] _currentTranslationsQueuedPerSecond = new int[ Settings.TranslationQueueWatchWindow ];
+      private float? _timeExceededThreshold;
+
       private bool _isInTranslatedMode = true;
       private bool _hooksEnabled = true;
+      private bool _forceShutdown = false;
 
       public void Initialize()
       {
@@ -231,9 +235,63 @@ namespace XUnity.AutoTranslator.Plugin.Core
          job = new TranslationJob( key );
          _unstartedJobs.Add( key.ForcedRelevantKey, job );
 
+         AddToThresholdTimeAndCheck();
+
          return job;
       }
 
+      private void AddToThresholdTimeAndCheck()
+      {
+         var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
+         var newIdx = ( (int)Time.time ) % Settings.TranslationQueueWatchWindow;
+         if( previousIdx != newIdx )
+         {
+            _currentTranslationsQueuedPerSecond[ newIdx ] = 0;
+         }
+         _currentTranslationsQueuedPerSecond[ newIdx ]++;
+
+         var translationsInWindow = _currentTranslationsQueuedPerSecond.Sum();
+         var translationsPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
+         if( translationsPerSecond > Settings.MaxTranslationsQueuedPerSecond )
+         {
+            // ABOVE THRESHOLD, remember time
+            if( !_timeExceededThreshold.HasValue )
+            {
+               _timeExceededThreshold = Time.time;
+            }
+
+            if( Time.time - _timeExceededThreshold.Value > Settings.MaxSecondsAboveTranslationThreshold )
+            {
+               _unstartedJobs.Clear();
+               _forceShutdown = true;
+
+               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: Shutting down... spam detected." );
+            }
+         }
+         else
+         {
+            _timeExceededThreshold = null;
+         }
+      }
+
+      private void ResetThresholdTimerIfRequired()
+      {
+         var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
+         var newIdx = ( (int)Time.time ) % Settings.TranslationQueueWatchWindow;
+         if( previousIdx != newIdx )
+         {
+            _currentTranslationsQueuedPerSecond[ newIdx ] = 0;
+         }
+
+         var translationsInWindow = _currentTranslationsQueuedPerSecond.Sum();
+         var translationsPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
+
+         if( translationsPerSecond <= Settings.MaxTranslationsQueuedPerSecond )
+         {
+            _timeExceededThreshold = null;
+         }
+      }
+
       private void AddTranslation( TranslationKeys key, string value )
       {
          _translations[ key.OriginalKey ] = value;
@@ -279,7 +337,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       private string Override_TextChanged( object ui, string text )
       {
-         if( _hooksEnabled )
+         if( _hooksEnabled && !_forceShutdown )
          {
             return TranslateOrQueueWebJob( ui, text, true );
          }
@@ -288,7 +346,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Hook_TextChanged( object ui )
       {
-         if( _hooksEnabled )
+         if( _hooksEnabled && !_forceShutdown )
          {
             TranslateOrQueueWebJob( ui, null, false );
          }
@@ -296,7 +354,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Hook_TextInitialized( object ui )
       {
-         if( _hooksEnabled )
+         if( _hooksEnabled && !_forceShutdown )
          {
             TranslateOrQueueWebJob( ui, null, true );
          }
@@ -522,7 +580,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                }
                else
                {
-                  if( !_startedOperationsForNonStabilizableComponents.Contains( text ) )
+                  if( !_startedOperationsForNonStabilizableComponents.Contains( text ) && !text.ContainsNumbers() )
                   {
                      _startedOperationsForNonStabilizableComponents.Add( text );
 
@@ -587,9 +645,13 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public void Update()
       {
+         if( _forceShutdown ) return;
+
          try
          {
             CopyToClipboard();
+            ResetThresholdTimerIfRequired();
+
             KickoffTranslations();
             FinishTranslations();
 
@@ -801,8 +863,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       private void CopyToClipboard()
       {
-         if( Settings.CopyToClipboard 
-            && _textsToCopyToClipboardOrdered.Count > 0 
+         if( Settings.CopyToClipboard
+            && _textsToCopyToClipboardOrdered.Count > 0
             && Time.realtimeSinceStartup - _clipboardUpdated > Settings.ClipboardDebounceTime )
          {
             try

+ 10 - 6
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -11,9 +11,13 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
    {
       // cannot be changed
       public static readonly int MaxErrors = 5;
-      public static readonly int MaxConcurrentTranslations = 5;
+      public static readonly int MaxConcurrentTranslations = 3;
       public static readonly TimeSpan WebClientLifetime = TimeSpan.FromSeconds( 20 );
-      public static readonly float ClipboardDebounceTime = 0.5f;
+      public static readonly float ClipboardDebounceTime = 1f;
+
+      public static readonly float MaxTranslationsQueuedPerSecond = 6;
+      public static readonly int MaxSecondsAboveTranslationThreshold = 10;
+      public static readonly int TranslationQueueWatchWindow = 5;
       
       // can be changed
       public static string ServiceEndpoint;
@@ -25,7 +29,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static int MaxCharactersPerTranslation;
       public static bool EnablePrintHierarchy;
       public static string AutoTranslationsFilePath;
-      public static bool EnableIMGUI = false;
+      public static bool EnableIMGUI;
       public static bool EnableUGUI;
       public static bool EnableNGUI;
       public static bool EnableTextMeshPro;
@@ -69,7 +73,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          TranslationDirectory = Config.Current.Preferences[ "Files" ][ "Directory" ].GetOrDefault( @"Translation" );
          OutputFile = Config.Current.Preferences[ "Files" ][ "OutputFile" ].GetOrDefault( @"Translation\_AutoGeneratedTranslations.{lang}.txt" );
 
-         //EnableIMGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableIMGUI" ].GetOrDefault( false );
+         EnableIMGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableIMGUI" ].GetOrDefault( false );
          EnableUGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableUGUI" ].GetOrDefault( true );
          EnableNGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableNGUI" ].GetOrDefault( true );
          EnableTextMeshPro = Config.Current.Preferences[ "TextFrameworks" ][ "EnableTextMeshPro" ].GetOrDefault( true );
@@ -80,8 +84,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          IgnoreWhitespaceInDialogue = Config.Current.Preferences[ "Behaviour" ][ "IgnoreWhitespaceInDialogue" ].GetOrDefault( true );
          MinDialogueChars = Config.Current.Preferences[ "Behaviour" ][ "MinDialogueChars" ].GetOrDefault( 20 );
          ForceSplitTextAfterCharacters = Config.Current.Preferences[ "Behaviour" ][ "ForceSplitTextAfterCharacters" ].GetOrDefault( 0 );
-         //CopyToClipboard = Config.Current.Preferences[ "Behaviour" ][ "CopyToClipboard" ].GetOrDefault( false );
-         //MaxClipboardCopyCharacters = Config.Current.Preferences[ "Behaviour" ][ "MaxClipboardCopyCharacters" ].GetOrDefault( 450 );
+         CopyToClipboard = Config.Current.Preferences[ "Behaviour" ][ "CopyToClipboard" ].GetOrDefault( false );
+         MaxClipboardCopyCharacters = Config.Current.Preferences[ "Behaviour" ][ "MaxClipboardCopyCharacters" ].GetOrDefault( 450 );
 
          BaiduAppId = Config.Current.Preferences[ "Baidu" ][ "BaiduAppId" ].GetOrDefault( "" );
          BaiduAppSecret = Config.Current.Preferences[ "Baidu" ][ "BaiduAppSecret" ].GetOrDefault( "" );

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

@@ -9,6 +9,30 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
 {
    public static class StringExtensions
    {
+      private static readonly HashSet<char> Numbers = new HashSet<char>
+      {
+         '0',
+         '1',
+         '2',
+         '3',
+         '4',
+         '5',
+         '6',
+         '7',
+         '8',
+         '9',
+         '0',
+         '1',
+         '2',
+         '3',
+         '4',
+         '5',
+         '6',
+         '7',
+         '8',
+         '9'
+      };
+
       public static string ChangeToSingleLineForDialogue( this string that )
       {
          if( that.Length > Settings.MinDialogueChars ) // long strings often indicate dialog
@@ -55,6 +79,18 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
          return text.Replace( "\n", "" ).Replace( "\r", "" ).Replace( " ", "" ).Replace( " ", "" );
       }
 
+      public static bool ContainsNumbers( this string text )
+      {
+         foreach( var c in text )
+         {
+            if( Numbers.Contains( c ) )
+            {
+               return true;
+            }
+         }
+         return false;
+      }
+
       public static string UnescapeJson( this string str )
       {
          if( str == null ) return null;

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

@@ -56,7 +56,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
          try
          {
             ServicePoint = ServicePointManager.FindServicePoint( new Uri( "http://api.fanyi.baidu.com" ) );
-            ServicePoint.ConnectionLimit = 5;
+            ServicePoint.ConnectionLimit = Settings.MaxConcurrentTranslations;
          }
          catch
          {

+ 2 - 1
src/XUnity.AutoTranslator.Plugin.Core/Web/DefaultEndpoint.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Net;
 using UnityEngine;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
 
 namespace XUnity.AutoTranslator.Plugin.Core.Web
 {
@@ -28,7 +29,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
          try
          {
             ServicePoint = ServicePointManager.FindServicePoint( new Uri( Identifier ) );
-            ServicePoint.ConnectionLimit = 100;
+            ServicePoint.ConnectionLimit = Settings.MaxConcurrentTranslations;
          }
          catch
          {

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

@@ -48,7 +48,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Web
             //};
 
             ServicePoint = ServicePointManager.FindServicePoint( new Uri( "http://translate.googleapis.com" ) );
-            ServicePoint.ConnectionLimit = 5;
+            ServicePoint.ConnectionLimit = Settings.MaxConcurrentTranslations;
             
          }
          catch

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

@@ -2,9 +2,9 @@
 
   <PropertyGroup>
     <TargetFramework>net35</TargetFramework>
-    <AssemblyVersion>2.4.1.0</AssemblyVersion>
-    <FileVersion>2.4.1.0</FileVersion>
-    <Version>2.4.1</Version>
+    <AssemblyVersion>2.5.0.0</AssemblyVersion>
+    <FileVersion>2.5.0.0</FileVersion>
+    <Version>2.5.0</Version>
   </PropertyGroup>
 
   <ItemGroup>

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

@@ -2,9 +2,9 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <AssemblyVersion>2.4.1.0</AssemblyVersion>
-      <FileVersion>2.4.1.0</FileVersion>
-      <Version>2.4.1</Version>
+      <AssemblyVersion>2.5.0.0</AssemblyVersion>
+      <FileVersion>2.5.0.0</FileVersion>
+      <Version>2.5.0</Version>
    </PropertyGroup>
 
    <ItemGroup>