12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876 |
- 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.Hooks.NGUI;
- using UnityEngine.SceneManagement;
- using XUnity.AutoTranslator.Plugin.Core.Constants;
- using XUnity.AutoTranslator.Plugin.Core.Debugging;
- using Harmony;
- using XUnity.AutoTranslator.Plugin.Core.Parsing;
- using System.Diagnostics;
- using XUnity.AutoTranslator.Plugin.Core.UI;
- using XUnity.AutoTranslator.Plugin.Core.Endpoints;
- using XUnity.AutoTranslator.Plugin.Core.Web.Internal;
- namespace XUnity.AutoTranslator.Plugin.Core
- {
- /// <summary>
- /// Main plugin class for the AutoTranslator.
- /// </summary>
- public class AutoTranslationPlugin : MonoBehaviour
- {
- private static readonly char[][] TranslationSplitters = new char[][] { new char[] { '\t' }, new char[] { '=' } };
- /// <summary>
- /// Allow the instance to be accessed statically, as only one will exist.
- /// </summary>
- internal static AutoTranslationPlugin Current;
- private XuaWindow _window;
- /// <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>();
- private Dictionary<string, TranslationJob> _ongoingJobs = new Dictionary<string, TranslationJob>();
- /// <summary>
- /// All the translations are stored in this dictionary.
- /// </summary>
- private Dictionary<string, string> _staticTranslations = new Dictionary<string, string>();
- private Dictionary<string, string> _translations = new Dictionary<string, string>();
- private Dictionary<string, string> _reverseTranslations = 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>();
- /// <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 = 0.0f;
- /// <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>();
- /// <summary>
- /// This function will check if there are symbols of a given language contained in a string.
- /// </summary>
- private Func<string, bool> _symbolCheck;
- /// <summary>
- /// Texts currently being scheduled for translation by 'immediate' components.
- /// </summary>
- private HashSet<string> _immediatelyTranslating = new HashSet<string>();
- private Dictionary<string, byte[]> _translatedImages = new Dictionary<string, byte[]>( StringComparer.InvariantCultureIgnoreCase );
- private HashSet<string> _untranslatedImages = new HashSet<string>();
- private Component _advEngine;
- private float? _nextAdvUpdate;
- private HttpSecurity _httpSecurity;
- private List<ConfiguredEndpoint> _configuredEndpoints;
- private ConfiguredEndpoint _endpoint;
- private int[] _currentTranslationsQueuedPerSecondRollingWindow = new int[ Settings.TranslationQueueWatchWindow ];
- private float? _timeExceededThreshold;
- private float _translationsQueuedPerSecond;
- private bool _isInTranslatedMode = true;
- private bool _textHooksEnabled = true;
- private bool _imageHooksEnabled = true;
- private bool _batchLogicHasFailed = false;
- private int _availableBatchOperations = Settings.MaxAvailableBatchOperations;
- private float _batchOperationSecondCounter = 0;
- private string[] _previouslyQueuedText = new string[ Settings.PreviousTextStaggerCount ];
- private int _staggerTextCursor = 0;
- private int _concurrentStaggers = 0;
- private int _frameForLastQueuedTranslation = -1;
- private int _consecutiveFramesTranslated = 0;
- private int _secondForQueuedTranslation = -1;
- private int _consecutiveSecondsTranslated = 0;
- private bool _hasOverrideFont = false;
- private bool _overrideFont = false;
- private bool _initialized = false;
- private bool _temporarilyDisabled = false;
- private string _requireSpriteRendererCheckCausedBy = null;
- private int _lastSpriteUpdateFrame = -1;
- private bool _isCalledFromSceneManager = false;
- /// <summary>
- /// Initialized the plugin.
- /// </summary>
- public void Initialize()
- {
- // Setup 'singleton'
- Current = this;
- // Setup logger, if it was not already initialized by a plugin-version
- if( XuaLogger.Current == null )
- {
- XuaLogger.Current = new ConsoleLogger();
- }
- // Setup configuration
- try
- {
- Settings.Configure();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred during configuration. Shutting plugin down." );
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- return;
- }
- // Setup console, if enabled
- if( Settings.EnableConsole )
- {
- DebugConsole.Enable();
- }
- // Setup hooks
- HooksSetup.InstallTextHooks();
- HooksSetup.InstallImageHooks();
- HooksSetup.InstallTextGetterCompatHooks();
- _httpSecurity = new HttpSecurity();
- try
- {
- var context = new InitializationContext( _httpSecurity, Settings.FromLanguage, Settings.Language );
- _configuredEndpoints = KnownTranslateEndpoints.CreateEndpoints( gameObject, context )
- .OrderBy( x => x.Error != null )
- .ThenBy( x => x.Endpoint.FriendlyName )
- .ToList();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while constructing endpoints. Shutting plugin down." );
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- return;
- }
- var primaryEndpoint = _configuredEndpoints.FirstOrDefault( x => x.Endpoint.Id == Settings.ServiceEndpoint );
- if( primaryEndpoint != null )
- {
- if( primaryEndpoint.Error != null )
- {
- XuaLogger.Current.Error( primaryEndpoint.Error, "Error occurred during the initialization of the selected translate endpoint." );
- }
- else
- {
- _endpoint = primaryEndpoint;
- }
- }
- else if( !string.IsNullOrEmpty( Settings.ServiceEndpoint ) )
- {
- XuaLogger.Current.Error( $"Could not find the configured endpoint '{Settings.ServiceEndpoint}'." );
- }
- // TODO: Perhaps some bleeding edge check to see if this is required?
- var callback = _httpSecurity.GetCertificateValidationCheck();
- if( callback != null )
- {
- ServicePointManager.ServerCertificateValidationCallback += callback;
- }
- // Save again because configuration may be modified by endpoints
- try
- {
- PluginEnvironment.Current.SaveConfig();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred during while saving configuration." );
- }
- if( !LanguageHelper.IsFromLanguageSupported( Settings.FromLanguage ) )
- {
- XuaLogger.Current.Error( $"The plugin has been configured to use the 'FromLanguage={Settings.FromLanguage}'. This language is not supported. Shutting plugin down." );
- _endpoint = null;
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- }
- _symbolCheck = LanguageHelper.GetSymbolCheck( Settings.FromLanguage );
- if( !string.IsNullOrEmpty( Settings.OverrideFont ) )
- {
- var available = Font.GetOSInstalledFontNames();
- if( !available.Contains( Settings.OverrideFont ) )
- {
- XuaLogger.Current.Error( $"The specified override font is not available. Available fonts: " + string.Join( ", ", available ) );
- Settings.OverrideFont = null;
- }
- else
- {
- _hasOverrideFont = true;
- }
- _overrideFont = _hasOverrideFont;
- }
- try
- {
- EnableSceneLoadScan();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while settings up texture scene-load scans." );
- }
- LoadTranslations();
- LoadStaticTranslations();
- try
- {
- _window = new XuaWindow(
- new List<ToggleViewModel>
- {
- new ToggleViewModel(
- " Translated",
- "<b>TRANSLATED</b>\nThe plugin currently displays translated texts. Disabling this does not mean the plugin will no longer perform translations, just that they will not be displayed.",
- "<b>NOT TRANSLATED</b>\nThe plugin currently displays untranslated texts.",
- ToggleTranslation, () => _isInTranslatedMode )
- },
- _configuredEndpoints.Select( x =>
- new TranslatorDropdownOptionViewModel( () => x == _endpoint, x, OnEndpointSelected ) ).ToList(),
- new List<ButtonViewModel>
- {
- new ButtonViewModel( "Reboot", "<b>REBOOT PLUGIN</b>\nReboots the plugin if it has been shutdown. This only works if the plugin was shut down due to consequtive errors towards the translation endpoint.", RebootPlugin, () => Settings.IsShutdown && !Settings.IsShutdownFatal ),
- new ButtonViewModel( "Reload", "<b>RELOAD TRANSLATION</b>\nReloads all translation text files and texture files from disk.", ReloadTranslations, null ),
- new ButtonViewModel( "Hook", "<b>MANUAL HOOK</b>\nTraverses the unity object tree for looking for anything that can be translated. Performs a translation if something is found.", ManualHook, null )
- },
- new List<LabelViewModel>
- {
- new LabelViewModel( "Version: ", () => PluginData.Version ),
- new LabelViewModel( "Status: ", () => Settings.IsShutdown ? "Shutdown" : "Running" ),
- new LabelViewModel( "Served translations: ", () => $"{Settings.TranslationCount} / {Settings.MaxTranslationsBeforeShutdown}" ),
- new LabelViewModel( "Queued translations: ", () => $"{(_unstartedJobs.Count + _ongoingJobs.Count)} / {Settings.MaxUnstartedJobs}" ),
- new LabelViewModel( "Error'ed translations: ", () => $"{_consecutiveErrors} / {Settings.MaxErrors}" ),
- } );
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while setting up UI." );
- }
- UnityTextParsers.Initialize( text => IsTranslatable( text ) && IsBelowMaxLength( text ) );
- // 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 void OnEndpointSelected( ConfiguredEndpoint endpoint )
- {
- if( _endpoint != endpoint )
- {
- _endpoint = endpoint;
- if( Settings.IsShutdown && !Settings.IsShutdownFatal )
- {
- RebootPlugin();
- ManualHook();
- }
- Settings.SetEndpoint( _endpoint.Endpoint.Id );
- }
- }
- private IEnumerable<string> GetTranslationFiles()
- {
- return Directory.GetFiles( Path.Combine( PluginEnvironment.Current.DataPath, Settings.TranslationDirectory ).Parameterize(), $"*.txt", SearchOption.AllDirectories )
- .Select( x => x.Replace( "/", "\\" ) );
- }
- private IEnumerable<string> GetTextureFiles()
- {
- return Directory.GetFiles( Path.Combine( PluginEnvironment.Current.DataPath, Settings.TextureDirectory ).Parameterize(), $"*.png", SearchOption.AllDirectories )
- .Select( x => x.Replace( "/", "\\" ) );
- }
- private void MaintenanceLoop( object state )
- {
- while( true )
- {
- try
- {
- ObjectReferenceMapper.Cull();
- }
- catch( Exception e )
- {
- XuaLogger.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 )
- {
- XuaLogger.Current.Error( e, "An error occurred while saving translations to disk." );
- }
- }
- private void EnableSceneLoadScan()
- {
- XuaLogger.Current.Info( "Probing whether OnLevelWasLoaded or SceneManager is supported in this version of Unity. Any warnings related to OnLevelWasLoaded coming from Unity can safely be ignored." );
- if( Features.SupportsSceneManager )
- {
- XuaLogger.Current.Info( "SceneManager is supported in this version of Unity." );
- EnableSceneLoadScanInternal();
- }
- else
- {
- XuaLogger.Current.Info( "SceneManager is not supported in this version of Unity. Falling back to OnLevelWasLoaded and Application level API." );
- }
- }
- private void EnableSceneLoadScanInternal()
- {
- // do this in a different class to avoid having an anonymous method with references to the "Scene" class
- SceneManagerLoader.EnableSceneLoadScanInternal( this );
- }
- internal void OnLevelWasLoadedFromSceneManager( int id )
- {
- try
- {
- _isCalledFromSceneManager = true;
- OnLevelWasLoaded( id );
- }
- finally
- {
- _isCalledFromSceneManager = false;
- }
- }
- private void OnLevelWasLoaded( int id )
- {
- if( !Features.SupportsSceneManager || ( Features.SupportsSceneManager && _isCalledFromSceneManager ) )
- {
- if( Settings.EnableTextureScanOnSceneLoad && ( Settings.EnableTextureDumping || Settings.EnableTextureTranslation ) )
- {
- XuaLogger.Current.Info( "Performing texture lookup during scene load..." );
- var startTime = Time.realtimeSinceStartup;
- ManualHookForTextures();
- var endTime = Time.realtimeSinceStartup;
- XuaLogger.Current.Info( $"Finished texture lookup (took {Math.Round( endTime - startTime, 2 )} seconds)" );
- }
- }
- }
- /// <summary>
- /// Loads the translations found in Translation.{lang}.txt
- /// </summary>
- private void LoadTranslations()
- {
- try
- {
- lock( _writeToFileSync )
- {
- Directory.CreateDirectory( Path.Combine( PluginEnvironment.Current.DataPath, Settings.TranslationDirectory ).Parameterize() );
- Directory.CreateDirectory( Path.GetDirectoryName( Settings.AutoTranslationsFilePath ) );
- var mainTranslationFile = Settings.AutoTranslationsFilePath;
- LoadTranslationsInFile( mainTranslationFile );
- foreach( var fullFileName in GetTranslationFiles().Reverse().Except( new[] { mainTranslationFile } ) )
- {
- LoadTranslationsInFile( fullFileName );
- }
- }
- if( Settings.EnableTextureTranslation || Settings.EnableTextureDumping )
- {
- _translatedImages.Clear();
- _untranslatedImages.Clear();
- Directory.CreateDirectory( Path.Combine( PluginEnvironment.Current.DataPath, Settings.TextureDirectory ).Parameterize() );
- foreach( var fullFileName in GetTextureFiles() )
- {
- RegisterImageFromFile( fullFileName );
- }
- }
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while loading translations." );
- }
- }
- private void RegisterImageFromFile( string fullFileName )
- {
- var fileName = Path.GetFileNameWithoutExtension( fullFileName );
- var startHash = fileName.LastIndexOf( "[" );
- var endHash = fileName.LastIndexOf( "]" );
- if( endHash > -1 && startHash > -1 && endHash > startHash )
- {
- var takeFrom = startHash + 1;
- // load based on whether or not the key is image hashed
- var parts = fileName.Substring( takeFrom, endHash - takeFrom ).Split( '-' );
- string key;
- string originalHash;
- if( parts.Length == 1 )
- {
- key = parts[ 0 ];
- originalHash = parts[ 0 ];
- }
- else if( parts.Length == 2 )
- {
- key = parts[ 0 ];
- originalHash = parts[ 1 ];
- }
- else
- {
- XuaLogger.Current.Warn( $"Image not loaded (unknown hash): {fullFileName}." );
- return;
- }
- var data = File.ReadAllBytes( fullFileName );
- var currentHash = HashHelper.Compute( data );
- var isModified = StringComparer.InvariantCultureIgnoreCase.Compare( originalHash, currentHash ) != 0;
- // only load images that someone has modified!
- if( Settings.LoadUnmodifiedTextures || isModified )
- {
- RegisterTranslatedImage( key, data );
- XuaLogger.Current.Debug( $"Image loaded: {fullFileName}." );
- }
- else
- {
- RegisterUntranslatedImage( key );
- XuaLogger.Current.Warn( $"Image not loaded (unmodified): {fullFileName}." );
- }
- //if( Settings.DeleteUnmodifiedTextures && !isModified )
- //{
- // try
- // {
- // File.Delete( fullFileName );
- // Logger.Current.Warn( $"Image deleted (unmodified): {fullFileName}." );
- // }
- // catch( Exception e )
- // {
- // Logger.Current.Warn( e, $"An error occurred while trying to delete unmodified image: {fullFileName}." );
- // }
- //}
- }
- else
- {
- XuaLogger.Current.Warn( $"Image not loaded (no hash): {fullFileName}." );
- }
- }
- private void RegisterImageFromData( string textureName, string key, byte[] data )
- {
- var name = textureName.SanitizeForFileSystem();
- var root = Path.Combine( PluginEnvironment.Current.DataPath, Settings.TextureDirectory ).Parameterize();
- var originalHash = HashHelper.Compute( data );
- // allow hash and key to be the same; only store one of them then!
- string fileName;
- if( key == originalHash )
- {
- fileName = name + " [" + key + "].png";
- }
- else
- {
- fileName = name + " [" + key + "-" + originalHash + "].png";
- }
- var fullName = Path.Combine( root, fileName );
- File.WriteAllBytes( fullName, data );
- XuaLogger.Current.Info( "Dumped texture file: " + fileName );
- if( Settings.LoadUnmodifiedTextures )
- {
- RegisterTranslatedImage( key, data );
- }
- else
- {
- RegisterUntranslatedImage( key );
- }
- }
- private void RegisterTranslatedImage( string key, byte[] data )
- {
- _translatedImages[ key ] = data;
- }
- private void RegisterUntranslatedImage( string key )
- {
- _untranslatedImages.Add( key );
- }
- private void LoadTranslationsInFile( string fullFileName )
- {
- if( File.Exists( fullFileName ) )
- {
- XuaLogger.Current.Debug( $"Loading texts: {fullFileName}." );
- string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
- foreach( string translation in translations )
- {
- for( int i = 0 ; i < TranslationSplitters.Length ; i++ )
- {
- var splitter = TranslationSplitters[ i ];
- string[] kvp = translation.Split( splitter, StringSplitOptions.None );
- if( kvp.Length == 2 )
- {
- string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
- string value = TextHelper.Decode( kvp[ 1 ] );
- if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) && IsTranslatable( key ) )
- {
- AddTranslation( key, value );
- break;
- }
- }
- }
- }
- }
- }
- private void LoadStaticTranslations()
- {
- if( Settings.UseStaticTranslations && Settings.FromLanguage == Settings.DefaultFromLanguage && Settings.Language == Settings.DefaultLanguage )
- {
- var tab = new char[] { '\t' };
- var equals = new char[] { '=' };
- var splitters = new char[][] { tab, equals };
- // load static translations from previous titles
- string[] translations = Properties.Resources.StaticTranslations.Split( new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries );
- foreach( string translation in translations )
- {
- for( int i = 0 ; i < splitters.Length ; i++ )
- {
- var splitter = splitters[ i ];
- string[] kvp = translation.Split( splitter, StringSplitOptions.None );
- if( kvp.Length >= 2 )
- {
- string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
- string value = TextHelper.Decode( kvp[ 1 ].TrimIfConfigured() );
- if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
- {
- _staticTranslations[ key ] = value;
- break;
- }
- }
- }
- }
- }
- }
- private TranslationJob GetOrCreateTranslationJobFor( object ui, TranslationKey key, TranslationContext context )
- {
- var lookupKey = key.GetDictionaryLookupKey();
- if( _unstartedJobs.TryGetValue( lookupKey, out TranslationJob unstartedJob ) )
- {
- unstartedJob.Associate( context );
- return unstartedJob;
- }
- if( _ongoingJobs.TryGetValue( lookupKey, out TranslationJob ongoingJob ) )
- {
- ongoingJob.Associate( context );
- return ongoingJob;
- }
- foreach( var completedJob in _completedJobs )
- {
- if( completedJob.Key.GetDictionaryLookupKey() == lookupKey )
- {
- completedJob.Associate( context );
- return completedJob;
- }
- }
- XuaLogger.Current.Debug( "Queued: '" + lookupKey + "'" );
- ongoingJob = new TranslationJob( key );
- if( ui != null )
- {
- ongoingJob.OriginalSources.Add( ui );
- }
- ongoingJob.Associate( context );
- _unstartedJobs.Add( lookupKey, ongoingJob );
- CheckStaggerText( lookupKey );
- CheckConsecutiveFrames();
- CheckConsecutiveSeconds();
- CheckThresholds();
- return ongoingJob;
- }
- private void CheckConsecutiveSeconds()
- {
- var currentSecond = (int)Time.time;
- var lastSecond = currentSecond - 1;
- if( lastSecond == _secondForQueuedTranslation )
- {
- // we also queued something last frame, lets increment our counter
- _consecutiveSecondsTranslated++;
- if( _consecutiveSecondsTranslated > Settings.MaximumConsecutiveSecondsTranslated )
- {
- // Shutdown, this wont be tolerated!!!
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.Current.Error( $"SPAM DETECTED: Translations were queued every second for more than {Settings.MaximumConsecutiveSecondsTranslated} consecutive seconds. Shutting down plugin." );
- }
- }
- else if( currentSecond == _secondForQueuedTranslation )
- {
- // 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
- _consecutiveSecondsTranslated = 0;
- }
- _secondForQueuedTranslation = currentSecond;
- }
- private void CheckConsecutiveFrames()
- {
- var currentFrame = Time.frameCount;
- var lastFrame = currentFrame - 1;
- if( lastFrame == _frameForLastQueuedTranslation )
- {
- // we also queued something last frame, lets increment our counter
- _consecutiveFramesTranslated++;
- if( _consecutiveFramesTranslated > Settings.MaximumConsecutiveFramesTranslated )
- {
- // Shutdown, this wont be tolerated!!!
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.Current.Error( $"SPAM DETECTED: Translations were queued every frame for more than {Settings.MaximumConsecutiveFramesTranslated} consecutive frames. Shutting down plugin." );
- }
- }
- else if( currentFrame == _frameForLastQueuedTranslation )
- {
- // do nothing, there may be multiple translations per frame, that wont increase this counter
- }
- else if( _consecutiveFramesTranslated > 0 )
- {
- // but if multiple Update frames has passed, we will reset the counter
- _consecutiveFramesTranslated--;
- }
- _frameForLastQueuedTranslation = currentFrame;
- }
- private void PeriodicResetFrameCheck()
- {
- var currentSecond = (int)Time.time;
- if( currentSecond % 100 == 0 )
- {
- _consecutiveFramesTranslated = 0;
- }
- }
- private void CheckStaggerText( string untranslatedText )
- {
- bool wasProblematic = false;
- for( int i = 0 ; i < _previouslyQueuedText.Length ; i++ )
- {
- var previouslyQueuedText = _previouslyQueuedText[ i ];
- if( previouslyQueuedText != null )
- {
- if( untranslatedText.RemindsOf( previouslyQueuedText ) )
- {
- wasProblematic = true;
- break;
- }
- }
- }
- if( wasProblematic )
- {
- _concurrentStaggers++;
- if( _concurrentStaggers > Settings.MaximumStaggers )
- {
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.Current.Error( $"SPAM DETECTED: Text that is 'scrolling in' is being translated. Disable that feature. Shutting down plugin." );
- }
- }
- else
- {
- _concurrentStaggers = 0;
- }
- _previouslyQueuedText[ _staggerTextCursor % _previouslyQueuedText.Length ] = untranslatedText;
- _staggerTextCursor++;
- }
- private void CheckThresholds()
- {
- if( _unstartedJobs.Count > Settings.MaxUnstartedJobs )
- {
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.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();
- _ongoingJobs.Clear();
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.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 IncrementBatchOperations()
- {
- _batchOperationSecondCounter += Time.deltaTime;
- if( _batchOperationSecondCounter > Settings.IncreaseBatchOperationsEvery )
- {
- if( _availableBatchOperations < Settings.MaxAvailableBatchOperations )
- {
- _availableBatchOperations++;
- }
- _batchOperationSecondCounter = 0;
- }
- }
- 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 bool IsImageRegistered( string key )
- {
- return _translatedImages.ContainsKey( key ) || _untranslatedImages.Contains( key );
- }
- private bool TryGetTranslatedImage( string key, out byte[] data )
- {
- return _translatedImages.TryGetValue( key, out data );
- }
- private void AddTranslation( string key, string value )
- {
- _translations[ key ] = value;
- _reverseTranslations[ value ] = key;
- }
- private void AddTranslation( TranslationKey key, string value )
- {
- var lookup = key.GetDictionaryLookupKey();
- _translations[ lookup ] = value;
- _reverseTranslations[ value ] = lookup;
- }
- private void UpdateSpriteRenderers()
- {
- if( Settings.EnableSpriteRendererHooking && ( Settings.EnableTextureTranslation || Settings.EnableTextureDumping ) )
- {
- if( _requireSpriteRendererCheckCausedBy != null )
- {
- try
- {
- var start = Time.realtimeSinceStartup;
- var spriteRenderers = GameObject.FindObjectsOfType<SpriteRenderer>();
- foreach( var sr in spriteRenderers )
- {
- // simulate a hook
- Hook_ImageChangedOnComponent( sr, null, false, false );
- }
- var end = Time.realtimeSinceStartup;
- var delta = Math.Round( end - start, 2 );
- XuaLogger.Current.Debug( $"Update SpriteRenderers caused by {_requireSpriteRendererCheckCausedBy} component (took " + delta + " seconds)" );
- }
- finally
- {
- _requireSpriteRendererCheckCausedBy = null;
- }
- }
- }
- }
- private void QueueNewUntranslatedForClipboard( TranslationKey key )
- {
- if( Settings.CopyToClipboard && Features.SupportsClipboard )
- {
- if( !_textsToCopyToClipboard.Contains( key.RelevantText ) )
- {
- _textsToCopyToClipboard.Add( key.RelevantText );
- _textsToCopyToClipboardOrdered.Add( key.RelevantText );
- _clipboardUpdated = Time.realtimeSinceStartup;
- }
- }
- }
- private void QueueNewUntranslatedForDisk( TranslationKey key )
- {
- _newUntranslated.Add( key.GetDictionaryLookupKey() );
- }
- private void QueueNewTranslationForDisk( TranslationKey key, string value )
- {
- lock( _writeToFileSync )
- {
- _newTranslations[ key.GetDictionaryLookupKey() ] = value;
- }
- }
- private void QueueNewTranslationForDisk( string key, string value )
- {
- lock( _writeToFileSync )
- {
- _newTranslations[ key ] = value;
- }
- }
- private bool TryGetTranslation( TranslationKey key, out string value )
- {
- return TryGetTranslation( key.GetDictionaryLookupKey(), out value );
- }
- private bool TryGetTranslation( string key, out string value )
- {
- var result = _translations.TryGetValue( key, out value );
- if( result )
- {
- return result;
- }
- else if( _staticTranslations.Count > 0 )
- {
- if( _staticTranslations.TryGetValue( key, out value ) )
- {
- QueueNewTranslationForDisk( key, value );
- AddTranslation( key, value );
- return true;
- }
- }
- return result;
- }
- internal bool TryGetReverseTranslation( string value, out string key )
- {
- return _reverseTranslations.TryGetValue( value, out key );
- }
- internal string Hook_TextChanged_WithResult( object ui, string text )
- {
- if( !ui.IsKnownTextType() ) return null;
- if( _textHooksEnabled && !_temporarilyDisabled )
- {
- return TranslateOrQueueWebJob( ui, text, false );
- }
- return null;
- }
- internal string ExternalHook_TextChanged_WithResult( object ui, string text )
- {
- if( !ui.IsKnownTextType() ) return null;
- if( _textHooksEnabled && !_temporarilyDisabled )
- {
- return TranslateOrQueueWebJob( ui, text, true );
- }
- return null;
- }
- internal void Hook_TextChanged( object ui, bool onEnable )
- {
- if( _textHooksEnabled && !_temporarilyDisabled )
- {
- TranslateOrQueueWebJob( ui, null, false );
- }
- if( onEnable )
- {
- CheckSpriteRenderer( ui );
- }
- }
- internal void Hook_ImageChangedOnComponent( object source, Texture2D texture, bool isPrefixHooked, bool onEnable )
- {
- if( !_imageHooksEnabled ) return;
- if( !source.IsKnownImageType() ) return;
- HandleImage( source, texture, isPrefixHooked );
- if( onEnable )
- {
- CheckSpriteRenderer( source );
- }
- }
- internal void Hook_ImageChanged( Texture2D texture, bool isPrefixHooked )
- {
- if( !_imageHooksEnabled ) return;
- if( texture == null ) return;
- HandleImage( null, texture, isPrefixHooked );
- }
- private void SetTranslatedText( object ui, string translatedText, TextTranslationInfo info )
- {
- info?.SetTranslatedText( translatedText );
- if( _isInTranslatedMode )
- {
- SetText( ui, translatedText, true, info );
- }
- }
- internal void Hook_HandleComponent( object ui )
- {
- if( _hasOverrideFont )
- {
- var info = ui.GetOrCreateTextTranslationInfo();
- if( _overrideFont )
- {
- info?.ChangeFont( ui );
- }
- else
- {
- info?.UnchangeFont( ui );
- }
- }
- if( Settings.ForceUIResizing )
- {
- var info = ui.GetOrCreateTextTranslationInfo();
- if( info?.IsCurrentlySettingText == false )
- {
- // force UI resizing is highly problematic for NGUI because text should somehow
- // be set after changing "resize" properties... brilliant stuff
- if( ui.GetType() != ClrTypes.UILabel )
- {
- info?.ResizeUI( ui );
- }
- }
- }
- }
- private void CheckSpriteRenderer( object ui )
- {
- if( Settings.EnableSpriteRendererHooking )
- {
- var currentFrame = Time.frameCount;
- var lastFrame = currentFrame - 1;
- if( lastFrame != _lastSpriteUpdateFrame && currentFrame != _lastSpriteUpdateFrame )
- {
- _requireSpriteRendererCheckCausedBy = ui?.GetType().Name;
- }
- _lastSpriteUpdateFrame = currentFrame;
- }
- }
- /// <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, TextTranslationInfo info )
- {
- if( !info?.IsCurrentlySettingText ?? true )
- {
- try
- {
- _textHooksEnabled = false;
- if( info != null )
- {
- info.IsCurrentlySettingText = true;
- }
- if( Settings.EnableUIResizing || Settings.ForceUIResizing )
- {
- if( isTranslated || Settings.ForceUIResizing )
- {
- info?.ResizeUI( ui );
- }
- else
- {
- info?.UnresizeUI( ui );
- }
- }
- // NGUI only behaves if you set the text after the resize behaviour
- ui.SetText( text );
- info?.ResetScrollIn( ui );
- }
- catch( TargetInvocationException )
- {
- // might happen with NGUI
- }
- catch( NullReferenceException )
- {
- // This is likely happened due to a scene change.
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while setting text on a component." );
- }
- finally
- {
- _textHooksEnabled = 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
- && !_reverseTranslations.ContainsKey( str )
- && !Settings.IgnoreTextStartingWith.Any( x => str.StartsWithStrict( x ) );
- }
- private bool IsBelowMaxLength( string str )
- {
- return str.Length <= Settings.MaxCharactersPerTranslation;
- }
- private bool IsBelowMaxLengthStrict( string str )
- {
- return str.Length <= ( Settings.MaxCharactersPerTranslation / 2 );
- }
- private bool ShouldTranslateImageComponent( object ui )
- {
- var component = ui as Component;
- if( component != null )
- {
- // dummy check
- var go = component.gameObject;
- var ignore = go.HasIgnoredName();
- if( ignore )
- {
- return false;
- }
- var behaviour = component as Behaviour;
- if( behaviour?.isActiveAndEnabled == false )
- {
- return false;
- }
- }
- return true;
- }
- private bool ShouldTranslateTextComponent( object ui, bool ignoreComponentState )
- {
- var component = ui as Component;
- if( component != null )
- {
- // dummy check
- var go = component.gameObject;
- var ignore = go.HasIgnoredName();
- if( ignore )
- {
- return false;
- }
- if( !ignoreComponentState )
- {
- var behaviour = component as Behaviour;
- if( behaviour?.isActiveAndEnabled == false )
- {
- return false;
- }
- }
- var inputField = component.gameObject.GetFirstComponentInSelfOrAncestor( ClrTypes.InputField )
- ?? component.gameObject.GetFirstComponentInSelfOrAncestor( ClrTypes.TMP_InputField );
- return inputField == null;
- }
- return true;
- }
- private string TranslateOrQueueWebJob( object ui, string text, bool ignoreComponentState )
- {
- var info = ui.GetOrCreateTextTranslationInfo();
- if( _ongoingOperations.Contains( ui ) )
- {
- return TranslateImmediate( ui, text, info, ignoreComponentState );
- }
- var supportsStabilization = ui.SupportsStabilization();
- if( Settings.Delay == 0 || !supportsStabilization )
- {
- return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
- }
- else
- {
- StartCoroutine(
- DelayForSeconds( Settings.Delay, () =>
- {
- TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
- } ) );
- }
- return null;
- }
- private static bool IsCurrentlySetting( TextTranslationInfo info )
- {
- if( info == null ) return false;
- return info.IsCurrentlySettingText;
- }
- private void HandleImage( object source, Texture2D texture, bool isPrefixHooked )
- {
- if( Settings.EnableTextureDumping )
- {
- try
- {
- DumpTexture( source, texture );
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while dumping texture." );
- }
- }
- if( Settings.EnableTextureTranslation )
- {
- try
- {
- TranslateTexture( source, texture, isPrefixHooked, null );
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while translating texture." );
- }
- }
- }
- private void TranslateTexture( object ui, TextureReloadContext context )
- {
- if( ui is Texture2D texture2d )
- {
- TranslateTexture( null, texture2d, false, context );
- }
- else
- {
- TranslateTexture( ui, null, false, context );
- }
- }
- private void TranslateTexture( object source, Texture2D texture, bool isPrefixHooked, TextureReloadContext context )
- {
- try
- {
- _imageHooksEnabled = false;
- texture = texture ?? source.GetTexture();
- if( texture == null ) return;
- var tti = texture.GetOrCreateTextureTranslationInfo();
- var iti = source.GetOrCreateImageTranslationInfo();
- var key = tti.GetKey( texture );
- if( string.IsNullOrEmpty( key ) ) return;
- bool hasContext = context != null;
- bool forceReload = false;
- if( hasContext )
- {
- forceReload = context.RegisterTextureInContextAndDetermineWhetherToReload( texture );
- }
- if( TryGetTranslatedImage( key, out var newData ) )
- {
- if( _isInTranslatedMode )
- {
- // handle texture
- if( !tti.IsTranslated || forceReload )
- {
- try
- {
- texture.LoadImageEx( newData, tti.IsNonReadable( texture ) );
- }
- finally
- {
- tti.IsTranslated = true;
- }
- }
- // handle containing component
- if( iti != null )
- {
- if( !iti.IsTranslated || hasContext )
- {
- try
- {
- if( !isPrefixHooked )
- {
- source.SetAllDirtyEx();
- }
- }
- finally
- {
- iti.IsTranslated = true;
- }
- }
- }
- }
- }
- else
- {
- // if we cannot find the texture, and the texture is considered translated... hmmm someone has removed a file
- // handle texture
- var originalData = tti.GetOriginalData( texture );
- if( originalData != null )
- {
- if( tti.IsTranslated )
- {
- try
- {
- texture.LoadImageEx( originalData, tti.IsNonReadable( texture ) );
- }
- finally
- {
- tti.IsTranslated = true;
- }
- }
- // handle containing component
- if( iti != null )
- {
- if( iti.IsTranslated )
- {
- try
- {
- if( !isPrefixHooked )
- {
- source.SetAllDirtyEx();
- }
- }
- finally
- {
- iti.IsTranslated = true;
- }
- }
- }
- }
- }
- if( !_isInTranslatedMode )
- {
- var originalData = tti.GetOriginalData( texture );
- if( originalData != null )
- {
- // handle texture
- if( tti.IsTranslated )
- {
- try
- {
- texture.LoadImageEx( originalData, tti.IsNonReadable( texture ) );
- }
- finally
- {
- tti.IsTranslated = false;
- }
- }
- // handle containing component
- if( iti != null )
- {
- if( iti.IsTranslated )
- {
- try
- {
- if( !isPrefixHooked )
- {
- source.SetAllDirtyEx();
- }
- }
- finally
- {
- iti.IsTranslated = false;
- }
- }
- }
- }
- }
- if( forceReload )
- {
- XuaLogger.Current.Info( $"Reloaded texture: {texture.name} ({key})." );
- }
- }
- finally
- {
- _imageHooksEnabled = true;
- }
- }
- private void DumpTexture( object source, Texture2D texture )
- {
- try
- {
- _imageHooksEnabled = false;
- texture = texture ?? source.GetTexture();
- if( texture == null ) return;
- var info = texture.GetOrCreateTextureTranslationInfo();
- if( info.HasDumpedAlternativeTexture ) return;
- try
- {
- if( ShouldTranslate( texture ) )
- {
- var key = info.GetKey( texture );
- if( string.IsNullOrEmpty( key ) ) return;
- if( !IsImageRegistered( key ) )
- {
- var name = texture.GetTextureName();
- //var format = "[" + texture.format.ToString() + "] ";
- var originalData = info.GetOrCreateOriginalData( texture );
- RegisterImageFromData( name, key, originalData );
- }
- }
- }
- finally
- {
- info.HasDumpedAlternativeTexture = true;
- }
- }
- finally
- {
- _imageHooksEnabled = true;
- }
- }
- private bool ShouldTranslate( Texture2D texture )
- {
- // convert to int so engine versions that does not have specific enums still work
- var format = (int)texture.format;
- // 1 = Alpha8
- // 9 = R16
- // 63 = R8
- return format != 1
- && format != 9
- && format != 63;
- }
- private string TranslateImmediate( object ui, string text, TextTranslationInfo info, bool ignoreComponentState )
- {
- // Get the trimmed text
- text = ( text ?? ui.GetText() ).TrimIfConfigured();
- if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslateTextComponent( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
- {
- info?.Reset( text );
- var textKey = new TranslationKey( ui, text, ui.IsSpammingComponent(), false );
- // if we already have translation loaded in our _translatios dictionary, simply load it and set text
- string translation;
- if( TryGetTranslation( textKey, out translation ) )
- {
- if( !string.IsNullOrEmpty( translation ) )
- {
- SetTranslatedText( ui, textKey.Untemplate( translation ), info );
- return translation;
- }
- }
- else
- {
- if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
- {
- var result = UnityTextParsers.GameLogTextParser.Parse( text );
- if( result.Succeeded )
- {
- translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, false );
- if( translation != null )
- {
- SetTranslatedText( ui, translation, info );
- return translation;
- }
- }
- }
- }
- }
- return null;
- }
- /// <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, TextTranslationInfo info, bool supportsStabilization, bool ignoreComponentState, TranslationContext context = null )
- {
- text = text ?? ui.GetText();
- // make sure text exists
- var originalText = text;
- if( context == null )
- {
- // Get the trimmed text
- text = text.TrimIfConfigured();
- }
- // Ensure that we actually want to translate this text and its owning UI element.
- if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslateTextComponent( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
- {
- //Logger.Current.Debug( "START: " + ui.GetType().Name + ": " + text );
- info?.Reset( originalText );
- var isSpammer = ui.IsSpammingComponent();
- var textKey = new TranslationKey( ui, text, isSpammer, context != null );
- // 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 ) )
- {
- if( context == null ) // never set text if operation is contextualized (only a part translation)
- {
- SetTranslatedText( ui, textKey.Untemplate( translation ), info );
- }
- return translation;
- }
- }
- else
- {
- if( context == null )
- {
- if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
- {
- var result = UnityTextParsers.GameLogTextParser.Parse( text );
- if( result.Succeeded )
- {
- translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, false );
- if( translation != null )
- {
- SetTranslatedText( ui, translation, info );
- return translation;
- }
- }
- }
- else if( UnityTextParsers.RichTextParser.CanApply( ui ) && IsBelowMaxLength( text ) )
- {
- var result = UnityTextParsers.RichTextParser.Parse( text );
- if( result.Succeeded )
- {
- var isWhitelisted = ui.IsWhitelistedForImmediateRichTextTranslation();
- translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, isWhitelisted );
- if( translation != null )
- {
- SetTranslatedText( ui, translation, info );
- return translation;
- }
- else if( isWhitelisted )
- {
- return null;
- }
- }
- }
- }
- if( supportsStabilization && context == null ) // never stabilize a text that is contextualized or that does not support stabilization
- {
- // 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 );
- originalText = stabilizedText;
- stabilizedText = stabilizedText.TrimIfConfigured();
- if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
- {
- var stabilizedTextKey = new TranslationKey( ui, stabilizedText, false );
- QueueNewUntranslatedForClipboard( stabilizedTextKey );
- info?.Reset( originalText );
- // once the text has stabilized, attempt to look it up
- if( TryGetTranslation( stabilizedTextKey, out translation ) )
- {
- if( !string.IsNullOrEmpty( translation ) )
- {
- // stabilized, no need to untemplate
- SetTranslatedText( ui, translation, info );
- }
- }
- else
- {
- if( context == null )
- {
- if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
- {
- var result = UnityTextParsers.GameLogTextParser.Parse( stabilizedText );
- if( result.Succeeded )
- {
- var translatedText = TranslateOrQueueWebJobImmediateByParserResult( ui, result, true );
- if( translatedText != null )
- {
- // stabilized, no need to untemplate
- SetTranslatedText( ui, translatedText, info );
- }
- return;
- }
- }
- else if( UnityTextParsers.RichTextParser.CanApply( ui ) && IsBelowMaxLength( stabilizedText ) )
- {
- var result = UnityTextParsers.RichTextParser.Parse( stabilizedText );
- if( result.Succeeded )
- {
- var translatedText = TranslateOrQueueWebJobImmediateByParserResult( ui, result, true );
- if( translatedText != null )
- {
- // stabilized, no need to untemplate
- SetTranslatedText( ui, translatedText, info );
- }
- return;
- }
- }
- }
- // Lets try not to spam a service that might not be there...
- if( _endpoint != null )
- {
- if( IsBelowMaxLength( stabilizedText ) )
- {
- if( !Settings.IsShutdown )
- {
- var job = GetOrCreateTranslationJobFor( ui, stabilizedTextKey, context );
- job.Components.Add( ui );
- }
- }
- }
- else
- {
- QueueNewUntranslatedForDisk( stabilizedTextKey );
- }
- }
- }
- } ) );
- }
- catch( Exception )
- {
- _ongoingOperations.Remove( ui );
- }
- }
- else if( !isSpammer || ( isSpammer && IsBelowMaxLengthStrict( text ) ) )
- {
- if( context != null )
- {
- // if there is a context, this is a part-translation, which means it is not a candidate for scrolling-in text
- if( _endpoint != null )
- {
- if( !Settings.IsShutdown )
- {
- // once the text has stabilized, attempt to look it up
- var job = GetOrCreateTranslationJobFor( ui, textKey, context );
- }
- }
- else
- {
- QueueNewUntranslatedForDisk( textKey );
- }
- }
- else
- {
- StartCoroutine(
- WaitForTextStablization(
- textKey: textKey,
- delay: 1.0f,
- onTextStabilized: () =>
- {
- // Lets try not to spam a service that might not be there...
- if( _endpoint != null )
- {
- // once the text has stabilized, attempt to look it up
- if( !Settings.IsShutdown )
- {
- if( !TryGetTranslation( textKey, out translation ) )
- {
- var job = GetOrCreateTranslationJobFor( ui, textKey, context );
- }
- }
- }
- else
- {
- QueueNewUntranslatedForDisk( textKey );
- }
- } ) );
- }
- }
- }
- }
- return null;
- }
- private string TranslateOrQueueWebJobImmediateByParserResult( object ui, ParserResult result, bool allowStartJob )
- {
- Dictionary<string, string> translations = new Dictionary<string, string>();
- // attempt to lookup ALL strings immediately; return result if possible; queue operations
- foreach( var kvp in result.Arguments )
- {
- var key = kvp.Key;
- var value = kvp.Value.TrimIfConfigured();
- if( !string.IsNullOrEmpty( value ) && IsTranslatable( value ) && IsBelowMaxLength( value ) )
- {
- string partTranslation;
- if( TryGetTranslation( value, out partTranslation ) )
- {
- translations.Add( key, partTranslation );
- }
- else if( allowStartJob )
- {
- // incomplete, must start job
- var context = new TranslationContext( ui, result );
- TranslateOrQueueWebJobImmediate( ui, value, null, false, true, context );
- }
- }
- else
- {
- // the value will do
- translations.Add( key, value );
- }
- }
- if( result.Arguments.Count == translations.Count )
- {
- return result.Untemplate( translations );
- }
- else
- {
- return null; // could not perform complete translation
- }
- }
- /// <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>
- private IEnumerator WaitForTextStablization( object ui, float delay, int maxTries, int currentTries, Action<string> onTextStabilized, Action onMaxTriesExceeded )
- {
- yield return 0; // wait a single frame to allow any external plugins to complete their hooking logic
- bool succeeded = false;
- while( currentTries < maxTries ) // shortcircuit
- {
- var beforeText = ui.GetText();
- yield return new WaitForSeconds( delay );
- var afterText = ui.GetText();
- //Logger.Current.Debug( "WAITING: " + ui.GetType().Name + ": " + afterText );
- if( beforeText == afterText )
- {
- onTextStabilized( afterText );
- succeeded = true;
- break;
- }
- currentTries++;
- }
- if( !succeeded )
- {
- onMaxTriesExceeded();
- }
- }
- /// <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. This version is
- /// for global text, where the component cannot tell us if the text
- /// has changed itself.
- /// </summary>
- private IEnumerator WaitForTextStablization( TranslationKey textKey, float delay, Action onTextStabilized, Action onFailed = null )
- {
- var text = textKey.GetDictionaryLookupKey();
- if( !_immediatelyTranslating.Contains( text ) )
- {
- _immediatelyTranslating.Add( text );
- try
- {
- yield return new WaitForSeconds( delay );
- bool succeeded = true;
- foreach( var otherImmediatelyTranslating in _immediatelyTranslating )
- {
- if( text != otherImmediatelyTranslating )
- {
- if( text.RemindsOf( otherImmediatelyTranslating ) )
- {
- succeeded = false;
- break;
- }
- }
- }
- if( succeeded )
- {
- onTextStabilized();
- }
- else
- {
- onFailed?.Invoke();
- }
- }
- finally
- {
- _immediatelyTranslating.Remove( text );
- }
- }
- }
- private IEnumerator DelayForSeconds( float delay, Action onContinue )
- {
- yield return new WaitForSeconds( delay );
- onContinue();
- }
- private IEnumerator EnableBatchingAfterDelay()
- {
- yield return new WaitForSeconds( 240 );
- _batchLogicHasFailed = false;
- XuaLogger.Current.Info( "Re-enabled batching." );
- }
- void Awake()
- {
- if( !_initialized )
- {
- _initialized = true;
- try
- {
- Initialize();
- ManualHook();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An unexpected error occurred during plugin initialization." );
- }
- }
- }
- void Start()
- {
- try
- {
- HooksSetup.InstallOverrideTextHooks();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An unexpected error occurred during plugin start." );
- }
- }
- void Update()
- {
- try
- {
- // perform this check every 100 frames!
- if( Time.frameCount % 100 == 0 )
- {
- ConnectionTrackingWebClient.CheckServicePoints();
- }
- if( Features.SupportsClipboard )
- {
- CopyToClipboard();
- }
- if( !Settings.IsShutdown )
- {
- UpdateSpriteRenderers();
- PeriodicResetFrameCheck();
- IncrementBatchOperations();
- ResetThresholdTimerIfRequired();
- KickoffTranslations();
- FinishTranslations();
- if( ClrTypes.AdvEngine != null && _nextAdvUpdate.HasValue && Time.time > _nextAdvUpdate )
- {
- _nextAdvUpdate = null;
- UpdateUtageText();
- }
- }
- if( Input.anyKey )
- {
- var isAltPressed = Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt );
- if( Settings.EnablePrintHierarchy && isAltPressed && Input.GetKeyDown( KeyCode.Y ) )
- {
- PrintObjects();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.T ) )
- {
- ToggleTranslation();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.F ) )
- {
- ToggleFont();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.D ) )
- {
- DumpUntranslated();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.R ) )
- {
- ReloadTranslations();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.U ) )
- {
- ManualHook();
- }
- else if( isAltPressed && Input.GetKeyDown( KeyCode.Q ) )
- {
- RebootPlugin();
- }
- else if( isAltPressed && ( Input.GetKeyDown( KeyCode.Alpha0 ) || Input.GetKeyDown( KeyCode.Keypad0 ) ) )
- {
- if( _window != null )
- {
- _window.IsShown = !_window.IsShown;
- }
- }
- }
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred in Update callback. " );
- }
- }
- void OnGUI()
- {
- if( _window != null )
- {
- try
- {
- DisableAutoTranslator();
- if( _window.IsShown ) _window.OnGUI();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred in XUnity.AutoTranslator UI. Disabling the UI." );
- _window = null;
- }
- finally
- {
- EnableAutoTranslator();
- }
- }
- }
- private void RebootPlugin()
- {
- if( Settings.IsShutdown )
- {
- if( !Settings.IsShutdownFatal )
- {
- _consecutiveErrors = 0;
- Settings.IsShutdown = false;
- XuaLogger.Current.Info( "Rebooted Auto Translator." );
- }
- else
- {
- XuaLogger.Current.Info( "Cannot reboot Auto Translator because the error that caused the shutdown is bad behaviour by the game." );
- }
- }
- else
- {
- XuaLogger.Current.Info( "Cannot reboot Auto Translator because it has not been shut down." );
- }
- }
- private void KickoffTranslations()
- {
- if( _endpoint == null ) return;
- if( Settings.EnableBatching && _endpoint.Endpoint.MaxTranslationsPerRequest > 1 && !_batchLogicHasFailed && _unstartedJobs.Count > 1 && _availableBatchOperations > 0 )
- {
- while( _unstartedJobs.Count > 0 && _availableBatchOperations > 0 )
- {
- if( _endpoint.IsBusy ) break;
- var kvps = _unstartedJobs.Take( _endpoint.Endpoint.MaxTranslationsPerRequest ).ToList();
- var untranslatedTexts = new List<string>();
- var jobs = new List<TranslationJob>();
- foreach( var kvp in kvps )
- {
- var key = kvp.Key;
- var job = kvp.Value;
- _unstartedJobs.Remove( key );
- if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
- var untranslatedText = job.Key.GetDictionaryLookupKey();
- if( _endpoint.CanTranslate( untranslatedText ) )
- {
- jobs.Add( job );
- untranslatedTexts.Add( untranslatedText );
- _ongoingJobs[ key ] = job;
- }
- else
- {
- XuaLogger.Current.Warn( $"Unqueued: '{untranslatedText}' because the current endpoint has already failed this translation 3 times." );
- job.State = TranslationJobState.Failed;
- }
- }
- if( jobs.Count > 0 )
- {
- _availableBatchOperations--;
- var jobsArray = jobs.ToArray();
- var endpoint = _endpoint;
- foreach( var untranslatedText in untranslatedTexts )
- {
- XuaLogger.Current.Debug( "Started: '" + untranslatedText + "'" );
- }
- StartCoroutine(
- endpoint.Translate(
- untranslatedTexts.ToArray(),
- Settings.FromLanguage,
- Settings.Language,
- translatedText => OnBatchTranslationCompleted( jobsArray, translatedText ),
- ( msg, e ) => OnTranslationFailed( endpoint, jobsArray, msg, e ) ) );
- }
- }
- }
- else
- {
- while( _unstartedJobs.Count > 0 )
- {
- if( _endpoint.IsBusy ) break;
- var kvp = _unstartedJobs.FirstOrDefault();
- var key = kvp.Key;
- var job = kvp.Value;
- _unstartedJobs.Remove( key );
- // lets see if the text should still be translated before kicking anything off
- if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
- var untranslatedText = job.Key.GetDictionaryLookupKey();
- if( _endpoint.CanTranslate( untranslatedText ) )
- {
- var endpoint = _endpoint;
- _ongoingJobs[ key ] = job;
- XuaLogger.Current.Debug( "Started: " + untranslatedText );
- StartCoroutine(
- endpoint.Translate(
- new[] { untranslatedText },
- Settings.FromLanguage,
- Settings.Language,
- translatedText => OnSingleTranslationCompleted( job, translatedText ),
- ( msg, e ) => OnTranslationFailed( endpoint, new[] { job }, msg, e ) ) );
- }
- else
- {
- XuaLogger.Current.Warn( $"Unqueued: '{untranslatedText}' because the current endpoint has already failed this translation 3 times." );
- job.State = TranslationJobState.Failed;
- }
- }
- }
- }
- private void OnBatchTranslationCompleted( TranslationJob[] jobs, string[] translatedTexts )
- {
- _consecutiveErrors = 0;
- var succeeded = jobs.Length == translatedTexts.Length;
- if( succeeded )
- {
- for( int i = 0 ; i < jobs.Length ; i++ )
- {
- Settings.TranslationCount++;
- var job = jobs[ i ];
- var translatedText = translatedTexts[ i ];
- if( !string.IsNullOrEmpty( translatedText ) )
- {
- translatedText = job.Key.RepairTemplate( translatedText );
- if( Settings.Language == Settings.Romaji && Settings.RomajiPostProcessing != TextPostProcessing.None )
- {
- translatedText = RomanizationHelper.PostProcess( translatedText, Settings.RomajiPostProcessing );
- }
- if( Settings.ForceSplitTextAfterCharacters > 0 )
- {
- translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
- }
- job.TranslatedText = translatedText;
- QueueNewTranslationForDisk( job.Key, translatedText );
- _completedJobs.Add( job );
- }
- AddTranslation( job.Key, translatedText );
- job.State = TranslationJobState.Succeeded;
- _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
- XuaLogger.Current.Info( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
- }
- }
- else
- {
- if( !_batchLogicHasFailed )
- {
- StartCoroutine( EnableBatchingAfterDelay() );
- }
- _batchLogicHasFailed = true;
- for( int i = 0 ; i < jobs.Length ; i++ )
- {
- Settings.TranslationCount++;
- var job = jobs[ i ];
- var key = job.Key.GetDictionaryLookupKey();
- if( !_unstartedJobs.ContainsKey( key ) )
- {
- _unstartedJobs[ key ] = job;
- }
- _ongoingJobs.Remove( key );
- }
- XuaLogger.Current.Error( "A batch operation failed. Disabling batching and restarting failed jobs." );
- }
- if( !Settings.IsShutdown )
- {
- if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
- {
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- }
- }
- }
- private void OnSingleTranslationCompleted( TranslationJob job, string[] translatedTexts )
- {
- var translatedText = translatedTexts[ 0 ];
- Settings.TranslationCount++;
- _consecutiveErrors = 0;
- if( !string.IsNullOrEmpty( translatedText ) )
- {
- translatedText = job.Key.RepairTemplate( translatedText );
- if( Settings.Language == Settings.Romaji && Settings.RomajiPostProcessing != TextPostProcessing.None )
- {
- translatedText = RomanizationHelper.PostProcess( translatedText, Settings.RomajiPostProcessing );
- }
-
- if( Settings.ForceSplitTextAfterCharacters > 0 )
- {
- translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
- }
- job.TranslatedText = translatedText;
- QueueNewTranslationForDisk( job.Key, translatedText );
- _completedJobs.Add( job );
- }
- AddTranslation( job.Key, translatedText );
- job.State = TranslationJobState.Succeeded;
- _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
- XuaLogger.Current.Info( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
- if( !Settings.IsShutdown )
- {
- if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
- {
- Settings.IsShutdown = true;
- Settings.IsShutdownFatal = true;
- XuaLogger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.Clear();
- }
- }
- }
- private void OnTranslationFailed( ConfiguredEndpoint endpoint, TranslationJob[] jobs, string error, Exception e )
- {
- if( e == null )
- {
- XuaLogger.Current.Error( error );
- }
- else
- {
- XuaLogger.Current.Error( e, error );
- }
- _consecutiveErrors++;
- if( jobs.Length == 1 )
- {
- Settings.TranslationCount++; // counts as a translation
- foreach( var job in jobs )
- {
- var untranslatedText = job.Key.GetDictionaryLookupKey();
- job.State = TranslationJobState.Failed;
- _ongoingJobs.Remove( untranslatedText );
- endpoint.RegisterTranslationFailureFor( untranslatedText );
- }
- }
- else
- {
- if( !_batchLogicHasFailed )
- {
- StartCoroutine( EnableBatchingAfterDelay() );
- }
- _batchLogicHasFailed = true;
- for( int i = 0 ; i < jobs.Length ; i++ )
- {
- Settings.TranslationCount++;
- var job = jobs[ i ];
- var key = job.Key.GetDictionaryLookupKey();
- if( !_unstartedJobs.ContainsKey( key ) )
- {
- _unstartedJobs[ key ] = job;
- }
- _ongoingJobs.Remove( key );
- }
- XuaLogger.Current.Error( "A batch operation failed. Disabling batching and restarting failed jobs." );
- }
- if( !Settings.IsShutdown )
- {
- if( _consecutiveErrors >= Settings.MaxErrors )
- {
- Settings.IsShutdown = true;
- XuaLogger.Current.Error( $"{Settings.MaxErrors} or more consecutive errors occurred. Shutting down plugin." );
- _unstartedJobs.Clear();
- _completedJobs.Clear();
- _ongoingJobs.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)
- try
- {
- var text = component.GetText().TrimIfConfigured();
- if( text == job.Key.OriginalText )
- {
- var info = component.GetOrCreateTextTranslationInfo();
- SetTranslatedText( component, job.TranslatedText, info );
- }
- }
- catch( NullReferenceException )
- {
- // might fail if compoent is no longer associated to game
- }
- }
- // handle each context
- foreach( var context in job.Contexts )
- {
- // are all jobs within this context completed? If so, we can set the text
- if( context.Jobs.All( x => x.State == TranslationJobState.Succeeded ) )
- {
- try
- {
- var text = context.Component.GetText().TrimIfConfigured();
- var result = context.Result;
- Dictionary<string, string> translations = new Dictionary<string, string>();
- var translatedText = TranslateOrQueueWebJobImmediateByParserResult( context.Component, result, false );
- if( !string.IsNullOrEmpty( translatedText ) )
- {
- if( result.PersistCombinedResult && !_translations.ContainsKey( context.Result.OriginalText ) )
- {
- AddTranslation( context.Result.OriginalText, translatedText );
- QueueNewTranslationForDisk( context.Result.OriginalText, translatedText );
- }
- if( text == result.OriginalText )
- {
- if( translatedText != null )
- {
- var info = context.Component.GetOrCreateTextTranslationInfo();
- SetTranslatedText( context.Component, translatedText, info );
- }
- }
- }
- }
- catch( NullReferenceException )
- {
- }
- }
- }
- // Utage support
- if( ClrTypes.AdvEngine != null
- && job.OriginalSources.Any( x => ClrTypes.AdvCommand.IsAssignableFrom( x.GetType() ) ) )
- {
- _nextAdvUpdate = Time.time + 0.5f;
- }
- }
- }
- }
- private void UpdateUtageText()
- {
- // After an object is destroyed, an equality check with null will return true. The variable does not go to null, you can still call GetInstanceID() on it, but the "==" operator is overloaded and behaves as expected.
- if( _advEngine == null || _advEngine?.gameObject == null )
- {
- _advEngine = (Component)GameObject.FindObjectOfType( Constants.ClrTypes.AdvEngine );
- }
- if( _advEngine != null )
- {
- AccessTools.Method( Constants.ClrTypes.AdvEngine, "ChangeLanguage" )?.Invoke( _advEngine, new object[ 0 ] );
- }
- }
- private void ReloadTranslations()
- {
- LoadTranslations();
- var context = new TextureReloadContext();
- foreach( var kvp in ObjectReferenceMapper.GetAllRegisteredObjects() )
- {
- var ui = kvp.Key;
- try
- {
- if( ui is Component component )
- {
- if( component.gameObject?.activeSelf ?? false )
- {
- var tti = kvp.Value as TextTranslationInfo;
- if( tti != null && !string.IsNullOrEmpty( tti.OriginalText ) )
- {
- var key = new TranslationKey( kvp.Key, tti.OriginalText, false );
- if( TryGetTranslation( key, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
- {
- SetTranslatedText( kvp.Key, translatedText, tti ); // no need to untemplatize the translated text
- }
- }
- }
- }
- if( Settings.EnableTextureTranslation )
- {
- TranslateTexture( ui, context );
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectReferenceMapper.Remove( ui );
- }
- }
- }
- 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 ToggleFont()
- {
- if( _hasOverrideFont )
- {
- _overrideFont = !_overrideFont;
- var objects = ObjectReferenceMapper.GetAllRegisteredObjects();
- XuaLogger.Current.Info( $"Toggling fonts of {objects.Count} objects." );
- if( _overrideFont )
- {
- // make sure we use the translated version of all texts
- foreach( var kvp in objects )
- {
- var tti = kvp.Value as TextTranslationInfo;
- if( tti != null )
- {
- var ui = kvp.Key;
- try
- {
- if( ( ui as Component )?.gameObject?.activeSelf ?? false )
- {
- tti?.ChangeFont( ui );
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectReferenceMapper.Remove( ui );
- }
- }
- }
- }
- else
- {
- // make sure we use the original version of all texts
- foreach( var kvp in objects )
- {
- var tti = kvp.Value as TextTranslationInfo;
- var ui = kvp.Key;
- try
- {
- if( ( ui as Component )?.gameObject?.activeSelf ?? false )
- {
- tti?.UnchangeFont( ui );
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectReferenceMapper.Remove( ui );
- }
- }
- }
- }
- }
- private void ToggleTranslation()
- {
- _isInTranslatedMode = !_isInTranslatedMode;
- var objects = ObjectReferenceMapper.GetAllRegisteredObjects();
- XuaLogger.Current.Info( $"Toggling translations of {objects.Count} objects." );
- if( _isInTranslatedMode )
- {
- // make sure we use the translated version of all texts
- foreach( var kvp in objects )
- {
- var ui = kvp.Key;
- try
- {
- if( ui is Component component )
- {
- if( component.gameObject?.activeSelf ?? false )
- {
- var tti = kvp.Value as TextTranslationInfo;
- if( tti != null && tti.IsTranslated )
- {
- SetText( ui, tti.TranslatedText, true, tti );
- }
- }
- }
- if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
- {
- TranslateTexture( ui, null );
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectReferenceMapper.Remove( ui );
- }
- }
- }
- else
- {
- // make sure we use the original version of all texts
- foreach( var kvp in objects )
- {
- var ui = kvp.Key;
- try
- {
- if( ui is Component component )
- {
- if( component.gameObject?.activeSelf ?? false )
- {
- var tti = kvp.Value as TextTranslationInfo;
- if( tti != null && tti.IsTranslated )
- {
- SetText( ui, tti.OriginalText, true, tti );
- }
- }
- }
- if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
- {
- TranslateTexture( ui, null );
- }
- }
- catch( Exception )
- {
- // not super pretty, no...
- ObjectReferenceMapper.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 )
- {
- XuaLogger.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 void ManualHook()
- {
- ManualHookForComponents();
- ManualHookForTextures();
- }
- private void ManualHookForComponents()
- {
- foreach( var root in GetAllRoots() )
- {
- TraverseChildrenManualHook( root );
- }
- }
- private void ManualHookForTextures()
- {
- if( Settings.EnableTextureScanOnSceneLoad && ( Settings.EnableTextureTranslation || Settings.EnableTextureDumping ) )
- {
- // scan all textures and update
- var textures = Resources.FindObjectsOfTypeAll<Texture2D>();
- foreach( var texture in textures )
- {
- Hook_ImageChanged( texture, false );
- }
- //// scan all components and set dirty
- //var components = GameObject.FindObjectsOfType<Component>();
- //foreach( var component in components )
- //{
- // component.SetAllDirtyEx();
- //}
- }
- }
- private IEnumerable<GameObject> GetAllRoots()
- {
- var objects = GameObject.FindObjectsOfType<GameObject>();
- foreach( var obj in objects )
- {
- if( obj.transform != null && obj.transform.parent == null )
- {
- yield return obj;
- }
- }
- }
- private void TraverseChildren( StreamWriter writer, GameObject obj, string identation )
- {
- if( obj != null )
- {
- var layer = LayerMask.LayerToName( obj.layer );
- var components = string.Join( ", ", obj.GetComponents<Component>().Select( x => x?.GetType()?.Name ).Where( x => x != null ).ToArray() );
- var line = string.Format( "{0,-50} {1,100}",
- identation + obj.name + " [" + layer + "]",
- components );
- writer.WriteLine( line );
- if( obj.transform != null )
- {
- for( int i = 0 ; i < obj.transform.childCount ; i++ )
- {
- var child = obj.transform.GetChild( i );
- TraverseChildren( writer, child.gameObject, identation + " " );
- }
- }
- }
- }
- private void TraverseChildrenManualHook( GameObject obj )
- {
- if( obj != null )
- {
- var components = obj.GetComponents<Component>();
- foreach( var component in components )
- {
- if( component.IsKnownTextType() )
- {
- Hook_TextChanged( component, false );
- }
- if( Settings.EnableTextureTranslation || Settings.EnableTextureDumping )
- {
- if( component.IsKnownImageType() )
- {
- Hook_ImageChangedOnComponent( component, null, false, false );
- }
- }
- }
- if( obj.transform != null )
- {
- for( int i = 0 ; i < obj.transform.childCount ; i++ )
- {
- var child = obj.transform.GetChild( i );
- TraverseChildrenManualHook( child.gameObject );
- }
- }
- }
- }
- /// <summary>
- /// Temporarily disables the Auto Translator functionality.
- /// </summary>
- public void DisableAutoTranslator()
- {
- _temporarilyDisabled = true;
- }
- /// <summary>
- /// Re-enables the Auto Translator functionality after
- /// having disabled it thorugh DisableAutoTranslator
- /// </summary>
- public void EnableAutoTranslator()
- {
- _temporarilyDisabled = false;
- }
- void OnApplicationQuit()
- {
- if( _configuredEndpoints == null ) return;
- foreach( var ce in _configuredEndpoints )
- {
- try
- {
- if( ce.Endpoint is IDisposable disposable ) disposable.Dispose();
- }
- catch( Exception e )
- {
- XuaLogger.Current.Error( e, "An error occurred while disposing endpoint." );
- }
- }
- }
- }
- internal static class SceneManagerLoader
- {
- public static void EnableSceneLoadScanInternal( AutoTranslationPlugin plugin )
- {
- // specified in own method, because of chance that this has changed through Unity lifetime
- SceneManager.sceneLoaded += ( arg1, arg2 ) => plugin.OnLevelWasLoadedFromSceneManager( arg1.buildIndex );
- }
- }
- }
|