From 7e5a888404f49724245e010ad5037e8acfabe51f Mon Sep 17 00:00:00 2001 From: Mantas-2155X Date: Sun, 13 Sep 2020 17:19:27 +0300 Subject: [PATCH] Translate item names when they're created, use that cache in searches to avoid need of searching twice --- AI_MakerSearch/Hooks.cs | 26 ++++++++++++++++++++++++++ AI_MakerSearch/Tools.cs | 6 ++++-- EC_MakerSearch/Hooks.cs | 26 ++++++++++++++++++++++++++ EC_MakerSearch/Tools.cs | 3 ++- HS2_MakerSearch/Hooks.cs | 26 ++++++++++++++++++++++++++ HS2_MakerSearch/Tools.cs | 4 +++- KK_MakerSearch/Hooks.cs | 26 ++++++++++++++++++++++++++ KK_MakerSearch/Tools.cs | 3 ++- PH_MakerSearch/Hooks.cs | 31 +++++++++++++++++++++++++++++++ PH_MakerSearch/PH_MakerSearch.cs | 9 ++++----- PH_MakerSearch/Tools.cs | 9 ++++++--- 11 files changed, 156 insertions(+), 13 deletions(-) diff --git a/AI_MakerSearch/Hooks.cs b/AI_MakerSearch/Hooks.cs index 52ffec7..0598f7d 100644 --- a/AI_MakerSearch/Hooks.cs +++ b/AI_MakerSearch/Hooks.cs @@ -1,3 +1,5 @@ +using System.Linq; +using System.Threading; using HarmonyLib; using CharaCustom; @@ -37,6 +39,8 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance) AI_MakerSearch.sex = Traverse.Create(__instance).Property("chaCtrl").Property("sex").GetValue(); + Tools.searchNameStrings.Clear(); + Tools.CreateUI(); // Switch between body Skin and Detail @@ -48,6 +52,28 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance) AI_MakerSearch.cvsEye.items[2].tglItem.onValueChanged.AddListener(on => CvsF_EyeLR_ChangeMenuFunc_SetMainCat()); } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectScrollController), "CreateList")] + private static void CustomSelectScrollController_CreateList_TranslateItems(CustomSelectScrollController.ScrollData[] ___scrollerDatas) + { + if (___scrollerDatas == null) + return; + + var t = new Thread(TranslateItems) + { + IsBackground = true, + Name = "Translate items", + Priority = ThreadPriority.BelowNormal + }; + + t.Start(); + + void TranslateItems() + { + foreach (var info in ___scrollerDatas.Where(info => !Tools.searchNameStrings.ContainsKey(info.info))) + TranslationHelper.Translate(info.info.name, s => Tools.searchNameStrings[info.info] = info.info.name + "/v" + s); + } + } + [HarmonyPostfix, HarmonyPatch(typeof(CustomChangeMainMenu), "ChangeWindowSetting")] private static void CustomChangeMainMenu_ChangeWindowSetting_SetMainCat(int no) { diff --git a/AI_MakerSearch/Tools.cs b/AI_MakerSearch/Tools.cs index 08b32ed..9d0ac2d 100644 --- a/AI_MakerSearch/Tools.cs +++ b/AI_MakerSearch/Tools.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using AIChara; @@ -14,7 +15,8 @@ namespace AI_MakerSearch public static class Tools { public static readonly InputField[] fields = new InputField[19]; - + public static readonly Dictionary searchNameStrings = new Dictionary(); + private static readonly string[] targets = { "CharaCustom/CustomControl/CanvasSub/SettingWindow/WinHair/H_Hair/Setting/Setting01", // Hair @@ -121,7 +123,7 @@ public static bool ItemMatchesSearch(CustomSelectInfo data, string searchStr) searchIn = data.name; if (AI_MakerSearch.useTranslatedCache.Value) - TranslationHelper.Translate(data.name, s => { searchIn = s; }); + searchIn = searchNameStrings.TryGetValue(data, out var cachedTranslation) ? cachedTranslation : data.name; break; case SearchBy.AssetBundle: diff --git a/EC_MakerSearch/Hooks.cs b/EC_MakerSearch/Hooks.cs index aca2c9e..c2f110d 100644 --- a/EC_MakerSearch/Hooks.cs +++ b/EC_MakerSearch/Hooks.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; using HarmonyLib; using ChaCustom; @@ -12,9 +15,32 @@ private static void CustomControl_Initialize_CreateUI() EC_MakerSearch.ctrl = null; Tools.disvisibleMemory.Clear(); + Tools.searchNameStrings.Clear(); Tools.CreateUI(); } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectListCtrl), "Create")] + private static void CustomSelectListCtrl_Create_GetSelectInfos(List ___lstSelectInfo) + { + if (___lstSelectInfo == null) + return; + + var t = new Thread(TranslateItems) + { + IsBackground = true, + Name = "Translate items", + Priority = ThreadPriority.BelowNormal + }; + + t.Start(); + + void TranslateItems() + { + foreach (var info in ___lstSelectInfo.Where(info => !Tools.searchNameStrings.ContainsKey(info))) + TranslationHelper.Translate(info.name, s => Tools.searchNameStrings[info] = info.name + "/v" + s); + } + } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectListCtrl), "Update")] private static void CustomSelectListCtrl_Update_ChangeController(CustomSelectListCtrl __instance) { diff --git a/EC_MakerSearch/Tools.cs b/EC_MakerSearch/Tools.cs index f78aaf3..a1c0a13 100644 --- a/EC_MakerSearch/Tools.cs +++ b/EC_MakerSearch/Tools.cs @@ -16,6 +16,7 @@ public static class Tools { private static List fields; public static readonly HashSet disvisibleMemory = new HashSet(); + public static readonly Dictionary searchNameStrings = new Dictionary(); public static void CreateUI() { @@ -197,7 +198,7 @@ public static bool ItemMatchesSearch(CustomSelectInfo data, string searchStr) searchIn = data.name; if (EC_MakerSearch.useTranslatedCache.Value) - TranslationHelper.Translate(data.name, s => { searchIn = s; }); + searchIn = searchNameStrings.TryGetValue(data, out var cachedTranslation) ? cachedTranslation : data.name; break; case SearchBy.AssetBundle: diff --git a/HS2_MakerSearch/Hooks.cs b/HS2_MakerSearch/Hooks.cs index c9a3d84..7b08a6e 100644 --- a/HS2_MakerSearch/Hooks.cs +++ b/HS2_MakerSearch/Hooks.cs @@ -1,3 +1,5 @@ +using System.Linq; +using System.Threading; using HarmonyLib; using CharaCustom; @@ -37,6 +39,8 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance) HS2_MakerSearch.sex = Traverse.Create(__instance).Property("chaCtrl").Property("sex").GetValue(); + Tools.searchNameStrings.Clear(); + Tools.CreateUI(); // Switch between body Skin and Detail @@ -48,6 +52,28 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance) HS2_MakerSearch.cvsEye.items[2].tglItem.onValueChanged.AddListener(on => CvsF_EyeLR_ChangeMenuFunc_SetMainCat()); } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectScrollController), "CreateList")] + private static void CustomSelectScrollController_CreateList_TranslateItems(CustomSelectScrollController.ScrollData[] ___scrollerDatas) + { + if (___scrollerDatas == null) + return; + + var t = new Thread(TranslateItems) + { + IsBackground = true, + Name = "Translate items", + Priority = ThreadPriority.BelowNormal + }; + + t.Start(); + + void TranslateItems() + { + foreach (var info in ___scrollerDatas.Where(info => !Tools.searchNameStrings.ContainsKey(info.info))) + TranslationHelper.Translate(info.info.name, s => Tools.searchNameStrings[info.info] = info.info.name + "/v" + s); + } + } + [HarmonyPostfix, HarmonyPatch(typeof(CustomChangeMainMenu), "ChangeWindowSetting")] private static void CustomChangeMainMenu_ChangeWindowSetting_SetMainCat(int no) { diff --git a/HS2_MakerSearch/Tools.cs b/HS2_MakerSearch/Tools.cs index 9dab40b..4a32e17 100644 --- a/HS2_MakerSearch/Tools.cs +++ b/HS2_MakerSearch/Tools.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using AIChara; @@ -14,6 +15,7 @@ namespace HS2_MakerSearch public static class Tools { public static readonly InputField[] fields = new InputField[19]; + public static readonly Dictionary searchNameStrings = new Dictionary(); private static readonly string[] targets = { @@ -111,7 +113,7 @@ public static bool ItemMatchesSearch(CustomSelectInfo data, string searchStr) searchIn = data.name; if (HS2_MakerSearch.useTranslatedCache.Value) - TranslationHelper.Translate(data.name, s => { searchIn = s; }); + searchIn = searchNameStrings.TryGetValue(data, out var cachedTranslation) ? cachedTranslation : data.name; break; case SearchBy.AssetBundle: diff --git a/KK_MakerSearch/Hooks.cs b/KK_MakerSearch/Hooks.cs index 1e4ec2f..0e5698c 100644 --- a/KK_MakerSearch/Hooks.cs +++ b/KK_MakerSearch/Hooks.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; using HarmonyLib; using ChaCustom; @@ -12,9 +15,32 @@ private static void CustomControl_Initialize_CreateUI() KK_MakerSearch.ctrl = null; Tools.disvisibleMemory.Clear(); + Tools.searchNameStrings.Clear(); Tools.CreateUI(); } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectListCtrl), "Create")] + private static void CustomSelectListCtrl_Create_GetSelectInfos(List ___lstSelectInfo) + { + if (___lstSelectInfo == null) + return; + + var t = new Thread(TranslateItems) + { + IsBackground = true, + Name = "Translate items", + Priority = ThreadPriority.BelowNormal + }; + + t.Start(); + + void TranslateItems() + { + foreach (var info in ___lstSelectInfo.Where(info => !Tools.searchNameStrings.ContainsKey(info))) + TranslationHelper.Translate(info.name, s => Tools.searchNameStrings[info] = info.name + "/v" + s); + } + } + [HarmonyPostfix, HarmonyPatch(typeof(CustomSelectListCtrl), "Update")] private static void CustomSelectListCtrl_Update_ChangeController(CustomSelectListCtrl __instance) { diff --git a/KK_MakerSearch/Tools.cs b/KK_MakerSearch/Tools.cs index ef39b4e..339e30d 100644 --- a/KK_MakerSearch/Tools.cs +++ b/KK_MakerSearch/Tools.cs @@ -16,6 +16,7 @@ public static class Tools { private static List fields; public static readonly HashSet disvisibleMemory = new HashSet(); + public static readonly Dictionary searchNameStrings = new Dictionary(); public static void CreateUI() { @@ -197,7 +198,7 @@ public static bool ItemMatchesSearch(CustomSelectInfo data, string searchStr) searchIn = data.name; if (KK_MakerSearch.useTranslatedCache.Value) - TranslationHelper.Translate(data.name, s => { searchIn = s; }); + searchIn = searchNameStrings.TryGetValue(data, out var cachedTranslation) ? cachedTranslation : data.name; break; case SearchBy.AssetBundle: diff --git a/PH_MakerSearch/Hooks.cs b/PH_MakerSearch/Hooks.cs index ceace6b..27d03d4 100644 --- a/PH_MakerSearch/Hooks.cs +++ b/PH_MakerSearch/Hooks.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Threading; using HarmonyLib; namespace PH_MakerSearch @@ -7,9 +9,38 @@ public static class Hooks [HarmonyPostfix, HarmonyPatch(typeof(EditMode), "Setup")] private static void EditMode_Setup_CreateUI(EditMode __instance, MoveableThumbnailSelectUI ___itemSelectUI) { + Tools.searchNameStrings.Clear(); Tools.CreateUI(__instance, ___itemSelectUI); } + [HarmonyPostfix, HarmonyPatch(typeof(ThumbnailSelectUI), "SetupCells")] + private static void CustomSelectListCtrl_SetupCells_GetSelectInfos(ThumbnailSelectCell[] ___cells, List ___datas) + { + if (___cells == null) + return; + + var t = new Thread(TranslateItems) + { + IsBackground = true, + Name = "Translate items", + Priority = ThreadPriority.BelowNormal + }; + + t.Start(); + + void TranslateItems() + { + for (var i = 0; i < ___datas.Count; i++) + { + if(Tools.searchNameStrings.ContainsKey(___cells[i])) + continue; + + var idx = i; + TranslationHelper.Translate(___datas[i].name, s => Tools.searchNameStrings[___cells[idx]] = ___datas[idx].name + "/v" + s); + } + } + } + [HarmonyPostfix, HarmonyPatch(typeof(MoveableThumbnailSelectUI), "UpdateEnables")] private static void MoveableThumbnailSelectUI_UpdateEnables_ResetSearch(ThumbnailSelectUI ___select) { diff --git a/PH_MakerSearch/PH_MakerSearch.cs b/PH_MakerSearch/PH_MakerSearch.cs index 2c899ab..2eb34f1 100644 --- a/PH_MakerSearch/PH_MakerSearch.cs +++ b/PH_MakerSearch/PH_MakerSearch.cs @@ -1,6 +1,4 @@ -using System.Linq; - -using HarmonyLib; +using HarmonyLib; using BepInEx; using BepInEx.Configuration; @@ -43,8 +41,9 @@ public static void Search() var trav = Traverse.Create(selectUI); var datas = trav.Field("cells").GetValue(); - foreach (var data in datas.Where(data => !Tools.ItemMatchesSearch(data, searchString))) - data.gameObject.SetActive(false); + for (var i = 0; i < datas.Length; i++) + if(!Tools.ItemMatchesSearch(datas[i], searchString, i)) + datas[i].gameObject.SetActive(false); } } } \ No newline at end of file diff --git a/PH_MakerSearch/Tools.cs b/PH_MakerSearch/Tools.cs index 363bcd2..1fd2735 100644 --- a/PH_MakerSearch/Tools.cs +++ b/PH_MakerSearch/Tools.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using HarmonyLib; @@ -10,6 +11,8 @@ namespace PH_MakerSearch { public static class Tools { + public static readonly Dictionary searchNameStrings = new Dictionary(); + public static void CreateUI(EditMode mode, MoveableThumbnailSelectUI itemSelectUI) { if(mode == null || itemSelectUI == null) @@ -60,12 +63,12 @@ public static void CreateUI(EditMode mode, MoveableThumbnailSelectUI itemSelectU PH_MakerSearch.input = input; } - public static bool ItemMatchesSearch(ThumbnailSelectCell data, string searchStr) + public static bool ItemMatchesSearch(ThumbnailSelectCell data, string searchStr, int idx) { - var searchIn = Traverse.Create(data).Field("nameText").GetValue().text; + var searchIn = Traverse.Create(PH_MakerSearch.selectUI).Field("datas").GetValue>()[idx].name; if (PH_MakerSearch.useTranslatedCache.Value) - TranslationHelper.Translate(data.name, s => { searchIn = s; }); + searchIn = searchNameStrings.TryGetValue(data, out var cachedTranslation) ? cachedTranslation : searchIn; var rule = StringComparison.Ordinal; if (!PH_MakerSearch.caseSensitive.Value)