Exemplo n.º 1
0
        internal void Reset()
        {
            ResetCache(_cardCache);
            ResetCache(_fullNameCache, TranslationHelper.StringCacheInitializer);
            ResetCache(_nickNameCache);

            if (TranslationHelper.IsShuttingDown)
            {
                return;
            }

            _nicknamesSupported = GeBoAPI.Instance != null && GeBoAPI.Instance.ChaFileNameToIndex("nickname") >= 0;

            var presetCount = 0;

            _hasEntries = false;

            _reverseNames = TranslationHelper.ShowGivenNameFirst;


            // don't load presets if we're not going to use them
            if (TranslationHelper.Instance == null || !TranslationHelper.Instance.CurrentCardLoadTranslationEnabled)
            {
                return;
            }

            var nameCacheDone = new Dictionary <CharacterSex, HashSet <string> >(
                EnumEqualityComparer <CharacterSex> .Comparer);
            var aliases = TranslationHelper.StringCacheInitializer();
            var start   = Time.realtimeSinceStartup;

            foreach (var preset in NamePresets.Load())
            {
                presetCount++;
                var translation = preset.GetTranslation(AutoTranslatorSettings.DestinationLanguage);
                if (translation == null)
                {
                    continue;
                }

                var translatedName = BuildFullName(translation.FamilyName, translation.GivenName);

                ResetCache(nameCacheDone);
                var familyNames = preset.FamilyNames.Count > 0
                    ? preset.FamilyNames
                    : new List <string>(new[] { string.Empty });
                var givenNames = preset.GivenNames.Count > 0
                    ? preset.GivenNames
                    : new List <string>(new[] { string.Empty });

                foreach (var family in familyNames)
                {
                    foreach (var given in givenNames)
                    {
                        if (string.IsNullOrEmpty(family) && string.IsNullOrEmpty(given))
                        {
                            continue;
                        }
                        var origName = BuildFullName(family, given);
                        if (nameCacheDone[preset.Sex].Contains(origName))
                        {
                            continue;
                        }

                        aliases.Clear();

                        nameCacheDone[preset.Sex].Add(origName);
                        _fullNameCache[preset.Sex][origName] = translatedName;
                        TranslationHelper.Instance.AddTranslatedNameToCache(origName, translatedName);

                        var key = new CardNameCacheKey(family, given);
                        _cardCache[preset.Sex][key] = new CardNameCacheValue(
                            translation.FamilyName,
                            translation.GivenName);

                        if (!_nicknamesSupported)
                        {
                            continue;
                        }
                        if (!_nickNameCache[preset.Sex].TryGetValue(key, out var nickCache))
                        {
                            _nickNameCache[preset.Sex][key] = nickCache = TranslationHelper.StringCacheInitializer();
                        }


                        // add alternate given name spellings as possible nicknames as well as being suffixed
                        foreach (var alias in preset.GivenNames)
                        {
                            aliases[alias] = translation.GivenName;
                        }

                        // add family name variants so they can be suffixed
                        foreach (var alias in preset.FamilyNames)
                        {
                            aliases[alias] = translation.FamilyName;
                        }

                        // remove current given name
                        aliases.Remove(given);
                        foreach (var rawFamilyName in preset.FamilyNames)
                        {
                            aliases.Remove(rawFamilyName);
                        }


                        foreach (var nick in preset.NickNames)
                        {
                            nickCache[nick] = translation.NickName;
                        }

                        AddSuffixedNicknames(nickCache);

                        foreach (var alias in aliases.Where(alias => !nickCache.ContainsKey(alias.Key)))
                        {
                            nickCache[alias.Key] = alias.Value;
                        }
                    }
                }

                _hasEntries |= nameCacheDone.Any(k => k.Value.Count > 0);
            }

            Logger.LogDebug($"Loaded {presetCount} preset(s) in {Time.realtimeSinceStartup - start} second(s)");
        }
Exemplo n.º 2
0
        // ReSharper disable Unity.PerformanceAnalysis
        public bool TryTranslateCardNames(ChaFile chaFile, out Dictionary <string, string> result)
        {
            var origName = chaFile.GetFullName();

            if (!_hasEntries)
            {
                result = null;
                return(false);
            }

            void PopulateResult(IDictionary <string, string> output, string nameType, string nameValue)
            {
                if (nameValue != null)
                {
                    output[nameType] = nameValue;
                }
            }

            result = null;
            var key = new CardNameCacheKey(chaFile);
            var sex = chaFile.GetSex();

            if (!_cardCache[sex].TryGetValue(key, out var match))
            {
                return(false);
            }


            result = new Dictionary <string, string>(GeBoAPI.Instance.ChaFileNameCount);
            PopulateResult(result, "fullname", match.FullName);
            PopulateResult(result, "firstname", match.GivenName);
            PopulateResult(result, "lastname", match.FamilyName);

            var fullName = BuildFullName(match);

            if (!StringUtils.ContainsJapaneseChar(fullName))
            {
                CardNameTranslationManager.CacheRecentTranslation(new NameScope(sex), origName, fullName);
                var fullPath = chaFile.GetFullPath();
                if (!string.IsNullOrEmpty(fullPath))
                {
                    CharaFileInfoTranslationManager.CacheRecentTranslation(
                        new NameScope(chaFile.GetSex()), fullPath, fullName);
                }
            }


            var origNick = chaFile.GetName("nickname");

            if (string.IsNullOrEmpty(origNick))
            {
                return(true);
            }

            if (_nickNameCache[sex].TryGetValue(key, out var nickLookup) &&
                nickLookup.TryGetValue(origNick, out var translatedNick))
            {
                PopulateResult(result, "nickname", translatedNick);
            }
            else if (key.GivenName == origNick)
            {
                PopulateResult(result, "nickname", match.GivenName);
            }

            return(true);
        }