private IEnumerator ScheduleFileWriting() { yield return(CoroutineHelper.CreateWaitForSeconds(1)); _currentScheduledTask = null; ThreadPool.QueueUserWorkItem(SaveNewTranslationsToDisk); }
private IEnumerator EnableBatchingAfterDelay() { yield return(CoroutineHelper.CreateWaitForSeconds(60f)); HasBatchLogicFailed = false; XuaLogger.AutoTranslator.Info("Re-enabled batching."); }
/// <summary> /// Gets an enumerator that can be iterated through /// in a co-routine and will work in even ancient versions /// of Unity. /// </summary> /// <returns></returns> public IEnumerator GetSupportedEnumerator() { if (UnityFeatures.SupportsCustomYieldInstruction) // requires Unity 5.3 or later { yield return(this); } else { while (DetermineKeepWaiting()) { yield return(CoroutineHelper.CreateWaitForSeconds(0.2f)); } } }
public IEnumerator Translate(UntranslatedTextInfo[] untranslatedTextInfos, string from, string to, Action <string[]> success, Action <string, Exception> failure) { var startTime = Time.realtimeSinceStartup; var context = new TranslationContext(untranslatedTextInfos, from, to, success, failure); _ongoingTranslations++; //if( untranslatedTextInfos.Length > 0 && untranslatedTextInfos.Length <= 2 ) //{ // var maxLength = untranslatedTextInfos.Max( x => x.UntranslatedText.Length ); // var untranslatedTextInfo = untranslatedTextInfos.First( x => x.UntranslatedText.Length == maxLength ); // if( untranslatedTextInfo.ContextBefore.Count == 0 ) // { // foreach( var priorTranslation in _priorTranslations ) // { // if( startTime - priorTranslation.Time < MaxPriorTranslationAge ) // { // untranslatedTextInfo.ContextBefore.Add( priorTranslation.UntranslatedText ); // } // } // var parts = NewlineSplitter.Split( untranslatedTextInfo.UntranslatedText ); // foreach( var part in parts ) // { // var isGarbage = NewlineSplitter.IsMatch( part ); // if( !isGarbage ) // { // _priorTranslations.Add( new PriorTranslation { Time = startTime, UntranslatedText = part } ); ; // } // } // while( _priorTranslations.Count > MaxPriorTranslations ) // { // _priorTranslations.RemoveAt( 0 ); // } // } //} try { if (Settings.SimulateDelayedError) { yield return(CoroutineHelper.CreateWaitForSeconds(1f)); context.FailWithoutThrowing("Simulating delayed error. Press CTRL+ALT+NP8 to disable!", null); } else if (Settings.SimulateError) { context.FailWithoutThrowing("Simulating error. Press CTRL+ALT+NP9 to disable!", null); } else { bool ok = false; var iterator = Endpoint.Translate(context); if (iterator != null) { TryMe : try { ok = iterator.MoveNext(); // check for timeout var now = Time.realtimeSinceStartup; if (now - startTime > Settings.Timeout) { ok = false; context.FailWithoutThrowing($"Timeout occurred during translation (took more than {Settings.Timeout} seconds)", null); } } catch (TranslationContextException) { ok = false; } catch (Exception e) { ok = false; context.FailWithoutThrowing("Error occurred during translation.", e); } if (ok) { yield return(iterator.Current); goto TryMe; } } } } finally { _ongoingTranslations--; context.FailIfNotCompleted(); } }