Swap magazines when dragging mag over weapon

This commit is contained in:
Tyfon
2024-09-15 15:45:39 -07:00
parent 0cc567a704
commit f3062b2d40
2 changed files with 35 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ global using MoveSameSpaceError = InteractionsHandlerClass.GClass3353;
global using NotModdableInRaidError = GClass3321;
global using MultitoolNeededError = GClass3322;
global using ModVitalPartInRaidError = GClass3323;
global using SlotNotEmptyError = EFT.InventoryLogic.Slot.GClass3339;
// Operations
global using ItemOperation = GStruct413;

View File

@@ -43,6 +43,7 @@ public static class SwapPatches
new DetectGridHighlightPrecheckPatch().Enable();
new DetectSlotHighlightPrecheckPatch().Enable();
new SlotCanAcceptSwapPatch().Enable();
new WeaponApplyPatch().Enable();
new DetectFilterForSwapPatch().Enable();
new FixNoGridErrorPatch().Enable();
new SwapOperationRaiseEventsPatch().Enable();
@@ -408,6 +409,39 @@ public static class SwapPatches
}
}
public class WeaponApplyPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(Weapon), nameof(Weapon.Apply));
}
// Allow dragging magazines onto weapons and do a mag swap
[PatchPostfix]
public static void Postfix(Weapon __instance, TraderControllerClass itemController, Item item, bool simulate, ref ItemOperation __result)
{
if (!Settings.SwapItems.Value || MultiSelect.Active)
{
return;
}
// Check if the source container is a non-interactable GridView. Specifically for StashSearch, but may exist in other scenarios?
if (SourceContainer != null && SourceContainer is GridView && new R.GridView(SourceContainer).NonInteractable)
{
return;
}
if (__result.Succeeded || item is not MagazineClass || __result.Error is not SlotNotEmptyError)
{
return;
}
Slot magazineSlot = __instance.GetMagazineSlot();
__result = InteractionsHandlerClass.Swap(item, magazineSlot.ContainedItem.Parent, magazineSlot.ContainedItem, item.Parent, itemController, simulate);
}
}
// The patched method here is called when iterating over all slots to highlight ones that the dragged item can interact with
// Since swap has no special highlight, I just skip the patch here (minor perf savings, plus makes debugging a million times easier)
public class DetectGridHighlightPrecheckPatch : ModulePatch