Bläddra i källkod

frame check implementation

Scrublord1336 6 år sedan
förälder
incheckning
6e88572a13

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

@@ -97,6 +97,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       private int _availableBatchOperations = Settings.MaxAvailableBatchOperations;
       private float _batchOperationSecondCounter = 0;
+      private int _frameForLastQueuedTranslation = -1;
+      private int _consecutiveFramesQueued = 0;
 
       public void Initialize()
       {
@@ -315,11 +317,47 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
          _unstartedJobs.Add( lookupKey, ongoingJob );
 
+         CheckConsecutiveFrames();
          CheckThresholds();
 
          return ongoingJob;
       }
 
+      public void CheckConsecutiveFrames()
+      {
+         var currentFrame = Time.frameCount;
+         var lastFrame = currentFrame - 1;
+
+         if( lastFrame == _frameForLastQueuedTranslation )
+         {
+            // we also queued something last frame, lets increment our counter
+            _consecutiveFramesQueued++;
+
+            if( _consecutiveFramesQueued > Settings.MaximumConcurrentFrameTranslations )
+            {
+               // Shutdown, this wont be tolerated!!!
+               _unstartedJobs.Clear();
+               _completedJobs.Clear();
+               _ongoingJobs.Clear();
+
+               Settings.IsShutdown = true;
+               Logger.Current.Error( $"SPAM DETECTED: Translations were queued every frame for more than {Settings.MaximumConcurrentFrameTranslations} consecutive frames. Shutting down plugin." );
+            }
+
+         }
+         else if( currentFrame == _frameForLastQueuedTranslation )
+         {
+            // do nothing, there may be multiple translations per frame, that wont increase this counter
+         }
+         else
+         {
+            // but if multiple Update frames has passed, we will reset the counter
+            _consecutiveFramesQueued = 0;
+         }
+
+         _frameForLastQueuedTranslation = currentFrame;
+      }
+
       private void CheckThresholds()
       {
          if( _unstartedJobs.Count > Settings.MaxUnstartedJobs )

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

@@ -18,6 +18,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static readonly int MaxUnstartedJobs = 3500;
       public static readonly float IncreaseBatchOperationsEvery = 30;
       public static readonly bool EnableObjectTracking = true;
+      public static readonly int MaximumConcurrentFrameTranslations = 60;
 
       public static bool IsShutdown = false;
       public static int TranslationCount = 0;