Program.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading;
  5. using XUnity.AutoTranslator.Plugin.ExtProtocol;
  6. namespace Lec.ExtProtocol
  7. {
  8. class Program
  9. {
  10. static void Main( string[] args )
  11. {
  12. try
  13. {
  14. if( args.Length == 0 )
  15. {
  16. Console.WriteLine( "This program is automatically run during a game session if LEC is selected, and will be automatically stopped afterwards. This program cannot be run independently." );
  17. Console.WriteLine( "Press any key to exit." );
  18. Console.ReadKey();
  19. return;
  20. }
  21. var powerTranslatorPathPayload = args[ 0 ];
  22. var powerTranslatorPath = Encoding.UTF8.GetString( Convert.FromBase64String( powerTranslatorPathPayload ) );
  23. var dllPath = Path.Combine( powerTranslatorPath, @"Nova\JaEn\EngineDll_je.dll" );
  24. using( var translator = new LecTranslationLibrary( dllPath ) )
  25. {
  26. using( var stdout = Console.OpenStandardOutput() )
  27. using( var writer = new StreamWriter( stdout ) )
  28. using( var stdin = Console.OpenStandardInput() )
  29. using( var reader = new StreamReader( stdin ) )
  30. {
  31. writer.AutoFlush = true;
  32. while( true )
  33. {
  34. var receivedPayload = reader.ReadLine();
  35. if( string.IsNullOrEmpty( receivedPayload ) ) return;
  36. var message = ExtProtocolConvert.Decode( receivedPayload ) as TranslationRequest;
  37. if( message == null ) return;
  38. var translatedLine = translator.Translate( message.UntranslatedText );
  39. var response = new TranslationResponse
  40. {
  41. Id = message.Id,
  42. TranslatedText = translatedLine
  43. };
  44. var translatedPayload = ExtProtocolConvert.Encode( response );
  45. writer.WriteLine( translatedPayload );
  46. }
  47. }
  48. }
  49. }
  50. catch( Exception )
  51. {
  52. // "Graceful shutdown"
  53. }
  54. }
  55. }
  56. }