using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using SimpleJSON; using XUnity.AutoTranslator.Plugin.Core.Endpoints; using XUnity.AutoTranslator.Plugin.Core.Endpoints.Http; using XUnity.AutoTranslator.Plugin.Core.Utilities; using XUnity.AutoTranslator.Plugin.Core.Web; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyCompany("gravydevsupreme, Bepis, others at https://github.com/bbepis/XUnity.AutoTranslator")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright © 2018 / MIT License")] [assembly: AssemblyFileVersion("5.6.1.0")] [assembly: AssemblyInformationalVersion("5.6.1+7f1f3b9e8fc7d93a97734773804ba9c8fdf57714")] [assembly: AssemblyProduct("YandexTranslate")] [assembly: AssemblyTitle("YandexTranslate")] [assembly: AssemblyVersion("5.6.1.0")] namespace YandexTranslate; internal class YandexTranslateEndpoint : HttpEndpoint { private static readonly HashSet SupportedLanguages = new HashSet { "az", "sq", "am", "en", "ar", "hy", "af", "eu", "ba", "be", "bn", "my", "bg", "bs", "cy", "hu", "vi", "ht", "gl", "nl", "mrj", "el", "ka", "gu", "da", "he", "yi", "id", "ga", "it", "is", "es", "kk", "kn", "ca", "ky", "zh", "ko", "xh", "km", "lo", "la", "lv", "lt", "lb", "mg", "ms", "ml", "mt", "mk", "mi", "mr", "mhr", "mn", "de", "ne", "no", "pa", "pap", "fa", "pl", "pt", "ro", "ru", "ceb", "sr", "si", "sk", "sl", "sw", "su", "tg", "th", "tl", "ta", "tt", "te", "tr", "udm", "uz", "uk", "ur", "fi", "fr", "hi", "hr", "cs", "sv", "gd", "et", "eo", "jv", "ja" }; private static readonly string HttpsServiceUrl = "https://translate.api.cloud.yandex.net/translate/v2/translate"; private string _key; public override string Id => "YandexTranslate"; public override string FriendlyName => "Yandex Translate"; private string FixLanguage(string lang) { if (lang == "zh-CN" || lang == "zh-Hans") { return "zh"; } return lang; } public override void Initialize(IInitializationContext context) { //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0074: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) _key = context.GetOrCreateSetting("Yandex", "YandexAPIKey", ""); context.DisableCertificateChecksFor(new string[1] { "translate.api.cloud.yandex.net" }); if (string.IsNullOrEmpty(_key)) { throw new EndpointInitializationException("The YandexTranslate endpoint requires an API key which has not been provided."); } if (!SupportedLanguages.Contains(FixLanguage(context.SourceLanguage))) { throw new EndpointInitializationException("The source language '" + context.SourceLanguage + "' is not supported."); } if (!SupportedLanguages.Contains(FixLanguage(context.DestinationLanguage))) { throw new EndpointInitializationException("The destination language '" + context.DestinationLanguage + "' is not supported."); } } public override void OnCreateRequest(IHttpRequestCreationContext context) { //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0079: Expected O, but got Unknown string text = FixLanguage(((ITranslationContextBase)context).SourceLanguage); string text2 = FixLanguage(((ITranslationContextBase)context).DestinationLanguage); string text3 = JsonHelper.Escape(((ITranslationContextBase)context).UntranslatedText ?? string.Empty); string text4 = "{\"sourceLanguageCode\":\"" + text + "\",\"targetLanguageCode\":\"" + text2 + "\",\"texts\":[\"" + text3 + "\"]}"; XUnityWebRequest val = new XUnityWebRequest("POST", HttpsServiceUrl, text4); val.Headers["Authorization"] = "Api-Key " + _key; val.Headers["Content-Type"] = "application/json; charset=utf-8"; val.Headers["Accept"] = "application/json"; context.Complete(val); } public override void OnExtractTranslation(IHttpTranslationExtractionContext context) { string data = ((IHttpResponseInspectionContext)context).Response.Data; if (string.IsNullOrEmpty(data)) { ((ITranslationContextBase)context).Fail("Empty response from Yandex."); } JSONNode obj = JSON.Parse(data); if (obj == (object)null) { ((ITranslationContextBase)context).Fail("Failed to parse JSON from Yandex response."); } JSONNode val = obj["translations"]; if (val == (object)null || val.Count == 0) { ((ITranslationContextBase)context).Fail("No translations found in response."); } JSONNode obj2 = val[0]["text"]; string text = ((obj2 != null) ? obj2.Value : null); if (string.IsNullOrEmpty(text)) { ((ITranslationContextBase)context).Fail("Received no translation."); } context.Complete(text); } } internal static class GeneratedInfo { public const string PROJECT_VERSION = "5.6.1"; }