TranslationManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using UnityEngine;
  8. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  9. using XUnity.AutoTranslator.Plugin.Core.Endpoints;
  10. using XUnity.AutoTranslator.Plugin.Core.Web;
  11. namespace XUnity.AutoTranslator.Plugin.Core
  12. {
  13. class TranslationManager
  14. {
  15. public event Action<TranslationJob> JobCompleted;
  16. public event Action<TranslationJob> JobFailed;
  17. private readonly List<TranslationEndpointManager> _endpointsWithUnstartedJobs;
  18. public TranslationManager()
  19. {
  20. _endpointsWithUnstartedJobs = new List<TranslationEndpointManager>();
  21. ConfiguredEndpoints = new List<TranslationEndpointManager>();
  22. AllEndpoints = new List<TranslationEndpointManager>();
  23. }
  24. public int OngoingTranslations { get; set; }
  25. public int UnstartedTranslations { get; set; }
  26. public List<TranslationEndpointManager> ConfiguredEndpoints { get; private set; }
  27. public List<TranslationEndpointManager> AllEndpoints { get; private set; }
  28. public TranslationEndpointManager CurrentEndpoint { get; set; }
  29. public void InitializeEndpoints( GameObject go )
  30. {
  31. try
  32. {
  33. var httpSecurity = new HttpSecurity();
  34. var context = new InitializationContext( httpSecurity, Settings.FromLanguage, Settings.Language );
  35. CreateEndpoints( go, context );
  36. AllEndpoints = AllEndpoints
  37. .OrderBy( x => x.Error != null )
  38. .ThenBy( x => x.Endpoint.FriendlyName )
  39. .ToList();
  40. var primaryEndpoint = AllEndpoints.FirstOrDefault( x => x.Endpoint.Id == Settings.ServiceEndpoint );
  41. if( primaryEndpoint != null )
  42. {
  43. if( primaryEndpoint.Error != null )
  44. {
  45. XuaLogger.Current.Error( primaryEndpoint.Error, "Error occurred during the initialization of the selected translate endpoint." );
  46. }
  47. else
  48. {
  49. CurrentEndpoint = primaryEndpoint;
  50. }
  51. }
  52. else if( !string.IsNullOrEmpty( Settings.ServiceEndpoint ) )
  53. {
  54. XuaLogger.Current.Error( $"Could not find the configured endpoint '{Settings.ServiceEndpoint}'." );
  55. }
  56. if( Settings.DisableCertificateValidation )
  57. {
  58. XuaLogger.Current.Info( $"Disabling certificate checks for endpoints because of configuration." );
  59. ServicePointManager.ServerCertificateValidationCallback += ( a1, a2, a3, a4 ) => true;
  60. }
  61. else
  62. {
  63. var callback = httpSecurity.GetCertificateValidationCheck();
  64. if( callback != null && !Features.SupportsNet4x )
  65. {
  66. XuaLogger.Current.Info( $"Disabling certificate checks for endpoints because a .NET 3.x runtime is used." );
  67. ServicePointManager.ServerCertificateValidationCallback += callback;
  68. }
  69. else
  70. {
  71. XuaLogger.Current.Info( $"Not disabling certificate checks for endpoints because a .NET 4.x runtime is used." );
  72. }
  73. }
  74. // save config because the initialization phase of plugins may have changed the config
  75. Settings.Save();
  76. }
  77. catch( Exception e )
  78. {
  79. XuaLogger.Current.Error( e, "An error occurred while constructing endpoints. Shutting plugin down." );
  80. Settings.IsShutdown = true;
  81. }
  82. }
  83. public void CreateEndpoints( GameObject go, InitializationContext context )
  84. {
  85. var pluginFolder = Path.Combine( PluginEnvironment.Current.PluginPath, Settings.TranslatorsFolder );
  86. var dynamicTypes = AssemblyLoader.GetAllTypesOf<ITranslateEndpoint>( pluginFolder );
  87. foreach( var type in dynamicTypes )
  88. {
  89. AddEndpoint( go, context, type );
  90. }
  91. }
  92. private void AddEndpoint( GameObject go, InitializationContext context, Type type )
  93. {
  94. ITranslateEndpoint endpoint;
  95. try
  96. {
  97. if( typeof( MonoBehaviour ).IsAssignableFrom( type ) )
  98. {
  99. // allow implementing plugins to hook into Unity lifecycle
  100. endpoint = (ITranslateEndpoint)go.AddComponent( type );
  101. UnityEngine.Object.DontDestroyOnLoad( (UnityEngine.Object)endpoint );
  102. }
  103. else
  104. {
  105. // or... just use any old object
  106. endpoint = (ITranslateEndpoint)Activator.CreateInstance( type );
  107. }
  108. }
  109. catch( Exception e )
  110. {
  111. XuaLogger.Current.Error( e, "Could not instantiate class: " + type.Name );
  112. return;
  113. }
  114. try
  115. {
  116. endpoint.Initialize( context );
  117. var manager = new TranslationEndpointManager( endpoint, null );
  118. RegisterEndpoint( manager );
  119. }
  120. catch( Exception e )
  121. {
  122. var manager = new TranslationEndpointManager( endpoint, e );
  123. RegisterEndpoint( manager );
  124. }
  125. }
  126. public void KickoffTranslations()
  127. {
  128. // iterate in reverse order so we can remove from list while iterating
  129. var endpoints = _endpointsWithUnstartedJobs;
  130. for( int i = endpoints.Count - 1; i >= 0; i-- )
  131. {
  132. var endpoint = endpoints[ i ];
  133. if( Settings.EnableBatching && endpoint.CanBatch )
  134. {
  135. while( endpoint.HasUnstartedBatch )
  136. {
  137. if( endpoint.IsBusy ) break;
  138. endpoint.HandleNextBatch();
  139. }
  140. }
  141. else
  142. {
  143. while( endpoint.HasUnstartedJob )
  144. {
  145. if( endpoint.IsBusy ) break;
  146. endpoint.HandleNextJob();
  147. }
  148. }
  149. }
  150. }
  151. public void ScheduleUnstartedJobs( TranslationEndpointManager endpoint )
  152. {
  153. _endpointsWithUnstartedJobs.Add( endpoint );
  154. }
  155. public void UnscheduleUnstartedJobs( TranslationEndpointManager endpoint )
  156. {
  157. _endpointsWithUnstartedJobs.Remove( endpoint );
  158. }
  159. public void ClearAllJobs()
  160. {
  161. foreach( var endpoint in ConfiguredEndpoints )
  162. {
  163. endpoint.ClearAllJobs();
  164. }
  165. }
  166. public void RebootAllEndpoints()
  167. {
  168. foreach( var endpoint in ConfiguredEndpoints )
  169. {
  170. endpoint.ConsecutiveErrors = 0;
  171. }
  172. }
  173. public void RegisterEndpoint( TranslationEndpointManager translationEndpointManager )
  174. {
  175. translationEndpointManager.Manager = this;
  176. AllEndpoints.Add( translationEndpointManager );
  177. if( translationEndpointManager.Error == null )
  178. {
  179. ConfiguredEndpoints.Add( translationEndpointManager );
  180. }
  181. }
  182. public void InvokeJobCompleted( TranslationJob job )
  183. {
  184. JobCompleted?.Invoke( job );
  185. }
  186. public void InvokeJobFailed( TranslationJob job )
  187. {
  188. JobFailed?.Invoke( job );
  189. }
  190. }
  191. }