12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using ExIni;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Globalization;
- using XUnity.AutoTranslator.Plugin.Core.Extensions;
- using UnityEngine.EventSystems;
- using XUnity.AutoTranslator.Plugin.Core.Configuration;
- using XUnity.AutoTranslator.Plugin.Core.Utilities;
- using XUnity.AutoTranslator.Plugin.Core.Web;
- using XUnity.AutoTranslator.Plugin.Core.Hooks;
- using XUnity.AutoTranslator.Plugin.Core.Hooks.TextMeshPro;
- using XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI;
- using XUnity.AutoTranslator.Plugin.Core.IMGUI;
- using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
- using UnityEngine.SceneManagement;
- using XUnity.AutoTranslator.Plugin.Core.Constants;
- using XUnity.AutoTranslator.Plugin.Core.Debugging;
- using XUnity.AutoTranslator.Plugin.Core.Batching;
- namespace XUnity.AutoTranslator.Plugin.Core
- {
- public class AutoTranslationPlugin : MonoBehaviour
- {
- /// <summary>
- /// Allow the instance to be accessed statically, as only one will exist.
- /// </summary>
- public static AutoTranslationPlugin Current;
- /// <summary>
- /// These are the currently running translation jobs (being translated by an http request).
- /// </summary>
- private List<TranslationJob> _completedJobs = new List<TranslationJob>();
- private Dictionary<string, TranslationJob> _unstartedJobs = new Dictionary<string, TranslationJob>();
- /// <summary>
- /// All the translations are stored in this dictionary.
- /// </summary>
- private Dictionary<string, string> _translations = new Dictionary<string, string>();
- /// <summary>
- /// These are the new translations that has not yet been persisted to the file system.
- /// </summary>
- private object _writeToFileSync = new object();
- private Dictionary<string, string> _newTranslations = new Dictionary<string, string>();
- private HashSet<string> _newUntranslated = new HashSet<string>();
- private HashSet<string> _translatedTexts = new HashSet<string>();
- /// <summary>
- /// Keeps track of things to copy to clipboard.
- /// </summary>
- private List<string> _textsToCopyToClipboardOrdered = new List<string>();
- private HashSet<string> _textsToCopyToClipboard = new HashSet<string>();
- private float _clipboardUpdated = Time.realtimeSinceStartup;
- /// <summary>
- /// The number of http translation errors that has occurred up until now.
- /// </summary>
- private int _consecutiveErrors = 0;
- /// <summary>
- /// This is a hash set that contains all Text components that is currently being worked on by
- /// the translation plugin.
- /// </summary>
- private HashSet<object> _ongoingOperations = new HashSet<object>();
- private HashSet<string> _startedOperationsForNonStabilizableComponents = new HashSet<string>();
- /// <summary>
- /// This function will check if there are symbols of a given language contained in a string.
- /// </summary>
- private Func<string, bool> _symbolCheck;
- private IKnownEndpoint _endpoint;
- private int[] _currentTranslationsQueuedPerSecondRollingWindow = new int[ Settings.TranslationQueueWatchWindow ];
- private float? _timeExceededThreshold;
- private float _translationsQueuedPerSecond;
- private bool _isInTranslatedMode = true;
- private bool _hooksEnabled = true;
- private bool _batchLogicHasFailed = false;
- public void Initialize()
- {
- Current = this;
- Logger.Current = new ConsoleLogger();
- Settings.Configure();
- if( Settings.EnableConsole ) DebugConsole.Enable();
- HooksSetup.InstallHooks( Override_TextChanged );
- try
- {
- _endpoint = KnownEndpoints.FindEndpoint( Settings.ServiceEndpoint );
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An unexpected error occurred during initialization of endpoint." );
- }
- _symbolCheck = TextHelper.GetSymbolCheck( Settings.FromLanguage );
- LoadTranslations();
- // start a thread that will periodically removed unused references
- var t1 = new Thread( MaintenanceLoop );
- t1.IsBackground = true;
- t1.Start();
- // start a thread that will periodically save new translations
- var t2 = new Thread( SaveTranslationsLoop );
- t2.IsBackground = true;
- t2.Start();
- }
- private string[] GetTranslationFiles()
- {
- return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ), $"*.txt", SearchOption.AllDirectories ) // FIXME: Add $"*{Language}.txt"
- .Union( new[] { Settings.AutoTranslationsFilePath } )
- .Select( x => x.Replace( "/", "\\" ) )
- .Distinct()
- .OrderBy( x => x )
- .ToArray();
- }
- private void MaintenanceLoop( object state )
- {
- while( true )
- {
- try
- {
- ObjectExtensions.Cull();
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An unexpected error occurred while removing GC'ed resources." );
- }
- Thread.Sleep( 1000 * 60 );
- }
- }
- private void SaveTranslationsLoop( object state )
- {
- try
- {
- while( true )
- {
- if( _newTranslations.Count > 0 )
- {
- lock( _writeToFileSync )
- {
- if( _newTranslations.Count > 0 )
- {
- using( var stream = File.Open( Settings.AutoTranslationsFilePath, FileMode.Append, FileAccess.Write ) )
- using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
- {
- foreach( var kvp in _newTranslations )
- {
- writer.WriteLine( TextHelper.Encode( kvp.Key ) + '=' + TextHelper.Encode( kvp.Value ) );
- }
- writer.Flush();
- }
- _newTranslations.Clear();
- }
- }
- }
- else
- {
- Thread.Sleep( 5000 );
- }
- }
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An error occurred while saving translations to disk." );
- }
- }
- /// <summary>
- /// Loads the translations found in Translation.{lang}.txt
- /// </summary>
- private void LoadTranslations()
- {
- try
- {
- lock( _writeToFileSync )
- {
- Directory.CreateDirectory( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ) );
- Directory.CreateDirectory( Path.GetDirectoryName( Path.Combine( Config.Current.DataPath, Settings.OutputFile ) ) );
- foreach( var fullFileName in GetTranslationFiles() )
- {
- if( File.Exists( fullFileName ) )
- {
- string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
- foreach( string translation in translations )
- {
- string[] kvp = translation.Split( new char[] { '=', '\t' }, StringSplitOptions.None );
- if( kvp.Length >= 2 )
- {
- string key = TextHelper.Decode( kvp[ 0 ].Trim() );
- string value = TextHelper.Decode( kvp[ 1 ].Trim() );
- if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
- {
- AddTranslation( key, value );
- }
- }
- }
- }
- }
- }
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An error occurred while loading translations." );
- }
- }
- private TranslationJob GetOrCreateTranslationJobFor( TranslationKeys key )
- {
- if( _unstartedJobs.TryGetValue( key.GetDictionaryLookupKey(), out TranslationJob job ) )
- {
- return job;
- }
- foreach( var completedJob in _completedJobs )
- {
- if( completedJob.Keys.GetDictionaryLookupKey() == key.GetDictionaryLookupKey() )
- {
- return completedJob;
- }
- }
- Logger.Current.Debug( "Queued translation for: " + key.GetDictionaryLookupKey() );
- job = new TranslationJob( key );
- _unstartedJobs.Add( key.GetDictionaryLookupKey(), job );
- CheckThresholds();
- return job;
- }
- private void CheckThresholds()
- {
- if( _unstartedJobs.Count > Settings.MaxUnstartedJobs )
- {
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- Settings.IsShutdown = true;
- Logger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxUnstartedJobs} queued for translations due to unknown reasons. Shutting down plugin." );
- }
- var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
- var newIdx = ( (int)Time.time ) % Settings.TranslationQueueWatchWindow;
- if( previousIdx != newIdx )
- {
- _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ] = 0;
- }
- _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ]++;
- var translationsInWindow = _currentTranslationsQueuedPerSecondRollingWindow.Sum();
- _translationsQueuedPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
- if( _translationsQueuedPerSecond > Settings.MaxTranslationsQueuedPerSecond )
- {
- if( !_timeExceededThreshold.HasValue )
- {
- _timeExceededThreshold = Time.time;
- }
- if( Time.time - _timeExceededThreshold.Value > Settings.MaxSecondsAboveTranslationThreshold )
- {
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- Settings.IsShutdown = true;
- Logger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxTranslationsQueuedPerSecond} translations per seconds queued for a {Settings.MaxSecondsAboveTranslationThreshold} second period. Shutting down plugin." );
- }
- }
- 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 )
- {
- _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ] = 0;
- }
- var translationsInWindow = _currentTranslationsQueuedPerSecondRollingWindow.Sum();
- _translationsQueuedPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
- if( _translationsQueuedPerSecond <= Settings.MaxTranslationsQueuedPerSecond )
- {
- _timeExceededThreshold = null;
- }
- }
- private void AddTranslation( string key, string value )
- {
- _translations[ key ] = value;
- _translatedTexts.Add( value );
- }
- private void AddTranslation( TranslationKeys key, string value )
- {
- _translations[ key.GetDictionaryLookupKey() ] = value;
- _translatedTexts.Add( value );
- }
- private void QueueNewUntranslatedForClipboard( TranslationKeys key )
- {
- if( Settings.CopyToClipboard )
- {
- if( !_textsToCopyToClipboard.Contains( key.RelevantKey ) )
- {
- _textsToCopyToClipboard.Add( key.RelevantKey );
- _textsToCopyToClipboardOrdered.Add( key.RelevantKey );
- _clipboardUpdated = Time.realtimeSinceStartup;
- }
- }
- }
- private void QueueNewUntranslatedForDisk( TranslationKeys key )
- {
- _newUntranslated.Add( key.GetDictionaryLookupKey() );
- }
- private void QueueNewTranslationForDisk( TranslationKeys key, string value )
- {
- lock( _writeToFileSync )
- {
- _newTranslations[ key.GetDictionaryLookupKey() ] = value;
- }
- }
- private bool TryGetTranslation( TranslationKeys key, out string value )
- {
- return _translations.TryGetValue( key.GetDictionaryLookupKey(), out value );
- }
- private string Override_TextChanged( object ui, string text )
- {
- if( _hooksEnabled )
- {
- return TranslateOrQueueWebJob( ui, text, true );
- }
- return null;
- }
- public void Hook_TextChanged( object ui )
- {
- if( _hooksEnabled )
- {
- TranslateOrQueueWebJob( ui, null, false );
- }
- }
- public void Hook_TextInitialized( object ui )
- {
- if( _hooksEnabled )
- {
- TranslateOrQueueWebJob( ui, null, true );
- }
- }
- private void SetTranslatedText( object ui, string translatedText, TranslationKeys key, TranslationInfo info )
- {
- var untemplatedTranslatedText = key.Untemplate( translatedText );
- info?.SetTranslatedText( untemplatedTranslatedText );
- if( _isInTranslatedMode )
- {
- SetText( ui, untemplatedTranslatedText, true, info );
- }
- }
- /// <summary>
- /// Sets the text of a UI text, while ensuring this will not fire a text changed event.
- /// </summary>
- private void SetText( object ui, string text, bool isTranslated, TranslationInfo info )
- {
- if( !info?.IsCurrentlySettingText ?? true )
- {
- try
- {
- // TODO: Disable ANY Hook
- _hooksEnabled = false;
- if( info != null )
- {
- info.IsCurrentlySettingText = true;
- }
- ui.SetText( text );
- if( isTranslated )
- {
- info?.ResizeUI( ui );
- }
- else
- {
- info?.UnresizeUI( ui );
- }
- }
- catch( NullReferenceException )
- {
- // This is likely happened due to a scene change.
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An error occurred while setting text on a component." );
- }
- finally
- {
- _hooksEnabled = true;
- if( info != null )
- {
- info.IsCurrentlySettingText = false;
- }
- }
- }
- }
- /// <summary>
- /// Determines if a text should be translated.
- /// </summary>
- private bool IsTranslatable( string str )
- {
- return _symbolCheck( str ) && str.Length <= Settings.MaxCharactersPerTranslation && !_translatedTexts.Contains( str );
- }
- public bool ShouldTranslate( object ui )
- {
- var cui = ui as Component;
- if( cui != null )
- {
- var go = cui.gameObject;
- var isDummy = go.IsDummy();
- if( isDummy )
- {
- return false;
- }
- var inputField = cui.gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.InputField )
- ?? cui.gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.TMP_InputField );
- return inputField == null;
- }
- return true;
- }
- private string TranslateOrQueueWebJob( object ui, string text, bool isAwakening )
- {
- var info = ui.GetTranslationInfo( isAwakening );
- if( !info?.IsAwake ?? false )
- {
- return null;
- }
- if( _ongoingOperations.Contains( ui ) )
- {
- return null;
- }
- var supportsStabilization = SupportsStabilization( ui );
- if( Settings.Delay == 0 || !supportsStabilization )
- {
- return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization );
- }
- else
- {
- StartCoroutine(
- DelayForSeconds( Settings.Delay, () =>
- {
- TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization );
- } ) );
- }
- return null;
- }
- public static bool IsCurrentlySetting( TranslationInfo info )
- {
- if( info == null ) return false;
- return info.IsCurrentlySettingText;
- }
- /// <summary>
- /// Translates the string of a UI text or queues it up to be translated
- /// by the HTTP translation service.
- /// </summary>
- private string TranslateOrQueueWebJobImmediate( object ui, string text, TranslationInfo info, bool supportsStabilization )
- {
- // Get the trimmed text
- text = ( text ?? ui.GetText() ).Trim();
- // Ensure that we actually want to translate this text and its owning UI element.
- if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsCurrentlySetting( info ) )
- {
- info?.Reset( text );
- var textKey = new TranslationKeys( text, !supportsStabilization );
- // if we already have translation loaded in our _translatios dictionary, simply load it and set text
- string translation;
- if( TryGetTranslation( textKey, out translation ) )
- {
- QueueNewUntranslatedForClipboard( textKey );
- if( !string.IsNullOrEmpty( translation ) )
- {
- SetTranslatedText( ui, translation, textKey, info );
- return translation;
- }
- }
- else
- {
- if( supportsStabilization )
- {
- // if we dont know what text to translate it to, we need to figure it out.
- // this might take a while, so add the UI text component to the ongoing operations
- // list, so we dont start multiple operations for it, as its text might be constantly
- // changing.
- _ongoingOperations.Add( ui );
- // start a coroutine, that will execute once the string of the UI text has stopped
- // changing. For all texts except 'story' texts, this will add a delay for exactly
- // 0.5s to the translation. This is barely noticable.
- //
- // on the other hand, for 'story' texts, this will take the time that it takes
- // for the text to stop 'scrolling' in.
- try
- {
- StartCoroutine(
- WaitForTextStablization(
- ui: ui,
- delay: 1.0f, // 1 second to prevent '1 second tickers' from getting translated
- maxTries: 60, // 50 tries, about 1 minute
- currentTries: 0,
- onMaxTriesExceeded: () =>
- {
- _ongoingOperations.Remove( ui );
- },
- onTextStabilized: stabilizedText =>
- {
- _ongoingOperations.Remove( ui );
- if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
- {
- var stabilizedTextKey = new TranslationKeys( stabilizedText, false );
- QueueNewUntranslatedForClipboard( stabilizedTextKey );
- info?.Reset( stabilizedText );
- // once the text has stabilized, attempt to look it up
- if( TryGetTranslation( stabilizedTextKey, out translation ) )
- {
- if( !string.IsNullOrEmpty( translation ) )
- {
- SetTranslatedText( ui, translation, stabilizedTextKey, info );
- }
- }
- else
- {
- // Lets try not to spam a service that might not be there...
- if( _endpoint != null )
- {
- if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
- {
- var job = GetOrCreateTranslationJobFor( stabilizedTextKey );
- job.Components.Add( ui );
- }
- }
- else
- {
- QueueNewUntranslatedForDisk( stabilizedTextKey );
- }
- }
- }
- } ) );
- }
- catch( Exception )
- {
- _ongoingOperations.Remove( ui );
- }
- }
- else
- {
- if( !_startedOperationsForNonStabilizableComponents.Contains( textKey.GetDictionaryLookupKey() ) )
- {
- _startedOperationsForNonStabilizableComponents.Add( textKey.GetDictionaryLookupKey() );
- QueueNewUntranslatedForClipboard( textKey );
- // Lets try not to spam a service that might not be there...
- if( _endpoint != null )
- {
- if( _consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown )
- {
- GetOrCreateTranslationJobFor( textKey );
- }
- }
- else
- {
- QueueNewUntranslatedForDisk( textKey );
- }
- }
- }
- }
- }
- return null;
- }
- public bool SupportsStabilization( object ui )
- {
- return !( ui is GUIContent );
- }
- /// <summary>
- /// Utility method that allows me to wait to call an action, until
- /// the text has stopped changing. This is important for 'story'
- /// mode text, which 'scrolls' into place slowly.
- /// </summary>
- public IEnumerator WaitForTextStablization( object ui, float delay, int maxTries, int currentTries, Action<string> onTextStabilized, Action onMaxTriesExceeded )
- {
- if( currentTries < maxTries ) // shortcircuit
- {
- var beforeText = ui.GetText();
- yield return new WaitForSeconds( delay );
- var afterText = ui.GetText();
- if( beforeText == afterText )
- {
- onTextStabilized( afterText.Trim() );
- }
- else
- {
- StartCoroutine( WaitForTextStablization( ui, delay, maxTries, currentTries + 1, onTextStabilized, onMaxTriesExceeded ) );
- }
- }
- else
- {
- onMaxTriesExceeded();
- }
- }
- public IEnumerator DelayForSeconds( float delay, Action onContinue )
- {
- yield return new WaitForSeconds( delay );
- onContinue();
- }
- public void Update()
- {
- try
- {
- if( _endpoint != null )
- {
- _endpoint.OnUpdate();
- }
- CopyToClipboard();
- if( !Settings.IsShutdown )
- {
- ResetThresholdTimerIfRequired();
- KickoffTranslations();
- FinishTranslations();
- }
- if( Input.anyKey )
- {
- if( Settings.EnablePrintHierarchy && ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.Y ) )
- {
- PrintObjects();
- }
- else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.T ) )
- {
- ToggleTranslation();
- }
- else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.D ) )
- {
- DumpUntranslated();
- }
- else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.R ) )
- {
- ReloadTranslations();
- }
- }
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An error occurred in Update callback. " );
- }
- }
- // create this as a field instead of local var, to prevent new creation on EVERY game loop
- private readonly List<string> _kickedOff = new List<string>();
- private void KickoffTranslations()
- {
- if( _endpoint == null ) return;
- if( Settings.EnableBatching && _endpoint.SupportsLineSplitting && !_batchLogicHasFailed && _unstartedJobs.Count > 1 && _translationsQueuedPerSecond <= Settings.MaxTranslationsQueuedPerSecond )
- {
- while( _unstartedJobs.Count > 0 )
- {
- if( _endpoint.IsBusy ) break;
- var kvps = _unstartedJobs.Take( Settings.BatchSize ).ToList();
- var batch = new TranslationBatch();
- bool addedAny = false;
- foreach( var kvp in kvps )
- {
- var key = kvp.Key;
- var job = kvp.Value;
- _kickedOff.Add( key );
- batch.Add( job );
- if( !job.AnyComponentsStillHasOriginalUntranslatedText() ) continue;
- addedAny = true;
- }
- if( addedAny )
- {
- StartCoroutine( _endpoint.Translate( batch.GetFullTranslationKey(), Settings.FromLanguage, Settings.Language, translatedText => OnBatchTranslationCompleted( batch, translatedText ),
- () => OnTranslationFailed() ) );
- }
- }
- }
- else
- {
- foreach( var kvp in _unstartedJobs )
- {
- if( _endpoint.IsBusy ) break;
- var key = kvp.Key;
- var job = kvp.Value;
- _kickedOff.Add( key );
- // lets see if the text should still be translated before kicking anything off
- if( !job.AnyComponentsStillHasOriginalUntranslatedText() ) continue;
- StartCoroutine( _endpoint.Translate( job.Keys.GetDictionaryLookupKey(), Settings.FromLanguage, Settings.Language, translatedText => OnSingleTranslationCompleted( job, translatedText ),
- () => OnTranslationFailed() ) );
- }
- }
- for( int i = 0 ; i < _kickedOff.Count ; i++ )
- {
- _unstartedJobs.Remove( _kickedOff[ i ] );
- }
- _kickedOff.Clear();
- }
- public void OnBatchTranslationCompleted( TranslationBatch batch, string translatedTextBatch )
- {
- Settings.TranslationCount++;
- if( !Settings.IsShutdown )
- {
- if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
- {
- Settings.IsShutdown = true;
- Logger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
- }
- }
- _consecutiveErrors = 0;
- var succeeded = batch.MatchWithTranslations( translatedTextBatch );
- if( succeeded )
- {
- foreach( var tracker in batch.Trackers )
- {
- var job = tracker.Job;
- var translatedText = tracker.RawTranslatedText;
- if( !string.IsNullOrEmpty( translatedText ) )
- {
- if( Settings.ForceSplitTextAfterCharacters > 0 )
- {
- translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
- }
- job.TranslatedText = job.Keys.RepairTemplate( translatedText );
- QueueNewTranslationForDisk( job.Keys, translatedText );
- _completedJobs.Add( job );
- }
- }
- }
- else
- {
- // might as well re-add all translation jobs, and never do this again!
- _batchLogicHasFailed = true;
- foreach( var tracker in batch.Trackers )
- {
- var key = tracker.Job.Keys.GetDictionaryLookupKey();
- if( !_unstartedJobs.ContainsKey( key ) )
- {
- _unstartedJobs[ key ] = tracker.Job;
- }
- }
- Logger.Current.Error( "A batch operation failed. Disabling batching and restarting failed jobs." );
- }
- }
- private void OnSingleTranslationCompleted( TranslationJob job, string translatedText )
- {
- Settings.TranslationCount++;
- if( !Settings.IsShutdown )
- {
- if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
- {
- Settings.IsShutdown = true;
- Logger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
- }
- }
- _consecutiveErrors = 0;
- if( !string.IsNullOrEmpty( translatedText ) )
- {
- if( Settings.ForceSplitTextAfterCharacters > 0 )
- {
- translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
- }
- job.TranslatedText = job.Keys.RepairTemplate( translatedText );
- QueueNewTranslationForDisk( job.Keys, translatedText );
- _completedJobs.Add( job );
- }
- }
- private void OnTranslationFailed()
- {
- _consecutiveErrors++;
- if( !Settings.IsShutdown )
- {
- if( _consecutiveErrors > Settings.MaxErrors )
- {
- if( _endpoint.ShouldGetSecondChanceAfterFailure() )
- {
- Logger.Current.Warn( $"More than {Settings.MaxErrors} consecutive errors occurred. Entering fallback mode." );
- _consecutiveErrors = 0;
- }
- else
- {
- Settings.IsShutdown = true;
- Logger.Current.Error( $"More than {Settings.MaxErrors} consecutive errors occurred. Shutting down plugin." );
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- }
- }
- }
- }
- private void FinishTranslations()
- {
- if( _completedJobs.Count > 0 )
- {
- for( int i = _completedJobs.Count - 1 ; i >= 0 ; i-- )
- {
- var job = _completedJobs[ i ];
- _completedJobs.RemoveAt( i );
- foreach( var component in job.Components )
- {
- // update the original text, but only if it has not been chaanged already for some reason (could be other translator plugin or game itself)
- var text = component.GetText().Trim();
- if( text == job.Keys.OriginalText )
- {
- var info = component.GetTranslationInfo( false );
- SetTranslatedText( component, job.TranslatedText, job.Keys, info );
- }
- }
- AddTranslation( job.Keys, job.TranslatedText );
- }
- }
- }
- private void ReloadTranslations()
- {
- LoadTranslations();
- foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
- {
- var info = kvp.Value as TranslationInfo;
- if( info != null && !string.IsNullOrEmpty( info.OriginalText ) )
- {
- var key = new TranslationKeys( info.OriginalText, false );
- if( TryGetTranslation( key, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
- {
- SetTranslatedText( kvp.Key, translatedText, key, info );
- }
- }
- }
- }
- private string CalculateDumpFileName()
- {
- int idx = 0;
- string fileName = null;
- do
- {
- idx++;
- fileName = $"UntranslatedDump{idx}.txt";
- }
- while( File.Exists( fileName ) );
- return fileName;
- }
- private void DumpUntranslated()
- {
- if( _newUntranslated.Count > 0 )
- {
- using( var stream = File.Open( CalculateDumpFileName(), FileMode.Append, FileAccess.Write ) )
- using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
- {
- foreach( var untranslated in _newUntranslated )
- {
- writer.WriteLine( TextHelper.Encode( untranslated ) + '=' );
- }
- writer.Flush();
- }
- _newUntranslated.Clear();
- }
- }
- private void ToggleTranslation()
- {
- _isInTranslatedMode = !_isInTranslatedMode;
- if( _isInTranslatedMode )
- {
- // make sure we use the translated version of all texts
- foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
- {
- var ui = kvp.Key;
- try
- {
- if( ( ui as Component )?.gameObject?.activeSelf ?? false )
- {
- var info = (TranslationInfo)kvp.Value;
- if( info != null && info.IsTranslated )
- {
- SetText( ui, info.TranslatedText, true, info );
- }
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectExtensions.Remove( ui );
- }
- }
- }
- else
- {
- // make sure we use the original version of all texts
- foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
- {
- var ui = kvp.Key;
- try
- {
- if( ( ui as Component )?.gameObject?.activeSelf ?? false )
- {
- var info = (TranslationInfo)kvp.Value;
- if( info != null && info.IsTranslated )
- {
- SetText( ui, info.OriginalText, true, info );
- }
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectExtensions.Remove( ui );
- }
- }
- }
- }
- private void CopyToClipboard()
- {
- if( Settings.CopyToClipboard
- && _textsToCopyToClipboardOrdered.Count > 0
- && Time.realtimeSinceStartup - _clipboardUpdated > Settings.ClipboardDebounceTime )
- {
- try
- {
- var builder = new StringBuilder();
- foreach( var text in _textsToCopyToClipboardOrdered )
- {
- if( text.Length + builder.Length > Settings.MaxClipboardCopyCharacters ) break;
- builder.AppendLine( text );
- }
- TextEditor editor = (TextEditor)GUIUtility.GetStateObject( typeof( TextEditor ), GUIUtility.keyboardControl );
- editor.text = builder.ToString();
- editor.SelectAll();
- editor.Copy();
- }
- catch( Exception e )
- {
- Logger.Current.Error( e, "An error while copying text to clipboard." );
- }
- finally
- {
- _textsToCopyToClipboard.Clear();
- _textsToCopyToClipboardOrdered.Clear();
- }
- }
- }
- private void PrintObjects()
- {
- using( var stream = File.Open( Path.Combine( Environment.CurrentDirectory, "hierarchy.txt" ), FileMode.Create ) )
- using( var writer = new StreamWriter( stream ) )
- {
- foreach( var root in GetAllRoots() )
- {
- TraverseChildren( writer, root, "" );
- }
- writer.Flush();
- }
- }
- private IEnumerable<GameObject> GetAllRoots()
- {
- var objects = GameObject.FindObjectsOfType<GameObject>();
- foreach( var obj in objects )
- {
- if( obj.transform.parent == null )
- {
- yield return obj;
- }
- }
- }
- private void TraverseChildren( StreamWriter writer, GameObject obj, string identation )
- {
- var layer = LayerMask.LayerToName( obj.gameObject.layer );
- var components = string.Join( ", ", obj.GetComponents<Component>().Select( x => x.GetType().Name ).ToArray() );
- var line = string.Format( "{0,-50} {1,100}",
- identation + obj.gameObject.name + " [" + layer + "]",
- components );
- writer.WriteLine( line );
- for( int i = 0 ; i < obj.transform.childCount ; i++ )
- {
- var child = obj.transform.GetChild( i );
- TraverseChildren( writer, child.gameObject, identation + " " );
- }
- }
- }
- }
|