Add wip caching

This commit is contained in:
Atony committed 2026-06-13 12:10:21 -05:00
1 parent e3b0eb68c0
commit 83be29d6a2
18 files changed
+368 -126

No files matched your search

@@ -138,7 +138,7 @@ public class ModifierDisplayComponent(nint cppPtr) : MonoBehaviour(cppPtr)
internal void DestroyComponent(ModifierUiComponent component) internal void DestroyComponent(ModifierUiComponent component)
{ {
_modifiers.Remove(component.Modifier!); _modifiers.Remove(component.Modifier!);
component.gameObject.FakeDestroy(); component.gameObject.DeepDestroy();
RefreshModifiers(); RefreshModifiers();
} }
@@ -208,7 +208,7 @@ public class ModifierDisplayComponent(nint cppPtr) : MonoBehaviour(cppPtr)
continue; continue;
} }
mod.Value.gameObject.FakeDestroy(); mod.Value.gameObject.DeepDestroy();
_modifiers.Remove(mod.Key); _modifiers.Remove(mod.Key);
} }
+2 -1
View File
@@ -276,8 +276,9 @@ public static class TaskAdderPatches
__instance.transform.FindChild("TitleText_TMP")?.gameObject.DestroyImmediate(); __instance.transform.FindChild("TitleText_TMP")?.gameObject.DestroyImmediate();
} }
__instance.ActiveItems.ToArray().Do(x => x.gameObject.FakeDestroy()); __instance.ActiveItems.ToArray().Do(x => x.gameObject.DeepDestroy(false));
__instance.ActiveItems.Clear(); __instance.ActiveItems.Clear();
Utilities.Extensions.ClearGarbageCollector();
float num = 0f; float num = 0f;
float num2 = 0f; float num2 = 0f;
+2 -1
View File
@@ -143,8 +143,9 @@ public static class HudManagerPatches
foreach (var t in BottomLeft.GetComponentsInChildren<ActionButton>(true)) foreach (var t in BottomLeft.GetComponentsInChildren<ActionButton>(true))
{ {
t.gameObject.FakeDestroy(); t.gameObject.DeepDestroy(false);
} }
Utilities.Extensions.ClearGarbageCollector();
var gridArrange = BottomLeft.GetComponent<GridArrange>(); var gridArrange = BottomLeft.GetComponent<GridArrange>();
var aspectPosition = BottomLeft.GetComponent<AspectPosition>(); var aspectPosition = BottomLeft.GetComponent<AspectPosition>();
+3 -2
View File
@@ -92,9 +92,10 @@ public static class HatsTabPatches
if (loadRoutine != null) Coroutines.Stop(loadRoutine); if (loadRoutine != null) Coroutines.Stop(loadRoutine);
hatIndex = 0; hatIndex = 0;
foreach (var instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.FakeDestroy(); foreach (var instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.DeepDestroy(false);
__instance.ColorChips.Clear(); __instance.ColorChips.Clear();
__instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.FakeDestroy()); __instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.DeepDestroy(false));
Utilities.Extensions.ClearGarbageCollector();
var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false); var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false);
+3 -2
View File
@@ -81,9 +81,10 @@ public static class NameplatesTabPatches
private static void GenerateHats(NameplatesTab __instance, int page) private static void GenerateHats(NameplatesTab __instance, int page)
{ {
foreach (ColorChip instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.FakeDestroy(); foreach (var instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.DeepDestroy(false);
__instance.ColorChips.Clear(); __instance.ColorChips.Clear();
__instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.FakeDestroy()); __instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.DeepDestroy(false));
Utilities.Extensions.ClearGarbageCollector();
var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false); var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false);
+3 -2
View File
@@ -83,9 +83,10 @@ public static class SkinsTabPatches
private static void GenerateHats(SkinsTab __instance, int page) private static void GenerateHats(SkinsTab __instance, int page)
{ {
foreach (ColorChip instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.FakeDestroy(); foreach (var instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.DeepDestroy(false);
__instance.ColorChips.Clear(); __instance.ColorChips.Clear();
__instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.FakeDestroy()); __instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.DeepDestroy(false));
Utilities.Extensions.ClearGarbageCollector();
var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false); var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false);
+3 -2
View File
@@ -81,9 +81,10 @@ public static class VisorsTabPatches
private static void GenerateHats(VisorsTab __instance, int page) private static void GenerateHats(VisorsTab __instance, int page)
{ {
foreach (ColorChip instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.FakeDestroy(); foreach (var instanceColorChip in __instance.ColorChips) instanceColorChip.gameObject.DeepDestroy(false);
__instance.ColorChips.Clear(); __instance.ColorChips.Clear();
__instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.FakeDestroy()); __instance.scroller.Inner.GetComponentsInChildren<TextMeshPro>().Do(x => x.gameObject.DeepDestroy(false));
Utilities.Extensions.ClearGarbageCollector();
var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false); var groupNameText = __instance.GetComponentInChildren<TextMeshPro>(false);
+67 -17
View File
@@ -139,11 +139,25 @@ internal static class GameOptionsMenuPatch
private static void CustomMenuOneCreate(GameOptionsMenu menu) private static void CustomMenuOneCreate(GameOptionsMenu menu)
{ {
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups var existing = GameSettingMenuPatches.CachedGameSettings.FirstOrDefault(x => x.ModId == GameSettingMenuPatches.SelectedModIdx && x.Category == MenuCategory.CustomOne);
.Where(x => x.ParentMenu == MenuCategory.CustomOne) ?? []; if (existing != null)
foreach (var group in groups)
{ {
CreateGroup(menu, group); GameSettingMenuPatches.ShowGameOptions(existing, menu);
}
else
{
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
.Where(x => x.ParentMenu == MenuCategory.CustomOne) ?? [];
foreach (var group in groups)
{
CreateGroup(menu, group);
}
GameSettingMenuPatches.AddGameSettings(
MenuCategory.CustomOne,
menu.Children,
menu.settingsContainer
.GetComponentsInChildren<CategoryHeaderMasked>()
.ToList());
} }
} }
@@ -160,11 +174,25 @@ internal static class GameOptionsMenuPatch
private static void CustomMenuTwoCreate(GameOptionsMenu menu) private static void CustomMenuTwoCreate(GameOptionsMenu menu)
{ {
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups var existing = GameSettingMenuPatches.CachedGameSettings.FirstOrDefault(x => x.ModId == GameSettingMenuPatches.SelectedModIdx && x.Category == MenuCategory.CustomTwo);
.Where(x => x.ParentMenu == MenuCategory.CustomTwo) ?? []; if (existing != null)
foreach (var group in groups)
{ {
CreateGroup(menu, group); GameSettingMenuPatches.ShowGameOptions(existing, menu);
}
else
{
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
.Where(x => x.ParentMenu == MenuCategory.CustomTwo) ?? [];
foreach (var group in groups)
{
CreateGroup(menu, group);
}
GameSettingMenuPatches.AddGameSettings(
MenuCategory.CustomTwo,
menu.Children,
menu.settingsContainer
.GetComponentsInChildren<CategoryHeaderMasked>()
.ToList());
} }
} }
@@ -181,11 +209,25 @@ internal static class GameOptionsMenuPatch
private static void ModifiersCreate(GameOptionsMenu menu) private static void ModifiersCreate(GameOptionsMenu menu)
{ {
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups var existing = GameSettingMenuPatches.CachedGameSettings.FirstOrDefault(x => x.ModId == GameSettingMenuPatches.SelectedModIdx && x.Category == MenuCategory.Modifiers);
.Where(x => x.ShowInModifiersMenu || x.OptionableType?.IsAssignableTo(typeof(BaseModifier)) == true) ?? []; if (existing != null)
foreach (var group in groups)
{ {
CreateGroup(menu, group); GameSettingMenuPatches.ShowGameOptions(existing, menu);
}
else
{
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
.Where(x => x.ShowInModifiersMenu || x.OptionableType?.IsAssignableTo(typeof(BaseModifier)) == true) ?? [];
foreach (var group in groups)
{
CreateGroup(menu, group);
}
GameSettingMenuPatches.AddGameSettings(
MenuCategory.Modifiers,
menu.Children,
menu.settingsContainer
.GetComponentsInChildren<CategoryHeaderMasked>()
.ToList());
} }
} }
@@ -217,12 +259,20 @@ internal static class GameOptionsMenuPatch
return false; return false;
} }
var filteredGroups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups var existing = GameSettingMenuPatches.CachedGameSettings.FirstOrDefault(x => x.ModId == GameSettingMenuPatches.SelectedModIdx && x.Category == MenuCategory.Game);
.Where(x => x.OptionableType == null && !x.ShowInModifiersMenu && x.ParentMenu == MenuCategory.Roles) ?? []; if (existing != null)
foreach (var group in filteredGroups)
{ {
CreateGroup(__instance, group); GameSettingMenuPatches.ShowGameOptions(existing, __instance);
}
else
{
var filteredGroups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
.Where(x => x.OptionableType == null && !x.ShowInModifiersMenu && x.ParentMenu == MenuCategory.Roles) ?? [];
foreach (var group in filteredGroups)
{
CreateGroup(__instance, group);
}
} }
return false; return false;
@@ -262,7 +262,7 @@ internal static class GamePresetsTabPatches
iconSpriteRend.sprite = MiraAssets.RefreshIcon.LoadAsset(); iconSpriteRend.sprite = MiraAssets.RefreshIcon.LoadAsset();
Helpers.DivideSize(refreshButton.gameObject, 2f); Helpers.DivideSize(refreshButton.gameObject, 2f);
refreshButton.buttonText.gameObject.FakeDestroy(); refreshButton.buttonText.gameObject.DeepDestroy();
refreshButton.OnClick = new Button.ButtonClickedEvent(); refreshButton.OnClick = new Button.ButtonClickedEvent();
refreshButton.OnClick.AddListener((UnityAction)Refresh); refreshButton.OnClick.AddListener((UnityAction)Refresh);
+146 -38
View File
@@ -6,6 +6,7 @@ using MiraAPI.GameOptions;
using MiraAPI.Modifiers; using MiraAPI.Modifiers;
using MiraAPI.PluginLoading; using MiraAPI.PluginLoading;
using MiraAPI.Presets; using MiraAPI.Presets;
using MiraAPI.Utilities;
using MiraAPI.Utilities.Assets; using MiraAPI.Utilities.Assets;
using Reactor.Utilities.Extensions; using Reactor.Utilities.Extensions;
using TMPro; using TMPro;
@@ -40,6 +41,140 @@ internal static class GameSettingMenuPatches
private static PassiveButton? _customTwoButton; private static PassiveButton? _customTwoButton;
private static GameObject? _nextModButton; private static GameObject? _nextModButton;
private static GameObject? _previousModButton; private static GameObject? _previousModButton;
public static List<GameSettingsCache> CachedGameSettings { get; } = [];
public static List<RoleSettingsCache> CachedRoleSettings { get; } = [];
public sealed class GameSettingsCache
{
public int ModId { get; set; }
public MenuCategory Category { get; set; } = MenuCategory.Game;
public Il2CppSystem.Collections.Generic.List<OptionBehaviour> Options { get; set; }
public List<CategoryHeaderMasked> CategoryHeaders { get; set; }
public GameObject CacheHolder { get; set; }
}
public static GameSettingsCache GetCurrentSettings(MenuCategory category)
{
var existing = CachedGameSettings.FirstOrDefault(x => x.ModId == SelectedModIdx && x.Category == category);
if (existing != null)
{
return existing;
}
var menu = category switch
{
MenuCategory.Modifiers => _modifiersTab!,
MenuCategory.CustomOne => _customOneTab!,
MenuCategory.CustomTwo => _customTwoTab!,
_ => GameSettingMenu.Instance.GameSettingsTab,
};
return AddGameSettings(
category,
menu.Children,
menu.settingsContainer
.GetComponentsInChildren<CategoryHeaderMasked>()
.ToList());
}
public static GameSettingsCache AddGameSettings(MenuCategory category, Il2CppSystem.Collections.Generic.List<OptionBehaviour> options, List<CategoryHeaderMasked> categoryHeaders)
{
var modName = SelectedMod?.MiraPlugin.OptionsTitleText ?? "Vanilla";
var newHolder = new GameObject($"{modName} {category}");
newHolder.DontUnload().DontDestroy();
newHolder.SetActive(false);
var newCache = new GameSettingsCache
{
ModId = SelectedModIdx,
Category = category,
Options = options,
CategoryHeaders = categoryHeaders,
CacheHolder = newHolder,
};
CachedGameSettings.Add(newCache);
return newCache;
}
public static void HideGameOptions(GameSettingsCache cached, GameOptionsMenu gameOptMenu)
{
foreach (var obj in gameOptMenu.scrollBar.Inner.GetComponentsInChildren<Transform>())
{
if (obj.gameObject.name == "MapPicker" || !obj.gameObject)
{
continue;
}
obj.SetParent(cached.CacheHolder.transform);
}
}
public static void ShowGameOptions(GameSettingsCache cached, GameOptionsMenu gameOptMenu)
{
foreach (var obj in cached.CacheHolder.GetComponentsInChildren<Transform>())
{
if (!obj.gameObject)
{
continue;
}
obj.SetParent(gameOptMenu.scrollBar.Inner);
}
}
public sealed class RoleSettingsCache
{
public int ModId { get; set; }
public Il2CppSystem.Collections.Generic.List<OptionBehaviour> Options { get; set; }
public List<CategoryHeaderEditRole> RoleHeaders { get; set; }
public List<RoleOptionSetting> RoleOptions { get; set; }
public List<CategoryHeaderEditRole> CategoryHeaders { get; set; }
public GameObject CacheHolder { get; set; }
}
public static RoleSettingsCache GetCurrentRoleSettings()
{
var existing = CachedRoleSettings.FirstOrDefault(x => x.ModId == SelectedModIdx);
if (existing != null)
{
return existing;
}
var menu = GameSettingMenu.Instance.RoleSettingsTab;
return AddRoleSettings(
menu.advancedSettingChildren,
menu.RoleChancesSettings
.GetComponentsInChildren<CategoryHeaderEditRole>()
.ToList());
}
public static RoleSettingsCache AddRoleSettings(Il2CppSystem.Collections.Generic.List<OptionBehaviour> options, List<CategoryHeaderEditRole> categoryHeaders)
{
var modName = SelectedMod?.MiraPlugin.OptionsTitleText ?? "Vanilla";
var newHolder = new GameObject($"{modName} Roles");
newHolder.DontUnload().DontDestroy();
newHolder.SetActive(false);
var newCache = new RoleSettingsCache
{
ModId = SelectedModIdx,
Options = options,
CategoryHeaders = categoryHeaders,
CacheHolder = newHolder,
};
CachedRoleSettings.Add(newCache);
return newCache;
}
public static void HideRoleOptions(RoleSettingsCache cached, RolesSettingsMenu gameOptMenu)
{
foreach (var obj in gameOptMenu.scrollBar.Inner.GetComponentsInChildren<Transform>())
{
if (obj.gameObject.name == "MapPicker" || !obj.gameObject)
{
continue;
}
obj.SetParent(cached.CacheHolder.transform);
}
}
public static void ShowRoleOptions(RoleSettingsCache cached, RolesSettingsMenu gameOptMenu)
{
foreach (var obj in cached.CacheHolder.GetComponentsInChildren<Transform>())
{
if (!obj.gameObject)
{
continue;
}
obj.SetParent(gameOptMenu.scrollBar.Inner);
}
}
private static Dictionary<int, Vector3> OptionsPositions { get; } = []; private static Dictionary<int, Vector3> OptionsPositions { get; } = [];
private static Dictionary<int, Vector3> ModifiersPositions { get; } = []; private static Dictionary<int, Vector3> ModifiersPositions { get; } = [];
@@ -564,21 +699,6 @@ internal static class GameSettingMenuPatches
CleanupTab(settings, roles); CleanupTab(settings, roles);
} }
private static void ClearOptions(Il2CppSystem.Collections.Generic.List<OptionBehaviour> options)
{
foreach (var child in options)
{
if (child.TryCast<GameOptionsMapPicker>() || !child.gameObject)
{
continue;
}
child.gameObject.DestroyImmediate();
}
options.Clear();
}
private static void CleanupTab(GameOptionsMenu settings, RolesSettingsMenu roles) private static void CleanupTab(GameOptionsMenu settings, RolesSettingsMenu roles)
{ {
if (roles.roleChances != null) if (roles.roleChances != null)
@@ -608,24 +728,10 @@ internal static class GameSettingMenuPatches
void CleanupRoleSettings(RolesSettingsMenu rolesMenu) void CleanupRoleSettings(RolesSettingsMenu rolesMenu)
{ {
if (rolesMenu.advancedSettingChildren != null) HideRoleOptions(GetCurrentRoleSettings(), rolesMenu);
{ rolesMenu.advancedSettingChildren = null;
ClearOptions(rolesMenu.advancedSettingChildren);
rolesMenu.advancedSettingChildren = null;
}
rolesMenu.RoleChancesSettings rolesMenu.roleChances.Clear();
.transform
.GetComponentsInChildren<CategoryHeaderEditRole>()
.ToList()
.ForEach(header => header.gameObject.DestroyImmediate());
foreach (var role in rolesMenu.roleChances)
{
role.gameObject.DestroyImmediate();
}
rolesMenu.roleChances?.Clear();
rolesMenu.AdvancedRolesSettings.gameObject.SetActive(false); rolesMenu.AdvancedRolesSettings.gameObject.SetActive(false);
rolesMenu.RoleChancesSettings.gameObject.SetActive(true); rolesMenu.RoleChancesSettings.gameObject.SetActive(true);
@@ -643,14 +749,16 @@ internal static class GameSettingMenuPatches
void CleanupSettings(GameOptionsMenu gameOptMenu, MenuCategory menuCategory = MenuCategory.Roles) void CleanupSettings(GameOptionsMenu gameOptMenu, MenuCategory menuCategory = MenuCategory.Roles)
{ {
ClearOptions(gameOptMenu.Children); if (menuCategory is MenuCategory.Roles)
{
HideRoleOptions(GetCurrentRoleSettings(), gameOptMenu.RolesMenu);
}
else
{
HideGameOptions(GetCurrentSettings(menuCategory), gameOptMenu);
}
gameOptMenu.Children = null; gameOptMenu.Children = null;
gameOptMenu.settingsContainer
.GetComponentsInChildren<CategoryHeaderMasked>()
.ToList()
.ForEach(header => header.gameObject.DestroyImmediate());
gameOptMenu.Initialize(); gameOptMenu.Initialize();
var positions = OptionsPositions; var positions = OptionsPositions;
switch (menuCategory) switch (menuCategory)
@@ -53,23 +53,11 @@ public static class RoleSettingMenuPatches
[HarmonyPatch(nameof(RolesSettingsMenu.SetQuotaTab))] [HarmonyPatch(nameof(RolesSettingsMenu.SetQuotaTab))]
public static bool SetQuotaTabPatch(RolesSettingsMenu __instance) public static bool SetQuotaTabPatch(RolesSettingsMenu __instance)
{ {
foreach (var obj in Headers) GameSettingMenuPatches.HideRoleOptions(GameSettingMenuPatches.GetCurrentRoleSettings(), __instance);
{ __instance.advancedSettingChildren = null;
if (!obj)
{
continue;
}
obj.FakeDestroy();
}
Headers.Clear(); Headers.Clear();
foreach (var obj in RoleOptionSettings) __instance.roleChances.Clear();
{
if (!obj)
{
continue;
}
obj.gameObject.FakeDestroy();
}
RoleOptionSettings.Clear(); RoleOptionSettings.Clear();
CurrentRole = null; CurrentRole = null;
CurrentRoleOptions = null; CurrentRoleOptions = null;
@@ -147,6 +135,7 @@ public static class RoleSettingMenuPatches
num3++; num3++;
} }
Utilities.Extensions.ClearGarbageCollector();
return false; return false;
} }
@@ -168,6 +157,7 @@ public static class RoleSettingMenuPatches
if (roleGroups is null) if (roleGroups is null)
{ {
Utilities.Extensions.ClearGarbageCollector();
return false; return false;
} }
@@ -218,9 +208,9 @@ public static class RoleSettingMenuPatches
var blankLabel = quotaInst.transform.FindChild("BlankLabel").gameObject; var blankLabel = quotaInst.transform.FindChild("BlankLabel").gameObject;
var chanceLabel = quotaInst.transform.FindChild("Chance Label").gameObject; var chanceLabel = quotaInst.transform.FindChild("Chance Label").gameObject;
var countLabel = quotaInst.transform.FindChild("# Label").gameObject; var countLabel = quotaInst.transform.FindChild("# Label").gameObject;
blankLabel.FakeDestroy(); blankLabel.DeepDestroy(false);
chanceLabel.FakeDestroy(); chanceLabel.DeepDestroy(false);
countLabel.FakeDestroy(); countLabel.DeepDestroy(false);
categoryHeaderMasked.Background.sprite = MiraAssets.CategoryHeader.LoadAsset(); categoryHeaderMasked.Background.sprite = MiraAssets.CategoryHeader.LoadAsset();
categoryHeaderMasked.Background.sprite.texture.filterMode = FilterMode.Bilinear; categoryHeaderMasked.Background.sprite.texture.filterMode = FilterMode.Bilinear;
@@ -303,23 +293,10 @@ public static class RoleSettingMenuPatches
{ {
RoleGroupHidden[group] = !groupHidden; RoleGroupHidden[group] = !groupHidden;
} }
foreach (var obj in Headers) GameSettingMenuPatches.HideRoleOptions(GameSettingMenuPatches.GetCurrentRoleSettings(), __instance);
{ __instance.advancedSettingChildren = null;
if (!obj)
{
continue;
}
obj.FakeDestroy();
}
Headers.Clear(); Headers.Clear();
foreach (var obj in RoleOptionSettings) __instance.roleChances.Clear();
{
if (!obj)
{
continue;
}
obj.gameObject.FakeDestroy();
}
RoleOptionSettings.Clear(); RoleOptionSettings.Clear();
__instance.SetQuotaTab(); __instance.SetQuotaTab();
})); }));
@@ -330,6 +307,7 @@ public static class RoleSettingMenuPatches
ScrollerNum -= 0.4f; ScrollerNum -= 0.4f;
} }
} }
Utilities.Extensions.ClearGarbageCollector();
__instance.scrollBar.SetScrollBounds(__instance); __instance.scrollBar.SetScrollBounds(__instance);
return false; return false;
@@ -444,8 +422,9 @@ public static class RoleSettingMenuPatches
{ {
foreach (var optBehaviour in __instance.AdvancedRolesSettings.GetComponentsInChildren<OptionBehaviour>()) foreach (var optBehaviour in __instance.AdvancedRolesSettings.GetComponentsInChildren<OptionBehaviour>())
{ {
optBehaviour.gameObject.FakeDestroy(); optBehaviour.gameObject.DeepDestroy(false);
} }
Utilities.Extensions.ClearGarbageCollector();
CurrentRole = role; CurrentRole = role;
__instance.advancedSettingChildren.Clear(); __instance.advancedSettingChildren.Clear();
@@ -640,7 +619,7 @@ public static class RoleSettingMenuPatches
var newButton = Object.Instantiate(roleOptionSetting.buttons[0], roleOptionSetting.transform); var newButton = Object.Instantiate(roleOptionSetting.buttons[0], roleOptionSetting.transform);
newButton.name = "ConfigButton"; newButton.name = "ConfigButton";
newButton.transform.localPosition = new Vector3(0.4473f, -0.3f, -2f); newButton.transform.localPosition = new Vector3(0.4473f, -0.3f, -2f);
newButton.transform.FindChild("Text_TMP").gameObject.FakeDestroy(); newButton.transform.FindChild("Text_TMP").gameObject.DeepDestroy();
newButton.activeSprites.Destroy(); newButton.activeSprites.Destroy();
var btnRend = newButton.transform.FindChild("ButtonSprite").GetComponent<SpriteRenderer>(); var btnRend = newButton.transform.FindChild("ButtonSprite").GetComponent<SpriteRenderer>();
+2 -3
View File
@@ -36,7 +36,7 @@ public static class HudManagerPatches
if (role is ICustomRole customRole) if (role is ICustomRole customRole)
{ {
__instance.KillButton.ToggleVisible(isActive && customRole.Configuration.UseVanillaKillButton && !flag); __instance.KillButton.ToggleVisible(isActive && customRole.Configuration.UseVanillaKillButton && !flag);
__instance.ImpostorVentButton.ToggleVisible(isActive && (customRole.Configuration.CanUseVent) && !flag); __instance.ImpostorVentButton.ToggleVisible(isActive && customRole.Configuration.CanUseVent && !flag);
__instance.SabotageButton.gameObject.SetActive(isActive && customRole.Configuration.CanUseSabotage); __instance.SabotageButton.gameObject.SetActive(isActive && customRole.Configuration.CanUseSabotage);
} }
@@ -73,8 +73,7 @@ public static class HudManagerPatches
} }
else if (RoleTab != null) else if (RoleTab != null)
{ {
RoleTab.gameObject.FakeDestroy(); RoleTab.gameObject.DeepDestroy();
RoleTab = null;
} }
} }
} }
+2 -1
View File
@@ -6,6 +6,7 @@ using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Gameplay; using MiraAPI.Events.Vanilla.Gameplay;
using MiraAPI.Modifiers; using MiraAPI.Modifiers;
using MiraAPI.Roles; using MiraAPI.Roles;
using MiraAPI.Utilities;
using UnityEngine; using UnityEngine;
namespace MiraAPI.Patches.Roles; namespace MiraAPI.Patches.Roles;
@@ -32,7 +33,7 @@ public static class RoleManagerPatches
if (data.Role) if (data.Role)
{ {
data.Role.Deinitialize(targetPlayer); data.Role.Deinitialize(targetPlayer);
Object.Destroy(data.Role.gameObject); data.Role.gameObject.DeepDestroy();
} }
var roleBehaviour = Object.Instantiate<RoleBehaviour>(__instance.AllRoles.ToArray().First(r => r.Role == roleType), data.gameObject.transform); var roleBehaviour = Object.Instantiate<RoleBehaviour>(__instance.AllRoles.ToArray().First(r => r.Role == roleType), data.gameObject.transform);
roleBehaviour.Initialize(targetPlayer); roleBehaviour.Initialize(targetPlayer);
+2 -1
View File
@@ -62,9 +62,10 @@ public static class PresetManager
{ {
if (btn != null) if (btn != null)
{ {
btn.FakeDestroy(); btn.DeepDestroy(false);
} }
} }
Utilities.Extensions.ClearGarbageCollector();
plugin.InternalPresets.Clear(); plugin.InternalPresets.Clear();
if (!Directory.Exists(PresetDirectory)) if (!Directory.Exists(PresetDirectory))
+1 -1
View File
@@ -26,7 +26,7 @@ public class SavePresetPopup(nint cppPtr) : Minigame(cppPtr)
// Cleanup holder object // Cleanup holder object
public void OnDestroy() public void OnDestroy()
{ {
transform.parent.gameObject.FakeDestroy(); transform.parent.gameObject.DeepDestroy();
} }
public override void Close() public override void Close()
+1 -1
View File
@@ -123,7 +123,7 @@ public static class CustomRoleManager
if (roleBehaviour is not ICustomRole customRole) if (roleBehaviour is not ICustomRole customRole)
{ {
roleBehaviour.gameObject.Destroy(); roleBehaviour.gameObject.DeepDestroy();
return null; return null;
} }
+111 -13
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@@ -53,7 +54,7 @@ public static class Extensions
bool isComplete; bool isComplete;
if (self.amClosing == Minigame.CloseState.Closing) if (self.amClosing == Minigame.CloseState.Closing)
{ {
self.gameObject.FakeDestroy(); self.gameObject.DeepDestroy();
return; return;
} }
if (self.CloseSound && Constants.ShouldPlaySfx()) if (self.CloseSound && Constants.ShouldPlaySfx())
@@ -220,22 +221,119 @@ public static class Extensions
} }
/// <summary> /// <summary>
/// Hides the object away rather than destroy it to prevent memory leaks. /// Destroys the object properly.
/// </summary> /// </summary>
/// <param name="obj">The object to hide.</param> /// <param name="obj">The object to destroy.</param>
public static void FakeDestroy(this GameObject obj) /// <param name="clearGc">Whether to run the garbage collector immediately.</param>
public static void DeepDestroy(this GameObject? obj, bool clearGc = true)
{ {
obj.hideFlags = HideFlags.HideAndDontSave; Coroutines.Start(Nuke(obj, clearGc));
if (!toDestroyHolder)
{
toDestroyHolder = new GameObject("ToDestroyHolder");
toDestroyHolder.gameObject.SetActive(false);
}
obj.transform.SetParent(toDestroyHolder.transform);
obj.SetActive(false);
} }
private static GameObject toDestroyHolder; private static IEnumerator Nuke(GameObject? go, bool clearGc)
{
if (go == null)
yield break;
try
{
go.transform.SetParent(null, false);
}
catch
{
// ignored
}
try
{
go.SetActive(false);
}
catch
{
// ignored
}
foreach (var mb in go.GetComponentsInChildren<MonoBehaviour>(true))
{
if (mb == null)
continue;
try
{
mb.StopAllCoroutines();
}
catch
{
// ignored
}
try
{
mb.enabled = false;
}
catch
{
// ignored
}
}
foreach (var renderer in go.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null)
continue;
try
{
foreach (var mat in renderer.materials)
{
if (mat != null)
UnityEngine.Object.Destroy(mat);
}
}
catch
{
// ignored
}
}
foreach (var filter in go.GetComponentsInChildren<MeshFilter>(true))
{
if (filter == null)
continue;
try
{
var mesh = filter.mesh;
if (mesh != null)
UnityEngine.Object.Destroy(mesh);
}
catch
{
// ignored
}
}
UnityEngine.Object.Destroy(go);
yield return null;
if (clearGc)
{
yield return CoFreeResources();
}
}
public static void ClearGarbageCollector()
{
Coroutines.Start(CoFreeResources());
}
private static IEnumerator CoFreeResources()
{
yield return Resources.UnloadUnusedAssets();
GC.Collect(0, GCCollectionMode.Forced, blocking: true);
GC.WaitForPendingFinalizers();
GC.Collect(0, GCCollectionMode.Forced, blocking: true);
}
/// <summary> /// <summary>
/// Gets a cache of player's vote data components to improve performance. /// Gets a cache of player's vote data components to improve performance.
+1 -1
View File
@@ -539,7 +539,7 @@ public static class Helpers
} }
/// <summary> /// <summary>
/// Returns whether the role is blacklisted from appearing and spawning. (Only applicable to vanilla roles) /// Returns whether the role is blacklisted from appearing and spawning. Only applicable to vanilla roles.
/// </summary> /// </summary>
/// <param name="role">The role to check.</param> /// <param name="role">The role to check.</param>
/// <returns>The role's blacklist status.</returns> /// <returns>The role's blacklist status.</returns>