KnownWwwEndpoint.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Reflection;
  6. using Harmony;
  7. using UnityEngine;
  8. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  9. namespace XUnity.AutoTranslator.Plugin.Core.Web
  10. {
  11. public abstract class KnownWwwEndpoint : IKnownEndpoint
  12. {
  13. protected static readonly ConstructorInfo WwwConstructor = Constants.ClrTypes.WWW.GetConstructor( new[] { typeof( string ), typeof( byte[] ), typeof( Dictionary<string, string> ) } );
  14. private bool _isBusy = false;
  15. public KnownWwwEndpoint()
  16. {
  17. }
  18. public bool IsBusy => _isBusy;
  19. public virtual bool SupportsLineSplitting
  20. {
  21. get
  22. {
  23. return false;
  24. }
  25. }
  26. public IEnumerator Translate( string untranslatedText, string from, string to, Action<string> success, Action failure )
  27. {
  28. _isBusy = true;
  29. try
  30. {
  31. var setup = OnBeforeTranslate( Settings.TranslationCount );
  32. if( setup != null )
  33. {
  34. while( setup.MoveNext() )
  35. {
  36. yield return setup.Current;
  37. }
  38. }
  39. Logger.Current.Debug( "Starting translation for: " + untranslatedText );
  40. object www = null;
  41. try
  42. {
  43. var headers = new Dictionary<string, string>();
  44. ApplyHeaders( headers );
  45. var url = GetServiceUrl( untranslatedText, from, to );
  46. www = WwwConstructor.Invoke( new object[] { url, null, headers } );
  47. }
  48. catch( Exception e )
  49. {
  50. Logger.Current.Error( e, "Error occurred while setting up translation request." );
  51. }
  52. if( www != null )
  53. {
  54. yield return www;
  55. try
  56. {
  57. string error = null;
  58. try
  59. {
  60. error = (string)AccessTools.Property( Constants.ClrTypes.WWW, "error" ).GetValue( www, null );
  61. }
  62. catch( Exception e )
  63. {
  64. error = e.ToString();
  65. }
  66. if( error != null )
  67. {
  68. Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + error );
  69. failure();
  70. }
  71. else
  72. {
  73. var text = (string)AccessTools.Property( Constants.ClrTypes.WWW, "text" ).GetValue( www, null );
  74. if( text != null )
  75. {
  76. if( TryExtractTranslated( text, out var translatedText ) )
  77. {
  78. Logger.Current.Debug( $"Translation for '{untranslatedText}' succeded. Result: {translatedText}" );
  79. translatedText = translatedText ?? string.Empty;
  80. success( translatedText );
  81. }
  82. else
  83. {
  84. Logger.Current.Error( "Error occurred while extracting translation." );
  85. failure();
  86. }
  87. }
  88. else
  89. {
  90. Logger.Current.Error( "Error occurred while extracting text from response." );
  91. failure();
  92. }
  93. }
  94. }
  95. catch( Exception e )
  96. {
  97. Logger.Current.Error( e, "Error occurred while retrieving translation." );
  98. failure();
  99. }
  100. }
  101. else
  102. {
  103. failure();
  104. }
  105. }
  106. finally
  107. {
  108. _isBusy = false;
  109. }
  110. }
  111. public virtual void OnUpdate()
  112. {
  113. }
  114. public virtual bool ShouldGetSecondChanceAfterFailure()
  115. {
  116. return false;
  117. }
  118. public abstract string GetServiceUrl( string untranslatedText, string from, string to );
  119. public abstract void ApplyHeaders( Dictionary<string, string> headers );
  120. public abstract bool TryExtractTranslated( string result, out string translated );
  121. public virtual IEnumerator OnBeforeTranslate( int translationCount )
  122. {
  123. return null;
  124. }
  125. }
  126. }