Skip to content

Commit

Permalink
Translate item names when they're created, use that cache in searches…
Browse files Browse the repository at this point in the history
… to avoid need of searching twice
  • Loading branch information
Mantas-2155X committed Sep 13, 2020
1 parent e3a77e4 commit 7e5a888
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 13 deletions.
26 changes: 26 additions & 0 deletions AI_MakerSearch/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;
using System.Threading;
using HarmonyLib;

using CharaCustom;
Expand Down Expand Up @@ -37,6 +39,8 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance)

AI_MakerSearch.sex = Traverse.Create(__instance).Property("chaCtrl").Property("sex").GetValue<byte>();

Tools.searchNameStrings.Clear();

Tools.CreateUI();

// Switch between body Skin and Detail
Expand All @@ -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)
{
Expand Down
6 changes: 4 additions & 2 deletions AI_MakerSearch/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using AIChara;
Expand All @@ -14,7 +15,8 @@ namespace AI_MakerSearch
public static class Tools
{
public static readonly InputField[] fields = new InputField[19];

public static readonly Dictionary<CustomSelectInfo, string> searchNameStrings = new Dictionary<CustomSelectInfo, string>();

private static readonly string[] targets =
{
"CharaCustom/CustomControl/CanvasSub/SettingWindow/WinHair/H_Hair/Setting/Setting01", // Hair
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions EC_MakerSearch/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HarmonyLib;

using ChaCustom;
Expand All @@ -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<CustomSelectInfo> ___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)
{
Expand Down
3 changes: 2 additions & 1 deletion EC_MakerSearch/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class Tools
{
private static List<TMP_InputField> fields;
public static readonly HashSet<CustomSelectInfo> disvisibleMemory = new HashSet<CustomSelectInfo>();
public static readonly Dictionary<CustomSelectInfo, string> searchNameStrings = new Dictionary<CustomSelectInfo, string>();

public static void CreateUI()
{
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions HS2_MakerSearch/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;
using System.Threading;
using HarmonyLib;

using CharaCustom;
Expand Down Expand Up @@ -37,6 +39,8 @@ private static void CustomControl_Initialize_SetVars(CustomControl __instance)

HS2_MakerSearch.sex = Traverse.Create(__instance).Property("chaCtrl").Property("sex").GetValue<byte>();

Tools.searchNameStrings.Clear();

Tools.CreateUI();

// Switch between body Skin and Detail
Expand All @@ -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)
{
Expand Down
4 changes: 3 additions & 1 deletion HS2_MakerSearch/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using AIChara;
Expand All @@ -14,6 +15,7 @@ namespace HS2_MakerSearch
public static class Tools
{
public static readonly InputField[] fields = new InputField[19];
public static readonly Dictionary<CustomSelectInfo, string> searchNameStrings = new Dictionary<CustomSelectInfo, string>();

private static readonly string[] targets =
{
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions KK_MakerSearch/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HarmonyLib;

using ChaCustom;
Expand All @@ -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<CustomSelectInfo> ___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)
{
Expand Down
3 changes: 2 additions & 1 deletion KK_MakerSearch/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class Tools
{
private static List<TMP_InputField> fields;
public static readonly HashSet<CustomSelectInfo> disvisibleMemory = new HashSet<CustomSelectInfo>();
public static readonly Dictionary<CustomSelectInfo, string> searchNameStrings = new Dictionary<CustomSelectInfo, string>();

public static void CreateUI()
{
Expand Down Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions PH_MakerSearch/Hooks.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using HarmonyLib;

namespace PH_MakerSearch
Expand All @@ -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<CustomSelectSet> ___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)
{
Expand Down
9 changes: 4 additions & 5 deletions PH_MakerSearch/PH_MakerSearch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

using HarmonyLib;
using HarmonyLib;

using BepInEx;
using BepInEx.Configuration;
Expand Down Expand Up @@ -43,8 +41,9 @@ public static void Search()
var trav = Traverse.Create(selectUI);
var datas = trav.Field("cells").GetValue<ThumbnailSelectCell[]>();

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);
}
}
}
9 changes: 6 additions & 3 deletions PH_MakerSearch/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using HarmonyLib;
Expand All @@ -10,6 +11,8 @@ namespace PH_MakerSearch
{
public static class Tools
{
public static readonly Dictionary<ThumbnailSelectCell, string> searchNameStrings = new Dictionary<ThumbnailSelectCell, string>();

public static void CreateUI(EditMode mode, MoveableThumbnailSelectUI itemSelectUI)
{
if(mode == null || itemSelectUI == null)
Expand Down Expand Up @@ -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>().text;
var searchIn = Traverse.Create(PH_MakerSearch.selectUI).Field("datas").GetValue<List<CustomSelectSet>>()[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)
Expand Down

0 comments on commit 7e5a888

Please sign in to comment.