Esempio n. 1
0
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref ScenarioData asset, IAssetOrResourceLoadedContext context)
        {
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                outputFile: defaultTranslationFile,
                inputStreams: streams,
                allowTranslationOverride: false,
                closeStreams: true);

            foreach (var param in asset.list)
            {
                if (param.Command == Command.Text)
                {
                    for (int i = 0; i < param.Args.Length; i++)
                    {
                        var key = param.Args[i];
                        if (!string.IsNullOrEmpty(key))
                        {
                            if (cache.TryGetTranslation(key, true, out var translated))
                            {
                                param.Args[i] = translated;
                            }
                            else if (AutoTranslatorSettings.IsDumpingRedirectedResourcesEnabled && LanguageHelper.IsTranslatable(key))
                            {
                                cache.AddTranslationToCache(key, key);
                            }
                        }
                    }
                }
            }

            return(true);
        }
        protected override bool DumpAsset(string calculatedModificationPath, TextAsset asset, IAssetOrResourceLoadedContext context)
        {
            if (!textAssetHelper.IsTable(asset))
            {
                return(false);
            }

            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                outputFile: defaultTranslationFile,
                inputStreams: streams,
                allowTranslationOverride: false,
                closeStreams: true);

            bool DumpCell(string cellText)
            {
                if (!string.IsNullOrEmpty(cellText) && LanguageHelper.IsTranslatable(cellText))
                {
                    cache.AddTranslationToCache(cellText, cellText);
                    return(true);
                }
                return(false);
            }

            return(textAssetHelper.ActOnCells(asset, DumpCell, out TextAssetHelper.TableResult tableResult));
        }
        public override TextAndEncoding TranslateTextAsset(string calculatedModificationPath, TextAsset asset,
                                                           IAssetOrResourceLoadedContext context)
        {
            Logger.DebugLogDebug($"{GetType()} attempt to handle {calculatedModificationPath}");
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty)
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath} (no cache)");
                return(null);
            }

            var obj = LoadFromAsset(asset);

            if (obj != null && TranslateObject(ref obj, cache, calculatedModificationPath))
            {
                Logger.DebugLogDebug($"{GetType()} handled {calculatedModificationPath}");
                return(StoreAsset(obj));
            }

            Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath}");
            return(null);
        }
Esempio n. 4
0
        public override TextAndEncoding TranslateTextAsset(string calculatedModificationPath, TextAsset asset,
                                                           IAssetOrResourceLoadedContext context)
        {
            if (TextAssetMessagePackHelper.CanHandleAsset(asset, context, out var handler))
            {
                //return new TextAndEncoding(asset.bytes, null);
                var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
                var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
                var streams = redirectedResources.Select(x => x.OpenStream());
                var cache   = new SimpleTextTranslationCache(
                    defaultTranslationFile,
                    streams,
                    false,
                    true);

                if (cache.IsEmpty)
                {
                    Logger.LogDebug($"{GetType()} unable to handle {calculatedModificationPath} (no cache)");
                    return(null);
                }

                var obj = handler.Load(asset);

                if (obj != null && handler.Translate(ref obj, cache, calculatedModificationPath))
                {
                    Logger.LogDebug($"{GetType()} handled {calculatedModificationPath}");
                    return(handler.Store(obj));
                }
            }

            return(null);
        }
Esempio n. 5
0
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref T asset,
                                                     IAssetOrResourceLoadedContext context)
        {
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty && !DisableEmptyCacheCheck)
            {
                return(false);
            }

            var result = false;

            foreach (var entry in GetParams(asset))
            {
                if (UpdateParam(calculatedModificationPath, cache, entry))
                {
                    result = true;
                }
            }

            return(result);
        }
        /// <summary>
        ///     Caches forward and reverse translation of assets as they're loaded, but does not apply them.
        ///     to avoid breaking code that relies on original names being present.
        /// </summary>
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref MapInfo asset,
                                                     IAssetOrResourceLoadedContext context)
        {
            // updating the MapInfo assets directly breaks places that are doing lookups by mapName
            // instead of id, so we just register this as a place to lookup MapInfo translations and
            // return true so it appears handled
            Hooks.Init();

            // register new translations with helper without replacing
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty)
            {
                return(true);
            }

            var shouldTrack = IsTranslationRegistrationAllowed(calculatedModificationPath);

            // register with helper or dump without translating here
            foreach (var key in asset.param
                     .Select(entry => TextResourceHelper.GetSpecializedKey(entry, GetMapName(entry)))
                     .Where(k => !string.IsNullOrEmpty(k)))
            {
                if (cache.TryGetTranslation(key, true, out var translated))
                {
                    if (string.IsNullOrEmpty(translated))
                    {
                        continue;
                    }
                    _mapLookup[key] = translated;
                    _reverseMapLookup[translated] = key;
                    if (shouldTrack)
                    {
                        TrackReplacement(calculatedModificationPath, key, translated);
                    }
                    TranslationHelper.RegisterRedirectedResourceTextToPath(translated, calculatedModificationPath);
                }
                else if (AutoTranslatorSettings.IsDumpingRedirectedResourcesEnabled &&
                         !string.IsNullOrEmpty(key) && LanguageHelper.IsTranslatable(key))
                {
                    cache.AddTranslationToCache(key, key);
                }
            }

            return(true);
        }
        public override TextAndEncoding TranslateTextAsset(string calculatedModificationPath, TextAsset asset, IAssetOrResourceLoadedContext context)
        {
            if (!textAssetHelper.IsTable(asset))
            {
                return(null);
            }

            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                outputFile: defaultTranslationFile,
                inputStreams: streams,
                allowTranslationOverride: false,
                closeStreams: true);

            return(Translate(cache, ref asset));
        }
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref TextAsset asset, IAssetOrResourceLoadedContext context)
        {
            if (!Enabled || !textAssetHelper.IsTable(asset))
            {
                return(false);
            }

            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                outputFile: defaultTranslationFile,
                inputStreams: streams,
                allowTranslationOverride: false,
                closeStreams: true);

            return(TryRegisterTranslation(cache, ref asset));
        }
        public override TextAndEncoding TranslateTextAsset(string calculatedModificationPath, TextAsset asset,
                                                           IAssetOrResourceLoadedContext context)
        {
            Logger.DebugLogDebug($"{GetType()} attempt to handle {calculatedModificationPath}");
            if (!Enabled || asset.bytes == null)
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath}");
                return(null);
            }

            byte[] bytes       = null;
            var    defaultFile = Path.Combine(calculatedModificationPath, "translation.bytes");

            if (File.Exists(defaultFile))
            {
                bytes = File.ReadAllBytes(defaultFile);
            }

            if (bytes == null || bytes.Length == 0)
            {
                foreach (var entry in RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".bytes"))
                {
                    using (var stream = entry.OpenStream())
                    {
                        bytes = ReadBytes(stream);
                    }

                    if (bytes.Length > 0)
                    {
                        break;
                    }
                }
            }

            if (bytes == null || bytes.Length == 0)
            {
                Logger.DebugLogDebug($"{GetType()}  unable to handle {calculatedModificationPath}: no .bytes files");
                return(null);
            }

            Logger.DebugLogDebug($"{GetType()} handled {calculatedModificationPath}");
            return(new TextAndEncoding(bytes, _textResourceHelper.TableHelper.TextAssetEncoding));
        }
Esempio n. 10
0
        public static IEnumerable <Stream> GetRedirectionStreams(string calculatedModificationPath, Object asset,
                                                                 IAssetOrResourceLoadedContext context, bool useMapping = false)
        {
            var handled = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var entry in RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt"))
            {
                if (handled.Contains(entry.FullName))
                {
                    continue;
                }
                yield return(entry.OpenStream());

                handled.Add(entry.FullName);
            }

            if (!useMapping)
            {
                yield break;
            }

            foreach (var mappedPath in GetMappingForPath(calculatedModificationPath, asset, context))
            {
                foreach (var entry in RedirectedDirectory.GetFilesInDirectory(mappedPath.CalculatedModificationPath,
                                                                              ".txt"))
                {
                    TextResourceRedirector.Logger.LogDebug(
                        $"{nameof(GetRedirectionStreams)}: {calculatedModificationPath}: adding fallback: {entry.FullName}");
                    if (handled.Contains(entry.FullName))
                    {
                        continue;
                    }
                    yield return(entry.OpenStream());

                    handled.Add(entry.FullName);
                }
            }
        }
Esempio n. 11
0
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref ScenarioData asset,
                                                     IAssetOrResourceLoadedContext context)
        {
            Logger.DebugLogDebug($"{GetType()} attempt to handle {calculatedModificationPath}");
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty)
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath} (no cache)");
                return(false);
            }

            var result = false;

            foreach (var param in asset.list)
            {
                if (!TextResourceHelper.IsSupportedCommand(param.Command))
                {
                    Logger.DebugLogDebug($"{GetType()} skipping unsupported command: {param.Command}");
                    continue;
                }

                switch (param.Command)
                {
                case Command.Text:

                    // Text: 0 - jp speaker (if present), 1 - text
                    for (var i = 0; i < param.Args.Length && i < 2; i++)
                    {
                        var key = param.Args[i];
                        if (key.IsNullOrEmpty() || TextResourceHelper.TextKeysBlacklist.Contains(key) ||
                            StringIsSingleReplacement(key))
                        {
                            continue;
                        }
                        if (TryRegisterTranslation(cache, param, i, calculatedModificationPath))
                        {
                            result = true;
                        }
                    }

                    break;

                case Command.Calc:
                {
                    if (param.Args.Length >= 3 && TextResourceHelper.CalcKeys.Contains(param.Args[0]))
                    {
                        if (TryRegisterTranslation(cache, param, 2, calculatedModificationPath))
                        {
                            result = true;
                        }
                    }

                    break;
                }

                case Command.Format:
                {
                    if (param.Args.Length >= 2 && TextResourceHelper.FormatKeys.Contains(param.Args[0]))
                    {
                        if (TryRegisterTranslation(cache, param, 1, calculatedModificationPath))
                        {
                            result = true;
                        }
                    }

                    break;
                }

                case Command.Choice:
                {
                    for (var i = 0; i < param.Args.Length; i++)
                    {
                        if (TryRegisterTranslation(cache, param, i, calculatedModificationPath))
                        {
                            result = true;
                        }
                    }

                    break;
                }

#if false
                case ADV.Command.Switch:
                    // TODO
                    break;

#if AI
                case ADV.Command.InfoText:
                    // TODO
                    break;
#endif
                case ADV.Command.Jump:
                    // TODO
                    break;
#endif
                default:
                {
                    Logger.LogWarning(
                        $"{GetType()} expected to handle {param.Command}, but support not implemented");

                    break;
                }
                }
            }

            Logger.DebugLogDebug(result
                ? $"{GetType()} handled {calculatedModificationPath}"
                : $"{GetType()} unable to handle {calculatedModificationPath}");

            return(result);
        }
        /// <inheritdoc />
        /// <remarks>Always returns <c>true</c> to signal nothing else should handle these.</remarks>
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref NickName asset,
                                                     IAssetOrResourceLoadedContext context)
        {
            // updating the NickName assets directly causes issues, but after SaveData.LoadNickNameParam() they
            // are safe to manipulate
            InitHooks();

            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty)
            {
                return(true);
            }

            var replacementKey = calculatedModificationPath
                                 .Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                                 .LastOrDefault();

            // don't touch "player" entry
            if (string.IsNullOrEmpty(replacementKey) ||
                replacementKey.Equals("player", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            if (!_replacements.TryGetValue(replacementKey, out var replacements))
            {
                _replacements[replacementKey] = replacements = new Dictionary <string, string>();
            }

            foreach (var entry in asset.param)
            {
                if (!entry.isSpecial)
                {
                    continue;
                }
                var key = TextResourceHelper.GetSpecializedKey(entry, entry.Name);
                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }
                if (cache.TryGetTranslation(key, true, out var translated))
                {
                    replacements[key] = translated;
                    TranslationHelper.RegisterRedirectedResourceTextToPath(translated, calculatedModificationPath);
                }
                else if (AutoTranslatorSettings.IsDumpingRedirectedResourcesEnabled &&
                         LanguageHelper.IsTranslatable(key))
                {
                    cache.AddTranslationToCache(key, entry.Name);
                }
            }

            return(true);
        }
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref ExcelData asset,
                                                     IAssetOrResourceLoadedContext context)
        {
            Logger.DebugLogDebug($"{GetType()} attempt to handle {calculatedModificationPath}");
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            var result = false;

            if (cache.IsEmpty)
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath} (no cache)");
                return(false);
            }

            var columnsToTranslate =
                new HashSet <int>(TextResourceHelper.GetSupportedExcelColumns(calculatedModificationPath, asset));

            var filter = columnsToTranslate.Count > 0;

            var row = -1;

            var shouldTrack = IsTranslationRegistrationAllowed(calculatedModificationPath);

            foreach (var param in asset.list)
            {
                row++;
                if (param.list == null || param.list.Count < 1 || param.list[0] == "no")
                {
                    continue;
                }

                for (var i = 0; i < param.list.Count; i++)
                {
                    if (filter && !columnsToTranslate.Contains(i))
                    {
                        continue;
                    }

                    foreach (var key in TextResourceHelper.GetExcelRowTranslationKeys(asset.name, param.list, i))
                    {
                        if (string.IsNullOrEmpty(key))
                        {
                            continue;
                        }
                        Logger.DebugLogDebug(
                            $"Attempting excel replacement [{row}, {i}]: Searching for replacement key={key}");
                        if (cache.TryGetTranslation(key, true, out var translated))
                        {
                            result     = true;
                            translated = TextResourceHelper.PrepareTranslationForReplacement(asset, translated);
                            if (shouldTrack)
                            {
                                TrackReplacement(calculatedModificationPath, key, translated);
                            }
                            TranslationHelper.RegisterRedirectedResourceTextToPath(translated,
                                                                                   calculatedModificationPath);
                            Logger.DebugLogDebug(
                                $"Replacing [{row}, {i}]: key={key}: {param.list[i]} => {translated}");

                            param.list[i] = translated;
                            break;
                        }

                        if (!LanguageHelper.IsTranslatable(key))
                        {
                            continue;
                        }

                        TranslationHelper.RegisterRedirectedResourceTextToPath(key, calculatedModificationPath);
                        if (AutoTranslatorSettings.IsDumpingRedirectedResourcesEnabled)
                        {
                            cache.AddTranslationToCache(key, key);
                        }
                    }
                }
            }

            Logger.DebugLogDebug(result
                ? $"{GetType()} handled {calculatedModificationPath}"
                : $"{GetType()} unable to handle {calculatedModificationPath}");
            return(result);
        }
Esempio n. 14
0
 private static IEnumerable <Stream> GetRedirectionStreamsForPath(string path)
 {
     return(RedirectedDirectory.GetFilesInDirectory(path, ".txt").Select(x => x.OpenStream()));
 }
        protected override bool ReplaceOrUpdateAsset(string calculatedModificationPath, ref ScenarioData asset, IAssetOrResourceLoadedContext context)
        {
            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                outputFile: defaultTranslationFile,
                inputStreams: streams,
                allowTranslationOverride: false,
                closeStreams: true);

            foreach (var param in asset.list)
            {
                if (!textResourceHelper.IsSupportedCommand(param.Command))
                {
                    continue;
                }

                if (param.Command == Command.Text)
                {
                    for (int i = 0; i < param.Args.Length; i++)
                    {
                        var key = param.Args[i];
                        if (!key.IsNullOrEmpty() && !textResourceHelper.TextKeysBlacklist.Contains(key))
                        {
                            TryRegisterTranslation(cache, param, i);
                        }
                    }
                }
                else if (param.Command == Command.Calc)
                {
                    if (param.Args.Length >= 3 && textResourceHelper.CalcKeys.Contains(param.Args[0]))
                    {
                        TryRegisterTranslation(cache, param, 2);
                    }
                }
                else if (param.Command == Command.Format)
                {
                    if (param.Args.Length >= 2 && textResourceHelper.FormatKeys.Contains(param.Args[0]))
                    {
                        TryRegisterTranslation(cache, param, 1);
                    }
                }
                else if (param.Command == Command.Choice)
                {
                    for (int i = 0; i < param.Args.Length; i++)
                    {
                        TryRegisterTranslation(cache, param, i);
                    }
                }
#if false
                else if (param.Command == ADV.Command.Switch)
                {
                    // TODO
                }
#endif
#if false
                else if (param.Command == ADV.Command.InfoText)
                {
                    // TODO
                }
#endif
#if false
                else if (param.Command == ADV.Command.Jump)
                {
                    // TODO
                }
#endif
            }

            return(true);
        }
Esempio n. 16
0
        public override TextAndEncoding TranslateTextAsset(string calculatedModificationPath, TextAsset asset,
                                                           IAssetOrResourceLoadedContext context)
        {
            Logger.DebugLogDebug($"{GetType()} attempt to handle {calculatedModificationPath}");
            if (!Enabled || !TextAssetTableHelper.IsTable(asset))
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath}");
                return(null);
            }

            var defaultTranslationFile = Path.Combine(calculatedModificationPath, "translation.txt");
            var redirectedResources    = RedirectedDirectory.GetFilesInDirectory(calculatedModificationPath, ".txt");
            var streams = redirectedResources.Select(x => x.OpenStream());
            var cache   = new SimpleTextTranslationCache(
                defaultTranslationFile,
                streams,
                false,
                true);

            if (cache.IsEmpty)
            {
                Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath} (no cache)");
                return(null);
            }

            GetTableRules(calculatedModificationPath, asset, context, out var rowAllowed, out var colAllowed);


            bool DoTranslation(int rowIndex, int colIndex, string cellText, out string newCellText)
            {
                newCellText = null;
                if (rowAllowed != null && !rowAllowed(rowIndex) || colAllowed != null && !colAllowed(colIndex))
                {
                    return(false);
                }

                if (cache.TryGetTranslation(cellText, false, out newCellText))
                {
                    TranslationHelper.RegisterRedirectedResourceTextToPath(newCellText, calculatedModificationPath);
                    return(true);
                }


                if (string.IsNullOrEmpty(cellText) || !LanguageHelper.IsTranslatable(cellText))
                {
                    return(false);
                }

                TranslationHelper.RegisterRedirectedResourceTextToPath(cellText, calculatedModificationPath);
                if (AutoTranslatorSettings.IsDumpingRedirectedResourcesEnabled)
                {
                    cache.AddTranslationToCache(cellText, cellText);
                }

                return(false);
            }

            if (TextAssetTableHelper.TryTranslateTextAsset(ref asset, DoTranslation, out var result))
            {
                Logger.DebugLogDebug($"{GetType()} handled {calculatedModificationPath}");
                return(new TextAndEncoding(result, TextAssetTableHelper.TextAssetEncoding));
            }

            Logger.DebugLogDebug($"{GetType()} unable to handle {calculatedModificationPath}");
            return(null);
        }