Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove lag after 1st time opening hats #238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 74 additions & 24 deletions source/Patches/CustomHats/CustomHatPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using AmongUs.Data;
using HarmonyLib;
Expand All @@ -11,53 +12,105 @@
namespace TownOfUs.Patches.CustomHats
{

[HarmonyPatch(typeof(HatsTab), nameof(HatsTab.OnEnable))]
[HarmonyPatch]

public static class HatsTab_OnEnable
{
public static int CurrentPage = 0;

public static string LastHeader = string.Empty;

[HarmonyPatch(typeof(HatsTab), nameof(HatsTab.OnEnable))]

public static bool Prefix(HatsTab __instance)
{
__instance.currentHat = DestroyableSingleton<HatManager>.Instance.GetHatById(DataManager.Player.Customization.Hat);
var allHats = DestroyableSingleton<HatManager>.Instance.GetUnlockedHats();
var hatGroups = new SortedList<string, List<HatData>>(
new PaddedComparer<string>("Vanilla", "")
);
foreach (var hat in allHats)
__instance.currentHat = HatManager.Instance.GetHatById(DataManager.Player.Customization.Hat);
var allHats = HatManager.Instance.GetUnlockedHats().ToImmutableList();

if (HatCache.SortedHats == null)
{
if (!hatGroups.ContainsKey(hat.StoreName))
hatGroups[hat.StoreName] = new List<HatData>();
hatGroups[hat.StoreName].Add(hat);
var num = 0;
HatCache.SortedHats = new(new PaddedComparer<string>("Vanilla", ""));
foreach (var hat in allHats)
{
if (!HatCache.SortedHats.ContainsKey(hat.StoreName)) HatCache.SortedHats[hat.StoreName] = [];
HatCache.SortedHats[hat.StoreName].Add(hat);

if (!HatCache.StoreNames.ContainsValue(hat.StoreName))
{
HatCache.StoreNames.Add(num, hat.StoreName);
num++;
}
}
}

foreach (ColorChip instanceColorChip in __instance.ColorChips)
instanceColorChip.gameObject.Destroy();
GenHats(__instance, CurrentPage);

return false;
}

[HarmonyPatch(typeof(HatsTab), nameof(HatsTab.Update))]
[HarmonyPrefix]

public static void Update(HatsTab __instance)
{
if (Input.GetKeyDown(KeyCode.F3))
{
CurrentPage--;
CurrentPage = CurrentPage < 0 ? HatCache.StoreNames.Count - 1 : CurrentPage;
GenHats(__instance, CurrentPage);
}
else if (Input.GetKeyDown(KeyCode.F4))
{
CurrentPage++;
CurrentPage = CurrentPage > HatCache.StoreNames.Count - 1 ? 0 : CurrentPage;
GenHats(__instance, CurrentPage);
}
}

public static void GenHats(HatsTab __instance, int page)
{
foreach (ColorChip instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.Destroy();
__instance.ColorChips.Clear();

if (LastHeader != string.Empty)
{
var header = GameObject.Find(LastHeader);
if (header != null) header.Destroy();
}

var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false);
int hatIdx = 0;
foreach ((string groupName, List<HatData> hats) in hatGroups)
var group = HatCache.SortedHats.Where(x => x.Key == HatCache.StoreNames[page]);
foreach ((string groupName, List<HatData> hats) in group)
{
hatIdx = (hatIdx + 4) / 5 * 5;
var text = Object.Instantiate(groupNameText, __instance.scroller.Inner);
text.gameObject.transform.localScale = Vector3.one;
text.GetComponent<TextTranslatorTMP>().Destroy();
text.text = groupName;
text.text = $"{groupName}\nPress F3 & F4 to cycle pages";
text.alignment = TextAlignmentOptions.Center;
text.fontSize = 3f;
text.fontSizeMax = 3f;
text.fontSizeMin = 0f;

hatIdx = (hatIdx + 4) / 5 * 5;

LastHeader = text.name = $"{groupName} header";
float xLerp = __instance.XRange.Lerp(0.5f);
float yLerp = __instance.YStart - (hatIdx / __instance.NumPerRow) * __instance.YOffset;
float yLerp = __instance.YStart - hatIdx / __instance.NumPerRow * __instance.YOffset;
text.transform.localPosition = new Vector3(xLerp, yLerp, -1f);

hatIdx += 5;
foreach (var hat in hats.OrderBy(HatManager.Instance.allHats.IndexOf))
{
float num = __instance.XRange.Lerp(hatIdx % __instance.NumPerRow / (__instance.NumPerRow - 1f));
float num2 = __instance.YStart - hatIdx / __instance.NumPerRow * __instance.YOffset;
ColorChip colorChip = Object.Instantiate(__instance.ColorTabPrefab, __instance.scroller.Inner);

var colorChip = Object.Instantiate(__instance.ColorTabPrefab, __instance.scroller.Inner);
colorChip.gameObject.name = hat.ProductId;
colorChip.Button.OnClick.AddListener((Action)(() => __instance.ClickEquip()));
colorChip.Button.OnMouseOver.AddListener((Action)(() => __instance.SelectHat(hat)));
colorChip.Button.OnMouseOut.AddListener((Action)(() => __instance.SelectHat(HatManager.Instance.GetHatById(DataManager.Player.Customization.Hat))));
colorChip.Inner.SetHat(hat, __instance.HasLocalPlayer() ? PlayerControl.LocalPlayer.Data.DefaultOutfit.ColorId : DataManager.Player.Customization.Color);
colorChip.transform.localPosition = new Vector3(num, num2, -1f);
colorChip.Button.OnClick.AddListener((Action)(() => __instance.SelectHat(hat)));
colorChip.Inner.SetHat(hat, __instance.HasLocalPlayer() ? PlayerControl.LocalPlayer.Data.DefaultOutfit.ColorId : (int)DataManager.Player.Customization.Color);
colorChip.Inner.transform.localPosition = hat.ChipOffset + new Vector2(0f, -0.3f);
if (SubmergedCompatibility.Loaded)
{
Expand All @@ -67,13 +120,10 @@ public static bool Prefix(HatsTab __instance)
__instance.ColorChips.Add(colorChip);
hatIdx += 1;
}

}

__instance.scroller.ContentYBounds.max = -(__instance.YStart - (hatIdx + 1) / __instance.NumPerRow * __instance.YOffset) - 3f;
__instance.currentHatIsEquipped = true;

return false;
}
}
}
5 changes: 2 additions & 3 deletions source/Patches/CustomHats/HatCache.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Generic;
using UnityEngine;

namespace TownOfUs.Patches.CustomHats
{
public static class HatCache
{
public static Dictionary<string, Sprite> hatViewDatas= new Dictionary<string, Sprite>();

public static SortedList<string, List<HatData>> SortedHats = null;

public static Dictionary<int, string> StoreNames = [];
}
}
11 changes: 5 additions & 6 deletions source/Patches/CustomHats/HatLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ internal static class HatLoader

internal static void LoadHatsRoutine()
{
if (LoadedHats || !DestroyableSingleton<HatManager>.InstanceExists || DestroyableSingleton<HatManager>.Instance.allHats.Count == 0)
return;
if (LoadedHats || !HatManager.InstanceExists || HatManager.Instance.allHats.Count == 0) return;
LoadedHats = true;
Coroutines.Start(LoadHats());
}
Expand All @@ -32,17 +31,17 @@ internal static IEnumerator LoadHats()
var hatBehaviours = DiscoverHatBehaviours();

var hatData = new List<HatData>();
hatData.AddRange(DestroyableSingleton<HatManager>.Instance.allHats);
hatData.ForEach((Action<HatData>)(x => x.StoreName = "Vanilla"));
hatData.AddRange(HatManager.Instance.allHats);
hatData.ForEach(x => x.StoreName = "Vanilla");

var originalCount = DestroyableSingleton<HatManager>.Instance.allHats.ToList().Count;
var originalCount = HatManager.Instance.allHats.ToList().Count;
hatBehaviours.Reverse();
for (var i = 0; i < hatBehaviours.Count; i++)
{
hatBehaviours[i].displayOrder = originalCount + i;
hatData.Add(hatBehaviours[i]);
}
DestroyableSingleton<HatManager>.Instance.allHats = hatData.ToArray();
HatManager.Instance.allHats = hatData.ToArray();
}
catch (Exception e)
{
Expand Down
Loading