From 087714d394e5a411360218e52715540fb7285dee Mon Sep 17 00:00:00 2001 From: CactusPie Date: Mon, 20 Nov 2023 11:28:27 +0100 Subject: [PATCH] Switch to prefix patch --- .../QuickTransferPatch.cs | 136 +++++++++--------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/src/CactusPie.ContainerQuickLoot/QuickTransferPatch.cs b/src/CactusPie.ContainerQuickLoot/QuickTransferPatch.cs index a9257ba..b06c450 100644 --- a/src/CactusPie.ContainerQuickLoot/QuickTransferPatch.cs +++ b/src/CactusPie.ContainerQuickLoot/QuickTransferPatch.cs @@ -16,8 +16,8 @@ namespace CactusPie.ContainerQuickLoot return method; } - [PatchPostfix] - public static void PatchPostfix( + [PatchPrefix] + public static bool PatchPrefix( ref GStruct375 __result, object __instance, Item item, @@ -26,72 +26,76 @@ namespace CactusPie.ContainerQuickLoot GClass2585.EMoveItemOrder order, bool simulate) { - if (order == GClass2585.EMoveItemOrder.MoveToAnotherSide) + // We only execute this for ctrl+click move + if (order != GClass2585.EMoveItemOrder.MoveToAnotherSide) { - GameWorld gameWorld = Singleton.Instance; - - if (gameWorld == null) - { - return; - } - - Player player = GetLocalPlayerFromWorld(gameWorld); - var inventory = (Inventory)typeof(Player).GetProperty("Inventory", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(player); - - if (inventory == null) - { - return; - } - - ContainerCollection containerCollection = null; - - Item targetContainer = inventory.Equipment.GetAllItems().FirstOrDefault(x => - x.IsContainer && - x.TryGetItemComponent(out TagComponent tagComponent) && - tagComponent.Name.Contains("@loot") && - (containerCollection = x as ContainerCollection) != null && - containerCollection.Containers.Any(y => y.CanAccept(item))); - - if (targetContainer == null || containerCollection == null) - { - return; - } - - foreach (IContainer collectionContainer in containerCollection.Containers) - { - var container = collectionContainer as GClass2318; - - if (container == null) - { - return; - } - - // ReSharper disable once PossibleMultipleEnumeration - if (!(targets.SingleOrDefaultWithoutException() is EquipmentClass)) - { - continue; - } - - GClass2580 location = container.FindLocationForItem(item); - if (location == null) - { - continue; - } - - GStruct375 moveResult = GClass2585.Move(item, location, controller, simulate); - if (moveResult.Failed) - { - return; - } - - if (!moveResult.Value.ItemsDestroyRequired) - { - __result = moveResult.Cast(); - } - - return; - } + return true; } + + GameWorld gameWorld = Singleton.Instance; + + // If gameWorld is null that means the game is currently not in progress, for instance you're in your hideout + if (gameWorld == null) + { + return true; + } + + Player player = GetLocalPlayerFromWorld(gameWorld); + var inventory = (Inventory)typeof(Player).GetProperty("Inventory", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(player); + + if (inventory == null) + { + return true; + } + + ContainerCollection containerCollection = null; + + Item targetContainer = inventory.Equipment.GetAllItems().FirstOrDefault(x => + x.IsContainer && + x.TryGetItemComponent(out TagComponent tagComponent) && + tagComponent.Name.Contains("@loot") && + (containerCollection = x as ContainerCollection) != null && + containerCollection.Containers.Any(y => y.CanAccept(item))); + + if (targetContainer == null || containerCollection == null) + { + return true; + } + + foreach (IContainer collectionContainer in containerCollection.Containers) + { + if (!(collectionContainer is GClass2318 container)) + { + return true; + } + + // ReSharper disable once PossibleMultipleEnumeration + if (!(targets.SingleOrDefaultWithoutException() is EquipmentClass)) + { + continue; + } + + GClass2580 location = container.FindLocationForItem(item); + if (location == null) + { + continue; + } + + GStruct375 moveResult = GClass2585.Move(item, location, controller, simulate); + if (moveResult.Failed) + { + return true; + } + + if (!moveResult.Value.ItemsDestroyRequired) + { + __result = moveResult.Cast(); + } + + return false; + } + + return true; } private static Player GetLocalPlayerFromWorld(GameWorld gameWorld)