AutoTranslationPlugin.cs 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using ExIni;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using System.Globalization;
  15. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  16. using UnityEngine.EventSystems;
  17. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  18. using XUnity.AutoTranslator.Plugin.Core.Utilities;
  19. using XUnity.AutoTranslator.Plugin.Core.Web;
  20. using XUnity.AutoTranslator.Plugin.Core.Hooks;
  21. using XUnity.AutoTranslator.Plugin.Core.Hooks.TextMeshPro;
  22. using XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI;
  23. using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
  24. using UnityEngine.SceneManagement;
  25. using XUnity.AutoTranslator.Plugin.Core.Constants;
  26. using XUnity.AutoTranslator.Plugin.Core.Debugging;
  27. using XUnity.AutoTranslator.Plugin.Core.Batching;
  28. using Harmony;
  29. using XUnity.AutoTranslator.Plugin.Core.Parsing;
  30. using System.Diagnostics;
  31. using XUnity.AutoTranslator.Plugin.Core.UI;
  32. using XUnity.AutoTranslator.Plugin.Core.Endpoints;
  33. using XUnity.AutoTranslator.Plugin.Core.Web.Internal;
  34. namespace XUnity.AutoTranslator.Plugin.Core
  35. {
  36. public class AutoTranslationPlugin : MonoBehaviour
  37. {
  38. private static readonly char[][] TranslationSplitters = new char[][] { new char[] { '\t' }, new char[] { '=' } };
  39. /// <summary>
  40. /// Allow the instance to be accessed statically, as only one will exist.
  41. /// </summary>
  42. internal static AutoTranslationPlugin Current;
  43. private XuaWindow _window;
  44. /// <summary>
  45. /// These are the currently running translation jobs (being translated by an http request).
  46. /// </summary>
  47. private List<TranslationJob> _completedJobs = new List<TranslationJob>();
  48. private Dictionary<string, TranslationJob> _unstartedJobs = new Dictionary<string, TranslationJob>();
  49. private Dictionary<string, TranslationJob> _ongoingJobs = new Dictionary<string, TranslationJob>();
  50. /// <summary>
  51. /// All the translations are stored in this dictionary.
  52. /// </summary>
  53. private Dictionary<string, string> _staticTranslations = new Dictionary<string, string>();
  54. private Dictionary<string, string> _translations = new Dictionary<string, string>();
  55. private Dictionary<string, string> _reverseTranslations = new Dictionary<string, string>();
  56. /// <summary>
  57. /// These are the new translations that has not yet been persisted to the file system.
  58. /// </summary>
  59. private object _writeToFileSync = new object();
  60. private Dictionary<string, string> _newTranslations = new Dictionary<string, string>();
  61. private HashSet<string> _newUntranslated = new HashSet<string>();
  62. /// <summary>
  63. /// Keeps track of things to copy to clipboard.
  64. /// </summary>
  65. private List<string> _textsToCopyToClipboardOrdered = new List<string>();
  66. private HashSet<string> _textsToCopyToClipboard = new HashSet<string>();
  67. private float _clipboardUpdated = 0.0f;
  68. /// <summary>
  69. /// The number of http translation errors that has occurred up until now.
  70. /// </summary>
  71. private int _consecutiveErrors = 0;
  72. /// <summary>
  73. /// This is a hash set that contains all Text components that is currently being worked on by
  74. /// the translation plugin.
  75. /// </summary>
  76. private HashSet<object> _ongoingOperations = new HashSet<object>();
  77. /// <summary>
  78. /// This function will check if there are symbols of a given language contained in a string.
  79. /// </summary>
  80. private Func<string, bool> _symbolCheck;
  81. /// <summary>
  82. /// Texts currently being scheduled for translation by 'immediate' components.
  83. /// </summary>
  84. private HashSet<string> _immediatelyTranslating = new HashSet<string>();
  85. private Dictionary<string, byte[]> _translatedImages = new Dictionary<string, byte[]>( StringComparer.InvariantCultureIgnoreCase );
  86. private HashSet<string> _untranslatedImages = new HashSet<string>();
  87. private Component _advEngine;
  88. private float? _nextAdvUpdate;
  89. private HttpSecurity _httpSecurity;
  90. private List<ConfiguredEndpoint> _configuredEndpoints;
  91. private ConfiguredEndpoint _endpoint;
  92. private int[] _currentTranslationsQueuedPerSecondRollingWindow = new int[ Settings.TranslationQueueWatchWindow ];
  93. private float? _timeExceededThreshold;
  94. private float _translationsQueuedPerSecond;
  95. private bool _isInTranslatedMode = true;
  96. private bool _textHooksEnabled = true;
  97. private bool _imageHooksEnabled = true;
  98. private bool _batchLogicHasFailed = false;
  99. private int _availableBatchOperations = Settings.MaxAvailableBatchOperations;
  100. private float _batchOperationSecondCounter = 0;
  101. private string[] _previouslyQueuedText = new string[ Settings.PreviousTextStaggerCount ];
  102. private int _staggerTextCursor = 0;
  103. private int _concurrentStaggers = 0;
  104. private int _frameForLastQueuedTranslation = -1;
  105. private int _consecutiveFramesTranslated = 0;
  106. private int _secondForQueuedTranslation = -1;
  107. private int _consecutiveSecondsTranslated = 0;
  108. private bool _hasOverrideFont = false;
  109. private bool _overrideFont = false;
  110. private bool _initialized = false;
  111. private bool _temporarilyDisabled = false;
  112. private string _requireSpriteRendererCheckCausedBy = null;
  113. private int _lastSpriteUpdateFrame = -1;
  114. private bool _isCalledFromSceneManager = false;
  115. public void Initialize()
  116. {
  117. Current = this;
  118. if( XuaLogger.Current == null )
  119. {
  120. XuaLogger.Current = new ConsoleLogger();
  121. }
  122. try
  123. {
  124. Settings.Configure();
  125. }
  126. catch( Exception e )
  127. {
  128. XuaLogger.Current.Error( e, "An error occurred during configuration. Shutting plugin down." );
  129. Settings.IsShutdown = true;
  130. Settings.IsShutdownFatal = true;
  131. return;
  132. }
  133. if( Settings.EnableConsole ) DebugConsole.Enable();
  134. HooksSetup.InstallTextHooks();
  135. HooksSetup.InstallImageHooks();
  136. HooksSetup.InstallTextGetterCompatHooks();
  137. _httpSecurity = new HttpSecurity();
  138. try
  139. {
  140. var context = new InitializationContext( _httpSecurity, Settings.FromLanguage, Settings.Language );
  141. _configuredEndpoints = KnownEndpoints.CreateEndpoints( gameObject, context )
  142. .OrderBy( x => x.Error != null )
  143. .ThenBy( x => x.Endpoint.FriendlyName )
  144. .ToList();
  145. }
  146. catch( Exception e )
  147. {
  148. XuaLogger.Current.Error( e, "An error occurred while constructing endpoints. Shutting plugin down." );
  149. Settings.IsShutdown = true;
  150. Settings.IsShutdownFatal = true;
  151. return;
  152. }
  153. try
  154. {
  155. var primaryEndpoint = _configuredEndpoints.FirstOrDefault( x => x.Endpoint.Id == Settings.ServiceEndpoint );
  156. if( primaryEndpoint == null ) throw new Exception( "The primary endpoint was not properly configured." );
  157. if( primaryEndpoint.Error != null ) throw new Exception( "The primary endpoint was not properly configured.", primaryEndpoint.Error );
  158. _endpoint = primaryEndpoint;
  159. }
  160. catch( Exception e )
  161. {
  162. XuaLogger.Current.Error( e, "An unexpected error occurred during initialization of endpoint." );
  163. }
  164. // TODO: Perhaps some bleeding edge check to see if this is required?
  165. var callback = _httpSecurity.GetCertificateValidationCheck();
  166. if( callback != null )
  167. {
  168. ServicePointManager.ServerCertificateValidationCallback += callback;
  169. }
  170. // Save again because configuration may be modified by endpoints
  171. try
  172. {
  173. Config.Current.SaveConfig();
  174. }
  175. catch( Exception e )
  176. {
  177. XuaLogger.Current.Error( e, "An error occurred during while saving configuration." );
  178. }
  179. if( !LanguageHelper.IsFromLanguageSupported( Settings.FromLanguage ) )
  180. {
  181. XuaLogger.Current.Error( $"The plugin has been configured to use the 'FromLanguage={Settings.FromLanguage}'. This language is not supported. Shutting plugin down." );
  182. _endpoint = null;
  183. Settings.IsShutdown = true;
  184. Settings.IsShutdownFatal = true;
  185. }
  186. _symbolCheck = LanguageHelper.GetSymbolCheck( Settings.FromLanguage );
  187. if( !string.IsNullOrEmpty( Settings.OverrideFont ) )
  188. {
  189. var available = Font.GetOSInstalledFontNames();
  190. if( !available.Contains( Settings.OverrideFont ) )
  191. {
  192. XuaLogger.Current.Error( $"The specified override font is not available. Available fonts: " + string.Join( ", ", available ) );
  193. Settings.OverrideFont = null;
  194. }
  195. else
  196. {
  197. _hasOverrideFont = true;
  198. }
  199. _overrideFont = _hasOverrideFont;
  200. }
  201. try
  202. {
  203. EnableSceneLoadScan();
  204. }
  205. catch( Exception e )
  206. {
  207. XuaLogger.Current.Error( e, "An error occurred while settings up texture scene-load scans." );
  208. }
  209. LoadTranslations();
  210. LoadStaticTranslations();
  211. _window = new XuaWindow(
  212. new List<ToggleViewModel>
  213. {
  214. new ToggleViewModel(
  215. " Translated",
  216. "<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.",
  217. "<b>NOT TRANSLATED</b>\nThe plugin currently displays untranslated texts.",
  218. ToggleTranslation, () => _isInTranslatedMode )
  219. },
  220. _configuredEndpoints.Select( x =>
  221. new TranslatorDropdownOptionViewModel( () => x == _endpoint, x, OnEndpointSelected ) ).ToList(),
  222. new List<ButtonViewModel>
  223. {
  224. 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 ),
  225. new ButtonViewModel( "Reload", "<b>RELOAD TRANSLATION</b>\nReloads all translation text files and texture files from disk.", ReloadTranslations, null ),
  226. 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 )
  227. },
  228. new List<LabelViewModel>
  229. {
  230. new LabelViewModel( "Version: ", () => PluginData.Version ),
  231. new LabelViewModel( "Status: ", () => Settings.IsShutdown ? "Shutdown" : "Running" ),
  232. new LabelViewModel( "Served translations: ", () => $"{Settings.TranslationCount} / {Settings.MaxTranslationsBeforeShutdown}" ),
  233. new LabelViewModel( "Queued translations: ", () => $"{(_unstartedJobs.Count + _ongoingJobs.Count)} / {Settings.MaxUnstartedJobs}" ),
  234. new LabelViewModel( "Error'ed translations: ", () => $"{_consecutiveErrors} / {Settings.MaxErrors}" ),
  235. } );
  236. UnityTextParsers.Initialize( text => IsTranslatable( text ) && IsBelowMaxLength( text ) );
  237. // start a thread that will periodically removed unused references
  238. var t1 = new Thread( MaintenanceLoop );
  239. t1.IsBackground = true;
  240. t1.Start();
  241. // start a thread that will periodically save new translations
  242. var t2 = new Thread( SaveTranslationsLoop );
  243. t2.IsBackground = true;
  244. t2.Start();
  245. }
  246. private void OnEndpointSelected( ConfiguredEndpoint endpoint )
  247. {
  248. if( _endpoint != endpoint )
  249. {
  250. _endpoint = endpoint;
  251. if( Settings.IsShutdown && !Settings.IsShutdownFatal )
  252. {
  253. RebootPlugin();
  254. ManualHook();
  255. }
  256. Settings.SetEndpoint( _endpoint.Endpoint.Id );
  257. }
  258. }
  259. private IEnumerable<string> GetTranslationFiles()
  260. {
  261. return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ).Parameterize(), $"*.txt", SearchOption.AllDirectories )
  262. .Select( x => x.Replace( "/", "\\" ) );
  263. }
  264. private IEnumerable<string> GetTextureFiles()
  265. {
  266. return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TextureDirectory ).Parameterize(), $"*.png", SearchOption.AllDirectories )
  267. .Select( x => x.Replace( "/", "\\" ) );
  268. }
  269. private void MaintenanceLoop( object state )
  270. {
  271. while( true )
  272. {
  273. try
  274. {
  275. ObjectReferenceMapper.Cull();
  276. }
  277. catch( Exception e )
  278. {
  279. XuaLogger.Current.Error( e, "An unexpected error occurred while removing GC'ed resources." );
  280. }
  281. Thread.Sleep( 1000 * 60 );
  282. }
  283. }
  284. private void SaveTranslationsLoop( object state )
  285. {
  286. try
  287. {
  288. while( true )
  289. {
  290. if( _newTranslations.Count > 0 )
  291. {
  292. lock( _writeToFileSync )
  293. {
  294. if( _newTranslations.Count > 0 )
  295. {
  296. using( var stream = File.Open( Settings.AutoTranslationsFilePath, FileMode.Append, FileAccess.Write ) )
  297. using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
  298. {
  299. foreach( var kvp in _newTranslations )
  300. {
  301. writer.WriteLine( TextHelper.Encode( kvp.Key ) + '=' + TextHelper.Encode( kvp.Value ) );
  302. }
  303. writer.Flush();
  304. }
  305. _newTranslations.Clear();
  306. }
  307. }
  308. }
  309. else
  310. {
  311. Thread.Sleep( 5000 );
  312. }
  313. }
  314. }
  315. catch( Exception e )
  316. {
  317. XuaLogger.Current.Error( e, "An error occurred while saving translations to disk." );
  318. }
  319. }
  320. private void EnableSceneLoadScan()
  321. {
  322. 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." );
  323. if( Features.SupportsScenes )
  324. {
  325. XuaLogger.Current.Info( "SceneManager is supported in this version of Unity." );
  326. EnableSceneLoadScanInternal();
  327. }
  328. else
  329. {
  330. XuaLogger.Current.Info( "SceneManager is not supported in this version of Unity. Falling back to OnLevelWasLoaded and Application level API." );
  331. }
  332. }
  333. private void EnableSceneLoadScanInternal()
  334. {
  335. // do this in a different class to avoid having an anonymous method with references to the "Scene" class
  336. SceneManagerLoader.EnableSceneLoadScanInternal( this );
  337. }
  338. internal void OnLevelWasLoadedFromSceneManager( int id )
  339. {
  340. try
  341. {
  342. _isCalledFromSceneManager = true;
  343. OnLevelWasLoaded( id );
  344. }
  345. finally
  346. {
  347. _isCalledFromSceneManager = false;
  348. }
  349. }
  350. private void OnLevelWasLoaded( int id )
  351. {
  352. if( !Features.SupportsScenes || ( Features.SupportsScenes && _isCalledFromSceneManager ) )
  353. {
  354. if( Settings.EnableTextureScanOnSceneLoad && ( Settings.EnableTextureDumping || Settings.EnableTextureTranslation ) )
  355. {
  356. XuaLogger.Current.Info( "Performing texture lookup during scene load..." );
  357. var startTime = Time.realtimeSinceStartup;
  358. ManualHookForTextures();
  359. var endTime = Time.realtimeSinceStartup;
  360. XuaLogger.Current.Info( $"Finished texture lookup (took {Math.Round( endTime - startTime, 2 )} seconds)" );
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// Loads the translations found in Translation.{lang}.txt
  366. /// </summary>
  367. private void LoadTranslations()
  368. {
  369. try
  370. {
  371. lock( _writeToFileSync )
  372. {
  373. Directory.CreateDirectory( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ).Parameterize() );
  374. Directory.CreateDirectory( Path.GetDirectoryName( Settings.AutoTranslationsFilePath ) );
  375. var mainTranslationFile = Settings.AutoTranslationsFilePath;
  376. LoadTranslationsInFile( mainTranslationFile );
  377. foreach( var fullFileName in GetTranslationFiles().Reverse().Except( new[] { mainTranslationFile } ) )
  378. {
  379. LoadTranslationsInFile( fullFileName );
  380. }
  381. }
  382. if( Settings.EnableTextureTranslation || Settings.EnableTextureDumping )
  383. {
  384. _translatedImages.Clear();
  385. _untranslatedImages.Clear();
  386. Directory.CreateDirectory( Path.Combine( Config.Current.DataPath, Settings.TextureDirectory ).Parameterize() );
  387. foreach( var fullFileName in GetTextureFiles() )
  388. {
  389. RegisterImageFromFile( fullFileName );
  390. }
  391. }
  392. }
  393. catch( Exception e )
  394. {
  395. XuaLogger.Current.Error( e, "An error occurred while loading translations." );
  396. }
  397. }
  398. private void RegisterImageFromFile( string fullFileName )
  399. {
  400. var fileName = Path.GetFileNameWithoutExtension( fullFileName );
  401. var startHash = fileName.LastIndexOf( "[" );
  402. var endHash = fileName.LastIndexOf( "]" );
  403. if( endHash > -1 && startHash > -1 && endHash > startHash )
  404. {
  405. var takeFrom = startHash + 1;
  406. // load based on whether or not the key is image hashed
  407. var parts = fileName.Substring( takeFrom, endHash - takeFrom ).Split( '-' );
  408. string key;
  409. string originalHash;
  410. if( parts.Length == 1 )
  411. {
  412. key = parts[ 0 ];
  413. originalHash = parts[ 0 ];
  414. }
  415. else if( parts.Length == 2 )
  416. {
  417. key = parts[ 0 ];
  418. originalHash = parts[ 1 ];
  419. }
  420. else
  421. {
  422. XuaLogger.Current.Warn( $"Image not loaded (unknown hash): {fullFileName}." );
  423. return;
  424. }
  425. var data = File.ReadAllBytes( fullFileName );
  426. var currentHash = HashHelper.Compute( data );
  427. var isModified = StringComparer.InvariantCultureIgnoreCase.Compare( originalHash, currentHash ) != 0;
  428. // only load images that someone has modified!
  429. if( Settings.LoadUnmodifiedTextures || isModified )
  430. {
  431. RegisterTranslatedImage( key, data );
  432. XuaLogger.Current.Debug( $"Image loaded: {fullFileName}." );
  433. }
  434. else
  435. {
  436. RegisterUntranslatedImage( key );
  437. XuaLogger.Current.Warn( $"Image not loaded (unmodified): {fullFileName}." );
  438. }
  439. //if( Settings.DeleteUnmodifiedTextures && !isModified )
  440. //{
  441. // try
  442. // {
  443. // File.Delete( fullFileName );
  444. // Logger.Current.Warn( $"Image deleted (unmodified): {fullFileName}." );
  445. // }
  446. // catch( Exception e )
  447. // {
  448. // Logger.Current.Warn( e, $"An error occurred while trying to delete unmodified image: {fullFileName}." );
  449. // }
  450. //}
  451. }
  452. else
  453. {
  454. XuaLogger.Current.Warn( $"Image not loaded (no hash): {fullFileName}." );
  455. }
  456. }
  457. private void RegisterImageFromData( string textureName, string key, byte[] data )
  458. {
  459. var name = textureName.SanitizeForFileSystem();
  460. var root = Path.Combine( Config.Current.DataPath, Settings.TextureDirectory ).Parameterize();
  461. var originalHash = HashHelper.Compute( data );
  462. // allow hash and key to be the same; only store one of them then!
  463. string fileName;
  464. if( key == originalHash )
  465. {
  466. fileName = name + " [" + key + "].png";
  467. }
  468. else
  469. {
  470. fileName = name + " [" + key + "-" + originalHash + "].png";
  471. }
  472. var fullName = Path.Combine( root, fileName );
  473. File.WriteAllBytes( fullName, data );
  474. XuaLogger.Current.Info( "Dumped texture file: " + fileName );
  475. if( Settings.LoadUnmodifiedTextures )
  476. {
  477. RegisterTranslatedImage( key, data );
  478. }
  479. else
  480. {
  481. RegisterUntranslatedImage( key );
  482. }
  483. }
  484. private void RegisterTranslatedImage( string key, byte[] data )
  485. {
  486. _translatedImages[ key ] = data;
  487. }
  488. private void RegisterUntranslatedImage( string key )
  489. {
  490. _untranslatedImages.Add( key );
  491. }
  492. private void LoadTranslationsInFile( string fullFileName )
  493. {
  494. if( File.Exists( fullFileName ) )
  495. {
  496. XuaLogger.Current.Debug( $"Loading texts: {fullFileName}." );
  497. string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
  498. foreach( string translation in translations )
  499. {
  500. for( int i = 0 ; i < TranslationSplitters.Length ; i++ )
  501. {
  502. var splitter = TranslationSplitters[ i ];
  503. string[] kvp = translation.Split( splitter, StringSplitOptions.None );
  504. if( kvp.Length == 2 )
  505. {
  506. string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
  507. string value = TextHelper.Decode( kvp[ 1 ] );
  508. if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) && IsTranslatable( key ) )
  509. {
  510. AddTranslation( key, value );
  511. break;
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. private void LoadStaticTranslations()
  519. {
  520. if( Settings.UseStaticTranslations && Settings.FromLanguage == Settings.DefaultFromLanguage && Settings.Language == Settings.DefaultLanguage )
  521. {
  522. var tab = new char[] { '\t' };
  523. var equals = new char[] { '=' };
  524. var splitters = new char[][] { tab, equals };
  525. // load static translations from previous titles
  526. string[] translations = Properties.Resources.StaticTranslations.Split( new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries );
  527. foreach( string translation in translations )
  528. {
  529. for( int i = 0 ; i < splitters.Length ; i++ )
  530. {
  531. var splitter = splitters[ i ];
  532. string[] kvp = translation.Split( splitter, StringSplitOptions.None );
  533. if( kvp.Length >= 2 )
  534. {
  535. string key = TextHelper.Decode( kvp[ 0 ].TrimIfConfigured() );
  536. string value = TextHelper.Decode( kvp[ 1 ].TrimIfConfigured() );
  537. if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
  538. {
  539. _staticTranslations[ key ] = value;
  540. break;
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. private TranslationJob GetOrCreateTranslationJobFor( object ui, TranslationKey key, TranslationContext context )
  548. {
  549. var lookupKey = key.GetDictionaryLookupKey();
  550. if( _unstartedJobs.TryGetValue( lookupKey, out TranslationJob unstartedJob ) )
  551. {
  552. unstartedJob.Associate( context );
  553. return unstartedJob;
  554. }
  555. if( _ongoingJobs.TryGetValue( lookupKey, out TranslationJob ongoingJob ) )
  556. {
  557. ongoingJob.Associate( context );
  558. return ongoingJob;
  559. }
  560. foreach( var completedJob in _completedJobs )
  561. {
  562. if( completedJob.Key.GetDictionaryLookupKey() == lookupKey )
  563. {
  564. completedJob.Associate( context );
  565. return completedJob;
  566. }
  567. }
  568. XuaLogger.Current.Debug( "Queued translation for: " + lookupKey );
  569. ongoingJob = new TranslationJob( key );
  570. if( ui != null )
  571. {
  572. ongoingJob.OriginalSources.Add( ui );
  573. }
  574. ongoingJob.Associate( context );
  575. _unstartedJobs.Add( lookupKey, ongoingJob );
  576. CheckStaggerText( lookupKey );
  577. CheckConsecutiveFrames();
  578. CheckConsecutiveSeconds();
  579. CheckThresholds();
  580. return ongoingJob;
  581. }
  582. private void CheckConsecutiveSeconds()
  583. {
  584. var currentSecond = (int)Time.time;
  585. var lastSecond = currentSecond - 1;
  586. if( lastSecond == _secondForQueuedTranslation )
  587. {
  588. // we also queued something last frame, lets increment our counter
  589. _consecutiveSecondsTranslated++;
  590. if( _consecutiveSecondsTranslated > Settings.MaximumConsecutiveSecondsTranslated )
  591. {
  592. // Shutdown, this wont be tolerated!!!
  593. _unstartedJobs.Clear();
  594. _completedJobs.Clear();
  595. _ongoingJobs.Clear();
  596. Settings.IsShutdown = true;
  597. Settings.IsShutdownFatal = true;
  598. XuaLogger.Current.Error( $"SPAM DETECTED: Translations were queued every second for more than {Settings.MaximumConsecutiveSecondsTranslated} consecutive seconds. Shutting down plugin." );
  599. }
  600. }
  601. else if( currentSecond == _secondForQueuedTranslation )
  602. {
  603. // do nothing, there may be multiple translations per frame, that wont increase this counter
  604. }
  605. else
  606. {
  607. // but if multiple Update frames has passed, we will reset the counter
  608. _consecutiveSecondsTranslated = 0;
  609. }
  610. _secondForQueuedTranslation = currentSecond;
  611. }
  612. private void CheckConsecutiveFrames()
  613. {
  614. var currentFrame = Time.frameCount;
  615. var lastFrame = currentFrame - 1;
  616. if( lastFrame == _frameForLastQueuedTranslation )
  617. {
  618. // we also queued something last frame, lets increment our counter
  619. _consecutiveFramesTranslated++;
  620. if( _consecutiveFramesTranslated > Settings.MaximumConsecutiveFramesTranslated )
  621. {
  622. // Shutdown, this wont be tolerated!!!
  623. _unstartedJobs.Clear();
  624. _completedJobs.Clear();
  625. _ongoingJobs.Clear();
  626. Settings.IsShutdown = true;
  627. Settings.IsShutdownFatal = true;
  628. XuaLogger.Current.Error( $"SPAM DETECTED: Translations were queued every frame for more than {Settings.MaximumConsecutiveFramesTranslated} consecutive frames. Shutting down plugin." );
  629. }
  630. }
  631. else if( currentFrame == _frameForLastQueuedTranslation )
  632. {
  633. // do nothing, there may be multiple translations per frame, that wont increase this counter
  634. }
  635. else if( _consecutiveFramesTranslated > 0 )
  636. {
  637. // but if multiple Update frames has passed, we will reset the counter
  638. _consecutiveFramesTranslated--;
  639. }
  640. _frameForLastQueuedTranslation = currentFrame;
  641. }
  642. private void PeriodicResetFrameCheck()
  643. {
  644. var currentSecond = (int)Time.time;
  645. if( currentSecond % 100 == 0 )
  646. {
  647. _consecutiveFramesTranslated = 0;
  648. }
  649. }
  650. private void CheckStaggerText( string untranslatedText )
  651. {
  652. bool wasProblematic = false;
  653. for( int i = 0 ; i < _previouslyQueuedText.Length ; i++ )
  654. {
  655. var previouslyQueuedText = _previouslyQueuedText[ i ];
  656. if( previouslyQueuedText != null )
  657. {
  658. if( untranslatedText.RemindsOf( previouslyQueuedText ) )
  659. {
  660. wasProblematic = true;
  661. break;
  662. }
  663. }
  664. }
  665. if( wasProblematic )
  666. {
  667. _concurrentStaggers++;
  668. if( _concurrentStaggers > Settings.MaximumStaggers )
  669. {
  670. _unstartedJobs.Clear();
  671. _completedJobs.Clear();
  672. _ongoingJobs.Clear();
  673. Settings.IsShutdown = true;
  674. Settings.IsShutdownFatal = true;
  675. XuaLogger.Current.Error( $"SPAM DETECTED: Text that is 'scrolling in' is being translated. Disable that feature. Shutting down plugin." );
  676. }
  677. }
  678. else
  679. {
  680. _concurrentStaggers = 0;
  681. }
  682. _previouslyQueuedText[ _staggerTextCursor % _previouslyQueuedText.Length ] = untranslatedText;
  683. _staggerTextCursor++;
  684. }
  685. private void CheckThresholds()
  686. {
  687. if( _unstartedJobs.Count > Settings.MaxUnstartedJobs )
  688. {
  689. _unstartedJobs.Clear();
  690. _completedJobs.Clear();
  691. _ongoingJobs.Clear();
  692. Settings.IsShutdown = true;
  693. Settings.IsShutdownFatal = true;
  694. XuaLogger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxUnstartedJobs} queued for translations due to unknown reasons. Shutting down plugin." );
  695. }
  696. var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
  697. var newIdx = ( (int)Time.time ) % Settings.TranslationQueueWatchWindow;
  698. if( previousIdx != newIdx )
  699. {
  700. _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ] = 0;
  701. }
  702. _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ]++;
  703. var translationsInWindow = _currentTranslationsQueuedPerSecondRollingWindow.Sum();
  704. _translationsQueuedPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
  705. if( _translationsQueuedPerSecond > Settings.MaxTranslationsQueuedPerSecond )
  706. {
  707. if( !_timeExceededThreshold.HasValue )
  708. {
  709. _timeExceededThreshold = Time.time;
  710. }
  711. if( Time.time - _timeExceededThreshold.Value > Settings.MaxSecondsAboveTranslationThreshold )
  712. {
  713. _unstartedJobs.Clear();
  714. _completedJobs.Clear();
  715. _ongoingJobs.Clear();
  716. Settings.IsShutdown = true;
  717. Settings.IsShutdownFatal = true;
  718. XuaLogger.Current.Error( $"SPAM DETECTED: More than {Settings.MaxTranslationsQueuedPerSecond} translations per seconds queued for a {Settings.MaxSecondsAboveTranslationThreshold} second period. Shutting down plugin." );
  719. }
  720. }
  721. else
  722. {
  723. _timeExceededThreshold = null;
  724. }
  725. }
  726. private void IncrementBatchOperations()
  727. {
  728. _batchOperationSecondCounter += Time.deltaTime;
  729. if( _batchOperationSecondCounter > Settings.IncreaseBatchOperationsEvery )
  730. {
  731. if( _availableBatchOperations < Settings.MaxAvailableBatchOperations )
  732. {
  733. _availableBatchOperations++;
  734. }
  735. _batchOperationSecondCounter = 0;
  736. }
  737. }
  738. private void ResetThresholdTimerIfRequired()
  739. {
  740. var previousIdx = ( (int)( Time.time - Time.deltaTime ) ) % Settings.TranslationQueueWatchWindow;
  741. var newIdx = ( (int)Time.time ) % Settings.TranslationQueueWatchWindow;
  742. if( previousIdx != newIdx )
  743. {
  744. _currentTranslationsQueuedPerSecondRollingWindow[ newIdx ] = 0;
  745. }
  746. var translationsInWindow = _currentTranslationsQueuedPerSecondRollingWindow.Sum();
  747. _translationsQueuedPerSecond = (float)translationsInWindow / Settings.TranslationQueueWatchWindow;
  748. if( _translationsQueuedPerSecond <= Settings.MaxTranslationsQueuedPerSecond )
  749. {
  750. _timeExceededThreshold = null;
  751. }
  752. }
  753. private bool IsImageRegistered( string key )
  754. {
  755. return _translatedImages.ContainsKey( key ) || _untranslatedImages.Contains( key );
  756. }
  757. private bool TryGetTranslatedImage( string key, out byte[] data )
  758. {
  759. return _translatedImages.TryGetValue( key, out data );
  760. }
  761. private void AddTranslation( string key, string value )
  762. {
  763. _translations[ key ] = value;
  764. _reverseTranslations[ value ] = key;
  765. }
  766. private void AddTranslation( TranslationKey key, string value )
  767. {
  768. var lookup = key.GetDictionaryLookupKey();
  769. _translations[ lookup ] = value;
  770. _reverseTranslations[ value ] = lookup;
  771. }
  772. private void UpdateSpriteRenderers()
  773. {
  774. if( Settings.EnableSpriteRendererHooking && ( Settings.EnableTextureTranslation || Settings.EnableTextureDumping ) )
  775. {
  776. if( _requireSpriteRendererCheckCausedBy != null )
  777. {
  778. try
  779. {
  780. var start = Time.realtimeSinceStartup;
  781. var spriteRenderers = GameObject.FindObjectsOfType<SpriteRenderer>();
  782. foreach( var sr in spriteRenderers )
  783. {
  784. // simulate a hook
  785. Hook_ImageChangedOnComponent( sr, null, false, false );
  786. }
  787. var end = Time.realtimeSinceStartup;
  788. var delta = Math.Round( end - start, 2 );
  789. XuaLogger.Current.Debug( $"Update SpriteRenderers caused by {_requireSpriteRendererCheckCausedBy} component (took " + delta + " seconds)" );
  790. }
  791. finally
  792. {
  793. _requireSpriteRendererCheckCausedBy = null;
  794. }
  795. }
  796. }
  797. }
  798. private void QueueNewUntranslatedForClipboard( TranslationKey key )
  799. {
  800. if( Settings.CopyToClipboard && Features.SupportsClipboard )
  801. {
  802. if( !_textsToCopyToClipboard.Contains( key.RelevantText ) )
  803. {
  804. _textsToCopyToClipboard.Add( key.RelevantText );
  805. _textsToCopyToClipboardOrdered.Add( key.RelevantText );
  806. _clipboardUpdated = Time.realtimeSinceStartup;
  807. }
  808. }
  809. }
  810. private void QueueNewUntranslatedForDisk( TranslationKey key )
  811. {
  812. _newUntranslated.Add( key.GetDictionaryLookupKey() );
  813. }
  814. private void QueueNewTranslationForDisk( TranslationKey key, string value )
  815. {
  816. lock( _writeToFileSync )
  817. {
  818. _newTranslations[ key.GetDictionaryLookupKey() ] = value;
  819. }
  820. }
  821. private void QueueNewTranslationForDisk( string key, string value )
  822. {
  823. lock( _writeToFileSync )
  824. {
  825. _newTranslations[ key ] = value;
  826. }
  827. }
  828. private bool TryGetTranslation( TranslationKey key, out string value )
  829. {
  830. return TryGetTranslation( key.GetDictionaryLookupKey(), out value );
  831. }
  832. private bool TryGetTranslation( string key, out string value )
  833. {
  834. var result = _translations.TryGetValue( key, out value );
  835. if( result )
  836. {
  837. return result;
  838. }
  839. else if( _staticTranslations.Count > 0 )
  840. {
  841. if( _staticTranslations.TryGetValue( key, out value ) )
  842. {
  843. QueueNewTranslationForDisk( key, value );
  844. AddTranslation( key, value );
  845. return true;
  846. }
  847. }
  848. return result;
  849. }
  850. internal bool TryGetReverseTranslation( string value, out string key )
  851. {
  852. return _reverseTranslations.TryGetValue( value, out key );
  853. }
  854. internal string Hook_TextChanged_WithResult( object ui, string text )
  855. {
  856. if( !ui.IsKnownTextType() ) return null;
  857. if( _textHooksEnabled && !_temporarilyDisabled )
  858. {
  859. return TranslateOrQueueWebJob( ui, text, false );
  860. }
  861. return null;
  862. }
  863. internal string ExternalHook_TextChanged_WithResult( object ui, string text )
  864. {
  865. if( !ui.IsKnownTextType() ) return null;
  866. if( _textHooksEnabled && !_temporarilyDisabled )
  867. {
  868. return TranslateOrQueueWebJob( ui, text, true );
  869. }
  870. return null;
  871. }
  872. internal void Hook_TextChanged( object ui, bool onEnable )
  873. {
  874. if( _textHooksEnabled && !_temporarilyDisabled )
  875. {
  876. TranslateOrQueueWebJob( ui, null, false );
  877. }
  878. if( onEnable )
  879. {
  880. CheckSpriteRenderer( ui );
  881. }
  882. }
  883. internal void Hook_ImageChangedOnComponent( object source, Texture2D texture, bool isPrefixHooked, bool onEnable )
  884. {
  885. if( !_imageHooksEnabled ) return;
  886. if( !source.IsKnownImageType() ) return;
  887. HandleImage( source, texture, isPrefixHooked );
  888. if( onEnable )
  889. {
  890. CheckSpriteRenderer( source );
  891. }
  892. }
  893. internal void Hook_ImageChanged( Texture2D texture, bool isPrefixHooked )
  894. {
  895. if( !_imageHooksEnabled ) return;
  896. if( texture == null ) return;
  897. HandleImage( null, texture, isPrefixHooked );
  898. }
  899. private void SetTranslatedText( object ui, string translatedText, TextTranslationInfo info )
  900. {
  901. info?.SetTranslatedText( translatedText );
  902. if( _isInTranslatedMode )
  903. {
  904. SetText( ui, translatedText, true, info );
  905. }
  906. }
  907. internal void Hook_HandleComponent( object ui )
  908. {
  909. if( _hasOverrideFont )
  910. {
  911. var info = ui.GetOrCreateTextTranslationInfo();
  912. if( _overrideFont )
  913. {
  914. info?.ChangeFont( ui );
  915. }
  916. else
  917. {
  918. info?.UnchangeFont( ui );
  919. }
  920. }
  921. if( Settings.ForceUIResizing )
  922. {
  923. var info = ui.GetOrCreateTextTranslationInfo();
  924. if( info?.IsCurrentlySettingText == false )
  925. {
  926. // force UI resizing is highly problematic for NGUI because text should somehow
  927. // be set after changing "resize" properties... brilliant stuff
  928. if( ui.GetType() != ClrTypes.UILabel )
  929. {
  930. info?.ResizeUI( ui );
  931. }
  932. }
  933. }
  934. }
  935. private void CheckSpriteRenderer( object ui )
  936. {
  937. if( Settings.EnableSpriteRendererHooking )
  938. {
  939. var currentFrame = Time.frameCount;
  940. var lastFrame = currentFrame - 1;
  941. if( lastFrame != _lastSpriteUpdateFrame && currentFrame != _lastSpriteUpdateFrame )
  942. {
  943. _requireSpriteRendererCheckCausedBy = ui?.GetType().Name;
  944. }
  945. _lastSpriteUpdateFrame = currentFrame;
  946. }
  947. }
  948. /// <summary>
  949. /// Sets the text of a UI text, while ensuring this will not fire a text changed event.
  950. /// </summary>
  951. private void SetText( object ui, string text, bool isTranslated, TextTranslationInfo info )
  952. {
  953. if( !info?.IsCurrentlySettingText ?? true )
  954. {
  955. try
  956. {
  957. _textHooksEnabled = false;
  958. if( info != null )
  959. {
  960. info.IsCurrentlySettingText = true;
  961. }
  962. if( Settings.EnableUIResizing || Settings.ForceUIResizing )
  963. {
  964. if( isTranslated || Settings.ForceUIResizing )
  965. {
  966. info?.ResizeUI( ui );
  967. }
  968. else
  969. {
  970. info?.UnresizeUI( ui );
  971. }
  972. }
  973. // NGUI only behaves if you set the text after the resize behaviour
  974. ui.SetText( text );
  975. info?.ResetScrollIn( ui );
  976. }
  977. catch( TargetInvocationException )
  978. {
  979. // might happen with NGUI
  980. }
  981. catch( NullReferenceException )
  982. {
  983. // This is likely happened due to a scene change.
  984. }
  985. catch( Exception e )
  986. {
  987. XuaLogger.Current.Error( e, "An error occurred while setting text on a component." );
  988. }
  989. finally
  990. {
  991. _textHooksEnabled = true;
  992. if( info != null )
  993. {
  994. info.IsCurrentlySettingText = false;
  995. }
  996. }
  997. }
  998. }
  999. /// <summary>
  1000. /// Determines if a text should be translated.
  1001. /// </summary>
  1002. private bool IsTranslatable( string str )
  1003. {
  1004. return _symbolCheck( str )
  1005. //&& str.Length <= Settings.MaxCharactersPerTranslation
  1006. && !_reverseTranslations.ContainsKey( str )
  1007. && !Settings.IgnoreTextStartingWith.Any( x => str.StartsWithStrict( x ) );
  1008. }
  1009. private bool IsBelowMaxLength( string str )
  1010. {
  1011. return str.Length <= Settings.MaxCharactersPerTranslation;
  1012. }
  1013. private bool IsBelowMaxLengthStrict( string str )
  1014. {
  1015. return str.Length <= ( Settings.MaxCharactersPerTranslation / 2 );
  1016. }
  1017. private bool ShouldTranslateImageComponent( object ui )
  1018. {
  1019. var component = ui as Component;
  1020. if( component != null )
  1021. {
  1022. // dummy check
  1023. var go = component.gameObject;
  1024. var ignore = go.HasIgnoredName();
  1025. if( ignore )
  1026. {
  1027. return false;
  1028. }
  1029. var behaviour = component as Behaviour;
  1030. if( behaviour?.isActiveAndEnabled == false )
  1031. {
  1032. return false;
  1033. }
  1034. }
  1035. return true;
  1036. }
  1037. private bool ShouldTranslateTextComponent( object ui, bool ignoreComponentState )
  1038. {
  1039. var component = ui as Component;
  1040. if( component != null )
  1041. {
  1042. // dummy check
  1043. var go = component.gameObject;
  1044. var ignore = go.HasIgnoredName();
  1045. if( ignore )
  1046. {
  1047. return false;
  1048. }
  1049. if( !ignoreComponentState )
  1050. {
  1051. var behaviour = component as Behaviour;
  1052. if( behaviour?.isActiveAndEnabled == false )
  1053. {
  1054. return false;
  1055. }
  1056. }
  1057. var inputField = component.gameObject.GetFirstComponentInSelfOrAncestor( ClrTypes.InputField )
  1058. ?? component.gameObject.GetFirstComponentInSelfOrAncestor( ClrTypes.TMP_InputField );
  1059. return inputField == null;
  1060. }
  1061. return true;
  1062. }
  1063. private string TranslateOrQueueWebJob( object ui, string text, bool ignoreComponentState )
  1064. {
  1065. var info = ui.GetOrCreateTextTranslationInfo();
  1066. if( _ongoingOperations.Contains( ui ) )
  1067. {
  1068. return TranslateImmediate( ui, text, info, ignoreComponentState );
  1069. }
  1070. var supportsStabilization = ui.SupportsStabilization();
  1071. if( Settings.Delay == 0 || !supportsStabilization )
  1072. {
  1073. return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
  1074. }
  1075. else
  1076. {
  1077. StartCoroutine(
  1078. DelayForSeconds( Settings.Delay, () =>
  1079. {
  1080. TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
  1081. } ) );
  1082. }
  1083. return null;
  1084. }
  1085. private static bool IsCurrentlySetting( TextTranslationInfo info )
  1086. {
  1087. if( info == null ) return false;
  1088. return info.IsCurrentlySettingText;
  1089. }
  1090. private void HandleImage( object source, Texture2D texture, bool isPrefixHooked )
  1091. {
  1092. if( Settings.EnableTextureDumping )
  1093. {
  1094. try
  1095. {
  1096. DumpTexture( source, texture );
  1097. }
  1098. catch( Exception e )
  1099. {
  1100. XuaLogger.Current.Error( e, "An error occurred while dumping texture." );
  1101. }
  1102. }
  1103. if( Settings.EnableTextureTranslation )
  1104. {
  1105. try
  1106. {
  1107. TranslateTexture( source, texture, isPrefixHooked, null );
  1108. }
  1109. catch( Exception e )
  1110. {
  1111. XuaLogger.Current.Error( e, "An error occurred while translating texture." );
  1112. }
  1113. }
  1114. }
  1115. private void TranslateTexture( object ui, TextureReloadContext context )
  1116. {
  1117. if( ui is Texture2D texture2d )
  1118. {
  1119. TranslateTexture( null, texture2d, false, context );
  1120. }
  1121. else
  1122. {
  1123. TranslateTexture( ui, null, false, context );
  1124. }
  1125. }
  1126. private void TranslateTexture( object source, Texture2D texture, bool isPrefixHooked, TextureReloadContext context )
  1127. {
  1128. try
  1129. {
  1130. _imageHooksEnabled = false;
  1131. texture = texture ?? source.GetTexture();
  1132. if( texture == null ) return;
  1133. var tti = texture.GetOrCreateTextureTranslationInfo();
  1134. var iti = source.GetOrCreateImageTranslationInfo();
  1135. var key = tti.GetKey( texture );
  1136. if( string.IsNullOrEmpty( key ) ) return;
  1137. bool hasContext = context != null;
  1138. bool forceReload = false;
  1139. if( hasContext )
  1140. {
  1141. forceReload = context.RegisterTextureInContextAndDetermineWhetherToReload( texture );
  1142. }
  1143. if( TryGetTranslatedImage( key, out var newData ) )
  1144. {
  1145. if( _isInTranslatedMode )
  1146. {
  1147. // handle texture
  1148. if( !tti.IsTranslated || forceReload )
  1149. {
  1150. try
  1151. {
  1152. texture.LoadImageEx( newData, tti.IsNonReadable( texture ) );
  1153. }
  1154. finally
  1155. {
  1156. tti.IsTranslated = true;
  1157. }
  1158. }
  1159. // handle containing component
  1160. if( iti != null )
  1161. {
  1162. if( !iti.IsTranslated || hasContext )
  1163. {
  1164. try
  1165. {
  1166. if( !isPrefixHooked )
  1167. {
  1168. source.SetAllDirtyEx();
  1169. }
  1170. }
  1171. finally
  1172. {
  1173. iti.IsTranslated = true;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. else
  1180. {
  1181. // if we cannot find the texture, and the texture is considered translated... hmmm someone has removed a file
  1182. // handle texture
  1183. var originalData = tti.GetOriginalData( texture );
  1184. if( originalData != null )
  1185. {
  1186. if( tti.IsTranslated )
  1187. {
  1188. try
  1189. {
  1190. texture.LoadImageEx( originalData, tti.IsNonReadable( texture ) );
  1191. }
  1192. finally
  1193. {
  1194. tti.IsTranslated = true;
  1195. }
  1196. }
  1197. // handle containing component
  1198. if( iti != null )
  1199. {
  1200. if( iti.IsTranslated )
  1201. {
  1202. try
  1203. {
  1204. if( !isPrefixHooked )
  1205. {
  1206. source.SetAllDirtyEx();
  1207. }
  1208. }
  1209. finally
  1210. {
  1211. iti.IsTranslated = true;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. if( !_isInTranslatedMode )
  1218. {
  1219. var originalData = tti.GetOriginalData( texture );
  1220. if( originalData != null )
  1221. {
  1222. // handle texture
  1223. if( tti.IsTranslated )
  1224. {
  1225. try
  1226. {
  1227. texture.LoadImageEx( originalData, tti.IsNonReadable( texture ) );
  1228. }
  1229. finally
  1230. {
  1231. tti.IsTranslated = false;
  1232. }
  1233. }
  1234. // handle containing component
  1235. if( iti != null )
  1236. {
  1237. if( iti.IsTranslated )
  1238. {
  1239. try
  1240. {
  1241. if( !isPrefixHooked )
  1242. {
  1243. source.SetAllDirtyEx();
  1244. }
  1245. }
  1246. finally
  1247. {
  1248. iti.IsTranslated = false;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. }
  1254. if( forceReload )
  1255. {
  1256. XuaLogger.Current.Info( $"Reloaded texture: {texture.name} ({key})." );
  1257. }
  1258. }
  1259. finally
  1260. {
  1261. _imageHooksEnabled = true;
  1262. }
  1263. }
  1264. private void DumpTexture( object source, Texture2D texture )
  1265. {
  1266. try
  1267. {
  1268. _imageHooksEnabled = false;
  1269. texture = texture ?? source.GetTexture();
  1270. if( texture == null ) return;
  1271. var info = texture.GetOrCreateTextureTranslationInfo();
  1272. if( info.HasDumpedAlternativeTexture ) return;
  1273. try
  1274. {
  1275. if( ShouldTranslate( texture ) )
  1276. {
  1277. var key = info.GetKey( texture );
  1278. if( string.IsNullOrEmpty( key ) ) return;
  1279. if( !IsImageRegistered( key ) )
  1280. {
  1281. var name = texture.GetTextureName();
  1282. //var format = "[" + texture.format.ToString() + "] ";
  1283. var originalData = info.GetOrCreateOriginalData( texture );
  1284. RegisterImageFromData( name, key, originalData );
  1285. }
  1286. }
  1287. }
  1288. finally
  1289. {
  1290. info.HasDumpedAlternativeTexture = true;
  1291. }
  1292. }
  1293. finally
  1294. {
  1295. _imageHooksEnabled = true;
  1296. }
  1297. }
  1298. private bool ShouldTranslate( Texture2D texture )
  1299. {
  1300. // convert to int so engine versions that does not have specific enums still work
  1301. var format = (int)texture.format;
  1302. // 1 = Alpha8
  1303. // 9 = R16
  1304. // 63 = R8
  1305. return format != 1
  1306. && format != 9
  1307. && format != 63;
  1308. }
  1309. private string TranslateImmediate( object ui, string text, TextTranslationInfo info, bool ignoreComponentState )
  1310. {
  1311. // Get the trimmed text
  1312. text = ( text ?? ui.GetText() ).TrimIfConfigured();
  1313. if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslateTextComponent( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
  1314. {
  1315. info?.Reset( text );
  1316. var textKey = new TranslationKey( ui, text, ui.IsSpammingComponent(), false );
  1317. // if we already have translation loaded in our _translatios dictionary, simply load it and set text
  1318. string translation;
  1319. if( TryGetTranslation( textKey, out translation ) )
  1320. {
  1321. if( !string.IsNullOrEmpty( translation ) )
  1322. {
  1323. SetTranslatedText( ui, textKey.Untemplate( translation ), info );
  1324. return translation;
  1325. }
  1326. }
  1327. else
  1328. {
  1329. if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
  1330. {
  1331. var result = UnityTextParsers.GameLogTextParser.Parse( text );
  1332. if( result.Succeeded )
  1333. {
  1334. translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, false );
  1335. if( translation != null )
  1336. {
  1337. SetTranslatedText( ui, translation, info );
  1338. return translation;
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. return null;
  1345. }
  1346. /// <summary>
  1347. /// Translates the string of a UI text or queues it up to be translated
  1348. /// by the HTTP translation service.
  1349. /// </summary>
  1350. private string TranslateOrQueueWebJobImmediate( object ui, string text, TextTranslationInfo info, bool supportsStabilization, bool ignoreComponentState, TranslationContext context = null )
  1351. {
  1352. text = text ?? ui.GetText();
  1353. // make sure text exists
  1354. var originalText = text;
  1355. if( context == null )
  1356. {
  1357. // Get the trimmed text
  1358. text = text.TrimIfConfigured();
  1359. }
  1360. // Ensure that we actually want to translate this text and its owning UI element.
  1361. if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslateTextComponent( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
  1362. {
  1363. //Logger.Current.Debug( "START: " + ui.GetType().Name + ": " + text );
  1364. info?.Reset( originalText );
  1365. var isSpammer = ui.IsSpammingComponent();
  1366. var textKey = new TranslationKey( ui, text, isSpammer, context != null );
  1367. // if we already have translation loaded in our _translatios dictionary, simply load it and set text
  1368. string translation;
  1369. if( TryGetTranslation( textKey, out translation ) )
  1370. {
  1371. QueueNewUntranslatedForClipboard( textKey );
  1372. if( !string.IsNullOrEmpty( translation ) )
  1373. {
  1374. if( context == null ) // never set text if operation is contextualized (only a part translation)
  1375. {
  1376. SetTranslatedText( ui, textKey.Untemplate( translation ), info );
  1377. }
  1378. return translation;
  1379. }
  1380. }
  1381. else
  1382. {
  1383. if( context == null )
  1384. {
  1385. if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
  1386. {
  1387. var result = UnityTextParsers.GameLogTextParser.Parse( text );
  1388. if( result.Succeeded )
  1389. {
  1390. translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, false );
  1391. if( translation != null )
  1392. {
  1393. SetTranslatedText( ui, translation, info );
  1394. return translation;
  1395. }
  1396. }
  1397. }
  1398. else if( UnityTextParsers.RichTextParser.CanApply( ui ) && IsBelowMaxLength( text ) )
  1399. {
  1400. var result = UnityTextParsers.RichTextParser.Parse( text );
  1401. if( result.Succeeded )
  1402. {
  1403. var isWhitelisted = ui.IsWhitelistedForImmediateRichTextTranslation();
  1404. translation = TranslateOrQueueWebJobImmediateByParserResult( ui, result, isWhitelisted );
  1405. if( translation != null )
  1406. {
  1407. SetTranslatedText( ui, translation, info );
  1408. return translation;
  1409. }
  1410. else if( isWhitelisted )
  1411. {
  1412. return null;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. if( supportsStabilization && context == null ) // never stabilize a text that is contextualized or that does not support stabilization
  1418. {
  1419. // if we dont know what text to translate it to, we need to figure it out.
  1420. // this might take a while, so add the UI text component to the ongoing operations
  1421. // list, so we dont start multiple operations for it, as its text might be constantly
  1422. // changing.
  1423. _ongoingOperations.Add( ui );
  1424. // start a coroutine, that will execute once the string of the UI text has stopped
  1425. // changing. For all texts except 'story' texts, this will add a delay for exactly
  1426. // 0.5s to the translation. This is barely noticable.
  1427. //
  1428. // on the other hand, for 'story' texts, this will take the time that it takes
  1429. // for the text to stop 'scrolling' in.
  1430. try
  1431. {
  1432. StartCoroutine(
  1433. WaitForTextStablization(
  1434. ui: ui,
  1435. delay: 1.0f, // 1 second to prevent '1 second tickers' from getting translated
  1436. maxTries: 60, // 50 tries, about 1 minute
  1437. currentTries: 0,
  1438. onMaxTriesExceeded: () =>
  1439. {
  1440. _ongoingOperations.Remove( ui );
  1441. },
  1442. onTextStabilized: stabilizedText =>
  1443. {
  1444. _ongoingOperations.Remove( ui );
  1445. originalText = stabilizedText;
  1446. stabilizedText = stabilizedText.TrimIfConfigured();
  1447. if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
  1448. {
  1449. var stabilizedTextKey = new TranslationKey( ui, stabilizedText, false );
  1450. QueueNewUntranslatedForClipboard( stabilizedTextKey );
  1451. info?.Reset( originalText );
  1452. // once the text has stabilized, attempt to look it up
  1453. if( TryGetTranslation( stabilizedTextKey, out translation ) )
  1454. {
  1455. if( !string.IsNullOrEmpty( translation ) )
  1456. {
  1457. // stabilized, no need to untemplate
  1458. SetTranslatedText( ui, translation, info );
  1459. }
  1460. }
  1461. else
  1462. {
  1463. if( context == null )
  1464. {
  1465. if( UnityTextParsers.GameLogTextParser.CanApply( ui ) )
  1466. {
  1467. var result = UnityTextParsers.GameLogTextParser.Parse( stabilizedText );
  1468. if( result.Succeeded )
  1469. {
  1470. var translatedText = TranslateOrQueueWebJobImmediateByParserResult( ui, result, true );
  1471. if( translatedText != null )
  1472. {
  1473. // stabilized, no need to untemplate
  1474. SetTranslatedText( ui, translatedText, info );
  1475. }
  1476. return;
  1477. }
  1478. }
  1479. else if( UnityTextParsers.RichTextParser.CanApply( ui ) && IsBelowMaxLength( stabilizedText ) )
  1480. {
  1481. var result = UnityTextParsers.RichTextParser.Parse( stabilizedText );
  1482. if( result.Succeeded )
  1483. {
  1484. var translatedText = TranslateOrQueueWebJobImmediateByParserResult( ui, result, true );
  1485. if( translatedText != null )
  1486. {
  1487. // stabilized, no need to untemplate
  1488. SetTranslatedText( ui, translatedText, info );
  1489. }
  1490. return;
  1491. }
  1492. }
  1493. }
  1494. // Lets try not to spam a service that might not be there...
  1495. if( _endpoint != null )
  1496. {
  1497. if( IsBelowMaxLength( stabilizedText ) )
  1498. {
  1499. if( !Settings.IsShutdown )
  1500. {
  1501. var job = GetOrCreateTranslationJobFor( ui, stabilizedTextKey, context );
  1502. job.Components.Add( ui );
  1503. }
  1504. }
  1505. }
  1506. else
  1507. {
  1508. QueueNewUntranslatedForDisk( stabilizedTextKey );
  1509. }
  1510. }
  1511. }
  1512. } ) );
  1513. }
  1514. catch( Exception )
  1515. {
  1516. _ongoingOperations.Remove( ui );
  1517. }
  1518. }
  1519. else if( !isSpammer || ( isSpammer && IsBelowMaxLengthStrict( text ) ) )
  1520. {
  1521. if( context != null )
  1522. {
  1523. // if there is a context, this is a part-translation, which means it is not a candidate for scrolling-in text
  1524. if( _endpoint != null )
  1525. {
  1526. if( !Settings.IsShutdown )
  1527. {
  1528. // once the text has stabilized, attempt to look it up
  1529. var job = GetOrCreateTranslationJobFor( ui, textKey, context );
  1530. }
  1531. }
  1532. else
  1533. {
  1534. QueueNewUntranslatedForDisk( textKey );
  1535. }
  1536. }
  1537. else
  1538. {
  1539. StartCoroutine(
  1540. WaitForTextStablization(
  1541. textKey: textKey,
  1542. delay: 1.0f,
  1543. onTextStabilized: () =>
  1544. {
  1545. // Lets try not to spam a service that might not be there...
  1546. if( _endpoint != null )
  1547. {
  1548. // once the text has stabilized, attempt to look it up
  1549. if( !Settings.IsShutdown )
  1550. {
  1551. if( !TryGetTranslation( textKey, out translation ) )
  1552. {
  1553. var job = GetOrCreateTranslationJobFor( ui, textKey, context );
  1554. }
  1555. }
  1556. }
  1557. else
  1558. {
  1559. QueueNewUntranslatedForDisk( textKey );
  1560. }
  1561. } ) );
  1562. }
  1563. }
  1564. }
  1565. }
  1566. return null;
  1567. }
  1568. private string TranslateOrQueueWebJobImmediateByParserResult( object ui, ParserResult result, bool allowStartJob )
  1569. {
  1570. Dictionary<string, string> translations = new Dictionary<string, string>();
  1571. // attempt to lookup ALL strings immediately; return result if possible; queue operations
  1572. foreach( var kvp in result.Arguments )
  1573. {
  1574. var key = kvp.Key;
  1575. var value = kvp.Value.TrimIfConfigured();
  1576. if( !string.IsNullOrEmpty( value ) && IsTranslatable( value ) && IsBelowMaxLength( value ) )
  1577. {
  1578. string partTranslation;
  1579. if( TryGetTranslation( value, out partTranslation ) )
  1580. {
  1581. translations.Add( key, partTranslation );
  1582. }
  1583. else if( allowStartJob )
  1584. {
  1585. // incomplete, must start job
  1586. var context = new TranslationContext( ui, result );
  1587. TranslateOrQueueWebJobImmediate( ui, value, null, false, true, context );
  1588. }
  1589. }
  1590. else
  1591. {
  1592. // the value will do
  1593. translations.Add( key, value );
  1594. }
  1595. }
  1596. if( result.Arguments.Count == translations.Count )
  1597. {
  1598. return result.Untemplate( translations );
  1599. }
  1600. else
  1601. {
  1602. return null; // could not perform complete translation
  1603. }
  1604. }
  1605. /// <summary>
  1606. /// Utility method that allows me to wait to call an action, until
  1607. /// the text has stopped changing. This is important for 'story'
  1608. /// mode text, which 'scrolls' into place slowly.
  1609. /// </summary>
  1610. private IEnumerator WaitForTextStablization( object ui, float delay, int maxTries, int currentTries, Action<string> onTextStabilized, Action onMaxTriesExceeded )
  1611. {
  1612. yield return 0; // wait a single frame to allow any external plugins to complete their hooking logic
  1613. bool succeeded = false;
  1614. while( currentTries < maxTries ) // shortcircuit
  1615. {
  1616. var beforeText = ui.GetText();
  1617. yield return new WaitForSeconds( delay );
  1618. var afterText = ui.GetText();
  1619. //Logger.Current.Debug( "WAITING: " + ui.GetType().Name + ": " + afterText );
  1620. if( beforeText == afterText )
  1621. {
  1622. onTextStabilized( afterText );
  1623. succeeded = true;
  1624. break;
  1625. }
  1626. currentTries++;
  1627. }
  1628. if( !succeeded )
  1629. {
  1630. onMaxTriesExceeded();
  1631. }
  1632. }
  1633. /// <summary>
  1634. /// Utility method that allows me to wait to call an action, until
  1635. /// the text has stopped changing. This is important for 'story'
  1636. /// mode text, which 'scrolls' into place slowly. This version is
  1637. /// for global text, where the component cannot tell us if the text
  1638. /// has changed itself.
  1639. /// </summary>
  1640. private IEnumerator WaitForTextStablization( TranslationKey textKey, float delay, Action onTextStabilized, Action onFailed = null )
  1641. {
  1642. var text = textKey.GetDictionaryLookupKey();
  1643. if( !_immediatelyTranslating.Contains( text ) )
  1644. {
  1645. _immediatelyTranslating.Add( text );
  1646. try
  1647. {
  1648. yield return new WaitForSeconds( delay );
  1649. bool succeeded = true;
  1650. foreach( var otherImmediatelyTranslating in _immediatelyTranslating )
  1651. {
  1652. if( text != otherImmediatelyTranslating )
  1653. {
  1654. if( text.RemindsOf( otherImmediatelyTranslating ) )
  1655. {
  1656. succeeded = false;
  1657. break;
  1658. }
  1659. }
  1660. }
  1661. if( succeeded )
  1662. {
  1663. onTextStabilized();
  1664. }
  1665. else
  1666. {
  1667. onFailed?.Invoke();
  1668. }
  1669. }
  1670. finally
  1671. {
  1672. _immediatelyTranslating.Remove( text );
  1673. }
  1674. }
  1675. }
  1676. private IEnumerator DelayForSeconds( float delay, Action onContinue )
  1677. {
  1678. yield return new WaitForSeconds( delay );
  1679. onContinue();
  1680. }
  1681. void Awake()
  1682. {
  1683. if( !_initialized )
  1684. {
  1685. _initialized = true;
  1686. try
  1687. {
  1688. Initialize();
  1689. ManualHook();
  1690. }
  1691. catch( Exception e )
  1692. {
  1693. XuaLogger.Current.Error( e, "An unexpected error occurred during plugin initialization." );
  1694. }
  1695. }
  1696. }
  1697. void Start()
  1698. {
  1699. try
  1700. {
  1701. HooksSetup.InstallOverrideTextHooks();
  1702. }
  1703. catch( Exception e )
  1704. {
  1705. XuaLogger.Current.Error( e, "An unexpected error occurred during plugin start." );
  1706. }
  1707. }
  1708. void Update()
  1709. {
  1710. try
  1711. {
  1712. // perform this check every 100 frames!
  1713. if( Time.frameCount % 100 == 0 )
  1714. {
  1715. ConnectionTrackingWebClient.CheckServicePoints();
  1716. }
  1717. if( Features.SupportsClipboard )
  1718. {
  1719. CopyToClipboard();
  1720. }
  1721. if( !Settings.IsShutdown )
  1722. {
  1723. UpdateSpriteRenderers();
  1724. PeriodicResetFrameCheck();
  1725. IncrementBatchOperations();
  1726. ResetThresholdTimerIfRequired();
  1727. KickoffTranslations();
  1728. FinishTranslations();
  1729. if( ClrTypes.AdvEngine != null && _nextAdvUpdate.HasValue && Time.time > _nextAdvUpdate )
  1730. {
  1731. _nextAdvUpdate = null;
  1732. UpdateUtageText();
  1733. }
  1734. }
  1735. if( Input.anyKey )
  1736. {
  1737. var isAltPressed = Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt );
  1738. if( Settings.EnablePrintHierarchy && isAltPressed && Input.GetKeyDown( KeyCode.Y ) )
  1739. {
  1740. PrintObjects();
  1741. }
  1742. else if( isAltPressed && Input.GetKeyDown( KeyCode.T ) )
  1743. {
  1744. ToggleTranslation();
  1745. }
  1746. else if( isAltPressed && Input.GetKeyDown( KeyCode.F ) )
  1747. {
  1748. ToggleFont();
  1749. }
  1750. else if( isAltPressed && Input.GetKeyDown( KeyCode.D ) )
  1751. {
  1752. DumpUntranslated();
  1753. }
  1754. else if( isAltPressed && Input.GetKeyDown( KeyCode.R ) )
  1755. {
  1756. ReloadTranslations();
  1757. }
  1758. else if( isAltPressed && Input.GetKeyDown( KeyCode.U ) )
  1759. {
  1760. ManualHook();
  1761. }
  1762. else if( isAltPressed && Input.GetKeyDown( KeyCode.Q ) )
  1763. {
  1764. RebootPlugin();
  1765. }
  1766. else if( isAltPressed && ( Input.GetKeyDown( KeyCode.Alpha0 ) || Input.GetKeyDown( KeyCode.Keypad0 ) ) )
  1767. {
  1768. _window.IsShown = !_window.IsShown;
  1769. }
  1770. }
  1771. }
  1772. catch( Exception e )
  1773. {
  1774. XuaLogger.Current.Error( e, "An error occurred in Update callback. " );
  1775. }
  1776. }
  1777. void OnGUI()
  1778. {
  1779. try
  1780. {
  1781. DisableAutoTranslator();
  1782. if( _window.IsShown ) _window.OnGUI();
  1783. }
  1784. finally
  1785. {
  1786. EnableAutoTranslator();
  1787. }
  1788. }
  1789. private void RebootPlugin()
  1790. {
  1791. if( Settings.IsShutdown )
  1792. {
  1793. if( !Settings.IsShutdownFatal )
  1794. {
  1795. _consecutiveErrors = 0;
  1796. Settings.IsShutdown = false;
  1797. XuaLogger.Current.Info( "Rebooted Auto Translator." );
  1798. }
  1799. else
  1800. {
  1801. XuaLogger.Current.Info( "Cannot reboot Auto Translator because the error that caused the shutdown is bad behaviour by the game." );
  1802. }
  1803. }
  1804. else
  1805. {
  1806. XuaLogger.Current.Info( "Cannot reboot Auto Translator because it has not been shut down." );
  1807. }
  1808. }
  1809. private void KickoffTranslations()
  1810. {
  1811. if( _endpoint == null ) return;
  1812. if( Settings.EnableBatching && _endpoint.Endpoint.SupportsLineSplitting() && !_batchLogicHasFailed && _unstartedJobs.Count > 1 && _availableBatchOperations > 0 )
  1813. {
  1814. while( _unstartedJobs.Count > 0 && _availableBatchOperations > 0 )
  1815. {
  1816. if( _endpoint.IsBusy ) break;
  1817. var kvps = _unstartedJobs.Take( Settings.BatchSize ).ToList();
  1818. var batch = new TranslationBatch();
  1819. foreach( var kvp in kvps )
  1820. {
  1821. var key = kvp.Key;
  1822. var job = kvp.Value;
  1823. _unstartedJobs.Remove( key );
  1824. if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
  1825. batch.Add( job );
  1826. _ongoingJobs[ key ] = job;
  1827. }
  1828. if( !batch.IsEmpty )
  1829. {
  1830. _availableBatchOperations--;
  1831. var untranslatedText = batch.GetFullTranslationKey();
  1832. XuaLogger.Current.Debug( "Starting translation for: " + untranslatedText );
  1833. StartCoroutine(
  1834. _endpoint.Translate(
  1835. untranslatedText,
  1836. Settings.FromLanguage,
  1837. Settings.Language,
  1838. translatedText => OnBatchTranslationCompleted( batch, translatedText ),
  1839. ( msg, e ) => OnTranslationFailed( batch, msg, e ) ) );
  1840. }
  1841. }
  1842. }
  1843. else
  1844. {
  1845. while( _unstartedJobs.Count > 0 )
  1846. {
  1847. if( _endpoint.IsBusy ) break;
  1848. // ERROR ERROR ERROR --- MEGA BUG MEGA BUG, MUST REMOVE FROM _unstartedJobs INSIDE LOOP!!!!
  1849. var kvp = _unstartedJobs.FirstOrDefault();
  1850. var key = kvp.Key;
  1851. var job = kvp.Value;
  1852. _unstartedJobs.Remove( key );
  1853. // lets see if the text should still be translated before kicking anything off
  1854. if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
  1855. _ongoingJobs[ key ] = job;
  1856. var untranslatedText = job.Key.GetDictionaryLookupKey();
  1857. XuaLogger.Current.Debug( "Starting translation for: " + untranslatedText );
  1858. StartCoroutine(
  1859. _endpoint.Translate(
  1860. untranslatedText,
  1861. Settings.FromLanguage,
  1862. Settings.Language,
  1863. translatedText => OnSingleTranslationCompleted( job, translatedText ),
  1864. ( msg, e ) => OnTranslationFailed( job, msg, e ) ) );
  1865. }
  1866. }
  1867. }
  1868. private void OnBatchTranslationCompleted( TranslationBatch batch, string translatedTextBatch )
  1869. {
  1870. _consecutiveErrors = 0;
  1871. XuaLogger.Current.Debug( $"Translation for '{batch.GetFullTranslationKey()}' succeded. Result: {translatedTextBatch}" );
  1872. var succeeded = batch.MatchWithTranslations( translatedTextBatch );
  1873. if( succeeded )
  1874. {
  1875. foreach( var tracker in batch.Trackers )
  1876. {
  1877. Settings.TranslationCount++;
  1878. var job = tracker.Job;
  1879. var translatedText = tracker.RawTranslatedText;
  1880. if( !string.IsNullOrEmpty( translatedText ) )
  1881. {
  1882. if( Settings.ForceSplitTextAfterCharacters > 0 )
  1883. {
  1884. translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
  1885. }
  1886. job.TranslatedText = job.Key.RepairTemplate( translatedText );
  1887. QueueNewTranslationForDisk( job.Key, translatedText );
  1888. _completedJobs.Add( job );
  1889. }
  1890. AddTranslation( job.Key, job.TranslatedText );
  1891. job.State = TranslationJobState.Succeeded;
  1892. _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
  1893. }
  1894. }
  1895. else
  1896. {
  1897. // might as well re-add all translation jobs, and never do this again!
  1898. _batchLogicHasFailed = true;
  1899. foreach( var tracker in batch.Trackers )
  1900. {
  1901. Settings.TranslationCount++;
  1902. var key = tracker.Job.Key.GetDictionaryLookupKey();
  1903. if( !_unstartedJobs.ContainsKey( key ) )
  1904. {
  1905. _unstartedJobs[ key ] = tracker.Job;
  1906. }
  1907. _ongoingJobs.Remove( key );
  1908. }
  1909. XuaLogger.Current.Error( "A batch operation failed. Disabling batching and restarting failed jobs." );
  1910. }
  1911. if( !Settings.IsShutdown )
  1912. {
  1913. if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
  1914. {
  1915. Settings.IsShutdown = true;
  1916. Settings.IsShutdownFatal = true;
  1917. XuaLogger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
  1918. _unstartedJobs.Clear();
  1919. _completedJobs.Clear();
  1920. _ongoingJobs.Clear();
  1921. }
  1922. }
  1923. }
  1924. private void OnSingleTranslationCompleted( TranslationJob job, string translatedText )
  1925. {
  1926. Settings.TranslationCount++;
  1927. XuaLogger.Current.Debug( $"Translation for '{job.Key.GetDictionaryLookupKey()}' succeded. Result: {translatedText}" );
  1928. _consecutiveErrors = 0;
  1929. if( !string.IsNullOrEmpty( translatedText ) )
  1930. {
  1931. if( Settings.ForceSplitTextAfterCharacters > 0 )
  1932. {
  1933. translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
  1934. }
  1935. job.TranslatedText = job.Key.RepairTemplate( translatedText );
  1936. QueueNewTranslationForDisk( job.Key, translatedText );
  1937. _completedJobs.Add( job );
  1938. }
  1939. AddTranslation( job.Key, job.TranslatedText );
  1940. job.State = TranslationJobState.Succeeded;
  1941. _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
  1942. if( !Settings.IsShutdown )
  1943. {
  1944. if( Settings.TranslationCount > Settings.MaxTranslationsBeforeShutdown )
  1945. {
  1946. Settings.IsShutdown = true;
  1947. Settings.IsShutdownFatal = true;
  1948. XuaLogger.Current.Error( $"Maximum translations ({Settings.MaxTranslationsBeforeShutdown}) per session reached. Shutting plugin down." );
  1949. _unstartedJobs.Clear();
  1950. _completedJobs.Clear();
  1951. _ongoingJobs.Clear();
  1952. }
  1953. }
  1954. }
  1955. private void OnTranslationFailed( TranslationJob job, string error, Exception e )
  1956. {
  1957. if( e == null )
  1958. {
  1959. XuaLogger.Current.Error( error );
  1960. }
  1961. else
  1962. {
  1963. XuaLogger.Current.Error( e, error );
  1964. }
  1965. Settings.TranslationCount++; // counts as a translation
  1966. _consecutiveErrors++;
  1967. job.State = TranslationJobState.Failed;
  1968. _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
  1969. if( !Settings.IsShutdown )
  1970. {
  1971. if( _consecutiveErrors >= Settings.MaxErrors )
  1972. {
  1973. Settings.IsShutdown = true;
  1974. XuaLogger.Current.Error( $"{Settings.MaxErrors} or more consecutive errors occurred. Shutting down plugin." );
  1975. _unstartedJobs.Clear();
  1976. _completedJobs.Clear();
  1977. _ongoingJobs.Clear();
  1978. }
  1979. }
  1980. }
  1981. private void OnTranslationFailed( TranslationBatch batch, string error, Exception e )
  1982. {
  1983. if( e == null )
  1984. {
  1985. XuaLogger.Current.Error( error );
  1986. }
  1987. else
  1988. {
  1989. XuaLogger.Current.Error( e, error );
  1990. }
  1991. Settings.TranslationCount++; // counts as a translation
  1992. _consecutiveErrors++;
  1993. _batchLogicHasFailed = true;
  1994. foreach( var tracker in batch.Trackers )
  1995. {
  1996. tracker.Job.State = TranslationJobState.Failed;
  1997. _ongoingJobs.Remove( tracker.Job.Key.GetDictionaryLookupKey() );
  1998. }
  1999. if( !Settings.IsShutdown )
  2000. {
  2001. if( _consecutiveErrors >= Settings.MaxErrors )
  2002. {
  2003. Settings.IsShutdown = true;
  2004. XuaLogger.Current.Error( $"{Settings.MaxErrors} or more consecutive errors occurred. Shutting down plugin." );
  2005. _unstartedJobs.Clear();
  2006. _completedJobs.Clear();
  2007. _ongoingJobs.Clear();
  2008. }
  2009. }
  2010. }
  2011. private void FinishTranslations()
  2012. {
  2013. if( _completedJobs.Count > 0 )
  2014. {
  2015. for( int i = _completedJobs.Count - 1 ; i >= 0 ; i-- )
  2016. {
  2017. var job = _completedJobs[ i ];
  2018. _completedJobs.RemoveAt( i );
  2019. foreach( var component in job.Components )
  2020. {
  2021. // update the original text, but only if it has not been chaanged already for some reason (could be other translator plugin or game itself)
  2022. try
  2023. {
  2024. var text = component.GetText().TrimIfConfigured();
  2025. if( text == job.Key.OriginalText )
  2026. {
  2027. var info = component.GetOrCreateTextTranslationInfo();
  2028. SetTranslatedText( component, job.TranslatedText, info );
  2029. }
  2030. }
  2031. catch( NullReferenceException )
  2032. {
  2033. // might fail if compoent is no longer associated to game
  2034. }
  2035. }
  2036. // handle each context
  2037. foreach( var context in job.Contexts )
  2038. {
  2039. // are all jobs within this context completed? If so, we can set the text
  2040. if( context.Jobs.All( x => x.State == TranslationJobState.Succeeded ) )
  2041. {
  2042. try
  2043. {
  2044. var text = context.Component.GetText().TrimIfConfigured();
  2045. var result = context.Result;
  2046. Dictionary<string, string> translations = new Dictionary<string, string>();
  2047. var translatedText = TranslateOrQueueWebJobImmediateByParserResult( context.Component, result, false );
  2048. if( !string.IsNullOrEmpty( translatedText ) )
  2049. {
  2050. if( result.PersistCombinedResult && !_translations.ContainsKey( context.Result.OriginalText ) )
  2051. {
  2052. AddTranslation( context.Result.OriginalText, translatedText );
  2053. QueueNewTranslationForDisk( context.Result.OriginalText, translatedText );
  2054. }
  2055. if( text == result.OriginalText )
  2056. {
  2057. if( translatedText != null )
  2058. {
  2059. var info = context.Component.GetOrCreateTextTranslationInfo();
  2060. SetTranslatedText( context.Component, translatedText, info );
  2061. }
  2062. }
  2063. }
  2064. }
  2065. catch( NullReferenceException )
  2066. {
  2067. }
  2068. }
  2069. }
  2070. // Utage support
  2071. if( ClrTypes.AdvEngine != null
  2072. && job.OriginalSources.Any( x => ClrTypes.AdvCommand.IsAssignableFrom( x.GetType() ) ) )
  2073. {
  2074. _nextAdvUpdate = Time.time + 0.5f;
  2075. }
  2076. }
  2077. }
  2078. }
  2079. private void UpdateUtageText()
  2080. {
  2081. // 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.
  2082. if( _advEngine == null || _advEngine?.gameObject == null )
  2083. {
  2084. _advEngine = (Component)GameObject.FindObjectOfType( Constants.ClrTypes.AdvEngine );
  2085. }
  2086. if( _advEngine != null )
  2087. {
  2088. AccessTools.Method( Constants.ClrTypes.AdvEngine, "ChangeLanguage" )?.Invoke( _advEngine, new object[ 0 ] );
  2089. }
  2090. }
  2091. private void ReloadTranslations()
  2092. {
  2093. LoadTranslations();
  2094. var context = new TextureReloadContext();
  2095. foreach( var kvp in ObjectReferenceMapper.GetAllRegisteredObjects() )
  2096. {
  2097. var ui = kvp.Key;
  2098. try
  2099. {
  2100. if( ui is Component component )
  2101. {
  2102. if( component.gameObject?.activeSelf ?? false )
  2103. {
  2104. var tti = kvp.Value as TextTranslationInfo;
  2105. if( tti != null && !string.IsNullOrEmpty( tti.OriginalText ) )
  2106. {
  2107. var key = new TranslationKey( kvp.Key, tti.OriginalText, false );
  2108. if( TryGetTranslation( key, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
  2109. {
  2110. SetTranslatedText( kvp.Key, translatedText, tti ); // no need to untemplatize the translated text
  2111. }
  2112. }
  2113. }
  2114. }
  2115. if( Settings.EnableTextureTranslation )
  2116. {
  2117. TranslateTexture( ui, context );
  2118. }
  2119. }
  2120. catch( Exception )
  2121. {
  2122. // not super pretty, no...
  2123. ObjectReferenceMapper.Remove( ui );
  2124. }
  2125. }
  2126. }
  2127. private string CalculateDumpFileName()
  2128. {
  2129. int idx = 0;
  2130. string fileName = null;
  2131. do
  2132. {
  2133. idx++;
  2134. fileName = $"UntranslatedDump{idx}.txt";
  2135. }
  2136. while( File.Exists( fileName ) );
  2137. return fileName;
  2138. }
  2139. private void DumpUntranslated()
  2140. {
  2141. if( _newUntranslated.Count > 0 )
  2142. {
  2143. using( var stream = File.Open( CalculateDumpFileName(), FileMode.Append, FileAccess.Write ) )
  2144. using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
  2145. {
  2146. foreach( var untranslated in _newUntranslated )
  2147. {
  2148. writer.WriteLine( TextHelper.Encode( untranslated ) + '=' );
  2149. }
  2150. writer.Flush();
  2151. }
  2152. _newUntranslated.Clear();
  2153. }
  2154. }
  2155. private void ToggleFont()
  2156. {
  2157. if( _hasOverrideFont )
  2158. {
  2159. _overrideFont = !_overrideFont;
  2160. var objects = ObjectReferenceMapper.GetAllRegisteredObjects();
  2161. XuaLogger.Current.Info( $"Toggling fonts of {objects.Count} objects." );
  2162. if( _overrideFont )
  2163. {
  2164. // make sure we use the translated version of all texts
  2165. foreach( var kvp in objects )
  2166. {
  2167. var tti = kvp.Value as TextTranslationInfo;
  2168. if( tti != null )
  2169. {
  2170. var ui = kvp.Key;
  2171. try
  2172. {
  2173. if( ( ui as Component )?.gameObject?.activeSelf ?? false )
  2174. {
  2175. tti?.ChangeFont( ui );
  2176. }
  2177. }
  2178. catch( Exception )
  2179. {
  2180. // not super pretty, no...
  2181. ObjectReferenceMapper.Remove( ui );
  2182. }
  2183. }
  2184. }
  2185. }
  2186. else
  2187. {
  2188. // make sure we use the original version of all texts
  2189. foreach( var kvp in objects )
  2190. {
  2191. var tti = kvp.Value as TextTranslationInfo;
  2192. var ui = kvp.Key;
  2193. try
  2194. {
  2195. if( ( ui as Component )?.gameObject?.activeSelf ?? false )
  2196. {
  2197. tti?.UnchangeFont( ui );
  2198. }
  2199. }
  2200. catch( Exception )
  2201. {
  2202. // not super pretty, no...
  2203. ObjectReferenceMapper.Remove( ui );
  2204. }
  2205. }
  2206. }
  2207. }
  2208. }
  2209. private void ToggleTranslation()
  2210. {
  2211. _isInTranslatedMode = !_isInTranslatedMode;
  2212. var objects = ObjectReferenceMapper.GetAllRegisteredObjects();
  2213. XuaLogger.Current.Info( $"Toggling translations of {objects.Count} objects." );
  2214. if( _isInTranslatedMode )
  2215. {
  2216. // make sure we use the translated version of all texts
  2217. foreach( var kvp in objects )
  2218. {
  2219. var ui = kvp.Key;
  2220. try
  2221. {
  2222. if( ui is Component component )
  2223. {
  2224. if( component.gameObject?.activeSelf ?? false )
  2225. {
  2226. var tti = kvp.Value as TextTranslationInfo;
  2227. if( tti != null && tti.IsTranslated )
  2228. {
  2229. SetText( ui, tti.TranslatedText, true, tti );
  2230. }
  2231. }
  2232. }
  2233. if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
  2234. {
  2235. TranslateTexture( ui, null );
  2236. }
  2237. }
  2238. catch( Exception )
  2239. {
  2240. // not super pretty, no...
  2241. ObjectReferenceMapper.Remove( ui );
  2242. }
  2243. }
  2244. }
  2245. else
  2246. {
  2247. // make sure we use the original version of all texts
  2248. foreach( var kvp in objects )
  2249. {
  2250. var ui = kvp.Key;
  2251. try
  2252. {
  2253. if( ui is Component component )
  2254. {
  2255. if( component.gameObject?.activeSelf ?? false )
  2256. {
  2257. var tti = kvp.Value as TextTranslationInfo;
  2258. if( tti != null && tti.IsTranslated )
  2259. {
  2260. SetText( ui, tti.OriginalText, true, tti );
  2261. }
  2262. }
  2263. }
  2264. if( Settings.EnableTextureTranslation && Settings.EnableTextureToggling )
  2265. {
  2266. TranslateTexture( ui, null );
  2267. }
  2268. }
  2269. catch( Exception )
  2270. {
  2271. // not super pretty, no...
  2272. ObjectReferenceMapper.Remove( ui );
  2273. }
  2274. }
  2275. }
  2276. }
  2277. private void CopyToClipboard()
  2278. {
  2279. if( Settings.CopyToClipboard
  2280. && _textsToCopyToClipboardOrdered.Count > 0
  2281. && Time.realtimeSinceStartup - _clipboardUpdated > Settings.ClipboardDebounceTime )
  2282. {
  2283. try
  2284. {
  2285. var builder = new StringBuilder();
  2286. foreach( var text in _textsToCopyToClipboardOrdered )
  2287. {
  2288. if( text.Length + builder.Length > Settings.MaxClipboardCopyCharacters ) break;
  2289. builder.AppendLine( text );
  2290. }
  2291. TextEditor editor = (TextEditor)GUIUtility.GetStateObject( typeof( TextEditor ), GUIUtility.keyboardControl );
  2292. editor.text = builder.ToString();
  2293. editor.SelectAll();
  2294. editor.Copy();
  2295. }
  2296. catch( Exception e )
  2297. {
  2298. XuaLogger.Current.Error( e, "An error while copying text to clipboard." );
  2299. }
  2300. finally
  2301. {
  2302. _textsToCopyToClipboard.Clear();
  2303. _textsToCopyToClipboardOrdered.Clear();
  2304. }
  2305. }
  2306. }
  2307. private void PrintObjects()
  2308. {
  2309. using( var stream = File.Open( Path.Combine( Environment.CurrentDirectory, "hierarchy.txt" ), FileMode.Create ) )
  2310. using( var writer = new StreamWriter( stream ) )
  2311. {
  2312. foreach( var root in GetAllRoots() )
  2313. {
  2314. TraverseChildren( writer, root, "" );
  2315. }
  2316. writer.Flush();
  2317. }
  2318. }
  2319. private void ManualHook()
  2320. {
  2321. ManualHookForComponents();
  2322. ManualHookForTextures();
  2323. }
  2324. private void ManualHookForComponents()
  2325. {
  2326. foreach( var root in GetAllRoots() )
  2327. {
  2328. TraverseChildrenManualHook( root );
  2329. }
  2330. }
  2331. private void ManualHookForTextures()
  2332. {
  2333. if( Settings.EnableTextureScanOnSceneLoad && ( Settings.EnableTextureTranslation || Settings.EnableTextureDumping ) )
  2334. {
  2335. // scan all textures and update
  2336. var textures = Resources.FindObjectsOfTypeAll<Texture2D>();
  2337. foreach( var texture in textures )
  2338. {
  2339. Hook_ImageChanged( texture, false );
  2340. }
  2341. //// scan all components and set dirty
  2342. //var components = GameObject.FindObjectsOfType<Component>();
  2343. //foreach( var component in components )
  2344. //{
  2345. // component.SetAllDirtyEx();
  2346. //}
  2347. }
  2348. }
  2349. private IEnumerable<GameObject> GetAllRoots()
  2350. {
  2351. var objects = GameObject.FindObjectsOfType<GameObject>();
  2352. foreach( var obj in objects )
  2353. {
  2354. if( obj.transform != null && obj.transform.parent == null )
  2355. {
  2356. yield return obj;
  2357. }
  2358. }
  2359. }
  2360. private void TraverseChildren( StreamWriter writer, GameObject obj, string identation )
  2361. {
  2362. if( obj != null )
  2363. {
  2364. var layer = LayerMask.LayerToName( obj.layer );
  2365. var components = string.Join( ", ", obj.GetComponents<Component>().Select( x => x?.GetType()?.Name ).Where( x => x != null ).ToArray() );
  2366. var line = string.Format( "{0,-50} {1,100}",
  2367. identation + obj.name + " [" + layer + "]",
  2368. components );
  2369. writer.WriteLine( line );
  2370. if( obj.transform != null )
  2371. {
  2372. for( int i = 0 ; i < obj.transform.childCount ; i++ )
  2373. {
  2374. var child = obj.transform.GetChild( i );
  2375. TraverseChildren( writer, child.gameObject, identation + " " );
  2376. }
  2377. }
  2378. }
  2379. }
  2380. private void TraverseChildrenManualHook( GameObject obj )
  2381. {
  2382. if( obj != null )
  2383. {
  2384. var components = obj.GetComponents<Component>();
  2385. foreach( var component in components )
  2386. {
  2387. if( component.IsKnownTextType() )
  2388. {
  2389. Hook_TextChanged( component, false );
  2390. }
  2391. if( Settings.EnableTextureTranslation || Settings.EnableTextureDumping )
  2392. {
  2393. if( component.IsKnownImageType() )
  2394. {
  2395. Hook_ImageChangedOnComponent( component, null, false, false );
  2396. }
  2397. }
  2398. }
  2399. if( obj.transform != null )
  2400. {
  2401. for( int i = 0 ; i < obj.transform.childCount ; i++ )
  2402. {
  2403. var child = obj.transform.GetChild( i );
  2404. TraverseChildrenManualHook( child.gameObject );
  2405. }
  2406. }
  2407. }
  2408. }
  2409. public void DisableAutoTranslator()
  2410. {
  2411. _temporarilyDisabled = true;
  2412. }
  2413. public void EnableAutoTranslator()
  2414. {
  2415. _temporarilyDisabled = false;
  2416. }
  2417. void OnApplicationQuit()
  2418. {
  2419. if( _configuredEndpoints == null ) return;
  2420. foreach( var ce in _configuredEndpoints )
  2421. {
  2422. try
  2423. {
  2424. if( ce.Endpoint is IDisposable disposable ) disposable.Dispose();
  2425. }
  2426. catch( Exception e )
  2427. {
  2428. XuaLogger.Current.Error( e, "An error occurred while disposing endpoint." );
  2429. }
  2430. }
  2431. }
  2432. }
  2433. internal static class SceneManagerLoader
  2434. {
  2435. public static void EnableSceneLoadScanInternal( AutoTranslationPlugin plugin )
  2436. {
  2437. // specified in own method, because of chance that this has changed through Unity lifetime
  2438. SceneManager.sceneLoaded += ( arg1, arg2 ) => plugin.OnLevelWasLoadedFromSceneManager( arg1.buildIndex );
  2439. }
  2440. }
  2441. }