AutoTranslationPlugin.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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.IMGUI;
  24. using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
  25. using UnityEngine.SceneManagement;
  26. using XUnity.AutoTranslator.Plugin.Core.Constants;
  27. namespace XUnity.AutoTranslator.Plugin.Core
  28. {
  29. public class AutoTranslationPlugin : MonoBehaviour
  30. {
  31. /// <summary>
  32. /// Allow the instance to be accessed statically, as only one will exist.
  33. /// </summary>
  34. public static AutoTranslationPlugin Current;
  35. /// <summary>
  36. /// These are the currently running translation jobs (being translated by an http request).
  37. /// </summary>
  38. private List<TranslationJob> _completedJobs = new List<TranslationJob>();
  39. private Dictionary<string, TranslationJob> _unstartedJobs = new Dictionary<string, TranslationJob>();
  40. /// <summary>
  41. /// All the translations are stored in this dictionary.
  42. /// </summary>
  43. private Dictionary<string, string> _translations = new Dictionary<string, string>();
  44. /// <summary>
  45. /// These are the new translations that has not yet been persisted to the file system.
  46. /// </summary>
  47. private object _writeToFileSync = new object();
  48. private Dictionary<string, string> _newTranslations = new Dictionary<string, string>();
  49. private HashSet<string> _newUntranslated = new HashSet<string>();
  50. private HashSet<string> _translatedTexts = new HashSet<string>();
  51. /// <summary>
  52. /// The number of http translation errors that has occurred up until now.
  53. /// </summary>
  54. private int _consecutiveErrors = 0;
  55. /// <summary>
  56. /// This is a hash set that contains all Text components that is currently being worked on by
  57. /// the translation plugin.
  58. /// </summary>
  59. private HashSet<object> _ongoingOperations = new HashSet<object>();
  60. private HashSet<string> _startedOperationsForNonStabilizableComponents = new HashSet<string>();
  61. /// <summary>
  62. /// This function will check if there are symbols of a given language contained in a string.
  63. /// </summary>
  64. private Func<string, bool> _symbolCheck;
  65. private bool _isInTranslatedMode = true;
  66. private bool _hooksEnabled = true;
  67. public void Initialize()
  68. {
  69. Current = this;
  70. Settings.Configure();
  71. HooksSetup.InstallHooks( Override_TextChanged );
  72. AutoTranslateClient.Configure();
  73. _symbolCheck = TextHelper.GetSymbolCheck( Settings.FromLanguage );
  74. LoadTranslations();
  75. // start a thread that will periodically removed unused references
  76. var t1 = new Thread( RemovedUnusedReferences );
  77. t1.IsBackground = true;
  78. t1.Start();
  79. // start a thread that will periodically save new translations
  80. var t2 = new Thread( SaveTranslationsLoop );
  81. t2.IsBackground = true;
  82. t2.Start();
  83. }
  84. private string[] GetTranslationFiles()
  85. {
  86. return Directory.GetFiles( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ), $"*.txt", SearchOption.AllDirectories ) // FIXME: Add $"*{Language}.txt"
  87. .Union( new[] { Settings.AutoTranslationsFilePath } )
  88. .Select( x => x.Replace( "/", "\\" ) )
  89. .Distinct()
  90. .OrderBy( x => x )
  91. .ToArray();
  92. }
  93. private void RemovedUnusedReferences( object state )
  94. {
  95. while( true )
  96. {
  97. try
  98. {
  99. ObjectExtensions.Cull();
  100. }
  101. catch( Exception e )
  102. {
  103. Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An unexpected error occurred while removing GC'ed resources." + Environment.NewLine + e );
  104. }
  105. finally
  106. {
  107. Thread.Sleep( 1000 * 60 );
  108. }
  109. }
  110. }
  111. private void SaveTranslationsLoop( object state )
  112. {
  113. try
  114. {
  115. while( true )
  116. {
  117. if( _newTranslations.Count > 0 )
  118. {
  119. lock( _writeToFileSync )
  120. {
  121. if( _newTranslations.Count > 0 )
  122. {
  123. using( var stream = File.Open( Settings.AutoTranslationsFilePath, FileMode.Append, FileAccess.Write ) )
  124. using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
  125. {
  126. foreach( var kvp in _newTranslations )
  127. {
  128. writer.WriteLine( TextHelper.Encode( kvp.Key ) + '=' + TextHelper.Encode( kvp.Value ) );
  129. }
  130. writer.Flush();
  131. }
  132. _newTranslations.Clear();
  133. }
  134. }
  135. }
  136. else
  137. {
  138. Thread.Sleep( 5000 );
  139. }
  140. }
  141. }
  142. catch( Exception e )
  143. {
  144. Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while saving translations to disk. " + Environment.NewLine + e );
  145. }
  146. }
  147. /// <summary>
  148. /// Loads the translations found in Translation.{lang}.txt
  149. /// </summary>
  150. private void LoadTranslations()
  151. {
  152. try
  153. {
  154. lock( _writeToFileSync )
  155. {
  156. Directory.CreateDirectory( Path.Combine( Config.Current.DataPath, Settings.TranslationDirectory ) );
  157. Directory.CreateDirectory( Path.GetDirectoryName( Path.Combine( Config.Current.DataPath, Settings.OutputFile ) ) );
  158. foreach( var fullFileName in GetTranslationFiles() )
  159. {
  160. if( File.Exists( fullFileName ) )
  161. {
  162. string[] translations = File.ReadAllLines( fullFileName, Encoding.UTF8 );
  163. foreach( string translation in translations )
  164. {
  165. string[] kvp = translation.Split( new char[] { '=', '\t' }, StringSplitOptions.None );
  166. if( kvp.Length >= 2 )
  167. {
  168. string key = TextHelper.Decode( kvp[ 0 ].Trim() );
  169. string value = TextHelper.Decode( kvp[ 1 ].Trim() );
  170. if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
  171. {
  172. AddTranslation( key, value );
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. catch( Exception e )
  181. {
  182. Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while loading translations. " + Environment.NewLine + e );
  183. }
  184. }
  185. private TranslationJob GetOrCreateTranslationJobFor( string untranslatedText )
  186. {
  187. if( _unstartedJobs.TryGetValue( untranslatedText, out TranslationJob job ) )
  188. {
  189. return job;
  190. }
  191. foreach( var completedJob in _completedJobs )
  192. {
  193. if( completedJob.UntranslatedText == untranslatedText )
  194. {
  195. return completedJob;
  196. }
  197. }
  198. job = new TranslationJob( untranslatedText );
  199. _unstartedJobs.Add( untranslatedText, job );
  200. return job;
  201. }
  202. private void AddTranslation( string key, string value )
  203. {
  204. _translations[ key ] = value;
  205. _translatedTexts.Add( value );
  206. if( Settings.IgnoreWhitespaceInDialogue )
  207. {
  208. var newKey = key.ChangeToSingleLineForDialogue();
  209. _translations[ newKey ] = value;
  210. }
  211. }
  212. private void QueueNewUntranslatedForDisk( string key )
  213. {
  214. if( Settings.IgnoreWhitespaceInDialogue )
  215. {
  216. key = key.ChangeToSingleLineForDialogue();
  217. }
  218. _newUntranslated.Add( key );
  219. }
  220. private void QueueNewTranslationForDisk( string key, string value )
  221. {
  222. lock( _writeToFileSync )
  223. {
  224. _newTranslations[ key ] = value;
  225. }
  226. }
  227. private bool TryGetTranslation( string key, out string value )
  228. {
  229. return _translations.TryGetValue( key, out value ) || ( Settings.IgnoreWhitespaceInDialogue && _translations.TryGetValue( key.RemoveWhitespace(), out value ) );
  230. }
  231. private string Override_TextChanged( object ui, string text )
  232. {
  233. if( _hooksEnabled )
  234. {
  235. return TranslateOrQueueWebJob( ui, text, true );
  236. }
  237. return null;
  238. }
  239. public void Hook_TextChanged( object ui )
  240. {
  241. if( _hooksEnabled )
  242. {
  243. TranslateOrQueueWebJob( ui, null, false );
  244. }
  245. }
  246. public void Hook_TextInitialized( object ui )
  247. {
  248. if( _hooksEnabled )
  249. {
  250. TranslateOrQueueWebJob( ui, null, true );
  251. }
  252. }
  253. private void SetTranslatedText( object ui, string text, TranslationInfo info )
  254. {
  255. info?.SetTranslatedText( text );
  256. if( _isInTranslatedMode )
  257. {
  258. SetText( ui, text, true, info );
  259. }
  260. }
  261. /// <summary>
  262. /// Sets the text of a UI text, while ensuring this will not fire a text changed event.
  263. /// </summary>
  264. private void SetText( object ui, string text, bool isTranslated, TranslationInfo info )
  265. {
  266. if( !info?.IsCurrentlySettingText ?? true )
  267. {
  268. try
  269. {
  270. // TODO: Disable ANY Hook
  271. _hooksEnabled = false;
  272. if( info != null )
  273. {
  274. info.IsCurrentlySettingText = true;
  275. }
  276. ui.SetText( text );
  277. if( isTranslated )
  278. {
  279. info?.ResizeUI( ui );
  280. }
  281. else
  282. {
  283. info?.UnresizeUI( ui );
  284. }
  285. }
  286. catch( Exception e )
  287. {
  288. Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred while setting text on a component." + Environment.NewLine + e );
  289. }
  290. finally
  291. {
  292. _hooksEnabled = true;
  293. if( info != null )
  294. {
  295. info.IsCurrentlySettingText = false;
  296. }
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// Determines if a text should be translated.
  302. /// </summary>
  303. private bool IsTranslatable( string str )
  304. {
  305. return _symbolCheck( str ) && str.Length <= Settings.MaxCharactersPerTranslation && !_translatedTexts.Contains( str );
  306. }
  307. public bool ShouldTranslate( object ui )
  308. {
  309. var cui = ui as Component;
  310. if( cui != null )
  311. {
  312. var go = cui.gameObject;
  313. var isDummy = go.IsDummy();
  314. if( isDummy )
  315. {
  316. return false;
  317. }
  318. var inputField = cui.gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.InputField )
  319. ?? cui.gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.TMP_InputField );
  320. return inputField == null;
  321. }
  322. return true;
  323. }
  324. private string TranslateOrQueueWebJob( object ui, string text, bool isAwakening )
  325. {
  326. var info = ui.GetTranslationInfo( isAwakening );
  327. if( !info?.IsAwake ?? false )
  328. {
  329. return null;
  330. }
  331. if( _ongoingOperations.Contains( ui ) )
  332. {
  333. return null;
  334. }
  335. if( Settings.Delay == 0 || !SupportsStabilization( ui ) )
  336. {
  337. return TranslateOrQueueWebJobImmediate( ui, text, info );
  338. }
  339. else
  340. {
  341. StartCoroutine(
  342. DelayForSeconds( Settings.Delay, () =>
  343. {
  344. TranslateOrQueueWebJobImmediate( ui, text, info );
  345. } ) );
  346. }
  347. return null;
  348. }
  349. public static bool IsCurrentlySetting( TranslationInfo info )
  350. {
  351. if( info == null ) return false;
  352. return info.IsCurrentlySettingText;
  353. }
  354. /// <summary>
  355. /// Translates the string of a UI text or queues it up to be translated
  356. /// by the HTTP translation service.
  357. /// </summary>
  358. private string TranslateOrQueueWebJobImmediate( object ui, string text, TranslationInfo info )
  359. {
  360. // Get the trimmed text
  361. text = ( text ?? ui.GetText() ).Trim();
  362. // Ensure that we actually want to translate this text and its owning UI element.
  363. if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsCurrentlySetting( info ) )
  364. {
  365. info?.Reset( text );
  366. // if we already have translation loaded in our _translatios dictionary, simply load it and set text
  367. string translation;
  368. if( TryGetTranslation( text, out translation ) )
  369. {
  370. if( !string.IsNullOrEmpty( translation ) )
  371. {
  372. SetTranslatedText( ui, translation, info );
  373. return translation;
  374. }
  375. }
  376. else
  377. {
  378. if( SupportsStabilization( ui ) )
  379. {
  380. // if we dont know what text to translate it to, we need to figure it out.
  381. // this might take a while, so add the UI text component to the ongoing operations
  382. // list, so we dont start multiple operations for it, as its text might be constantly
  383. // changing.
  384. _ongoingOperations.Add( ui );
  385. // start a coroutine, that will execute once the string of the UI text has stopped
  386. // changing. For all texts except 'story' texts, this will add a delay for exactly
  387. // 0.5s to the translation. This is barely noticable.
  388. //
  389. // on the other hand, for 'story' texts, this will take the time that it takes
  390. // for the text to stop 'scrolling' in.
  391. try
  392. {
  393. StartCoroutine(
  394. WaitForTextStablization(
  395. ui: ui,
  396. delay: 0.5f,
  397. maxTries: 100, // 100 tries == 50 seconds
  398. currentTries: 0,
  399. onMaxTriesExceeded: () =>
  400. {
  401. _ongoingOperations.Remove( ui );
  402. },
  403. onTextStabilized: stabilizedText =>
  404. {
  405. _ongoingOperations.Remove( ui );
  406. if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
  407. {
  408. info?.Reset( stabilizedText );
  409. // once the text has stabilized, attempt to look it up
  410. if( TryGetTranslation( stabilizedText, out translation ) )
  411. {
  412. if( !string.IsNullOrEmpty( translation ) )
  413. {
  414. SetTranslatedText( ui, translation, info );
  415. }
  416. }
  417. else
  418. {
  419. // Lets try not to spam a service that might not be there...
  420. if( AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors )
  421. {
  422. var job = GetOrCreateTranslationJobFor( stabilizedText );
  423. job.Components.Add( ui );
  424. }
  425. else
  426. {
  427. QueueNewUntranslatedForDisk( stabilizedText );
  428. }
  429. }
  430. }
  431. } ) );
  432. }
  433. catch( Exception )
  434. {
  435. _ongoingOperations.Remove( ui );
  436. }
  437. }
  438. else
  439. {
  440. if( !_startedOperationsForNonStabilizableComponents.Contains( text ) )
  441. {
  442. _startedOperationsForNonStabilizableComponents.Add( text );
  443. // Lets try not to spam a service that might not be there...
  444. if( AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors )
  445. {
  446. GetOrCreateTranslationJobFor( text );
  447. }
  448. else
  449. {
  450. QueueNewUntranslatedForDisk( text );
  451. }
  452. }
  453. }
  454. }
  455. }
  456. return null;
  457. }
  458. public bool SupportsStabilization( object ui )
  459. {
  460. return !( ui is GUIContent );
  461. }
  462. /// <summary>
  463. /// Utility method that allows me to wait to call an action, until
  464. /// the text has stopped changing. This is important for 'story'
  465. /// mode text, which 'scrolls' into place slowly.
  466. /// </summary>
  467. public IEnumerator WaitForTextStablization( object ui, float delay, int maxTries, int currentTries, Action<string> onTextStabilized, Action onMaxTriesExceeded )
  468. {
  469. if( currentTries < maxTries ) // shortcircuit
  470. {
  471. var beforeText = ui.GetText();
  472. yield return new WaitForSeconds( delay );
  473. var afterText = ui.GetText();
  474. if( beforeText == afterText )
  475. {
  476. onTextStabilized( afterText.Trim() );
  477. }
  478. else
  479. {
  480. StartCoroutine( WaitForTextStablization( ui, delay, maxTries, currentTries + 1, onTextStabilized, onMaxTriesExceeded ) );
  481. }
  482. }
  483. else
  484. {
  485. onMaxTriesExceeded();
  486. }
  487. }
  488. public IEnumerator DelayForSeconds( float delay, Action onContinue )
  489. {
  490. yield return new WaitForSeconds( delay );
  491. onContinue();
  492. }
  493. public void Update()
  494. {
  495. try
  496. {
  497. KickoffTranslations();
  498. FinishTranslations();
  499. if( Input.anyKey )
  500. {
  501. if( Settings.EnablePrintHierarchy && ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.Y ) )
  502. {
  503. PrintObjects();
  504. }
  505. else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.T ) )
  506. {
  507. ToggleTranslation();
  508. }
  509. else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.D ) )
  510. {
  511. DumpUntranslated();
  512. }
  513. else if( ( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKey( KeyCode.RightAlt ) ) && Input.GetKeyDown( KeyCode.R ) )
  514. {
  515. ReloadTranslations();
  516. }
  517. }
  518. }
  519. catch( Exception e )
  520. {
  521. Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error occurred in Update callback. " + Environment.NewLine + e );
  522. }
  523. }
  524. // create this as a field instead of local var, to prevent new creation on EVERY game loop
  525. private readonly List<string> _kickedOff = new List<string>();
  526. private void KickoffTranslations()
  527. {
  528. foreach( var kvp in _unstartedJobs )
  529. {
  530. if( !AutoTranslateClient.HasAvailableClients ) break;
  531. var key = kvp.Key;
  532. var job = kvp.Value;
  533. _kickedOff.Add( key );
  534. // lets see if the text should still be translated before kicking anything off
  535. if( !job.AnyComponentsStillHasOriginalUntranslatedText() ) continue;
  536. StartCoroutine( AutoTranslateClient.TranslateByWWW( job.UntranslatedDialogueText, Settings.FromLanguage, Settings.Language, translatedText =>
  537. {
  538. _consecutiveErrors = 0;
  539. if( Settings.ForceSplitTextAfterCharacters > 0 )
  540. {
  541. translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
  542. }
  543. job.TranslatedText = translatedText;
  544. if( !string.IsNullOrEmpty( translatedText ) )
  545. {
  546. QueueNewTranslationForDisk( Settings.IgnoreWhitespaceInDialogue ? job.UntranslatedDialogueText : job.UntranslatedText, translatedText );
  547. _completedJobs.Add( job );
  548. }
  549. },
  550. () =>
  551. {
  552. _consecutiveErrors++;
  553. } ) );
  554. }
  555. for( int i = 0 ; i < _kickedOff.Count ; i++ )
  556. {
  557. _unstartedJobs.Remove( _kickedOff[ i ] );
  558. }
  559. _kickedOff.Clear();
  560. }
  561. private void FinishTranslations()
  562. {
  563. if( _completedJobs.Count > 0 )
  564. {
  565. for( int i = _completedJobs.Count - 1 ; i >= 0 ; i-- )
  566. {
  567. var job = _completedJobs[ i ];
  568. _completedJobs.RemoveAt( i );
  569. foreach( var component in job.Components )
  570. {
  571. // update the original text, but only if it has not been chaanged already for some reason (could be other translator plugin or game itself)
  572. var text = component.GetText().Trim();
  573. if( text == job.UntranslatedText )
  574. {
  575. var info = component.GetTranslationInfo( false );
  576. SetTranslatedText( component, job.TranslatedText, info );
  577. }
  578. }
  579. AddTranslation( job.UntranslatedText, job.TranslatedText );
  580. }
  581. }
  582. }
  583. private void ReloadTranslations()
  584. {
  585. LoadTranslations();
  586. foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
  587. {
  588. var info = kvp.Value as TranslationInfo;
  589. if( info != null && !string.IsNullOrEmpty( info.OriginalText ) )
  590. {
  591. if( TryGetTranslation( info.OriginalText, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
  592. {
  593. SetTranslatedText( kvp.Key, translatedText, info );
  594. }
  595. }
  596. }
  597. }
  598. private string CalculateDumpFileName()
  599. {
  600. int idx = 0;
  601. string fileName = null;
  602. do
  603. {
  604. idx++;
  605. fileName = $"UntranslatedDump{idx}.txt";
  606. }
  607. while( File.Exists( fileName ) );
  608. return fileName;
  609. }
  610. private void DumpUntranslated()
  611. {
  612. if( _newUntranslated.Count > 0 )
  613. {
  614. using( var stream = File.Open( CalculateDumpFileName(), FileMode.Append, FileAccess.Write ) )
  615. using( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
  616. {
  617. foreach( var untranslated in _newUntranslated )
  618. {
  619. writer.WriteLine( TextHelper.Encode( untranslated ) + '=' );
  620. }
  621. writer.Flush();
  622. }
  623. _newUntranslated.Clear();
  624. }
  625. }
  626. private void ToggleTranslation()
  627. {
  628. _isInTranslatedMode = !_isInTranslatedMode;
  629. if( _isInTranslatedMode )
  630. {
  631. // make sure we use the translated version of all texts
  632. foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
  633. {
  634. var ui = kvp.Key;
  635. try
  636. {
  637. if( ( ui as Component )?.gameObject?.activeSelf ?? false )
  638. {
  639. var info = (TranslationInfo)kvp.Value;
  640. if( info != null && info.IsTranslated )
  641. {
  642. SetText( ui, info.TranslatedText, true, info );
  643. }
  644. }
  645. }
  646. catch( Exception )
  647. {
  648. // not super pretty, no...
  649. ObjectExtensions.Remove( ui );
  650. }
  651. }
  652. }
  653. else
  654. {
  655. // make sure we use the original version of all texts
  656. foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
  657. {
  658. var ui = kvp.Key;
  659. try
  660. {
  661. if( ( ui as Component )?.gameObject?.activeSelf ?? false )
  662. {
  663. var info = (TranslationInfo)kvp.Value;
  664. if( info != null && info.IsTranslated )
  665. {
  666. SetText( ui, info.OriginalText, true, info );
  667. }
  668. }
  669. }
  670. catch( Exception )
  671. {
  672. // not super pretty, no...
  673. ObjectExtensions.Remove( ui );
  674. }
  675. }
  676. }
  677. }
  678. private void PrintObjects()
  679. {
  680. using( var stream = File.Open( Path.Combine( Environment.CurrentDirectory, "hierarchy.txt" ), FileMode.Create ) )
  681. using( var writer = new StreamWriter( stream ) )
  682. {
  683. foreach( var root in GetAllRoots() )
  684. {
  685. TraverseChildren( writer, root, "" );
  686. }
  687. writer.Flush();
  688. }
  689. }
  690. private IEnumerable<GameObject> GetAllRoots()
  691. {
  692. var objects = GameObject.FindObjectsOfType<GameObject>();
  693. foreach( var obj in objects )
  694. {
  695. if( obj.transform.parent == null )
  696. {
  697. yield return obj;
  698. }
  699. }
  700. }
  701. private void TraverseChildren( StreamWriter writer, GameObject obj, string identation )
  702. {
  703. var layer = LayerMask.LayerToName( obj.gameObject.layer );
  704. var components = string.Join( ", ", obj.GetComponents<Component>().Select( x => x.GetType().Name ).ToArray() );
  705. var line = string.Format( "{0,-50} {1,100}",
  706. identation + obj.gameObject.name + " [" + layer + "]",
  707. components );
  708. writer.WriteLine( line );
  709. for( int i = 0 ; i < obj.transform.childCount ; i++ )
  710. {
  711. var child = obj.transform.GetChild( i );
  712. TraverseChildren( writer, child.gameObject, identation + " " );
  713. }
  714. }
  715. }
  716. }