KnownWwwEndpoint.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Types.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 IEnumerator Translate( string untranslatedText, string from, string to, Action<string> success, Action failure )
  20. {
  21. _isBusy = true;
  22. try
  23. {
  24. var setup = OnBeforeTranslate( Settings.TranslationCount );
  25. if( setup != null )
  26. {
  27. while( setup.MoveNext() )
  28. {
  29. yield return setup.Current;
  30. }
  31. }
  32. Logger.Current.Debug( "Starting translation for: " + untranslatedText );
  33. object www = null;
  34. try
  35. {
  36. var headers = new Dictionary<string, string>();
  37. ApplyHeaders( headers );
  38. var url = GetServiceUrl( untranslatedText, from, to );
  39. www = WwwConstructor.Invoke( new object[] { url, null, headers } );
  40. }
  41. catch( Exception e )
  42. {
  43. Logger.Current.Error( e, "Error occurred while setting up translation request." );
  44. }
  45. if( www != null )
  46. {
  47. yield return www;
  48. try
  49. {
  50. string error = null;
  51. try
  52. {
  53. error = (string)AccessTools.Property( Constants.Types.WWW, "error" ).GetValue( www, null );
  54. }
  55. catch( Exception e )
  56. {
  57. error = e.ToString();
  58. }
  59. if( error != null )
  60. {
  61. Logger.Current.Error( "Error occurred while retrieving translation." + Environment.NewLine + error );
  62. failure();
  63. }
  64. else
  65. {
  66. var text = (string)AccessTools.Property( Constants.Types.WWW, "text" ).GetValue( www, null ); ;
  67. if( text != null )
  68. {
  69. if( TryExtractTranslated( text, out var translatedText ) )
  70. {
  71. Logger.Current.Debug( $"Translation for '{untranslatedText}' succeded. Result: {translatedText}" );
  72. translatedText = translatedText ?? string.Empty;
  73. success( translatedText );
  74. }
  75. else
  76. {
  77. Logger.Current.Error( "Error occurred while extracting translation." );
  78. failure();
  79. }
  80. }
  81. else
  82. {
  83. Logger.Current.Error( "Error occurred while extracting text from response." );
  84. failure();
  85. }
  86. }
  87. }
  88. catch( Exception e )
  89. {
  90. Logger.Current.Error( e, "Error occurred while retrieving translation." );
  91. failure();
  92. }
  93. }
  94. else
  95. {
  96. failure();
  97. }
  98. }
  99. finally
  100. {
  101. _isBusy = false;
  102. }
  103. }
  104. public virtual void OnUpdate()
  105. {
  106. }
  107. public virtual bool ShouldGetSecondChanceAfterFailure()
  108. {
  109. return false;
  110. }
  111. public abstract string GetServiceUrl( string untranslatedText, string from, string to );
  112. public abstract void ApplyHeaders( Dictionary<string, string> headers );
  113. public abstract bool TryExtractTranslated( string result, out string translated );
  114. public virtual IEnumerator OnBeforeTranslate( int translationCount )
  115. {
  116. return null;
  117. }
  118. }
  119. }