Switch to prefix patch

This commit is contained in:
CactusPie 2023-11-20 11:28:27 +01:00
parent 92ed9b1748
commit 087714d394

View File

@ -16,8 +16,8 @@ namespace CactusPie.ContainerQuickLoot
return method; return method;
} }
[PatchPostfix] [PatchPrefix]
public static void PatchPostfix( public static bool PatchPrefix(
ref GStruct375<GInterface275> __result, ref GStruct375<GInterface275> __result,
object __instance, object __instance,
Item item, Item item,
@ -26,72 +26,76 @@ namespace CactusPie.ContainerQuickLoot
GClass2585.EMoveItemOrder order, GClass2585.EMoveItemOrder order,
bool simulate) bool simulate)
{ {
if (order == GClass2585.EMoveItemOrder.MoveToAnotherSide) // We only execute this for ctrl+click move
if (order != GClass2585.EMoveItemOrder.MoveToAnotherSide)
{ {
GameWorld gameWorld = Singleton<GameWorld>.Instance; return true;
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<GClass2597> moveResult = GClass2585.Move(item, location, controller, simulate);
if (moveResult.Failed)
{
return;
}
if (!moveResult.Value.ItemsDestroyRequired)
{
__result = moveResult.Cast<GClass2597, GInterface275>();
}
return;
}
} }
GameWorld gameWorld = Singleton<GameWorld>.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<GClass2597> moveResult = GClass2585.Move(item, location, controller, simulate);
if (moveResult.Failed)
{
return true;
}
if (!moveResult.Value.ItemsDestroyRequired)
{
__result = moveResult.Cast<GClass2597, GInterface275>();
}
return false;
}
return true;
} }
private static Player GetLocalPlayerFromWorld(GameWorld gameWorld) private static Player GetLocalPlayerFromWorld(GameWorld gameWorld)