diff --git a/GlobalUsings.cs b/GlobalUsings.cs index c8d0597..443870f 100644 --- a/GlobalUsings.cs +++ b/GlobalUsings.cs @@ -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; diff --git a/Patches/SwapPatches.cs b/Patches/SwapPatches.cs index af16c2d..9869e1e 100644 --- a/Patches/SwapPatches.cs +++ b/Patches/SwapPatches.cs @@ -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