Ejemplo n.º 1
0
        public IEnumerator WaitOnFileInfo(ICharaFileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                yield break;
            }

            var path     = fileInfo.FullPath;
            var origName = fileInfo.Name;
            var sex      = fileInfo.Sex;

            bool NotDone()
            {
                return(_pathsInProgress.Contains(path));
            }

            // delay once if card does not appear active yet
            if (!NotDone())
            {
                yield return(null);
            }

            yield return(new WaitWhile(NotDone));

            if (sex != CharacterSex.Unspecified && fileInfo.FullPath == path)
            {
                ApplyTranslations(fileInfo);
            }
            Logger.DebugLogDebug($"WaitOnFileInfo: {fileInfo}: done: {path}");
        }
Ejemplo n.º 2
0
        internal static CharacterSex GuessSex(this ICharaFileInfo fileInfo)
        {
            try
            {
                if (MakerAPI.InsideMaker)
                {
                    return((CharacterSex)MakerAPI.GetMakerSex());
                }
            }
            catch (Exception err)
            {
                Logger?.LogException(err, $"{nameof(GuessSex)}: Unexpected error determining sex from Maker");
            }

            try
            {
                return((CharacterSex)Configuration.GuessSex(fileInfo.Club, fileInfo.Personality));
            }
            catch (Exception err)
            {
                Logger?.LogException(err,
                                     $"{nameof(GuessSex)}: Unexpected error attempting to guess sex for {fileInfo.GetPrettyTypeFullName()}");
            }

            return(CharacterSex.Unspecified);
        }
Ejemplo n.º 3
0
        internal static CharacterSex GuessSex(this ICharaFileInfo fileInfo)
        {
            var filename = Path.GetFileName(fileInfo.FullPath);

            if (filename.StartsWith("AISChaM_", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("ais_m_", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("HS2ChaM_", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("HS2_ill_M_", StringComparison.OrdinalIgnoreCase))
            {
                return(CharacterSex.Male);
            }

            if (filename.StartsWith("AISChaF", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("ais_f_", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("HS2ChaF_", StringComparison.OrdinalIgnoreCase) ||
                filename.StartsWith("HS2_ill_F_", StringComparison.OrdinalIgnoreCase))
            {
                return(CharacterSex.Female);
            }

            var path = PathUtils.NormalizePath(fileInfo.FullPath).ToLowerInvariant();

            if (path.Contains(MalePathKey))
            {
                return(CharacterSex.Male);
            }
            return(path.Contains(FemalePathKey) ? CharacterSex.Female : CharacterSex.Unspecified);
        }
Ejemplo n.º 4
0
 internal static void SafeNameUpdate(this ICharaFileInfo fileInfo, string path, string originalName,
                                     string newName)
 {
     if (fileInfo.FullPath == path && fileInfo.Name == originalName)
     {
         fileInfo.Name = newName;
     }
 }
Ejemplo n.º 5
0
        internal static void OnPointerEnterPostfix(MonoBehaviour instance, ICharaFileInfo wrapper)
        {
            try
            {
                if (!TranslationHelper.Instance.CurrentCardLoadTranslationEnabled)
                {
                    return;
                }
                var name = wrapper.Name;
                //Logger.LogDebug($"OnPointerEnterPostfix: name={name}");

                var textDrawName = Traverse.Create(instance)?.Field <Text>("textDrawName")?.Value;

                if (TranslationHelper.TryFastTranslateFullName(wrapper, out var tmpName))
                {
                    wrapper.Name = tmpName;
                    if (textDrawName != null)
                    {
                        textDrawName.text = tmpName;
                    }
                    return;
                }

                void Handler(ITranslationResult result)
                {
                    //Logger.LogDebug($"OnPointerEnterPostfix: Handler: {result}");
                    /*var newName = ProcessTranslationResult(scope, wrapper.FullPath, name, result);*/
                    if (!result.Succeeded || result.TranslatedText == name)
                    {
                        return;
                    }
                    wrapper.Name = result.TranslatedText;
                    if (textDrawName == null)
                    {
                        return;
                    }
                    textDrawName.text = result.TranslatedText;
                }

                if (textDrawName != null)
                {
                    textDrawName.text = name;
                }


                _pointerEnterCoroutine = TranslationHelper.Instance.StartCoroutine(
                    TranslationHelper.TranslateFileInfo(wrapper,
                                                        //TranslationHelper.CardNameManager.TranslateFullName(name, new NameScope((CharacterSex)sex),
                                                        Handler, _ => _pointerEnterCoroutine = null));
            }

            catch (Exception err)
            {
                Logger.LogException(err, instance, nameof(OnPointerEnterPostfix));
            }
        }
Ejemplo n.º 6
0
        public static bool TryGetRecentTranslation(ICharaFileInfo fileInfo, out string result)
        {
            result = null;
#if !CORRUPT_CACHE_CAUSING_DUPLICATE_NAMES_FIXED
            return(false);
#else
            var hit = RecentTranslationsByPath[new NameScope(fileInfo.Sex)].TryGetValue(fileInfo.FullPath, out result);
            Logger.DebugLogDebug(
                $"CharaFileInfoTranslationManager.TryGetRecentTranslation({fileInfo.Sex}, {fileInfo.FullPath}) => {result} {hit}");
            return(hit);
#endif
        }
Ejemplo n.º 7
0
        private static void ApplyTranslations(ICharaFileInfo fileInfo)
        {
            var scope = new NameScope(fileInfo.Sex);

            if (scope.Sex == CharacterSex.Unspecified)
            {
                return;
            }
            if (!TryGetRecentTranslation(fileInfo, out var result) || result.IsNullOrEmpty())
            {
                return;
            }
            fileInfo.Name = result;
        }
Ejemplo n.º 8
0
        internal static Action <string> MakeCachingCallbackWrapper(ICharaFileInfo fileInfo,
                                                                   Action <string> callback = null)
        {
            var sex      = fileInfo.Sex;
            var origName = fileInfo.Name;
            var path     = fileInfo.FullPath;

            void ICharaFileInfoCachingCallbackWrapper(string translationResult)
            {
                Logger.DebugLogDebug(
                    $"{nameof(ICharaFileInfoCachingCallbackWrapper)}: translationResult={translationResult}, origName={origName}, path={path}, callback={callback}");
                if (!translationResult.IsNullOrEmpty() &&
                    !TranslationHelper.NameStringComparer.Equals(translationResult, origName) &&
                    !path.IsNullOrEmpty())
                {
                    CacheRecentTranslation(new NameScope(sex), path, translationResult, false);
                }

                callback?.Invoke(translationResult);
            }

            return(ICharaFileInfoCachingCallbackWrapper);
        }
Ejemplo n.º 9
0
        internal static void FileListCtrlAddListPrefix(MonoBehaviour fileListCtrl, ICharaFileInfo info)
        {
            if (!TranslationHelper.Instance.CurrentCardLoadTranslationEnabled || fileListCtrl == null)
            {
                return;
            }

            // ReSharper disable once RedundantAssignment - used in DEBUG
            var start = Time.realtimeSinceStartup;

            try
            {
                var origName = info.Name;
                var sex      = Configuration.GuessSex(info.Club, info.Personality);
                var scope    = new NameScope((CharacterSex)sex);
                var path     = info.FullPath;

                void Handler(ITranslationResult result)
                {
                    var newName = ProcessTranslationResult(scope, origName, path, result);

                    if (fileListCtrl == null)
                    {
                        return;
                    }
                    if (TranslationHelper.NameStringComparer.Equals(origName, newName))
                    {
                        return;
                    }
                    var lstFileInfo = Traverse.Create(fileListCtrl)?.Field <List <CustomFileInfo> >("lstFileInfo")?.Value;

                    if (lstFileInfo == null)
                    {
                        return;
                    }


                    Logger.DebugLogDebug($"{nameof(FileListCtrlAddListPrefix)}.{nameof(Handler)}: {lstFileInfo}");

                    var entry = lstFileInfo?.FirstOrDefault(x =>
                    {
                        int index;
                        try
                        {
                            index = CharaFileInfoWrapper.CreateWrapper(x).Index;
                        }
                        catch
                        {
                            index = -1;
                        }

                        return(info.Index == index);
                    });

                    if (entry == null)
                    {
                        return;
                    }
                    entry.SafeNameUpdate(path, origName, newName);
                }

                //TranslationHelper.Instance.StartCoroutine(
                fileListCtrl.StartCoroutine(TranslationHelper.TranslateFileInfo(info, Handler));
            }
            catch (Exception err)
            {
                Logger.LogException(err, fileListCtrl, nameof(FileListCtrlAddListPrefix));
            }
            finally
            {
                Logger.DebugLogDebug($"FileListCtrlAddListPrefix: {Time.realtimeSinceStartup - start:000.0000000000}");
            }
        }
Ejemplo n.º 10
0
 public static bool FileInfoNeedsTranslation(ICharaFileInfo fileInfo)
 {
     return(StringUtils.ContainsJapaneseChar(fileInfo.Name));
 }
Ejemplo n.º 11
0
        public IEnumerator TranslateFileInfo(ICharaFileInfo fileInfo, params TranslationResultHandler[] callbacks)
        {
            var scope        = new NameScope(fileInfo.Sex);
            var path         = fileInfo.FullPath;
            var originalName = fileInfo.Name;
            var done         = false;
            var tmpCallbacks = callbacks.ToList();

            _pathsInProgress.Add(path);


            var cacheWrapper = MakeCachingCallbackWrapper(fileInfo);

            void DoneHandler(ITranslationResult result)
            {
                _pathsInProgress.Remove(path);
                if (result.Succeeded)
                {
                    cacheWrapper(result.TranslatedText);
                    CardNameTranslationManager.CacheRecentTranslation(scope, originalName, result.TranslatedText);
                    // must be set after CacheRecentTranslation
                    fileInfo.SafeNameUpdate(path, originalName, result.TranslatedText);
                }

                done = true;
            }

            tmpCallbacks.Add(DoneHandler);
            tmpCallbacks.Add(Handlers.AddNameToAutoTranslationCache(originalName));

            IEnumerator WhileNotDone()
            {
                while (!done)
                {
                    yield return(null);
                }
            }

            IEnumerator TranslationCoroutine(IEnumerable <TranslationResultHandler> handlers)
            {
                if (TryGetRecentTranslation(scope, path, out var cachedName))
                {
                    var result = new TranslationResult(originalName, cachedName);
                    if (result.Succeeded)
                    {
                        fileInfo.SafeNameUpdate(path, originalName, cachedName);
                    }

                    handlers.CallHandlers(result);
                    yield break;
                }

                yield return(null);

                if (!StringUtils.ContainsJapaneseChar(originalName))
                {
                    handlers.CallHandlers(new TranslationResult(false, originalName));
                    yield break;
                }

                var tmpHandlers = handlers.ToList();

                tmpHandlers.Add(_ => TranslateFileInfoLimiter.EndImmediately());
                yield return(TranslationHelper.Instance.StartCoroutine(
                                 TranslateFileInfoLimiter.Start().AppendCo(
                                     CardNameTranslationManager.Instance.TranslateFullName(
                                         originalName, scope, tmpHandlers.ToArray()))));
            }

            TranslationHelper.Instance.StartCoroutine(
                _pathTracker.TrackTranslationCoroutine(TranslationCoroutine, scope, path, tmpCallbacks));

            yield return(WhileNotDone());
        }
Ejemplo n.º 12
0
        internal static void FileListCtrlAddListPrefix(CustomFileListCtrl __instance, ICharaFileInfo info)
        {
            if (!TranslationHelper.Instance.CurrentCardLoadTranslationEnabled)
            {
                return;
            }
            // ReSharper disable once RedundantAssignment - used in DEBUG
            var start = Time.realtimeSinceStartup;

            try
            {
                var origName = info.Name;
                var sex      = Configuration.GuessSex(info.Club, info.Personality);
                var scope    = new NameScope((CharacterSex)sex);

                void Handler(ITranslationResult result)
                {
                    if (__instance == null)
                    {
                        return;
                    }

                    var newName = ProcessTranslationResult(scope, origName, info.FullPath, result);

                    if (TranslationHelper.NameStringComparer.Equals(origName, newName))
                    {
                        return;
                    }

                    var lstFileInfo = __instance.lstFileInfo;

                    var entry = lstFileInfo?.FirstOrDefault(x =>
                    {
                        int index;
                        try
                        {
                            index = x.index;
                        }
                        catch
                        {
                            index = -1;
                        }

                        if (index == -1)
                        {
                            index = Traverse.Create(x).Property <int>("index")?.Value ?? -1;
                        }

                        return(info.Index == index);
                    });

                    if (entry == null)
                    {
                        return;
                    }
                    entry.name = newName;
                }

                TranslationHelper.Instance.StartCoroutine(
                    TranslationHelper.TranslateFileInfo(info, Handler));
            }
            catch (Exception err)
            {
                Logger.LogException(err, __instance, nameof(FileListCtrlAddListPrefix));
            }
            finally
            {
                Logger.DebugLogDebug($"FileListCtrlAddListPrefix: {Time.realtimeSinceStartup - start:000.0000000000}");
            }
        }