Esempio n. 1
0
        private async Task <string> ExtendTranslationAsync(string result, string key, string language, IDictionary <string, object> args,
                                                           TranslationOptions options)
        {
            IDictionary <string, object> replaceArgs;

            if ((args?.ContainsKey("replace") ?? false) && args["replace"].GetType().IsClass)
            {
                replaceArgs = args["replace"].ToDictionary();
            }
            else
            {
                replaceArgs = args;
            }

            if (AllowInterpolation && (!(args?.ContainsKey("interpolate") ?? false) || args["interpolate"] is bool interpolate && interpolate))
            {
                result = await _interpolator.InterpolateAsync(result, key, language, replaceArgs);
            }

            if (AllowNesting && (!(args?.ContainsKey("nest") ?? false) || args["nest"] is bool nest && nest) && _interpolator.CanNest(result))
            {
                result = await _interpolator.NestAsync(result, language, replaceArgs,
                                                       (lang2, key2, args2) => TranslateAsync(lang2, key2, args2, options));
            }

            if (AllowPostprocessing && PostProcessors.Count > 0)
            {
                result = HandlePostProcessing(result, key, args);
            }

            return(result);
        }
Esempio n. 2
0
 public void SetUp()
 {
     _translator = new DefaultTranslator(_backend, null, _pluralResolver, _interpolator);
     _options    = new TranslationOptions {
         DefaultNamespace = "test"
     };
 }
Esempio n. 3
0
        /// <summary>
        /// Get the translation of an xliff file using the ETS API.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="languageDirection"></param>
        /// <param name="xliffFile"></param>
        /// <returns></returns>
        public static string GetTranslation(TranslationOptions options,
                                            LanguagePair languageDirection,
                                            Xliff xliffFile)
        {
            Log.Logger.Trace("");
            var text         = xliffFile.ToString();
            var queryString  = HttpUtility.ParseQueryString(string.Empty);
            var encodedInput = text.Base64Encode();

            lock (optionsLock)
            {
                if (options.ApiVersion == APIVersion.Unknown)
                {
                    SetETSApiVersion(options);
                }
            }

            if (options.ApiVersion == APIVersion.v1)
            {
                queryString["sourceLanguageId"] = languageDirection.SourceCulture.ToETSCode();
                queryString["targetLanguageId"] = languageDirection.TargetCulture.ToETSCode();
                queryString["text"]             = encodedInput;
            }
            else
            {
                // If LPPreferences doesn't contain the target language (source is always the same), figure out the
                // preferred LP. Previously set preferred LPs will stay, so this won't get run each time if you have
                // multiple LPs.
                if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                {
                    options.SetPreferredLanguages(new LanguagePair[] { languageDirection });
                    if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                    {
                        throw new Exception("There are no language pairs currently accessible via ETS.");
                    }
                }
                queryString["languagePairId"] = options.LPPreferences[languageDirection.TargetCulture].LanguagePairId;
                queryString["input"]          = encodedInput;
            }
            queryString["inputFormat"] = "application/x-xliff";

            Log.Logger.Debug("Sending translation request for: {0}", encodedInput);
            string jsonResult;

            try
            {
                jsonResult = ContactETSServer(ETSPost, options, "translations/quick", queryString);
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{Constants.Translation}: {e.Message}\n {e.StackTrace}\n Encoded Input: {encodedInput}");
                throw;
            }

            var encodedTranslation = JsonConvert.DeserializeObject <ETSTranslationOutput>(jsonResult).Translation;
            var decodedTranslation = encodedTranslation.Base64Decode();

            Log.Logger.Debug("Resultant translation is: {0}", encodedTranslation);
            return(decodedTranslation);
        }
    void DrawToolWindow()
    {
        GUILayout.Label("Select trasnlation type", EditorStyles.boldLabel);
        projectOutFileName = EditorGUILayout.TextField("Output file name: ", projectOutFileName);
        scenePrefix        = EditorGUILayout.TextField("Name key prefix: ", scenePrefix);
        translationOption  = (TranslationOptions)EditorGUILayout.EnumPopup("Translate object type of: ", translationOption);
        extension          = (Extensions)EditorGUILayout.EnumPopup("Select file's extension: ", extension);
        fieldTypes         = (GDEFiledTypes)EditorGUILayout.EnumPopup("Select GDE fields type: ", fieldTypes);


        if (GUILayout.Button("Refresh output file"))
        {
            UpdateObjectWithTranslationComponent();
        }

        else if (GUILayout.Button("Confirm"))
        {
            renameType = RenameType.Selection;
            if (string.IsNullOrEmpty(projectOutFileName) || projectOutFileName.Length > 40)
            {
                projectOutFileName = "results";
                EditorUtility.DisplayDialog("Translation plugin message", "Output file name can't be empty or longher than 40 characters. Output file saved as \"results.txt\".\nYou can retype the name of output file via 4Experience/Tools/Setup Translations window.", "OK");
            }
            InvokeSelectWithUpdate();
            AssetDatabase.Refresh();
        }
    }
        public ProviderConfDialog(TranslationOptions options, ITranslationProviderCredentialStore store, LanguagePair[] languagePairs)
        {
            credentialStore = store;
            Options         = options;
            LanguagePairs   = languagePairs;
            InitializeComponent();

            if (PluginConfiguration.CurrentInstance.DefaultConnection.HasValue &&
                Options.Host == null)
            {
                Options.Host = PluginConfiguration.CurrentInstance.DefaultConnection.Value.Host;
                Options.Port = PluginConfiguration.CurrentInstance.DefaultConnection.Value.Port;
            }
            // Set this outside the if statement in case Options Host & Port were already set to the default connection
            setDefaultTM.Checked = PluginConfiguration.CurrentInstance.DefaultConnection.HasValue &&
                                   PluginConfiguration.CurrentInstance.DefaultConnection.Value.Host == Options.Host &&
                                   PluginConfiguration.CurrentInstance.DefaultConnection.Value.Port == Options.Port;

            UpdateDialog();
            Text = @"SDL Machine Translation Edge";

            // .5 seconds after certain events, run the populate command. This prevents us from authenticating each
            // keypress (as that was causing massive lag).
            lpPopulationTimer.AutoReset = false;
            lpPopulationTimer.Elapsed  += lpPopulationTimer_Elapsed;
        }
        /// <summary>
        /// Verifies that the API Key passed by the user is a valid API key.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="credentials"></param>
        public static void VerifyBasicAPIToken(TranslationOptions options, GenericCredentials credentials)
        {
            _logger.Trace("");
            if (options == null)
            {
                throw new ArgumentNullException("Options is null");
            }
            var oldAPIKey = options.ApiToken;

            options.ApiToken = credentials["API-Key"];
            options.UseBasicAuthentication = credentials["UseApiKey"] != "true";

            try
            {
                // Make a request to the API using whatever path desired.
                ContactMtEdgeServer(MtEdgeGet, options, "language-pairs");
            }
            catch (AggregateException e)
            {
                _logger.Error($"{Constants.VerifyBasicAPIToken}: {e.Message}\n {e.StackTrace}");
                throw TranslateAggregateException(e);
            }
            catch (SocketException e)
            {
                _logger.Error($"{Constants.VerifyBasicAPIToken}: {e.Message}\n {e.StackTrace}");
                throw TranslateAggregateException(e);
            }
            finally
            {
                options.ApiToken = oldAPIKey;
            }
        }
        public void Constructor_Parameterless_NullOrEmptyValues()
        {
            var options = new TranslationOptions();

            Assert.AreEqual(options.Host, null);
            Assert.AreEqual(options.ApiToken, null);
            Assert.AreEqual(options.Port, 8001);
        }
        public ETSHelperTests()
        {
            apiKeyTranslationOptions = new TranslationOptions(new Uri(StringResource.ApiUrl));
            apiKeyTranslationOptions.UseBasicAuthentication = false;
            apiKeyTranslationOptions.ApiToken = StringResource.ApiKey;

            basicAuthTranslationOptions = new TranslationOptions(new Uri(StringResource.ApiUrl));
            basicAuthTranslationOptions.UseBasicAuthentication = true;
            userCredentials = new GenericCredentials(StringResource.Username, StringResource.Password);
        }
        public TranslationProviderLanguageDirectionTests()
        {
            TranslationOptions options = new TranslationOptions(new Uri(StringResource.ApiUrl));

            options.UseBasicAuthentication = false;
            options.ApiToken = StringResource.ApiKey;
            provider         = new TranslationProvider(options);

            settings = new SearchSettings();
        }
Esempio n. 10
0
        private async Task <string> ResolveTranslationAsync(string language, string ns, string key, IDictionary <string, object> args,
                                                            TranslationOptions options)
        {
            var needsPluralHandling  = CheckForSpecialArg(args, "count", typeof(int), typeof(long)) && _pluralResolver.NeedsPlural(language);
            var needsContextHandling = CheckForSpecialArg(args, "context", typeof(string));

            var finalKey     = key;
            var possibleKeys = new List <string>();

            possibleKeys.Add(finalKey);
            var pluralSuffix = string.Empty;

            if (needsPluralHandling)
            {
                var count = (int)Convert.ChangeType(args["count"], typeof(int));
                pluralSuffix = _pluralResolver.GetPluralSuffix(language, count);

                // Fallback for plural if context was not found
                if (needsContextHandling)
                {
                    possibleKeys.Add($"{finalKey}{pluralSuffix}");
                }
            }

            // Get key for context if needed
            if (needsContextHandling)
            {
                var context = (string)args["context"];
                finalKey = $"{finalKey}{ContextSeparator}{context}";
                possibleKeys.Add(finalKey);
            }

            // Get key for plural if needed
            if (needsPluralHandling)
            {
                finalKey = $"{finalKey}{pluralSuffix}";
                possibleKeys.Add(finalKey);
            }

            // Try to resolve the translation from the backend
            var result = await ResolveTranslationFromBackendAsync(language, ns, args, possibleKeys);

            if (result == null)
            {
                OnMissingKey(language, ns, key, possibleKeys);
            }

            // Try to resolve the translation from the backend for all fallback langauges
            if (result == null && options.FallbackLanguages != null && options.FallbackLanguages.Length > 0)
            {
                result = await ResolveFallbackTranslationAsync(ns, args, options.FallbackLanguages, possibleKeys);
            }

            return(result);
        }
        public void Constructor_Parameters_ValidValues()
        {
            var options = new TranslationOptions(new Uri(UTStringResource.ApiUrl));

            string host = UTStringResource.ApiHost;
            int    port = 8001;

            Assert.AreEqual(options.Port, port);
            // localhost should be resolved via DNS to wgitbuild#
            Assert.AreNotEqual(options.Uri.ToString(), UTStringResource.ApiUrl);
        }
Esempio n. 12
0
        public ConversionResult Convert(
            ShaderType shaderType, TargetVersion targetVersion, TranslationOptions options,
            string hlslCode, string entryPoint,
            Dictionary <AttributeSemantic, string> overrideAttributeNames = null)
        {
            var compilerPtr = NativeMethods.Hlsl2Glsl_ConstructCompiler(shaderType);

            if (compilerPtr == IntPtr.Zero)
            {
                throw new Exception("Failed to construct Hlsl2Glsl compiler");
            }

            try
            {
                var callbacks = new NativeMethods.Hlsl2Glsl_ParseCallbacks
                {
                    IncludeOpenCallback = null,
                    Data = IntPtr.Zero
                };
                var parseResult = NativeMethods.Hlsl2Glsl_Parse(compilerPtr, hlslCode, targetVersion, ref callbacks, options);
                if (parseResult != 1)
                {
                    throw GetInfoLogAndCreateException(compilerPtr, "Failed to parse HLSL code");
                }

                if (overrideAttributeNames != null)
                {
                    var setUserAttrNamesResult = NativeMethods.Hlsl2Glsl_SetUserAttributeNames(compilerPtr,
                                                                                               overrideAttributeNames.Keys.ToArray(), overrideAttributeNames.Values.ToArray(),
                                                                                               overrideAttributeNames.Count);
                    if (setUserAttrNamesResult != 1)
                    {
                        throw GetInfoLogAndCreateException(compilerPtr, "Failed to user attribute names");
                    }
                }

                var translateResult = NativeMethods.Hlsl2Glsl_Translate(compilerPtr, entryPoint, targetVersion, options);
                if (translateResult != 1)
                {
                    throw GetInfoLogAndCreateException(compilerPtr, "Failed to translate HLSL code");
                }

                var glsl = NativeMethods.Hlsl2Glsl_GetShader(compilerPtr);

                var uniforms = NativeMethods.Hlsl2Glsl_GetUniformInfo(compilerPtr);

                return(new ConversionResult(glsl, uniforms));
            }
            finally
            {
                NativeMethods.Hlsl2Glsl_DestructCompiler(compilerPtr);
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Construtor padrão
 /// </summary>
 /// <param name="nextMiddlewareInPipelineDelegate"></param>
 /// <param name="tokenManager"></param>
 /// <param name="logger"></param>
 /// <param name="errorLogger"></param>
 /// <param name="translateOptions"></param>
 /// <param name="globalizationOptions"></param>
 /// <param name="memoryCache"></param>
 /// <param name="httpClientFactory"></param>
 public TranslationMiddleware(RequestDelegate nextMiddlewareInPipelineDelegate, IClientTokenManager tokenManager, ILogger <TranslationMiddleware> logger, IErrorLogger errorLogger, IOptions <TranslationOptions> translateOptions, IOptions <GlobalizationOptions> globalizationOptions, IMemoryCache memoryCache, IHttpClientFactory httpClientFactory)
 {
     this.nextMiddlewareInPipelineDelegate = nextMiddlewareInPipelineDelegate;
     this.tokenManager             = tokenManager;
     this.logger                   = logger;
     this.errorLogger              = errorLogger;
     this.translateOptions         = translateOptions?.Value ?? throw new ArgumentException("Translate options required.");
     this.globalizationOptions     = globalizationOptions?.Value ?? throw new ArgumentException("Globalization options required.");
     this.memoryCache              = memoryCache ?? throw new ArgumentException("IMemoryCache options required.");
     this.httpClientFactory        = httpClientFactory ?? throw new ArgumentException("IHttpClientFactory required.");
     recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();
 }
Esempio n. 14
0
        public TranslationServiceUnitTests()
        {
            _translationDataProviderMock = new Mock <ITranslationDataProvider>();
            _translationOptionsMock      = new Mock <IOptions <TranslationOptions> >();
            var translationOptions = new TranslationOptions();

            _translationOptionsMock.Setup(x => x.Value).Returns(translationOptions);
            _cacheEntryMock = new Mock <ICacheEntry>();
            _cacheEntryMock.SetupGet(c => c.ExpirationTokens).Returns(new List <IChangeToken>());
            IList <ITranslationDataProvider> listProvider = new List <ITranslationDataProvider> {
                _translationDataProviderMock.Object
            };

            _translationService = new TranslationService(listProvider, GetCache(), _translationOptionsMock.Object);
        }
Esempio n. 15
0
        public void Test()
        {
            IExtensionPoint[] extensionPoints = { new StringExtensionPoint() };
            var generator = new JavaGenerator(extensionPoints.OfType <ITypeResolver>().ToArray());
            var options   = new TranslationOptions
            {
                IsBeautify     = true,
                MultiThreading = false
            };
            var translatorRunner = new TranslationRunner(generator, extensionPoints, options);
            var inDir            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles");
            var outDir           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFilesOut");

            translatorRunner.Translate(inDir, outDir, null);
        }
Esempio n. 16
0
 public static void SetETSApiVersion(TranslationOptions options)
 {
     try
     {
         options.APIVersion = APIVersion.v2;
         string systemInfo = ContactETSServer(ETSGet, options, "system/info");
         if (systemInfo == null)
         {
             options.APIVersion = APIVersion.v1;
         }
     }
     catch
     {
         options.APIVersion = APIVersion.v1;
     }
 }
Esempio n. 17
0
        void TranslateResxFiles()
        {
            var srcLng        = ((KeyValuePair <string, string>)cmbSourceResxLng.SelectedItem).Key;
            var destLanguages = new List <string>();

            foreach (ListViewItem item in lstResxLanguages.Items)
            {
                if (!item.Checked)
                {
                    continue;
                }
                if (item.Name == srcLng)
                {
                    continue;
                }
                destLanguages.Add(item.Name);
            }

            if (destLanguages.Count == 0)
            {
                MessageBox.Show(
                    "The source and the destination languages can not be the same.",
                    "",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

            var translationOptions = new TranslationOptions
            {
                ServiceType          = GetServiceType(),
                MsSubscriptionKey    = txtMsTranslationKey.Text,
                MsSubscriptionRegion = txtMsTranslationRegion.Text
            };

            IsBusy(true);
            new Action <string, string, TranslationOptions, List <string>, string, ResxProgressCallback>(
                TranslateResxFilesAsync).BeginInvoke(
                txtSourceResx.Text,
                srcLng,
                translationOptions,
                destLanguages,
                txtOutputDir.Text,
                ResxWorkingProgress,
                (x) => IsBusy(false),
                null);
        }
Esempio n. 18
0
 public static void SetETSApiVersion(TranslationOptions options)
 {
     try
     {
         options.ApiVersion = APIVersion.v2;
         var systemInfo = ContactETSServer(ETSGet, options, "system/info");
         if (systemInfo == null)
         {
             options.ApiVersion = APIVersion.v1;
         }
     }
     catch (Exception e)
     {
         Log.Logger.Error($"{Constants.ETSApiVersion}: {e.Message}\n {e.StackTrace}");
         options.ApiVersion = APIVersion.v1;
     }
 }
        public ConversionResult Convert(
            ShaderType shaderType, TargetVersion targetVersion, TranslationOptions options,
            string hlslCode, string entryPoint,
            Dictionary<AttributeSemantic, string> overrideAttributeNames = null)
        {
            var compilerPtr = NativeMethods.Hlsl2Glsl_ConstructCompiler(shaderType);

            if (compilerPtr == IntPtr.Zero)
                throw new Exception("Failed to construct Hlsl2Glsl compiler");

            try
            {
                var callbacks = new NativeMethods.Hlsl2Glsl_ParseCallbacks
                {
                    IncludeOpenCallback = null,
                    Data = IntPtr.Zero
                };
                var parseResult = NativeMethods.Hlsl2Glsl_Parse(compilerPtr, hlslCode, targetVersion, ref callbacks, options);
                if (parseResult != 1)
                    throw GetInfoLogAndCreateException(compilerPtr, "Failed to parse HLSL code");

                if (overrideAttributeNames != null)
                {
                    var setUserAttrNamesResult = NativeMethods.Hlsl2Glsl_SetUserAttributeNames(compilerPtr,
                        overrideAttributeNames.Keys.ToArray(), overrideAttributeNames.Values.ToArray(),
                        overrideAttributeNames.Count);
                    if (setUserAttrNamesResult != 1)
                        throw GetInfoLogAndCreateException(compilerPtr, "Failed to user attribute names");
                }

                var translateResult = NativeMethods.Hlsl2Glsl_Translate(compilerPtr, entryPoint, targetVersion, options);
                if (translateResult != 1)
                    throw GetInfoLogAndCreateException(compilerPtr, "Failed to translate HLSL code");

                var glsl = NativeMethods.Hlsl2Glsl_GetShader(compilerPtr);

                var uniforms = NativeMethods.Hlsl2Glsl_GetUniformInfo(compilerPtr);

                return new ConversionResult(glsl, uniforms);
            }
            finally
            {
                NativeMethods.Hlsl2Glsl_DestructCompiler(compilerPtr);
            }
        }
 public static void SetMtEdgeApiVersion(TranslationOptions options)
 {
     try
     {
         options.ApiVersion = APIVersion.v2;
         var systemInfo = ContactMtEdgeServer(MtEdgeGet, options, "system/info");
         if (systemInfo == null)
         {
             options.ApiVersion = APIVersion.v1;
         }
     }
     catch (Exception e)
     {
         _logger.Error($"{Constants.MTEdgeApiVersion}: {e.Message}\n {e.StackTrace}");
         options.ApiVersion = APIVersion.v1;
         throw;
     }
 }
Esempio n. 21
0
        /// <summary>
        /// Decode the binary Maxwell shader code to a translator context.
        /// </summary>
        /// <remarks>
        /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
        /// </remarks>
        /// <param name="channel">GPU channel</param>
        /// <param name="gas">GPU accessor state</param>
        /// <param name="counts">Cumulative shader resource counts</param>
        /// <param name="flags">Flags that controls shader translation</param>
        /// <param name="stage">Shader stage</param>
        /// <param name="gpuVa">GPU virtual address of the shader code</param>
        /// <returns>The generated translator context</returns>
        private TranslatorContext DecodeGraphicsShader(
            GpuChannel channel,
            GpuAccessorState gas,
            TranslationCounts counts,
            TranslationFlags flags,
            ShaderStage stage,
            ulong gpuVa)
        {
            if (gpuVa == 0)
            {
                return(null);
            }

            GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gas, (int)stage - 1);

            var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags);

            return(Translator.CreateContext(gpuVa, gpuAccessor, options, counts));
        }
    void SetTranslationComponent(TranslationOptions options)
    {
        switch (options)
        {
        case TranslationOptions.Text:
            ChangeTranslatableNames <Text, ChangeLanguageVersion>();
            break;

        //case TranslationOptions.VRTKTooltip:
        //    ChangeTranslatableNames<VRTK_ObjectTooltip, VRTKTooltipTranslator>();
        //    break;
        //case TranslationOptions.TMProText:
        //    ChangeTranslatableNames<Text, ChangeLanguageVersion>();
        //    break;
        default:
            Debug.LogError("Unrecognized Option");
            break;
        }
    }
Esempio n. 23
0
        /// <summary>
        /// Get an array of Language Pairs accessible by the credentials provided.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ETSLanguagePair[] GetLanguagePairs(TranslationOptions options)
        {
            Log.logger.Trace("");
            lock (languageLock)
            {
                if (LanguagePairsOnServer == null || !LanguagePairsOnServer.Any())
                {
                    try
                    {
                        // Ideally no exception should be thrown from ContactETSServer, but in rare cases
                        // it could successfully authenticate using username/password, but then reject the token. If
                        // that happens, open a message with the related error
                        string            jsonResult    = ContactETSServer(ETSGet, options, "language-pairs");
                        ETSLanguagePair[] languagePairs =
                            JsonConvert.DeserializeObject <LanguagePairResult>(jsonResult).LanguagePairs;
                        LanguagePairsOnServer = (languagePairs != null ? languagePairs : new ETSLanguagePair[0]);

                        // In 60 seconds, wipe the LPs so we query again. That way, if someone makes a change, we'll
                        // pick it up eventually.
                        Task.Factory.StartNew(() =>
                        {
                            System.Threading.Thread.Sleep(60000);
                            lock (languageLock)
                            {
                                ExpireLanguagePairs();
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        Log.logger.Error(e, "Unable to get the language pairs:");

                        if (Environment.UserInteractive)
                        {
                            MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        LanguagePairsOnServer = new ETSLanguagePair[0];
                    }
                }
            }
            return(LanguagePairsOnServer);
        }
Esempio n. 24
0
        /// <summary>
        /// Decode the binary Maxwell shader code to a translator context.
        /// </summary>
        /// <param name="channel">GPU channel</param>
        /// <param name="gas">GPU accessor state</param>
        /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
        /// <param name="localSizeX">Local group size X of the computer shader</param>
        /// <param name="localSizeY">Local group size Y of the computer shader</param>
        /// <param name="localSizeZ">Local group size Z of the computer shader</param>
        /// <param name="localMemorySize">Local memory size of the compute shader</param>
        /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
        /// <returns>The generated translator context</returns>
        private TranslatorContext DecodeComputeShader(
            GpuChannel channel,
            GpuAccessorState gas,
            ulong gpuVa,
            int localSizeX,
            int localSizeY,
            int localSizeZ,
            int localMemorySize,
            int sharedMemorySize)
        {
            if (gpuVa == 0)
            {
                return(null);
            }

            GpuAccessor gpuAccessor = new GpuAccessor(_context, channel, gas, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);

            var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, DefaultFlags | TranslationFlags.Compute);

            return(Translator.CreateContext(gpuVa, gpuAccessor, options));
        }
        /// <summary>
        /// Get dictionaries from the MT Edge server
        /// </summary>
        /// <param name="tradosToMtEdgeLP"></param>
        /// <param name="options"></param>
        public static void GetDictionaries(TradosToMTEdgeLP tradosToMtEdgeLP, TranslationOptions options)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            foreach (var item in tradosToMtEdgeLP.MtEdgeLPs)
            {
                queryString["sourceLanguageId"] = item.SourceLanguageId;
                queryString["targetLanguageId"] = item.TargetLanguageId;
                queryString["perPage"]          = "1000";        // set to 1000 to avoid the missing dictionaries
                var jsonResult = ContactMtEdgeServer(MtEdgeGet, options, "dictionaries", queryString);

                var result = JsonConvert.DeserializeObject <DictionaryInfo>(jsonResult);
                tradosToMtEdgeLP.Dictionaries = new List <DictionaryModel>(result.Dictionaries);
                tradosToMtEdgeLP.Dictionaries.Insert(0, new DictionaryModel
                {
                    DictionaryId     = Constants.NoDictionary,
                    SourceLanguageId = string.Empty,
                    TargetLanguageId = string.Empty
                });
            }
        }
        /// <summary>
        /// Get an array of Language Pairs accessible by the credentials provided.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static SDLMTEdgeLanguagePair[] GetLanguagePairs(TranslationOptions options)
        {
            lock (languageLock)
            {
                if (_languagePairsOnServer == null || !_languagePairsOnServer.Any())
                {
                    try
                    {
                        // Ideally no exception should be thrown from ContactMtEdgeServer, but in rare cases
                        // it could successfully authenticate using username/password, but then reject the token. If
                        // that happens, open a message with the related error
                        var jsonResult    = ContactMtEdgeServer(MtEdgeGet, options, "language-pairs");
                        var languagePairs = JsonConvert.DeserializeObject <LanguagePairResult>(jsonResult).LanguagePairs;
                        _languagePairsOnServer = (languagePairs != null ? languagePairs : new SDLMTEdgeLanguagePair[0]);

                        // In 60 seconds, wipe the LPs so we query again. That way, if someone makes a change, we'll
                        // pick it up eventually.
                        Task.Factory.StartNew(() =>
                        {
                            System.Threading.Thread.Sleep(60000);
                            lock (languageLock)
                            {
                                ExpireLanguagePairs();
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        _logger.Error($"{Constants.LanguagePairs}: {Constants.InaccessibleLangPairs}:  {e.Message}\n {e.StackTrace}");

                        if (Environment.UserInteractive)
                        {
                            MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        _languagePairsOnServer = new SDLMTEdgeLanguagePair[0];
                    }
                }
            }
            return(_languagePairsOnServer);
        }
Esempio n. 27
0
        static void HandleArguments(Options options)
        {
            TranslationFlags flags = TranslationFlags.DebugMode;

            if (options.Compute)
            {
                flags |= TranslationFlags.Compute;
            }

            byte[] data = File.ReadAllBytes(options.InputPath);

            TranslationOptions translationOptions = new TranslationOptions(options.TargetLanguage, options.TargetApi, flags);

            ShaderProgram program = Translator.CreateContext(0, new GpuAccessor(data), translationOptions).Translate(out _);

            if (options.OutputPath == null)
            {
                if (program.BinaryCode != null)
                {
                    using Stream outputStream = Console.OpenStandardOutput();

                    outputStream.Write(program.BinaryCode);
                }
                else
                {
                    Console.WriteLine(program.Code);
                }
            }
            else
            {
                if (program.BinaryCode != null)
                {
                    File.WriteAllBytes(options.OutputPath, program.BinaryCode);
                }
                else
                {
                    File.WriteAllText(options.OutputPath, program.Code);
                }
            }
        }
        private static string Translate(Dictionary <string, string> parameters, TranslationOptions options, string path)
        {
            var content = GetStringContentFromParameters(parameters);

            ServicePointManager.Expect100Continue      = true;
            ServicePointManager.DefaultConnectionLimit = 9999;
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = options.UseBasicAuthentication
                                        ? new AuthenticationHeaderValue("Bearer", options.ApiToken)
                                        : new AuthenticationHeaderValue("Basic", (options.ApiToken + ":").Base64Encode());

                var builder = new UriBuilder(options.Uri)
                {
                    Path   = $"/api/{options.ApiVersionString}/{path}",
                    Scheme = options.RequiresSecureProtocol ? Uri.UriSchemeHttps : Uri.UriSchemeHttp
                };
                var response = httpClient.PostAsync(builder.Uri, content).Result;
                var result   = response.Content.ReadAsStringAsync().Result;

                return(result);
            }
        }
Esempio n. 29
0
        /// <summary>
        /// Decode the binary Maxwell shader code to a translator context.
        /// </summary>
        /// <remarks>
        /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
        /// </remarks>
        /// <param name="state">Current GPU state</param>
        /// <param name="counts">Cumulative shader resource counts</param>
        /// <param name="flags">Flags that controls shader translation</param>
        /// <param name="stage">Shader stage</param>
        /// <param name="gpuVa">GPU virtual address of the shader code</param>
        /// <returns>The generated translator context</returns>
        private TranslatorContext DecodeGraphicsShader(
            GpuState state,
            TranslationCounts counts,
            TranslationFlags flags,
            ShaderStage stage,
            ulong gpuVa)
        {
            if (gpuVa == 0)
            {
                return(null);
            }

            GpuAccessorState gas = new GpuAccessorState(
                state.Get <PoolState>(MethodOffset.TexturePoolState).Address.Pack(),
                state.Get <PoolState>(MethodOffset.TexturePoolState).MaximumId,
                state.Get <int>(MethodOffset.TextureBufferIndex),
                state.Get <Boolean32>(MethodOffset.EarlyZForce));

            GpuAccessor gpuAccessor = new GpuAccessor(_context, state.Channel, gas, (int)stage - 1);

            var options = new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags);

            return(Translator.CreateContext(gpuVa, gpuAccessor, options, counts));
        }
        /// <summary>
        /// Using the username and password passed in via credentials, obtain the authentication token that will be
        /// later used to validate API calls.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="credentials"></param>
        /// <param name="useHTTP"></param>
        /// <returns></returns>
        public static string GetAuthToken(
            TranslationOptions options,
            GenericCredentials credentials,
            bool useHTTP = false)
        {
            _logger.Trace("");

            lock (optionsLock)
            {
                if (options.ApiVersion == APIVersion.Unknown)
                {
                    SetMtEdgeApiVersion(options);
                }
            }

            ServicePointManager.Expect100Continue      = true;
            ServicePointManager.DefaultConnectionLimit = 9999;
            ServicePointManager.SecurityProtocol       = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

            using (var httpClient = new HttpClient())
            {
                // Build the URI for querying the token
                var builder = new UriBuilder(options.Uri);
                builder.Path = string.Format("/api/{0}/auth", options.ApiVersionString);

                // Pass in the username and password as parameters to retrieve the auth token
                var queryString = HttpUtility.ParseQueryString(string.Empty);
                queryString["username"] = credentials.UserName;
                queryString["password"] = credentials.Password;
                builder.Query           = queryString.ToString();
                builder.Scheme          = useHTTP ? Uri.UriSchemeHttp : Uri.UriSchemeHttps;

                // Users may be hosting the service locally and therefore not sign their certificates. If so,
                // we'll want to accept all certificates. Otherwise, this would throw an exception.
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

                try
                {
                    var httpResponse = httpClient.PostAsync(builder.Uri, null).Result;
                    if (httpResponse.Content != null && httpResponse.StatusCode == HttpStatusCode.OK)
                    {
                        return(httpResponse.Content.ReadAsStringAsync().Result);
                    }
                    else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized || httpResponse.StatusCode == HttpStatusCode.BadRequest)
                    {
                        throw new UnauthorizedAccessException("The credentials provided are not authorized.");
                    }
                    throw new Exception("No response from the URI provided");
                }
                catch (Exception e)
                {
                    while (e.InnerException != null)
                    {
                        e = e.InnerException;
                    }

                    if (!useHTTP && e.HResult == (int)ErrorHResult.HandshakeFailure)
                    {
                        return(GetAuthToken(options, credentials, true));
                    }
                    _logger.Error($"{Constants.AuthToken}: {e.Message}\n {e.StackTrace}");
                    throw TranslateAggregateException(e);
                }
            }
        }
Esempio n. 31
0
 public static extern int Hlsl2Glsl_Parse(
     IntPtr handle,
     [MarshalAs(UnmanagedType.LPStr)] string shaderString,
     TargetVersion targetVersion,
     ref Hlsl2Glsl_ParseCallbacks callbacks,
     TranslationOptions options);
Esempio n. 32
0
 public static extern int Hlsl2Glsl_Translate(
     IntPtr handle, 
     [MarshalAs(UnmanagedType.LPStr)] string entry, 
     TargetVersion targetVersion, 
     TranslationOptions options);
        /// <summary>
        /// Queries the MTEdge server specified in options.
        /// </summary>
        /// <param name="mtEdgeHttpMethod">MtEdgePost or MtEdgeSGet</param>
        /// <param name="options">The translation options</param>
        /// <param name="path">The path after /api/vX/ for MTEdge api queries</param>
        /// <param name="parameters">data to include in the query</param>
        /// <param name="useHTTP">Whether to use HTTP over HTTPS</param>
        /// <param name="timesToRetry">number of times to retry the query</param>
        /// <returns>The string result of the request.</returns>
        private static string ContactMtEdgeServer(Func <Uri, HttpClient, HttpResponseMessage> mtEdgeHttpMethod,
                                                  TranslationOptions options,
                                                  string path,
                                                  NameValueCollection parameters = null,
                                                  bool useHTTP = false)
        {
            _logger.Trace("");

            lock (optionsLock)
            {
                if (options.ApiVersion == APIVersion.Unknown)
                {
                    SetMtEdgeApiVersion(options);
                }
            }

            ServicePointManager.Expect100Continue      = true;
            ServicePointManager.DefaultConnectionLimit = 9999;
            ServicePointManager.SecurityProtocol       = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = options.UseBasicAuthentication
                                        ? new AuthenticationHeaderValue("Bearer", options.ApiToken)
                                        : new AuthenticationHeaderValue("Basic", (options.ApiToken + ":").Base64Encode());

                var builder = new UriBuilder(options.Uri)
                {
                    Path   = $"/api/{options.ApiVersionString}/{path}",
                    Scheme = useHTTP ? Uri.UriSchemeHttp : Uri.UriSchemeHttps
                };

                if (parameters != null)
                {
                    builder.Query = parameters.ToString();
                }
                HttpResponseMessage httpResponse;

                try
                {
                    httpResponse = mtEdgeHttpMethod(builder.Uri, httpClient);
                }
                catch (Exception e)
                {
                    while (e.InnerException != null)
                    {
                        e = e.InnerException;
                    }

                    _logger.Error($"{Constants.MTEdgeServerContact}:\n {Constants.MtEdgeServerContactExResult} {e.HResult}\n {e.Message}\n {e.StackTrace}");
                    throw TranslateAggregateException(e);
                }

                if (httpResponse.Content != null && httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    return(httpResponse.Content.ReadAsStringAsync().Result);
                }
                switch (httpResponse.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    _logger.Error($"{Constants.MTEdgeServerContact}: {Constants.InvalidCredentials}");
                    throw new UnauthorizedAccessException("The credentials provided are not authorized.");

                case HttpStatusCode.BadRequest:
                    _logger.Error($"{Constants.MTEdgeServerContact}: {Constants.BadRequest} {0}", httpResponse.Content.ReadAsStringAsync().Result);
                    throw new Exception($"There was a problem with the request: { httpResponse.Content.ReadAsStringAsync().Result }");

                default:
                    _logger.Error($"{Constants.MTEdgeServerContact}: {(int)httpResponse.StatusCode} {Constants.StatusCode}");

                    return(null);
                }
            }
        }