TranslationJob.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using UnityEngine.UI;
  7. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  8. using XUnity.AutoTranslator.Plugin.Core.Parsing;
  9. namespace XUnity.AutoTranslator.Plugin.Core
  10. {
  11. public class TranslationJob
  12. {
  13. public TranslationJob( TranslationKey key )
  14. {
  15. Key = key;
  16. Components = new List<object>();
  17. OriginalSources = new HashSet<object>();
  18. Contexts = new HashSet<TranslationContext>();
  19. }
  20. public HashSet<TranslationContext> Contexts { get; private set; }
  21. public List<object> Components { get; private set; }
  22. public HashSet<object> OriginalSources { get; private set; }
  23. public TranslationKey Key { get; private set; }
  24. public string TranslatedText { get; set; }
  25. public TranslationJobState State { get; set; }
  26. public bool AnyComponentsStillHasOriginalUntranslatedTextOrContextual()
  27. {
  28. if( Components.Count == 0 || Contexts.Count > 0 ) return true; // we do not know
  29. for( int i = Components.Count - 1 ; i >= 0 ; i-- )
  30. {
  31. var component = Components[ i ];
  32. try
  33. {
  34. var text = component.GetText().TrimIfConfigured();
  35. if( text == Key.OriginalText )
  36. {
  37. return true;
  38. }
  39. }
  40. catch( NullReferenceException )
  41. {
  42. // might fail if compoent is no longer associated to game
  43. Components.RemoveAt( i );
  44. }
  45. }
  46. return false;
  47. }
  48. public void Associate( TranslationContext context )
  49. {
  50. if( context != null )
  51. {
  52. Contexts.Add( context );
  53. context.Jobs.Add( this );
  54. }
  55. }
  56. }
  57. public enum TranslationJobState
  58. {
  59. RunningOrQueued,
  60. Succeeded,
  61. Failed
  62. }
  63. public class TranslationContext
  64. {
  65. public TranslationContext( object component, ParserResult result )
  66. {
  67. Jobs = new HashSet<TranslationJob>();
  68. Component = component;
  69. Result = result;
  70. }
  71. public ParserResult Result { get; private set; }
  72. public HashSet<TranslationJob> Jobs { get; private set; }
  73. public object Component { get; private set; }
  74. }
  75. }