Dispose ItemContextClass created in InspectWindowUpdateStatsOnSwapPatch to prevent stack overflow from BSG's buggy implementation

This commit is contained in:
Tyfon
2024-05-20 17:13:23 -07:00
parent b8f85c06c6
commit 661ecb10a2

View File

@@ -35,6 +35,7 @@ namespace UIFixes
public static void Enable() public static void Enable()
{ {
new DetectSwapSourceContainerPatch().Enable(); new DetectSwapSourceContainerPatch().Enable();
new CleanupSwapSourceContainerPatch().Enable();
new GridViewCanAcceptSwapPatch().Enable(); new GridViewCanAcceptSwapPatch().Enable();
new DetectGridHighlightPrecheckPatch().Enable(); new DetectGridHighlightPrecheckPatch().Enable();
new DetectSlotHighlightPrecheckPatch().Enable(); new DetectSlotHighlightPrecheckPatch().Enable();
@@ -150,6 +151,20 @@ namespace UIFixes
} }
} }
public class CleanupSwapSourceContainerPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(ItemView), nameof(ItemView.OnEndDrag));
}
[PatchPrefix]
public static void Prefix(ItemView __instance)
{
SourceContainer = null;
}
}
public class GridViewCanAcceptSwapPatch : ModulePatch public class GridViewCanAcceptSwapPatch : ModulePatch
{ {
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
@@ -468,13 +483,20 @@ namespace UIFixes
[PatchPostfix] [PatchPostfix]
public static void Postfix(DraggedItemView __instance, ItemContextAbstractClass itemUnderCursor) public static void Postfix(DraggedItemView __instance, ItemContextAbstractClass itemUnderCursor)
{ {
if (itemUnderCursor?.Item == __instance.Item)
{
return;
}
if (SourceContainer is Component sourceComponent) if (SourceContainer is Component sourceComponent)
{ {
ItemSpecificationPanel panel = sourceComponent.GetComponentInParent<ItemSpecificationPanel>(); ItemSpecificationPanel panel = sourceComponent.GetComponentInParent<ItemSpecificationPanel>();
if (panel != null) if (panel != null)
{ {
Slot slot = new R.SlotItemAddress(__instance.ItemAddress).Slot; Slot slot = new R.SlotItemAddress(__instance.ItemAddress).Slot;
ItemContextClass itemUnderCursorContext = itemUnderCursor != null ? new ItemContextClass(itemUnderCursor, ItemRotation.Horizontal) : null;
// ItemContextClass must be disposed after using, or its buggy implementation causes an infinite loop / stack overflow
using ItemContextClass itemUnderCursorContext = itemUnderCursor != null ? new ItemContextClass(itemUnderCursor, ItemRotation.Horizontal) : null;
panel.method_15(slot, itemUnderCursorContext); panel.method_15(slot, itemUnderCursorContext);
} }
} }