Re-enable ammobox unload in-place, fixed for fika

This commit is contained in:
Tyfon
2024-07-25 12:25:53 -07:00
parent 458a2df57c
commit e2d51f25d9
3 changed files with 124 additions and 106 deletions

View File

@@ -1,5 +1,6 @@
using Comfort.Common; using Comfort.Common;
using EFT; using EFT;
using EFT.Communications;
using EFT.InventoryLogic; using EFT.InventoryLogic;
using EFT.UI; using EFT.UI;
using HarmonyLib; using HarmonyLib;
@@ -15,8 +16,6 @@ namespace UIFixes;
public static class UnloadAmmoPatches public static class UnloadAmmoPatches
{ {
//private static UnloadAmmoBoxState UnloadState = null;
public static void Enable() public static void Enable()
{ {
new TradingPlayerPatch().Enable(); new TradingPlayerPatch().Enable();
@@ -24,8 +23,7 @@ public static class UnloadAmmoPatches
new UnloadScavTransferPatch().Enable(); new UnloadScavTransferPatch().Enable();
new NoScavStashPatch().Enable(); new NoScavStashPatch().Enable();
//new UnloadAmmoBoxPatch().Enable(); new UnloadAmmoBoxPatch().Enable();
//new QuickFindUnloadAmmoBoxPatch().Enable();
} }
public class TradingPlayerPatch : ModulePatch public class TradingPlayerPatch : ModulePatch
@@ -106,108 +104,128 @@ public static class UnloadAmmoPatches
} }
} }
// public class UnloadAmmoBoxPatch : ModulePatch public class UnloadAmmoBoxPatch : ModulePatch
// { {
// protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
// { {
// return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.UnloadAmmo)); return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.UnloadAmmo));
// } }
// [PatchPrefix] [PatchPrefix]
// public static void Prefix(Item item) public static bool Prefix(Item item, ref Task __result, InventoryContainerClass ___inventoryControllerClass)
// { {
// if (Settings.UnloadAmmoBoxInPlace.Value && item is AmmoBox) if (!Settings.UnloadAmmoBoxInPlace.Value || item is not AmmoBox ammoBox)
// { {
// UnloadState = new(); return true;
// } }
// }
// [PatchPostfix] if (ammoBox.Cartridges.Last is not BulletClass lastBullet)
// public static async void Postfix(Task __result) {
// { return true;
// if (!Settings.UnloadAmmoBoxInPlace.Value) }
// {
// return;
// }
// await __result; __result = UnloadAmmoBox(ammoBox, ___inventoryControllerClass);
// UnloadState = null; return false;
// } }
// }
// public class QuickFindUnloadAmmoBoxPatch : ModulePatch private static async Task UnloadAmmoBox(AmmoBox ammoBox, InventoryControllerClass inventoryController)
// { {
// protected override MethodBase GetTargetMethod() BulletClass lastBullet = ammoBox.Cartridges.Last as BulletClass;
// { IEnumerable<LootItemClass> containers = inventoryController.Inventory.Stash != null ?
// return AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.QuickFindAppropriatePlace)); [inventoryController.Inventory.Equipment, inventoryController.Inventory.Stash] :
// } [inventoryController.Inventory.Equipment];
// [PatchPrefix] // Explicitly add the current parent before its moved. IgnoreParentItem will be sent along later
// public static void Prefix(Item item, TraderControllerClass controller, ref IEnumerable<LootItemClass> targets, ref InteractionsHandlerClass.EMoveItemOrder order) containers = containers.Prepend(ammoBox.Parent.Container.ParentItem as LootItemClass);
// {
// if (UnloadState == null)
// {
// return;
// }
// if (item.Parent.Container.ParentItem is not AmmoBox box) // Move the box to a temporary stash so it can unload in place
// { TraderControllerClass tempController = GetTempController();
// return; StashClass tempStash = tempController.RootItem as StashClass;
// } var moveOperation = InteractionsHandlerClass.Move(ammoBox, tempStash.Grid.FindLocationForItem(ammoBox), inventoryController, true);
if (moveOperation.Succeeded)
{
IResult networkResult = await inventoryController.TryRunNetworkTransaction(moveOperation);
if (networkResult.Failed)
{
moveOperation = new GClass3370(networkResult.Error);
}
// // Ammo boxes with multiple stacks will loop through this code, so we only want to move the box once // Surprise! The operation is STILL not done. <insert enraged, profanity-laced, unhinged anti-BSG rant here>
// if (UnloadState.initialized) await Task.Yield();
// { }
// order = UnloadState.order;
// targets = UnloadState.targets;
// }
// else
// {
// // Have to do this for them, since the calls to get parent will be wrong once we move the box
// if (!order.HasFlag(InteractionsHandlerClass.EMoveItemOrder.IgnoreItemParent))
// {
// LootItemClass parent = (item.GetNotMergedParent() as LootItemClass) ?? (item.GetRootMergedItem() as EquipmentClass);
// if (parent != null)
// {
// UnloadState.targets = targets = order.HasFlag(InteractionsHandlerClass.EMoveItemOrder.PrioritizeParent) ?
// parent.ToEnumerable().Concat(targets).Distinct() :
// targets.Concat(parent.ToEnumerable()).Distinct();
// }
// UnloadState.order = order |= InteractionsHandlerClass.EMoveItemOrder.IgnoreItemParent; if (moveOperation.Failed)
// } {
NotificationManagerClass.DisplayWarningNotification(moveOperation.Error.ToString(), ENotificationDurationType.Default);
return;
}
// var operation = InteractionsHandlerClass.Move(box, UnloadState.fakeStash.Grid.FindLocationForItem(box), controller, false); bool unloadedAny = false;
// operation.Value.RaiseEvents(controller, CommandStatus.Begin); ItemOperation operation = default;
// operation.Value.RaiseEvents(controller, CommandStatus.Succeed); for (BulletClass bullet = lastBullet; bullet != null; bullet = ammoBox.Cartridges.Last as BulletClass)
{
operation = InteractionsHandlerClass.QuickFindAppropriatePlace(
bullet,
inventoryController,
containers,
InteractionsHandlerClass.EMoveItemOrder.UnloadAmmo | InteractionsHandlerClass.EMoveItemOrder.IgnoreItemParent,
true);
// UnloadState.initialized = true; if (operation.Failed)
// } {
// } break;
// } }
// public class UnloadAmmoBoxState unloadedAny = true;
// {
// public StashClass fakeStash;
// public TraderControllerClass fakeController;
// public bool initialized; IResult networkResult = await inventoryController.TryRunNetworkTransaction(operation);
// public InteractionsHandlerClass.EMoveItemOrder order; if (networkResult.Failed)
// public IEnumerable<LootItemClass> targets; {
operation = new GClass3370(networkResult.Error);
break;
}
// public UnloadAmmoBoxState() if (operation.Value is GInterface343 raisable)
// { {
// if (Plugin.InRaid()) raisable.TargetItem.RaiseRefreshEvent(false, true);
// { }
// fakeStash = Singleton<GameWorld>.Instance.R().Stash;
// }
// else
// {
// fakeStash = (StashClass)Singleton<ItemFactory>.Instance.CreateItem("FakeStash", "566abbc34bdc2d92178b4576", null);
// var profile = PatchConstants.BackEndSession.Profile; // Surprise! The operation STILL IS NOT DONE. <insert enraged, profanity-laced, unhinged anti-BSG rant here>
// fakeController = new(fakeStash, profile.ProfileId, profile.Nickname); await Task.Yield();
// } }
// }
// } if (unloadedAny && Singleton<GUISounds>.Instantiated)
{
Singleton<GUISounds>.Instance.PlayItemSound(lastBullet.ItemSound, EInventorySoundType.drop, false);
}
if (operation.Succeeded)
{
tempController.DestroyItem(ammoBox);
}
else
{
ammoBox.RaiseRefreshEvent(false, true);
}
if (operation.Failed)
{
NotificationManagerClass.DisplayWarningNotification(operation.Error.ToString(), ENotificationDurationType.Default);
}
}
private static TraderControllerClass GetTempController()
{
if (Plugin.InRaid())
{
return Singleton<GameWorld>.Instance.R().TraderController;
}
else
{
var profile = PatchConstants.BackEndSession.Profile;
StashClass fakeStash = (StashClass)Singleton<ItemFactory>.Instance.CreateItem("FakeStash", "566abbc34bdc2d92178b4576", null);
return new(fakeStash, profile.ProfileId, profile.Nickname);
}
}
}
} }

6
R.cs
View File

@@ -872,15 +872,15 @@ public static class R
public class GameWorld(object value) : Wrapper(value) public class GameWorld(object value) : Wrapper(value)
{ {
public static Type Type { get; private set; } public static Type Type { get; private set; }
private static FieldInfo StashField; private static FieldInfo TraderControllerField;
public static void InitTypes() public static void InitTypes()
{ {
Type = typeof(EFT.GameWorld); Type = typeof(EFT.GameWorld);
StashField = AccessTools.Field(Type, "stashClass"); TraderControllerField = AccessTools.Field(Type, "traderControllerClass");
} }
public StashClass Stash { get { return (StashClass)StashField.GetValue(Value); } } public TraderControllerClass TraderController { get { return (TraderControllerClass)TraderControllerField.GetValue(Value); } }
} }
} }

View File

@@ -96,7 +96,7 @@ internal class Settings
public static ConfigEntry<bool> SwapItems { get; set; } public static ConfigEntry<bool> SwapItems { get; set; }
public static ConfigEntry<bool> SwapMags { get; set; } public static ConfigEntry<bool> SwapMags { get; set; }
public static ConfigEntry<bool> AlwaysSwapMags { get; set; } public static ConfigEntry<bool> AlwaysSwapMags { get; set; }
//public static ConfigEntry<bool> UnloadAmmoBoxInPlace { get; set; } // Advanced public static ConfigEntry<bool> UnloadAmmoBoxInPlace { get; set; } // Advanced
public static ConfigEntry<bool> SwapImpossibleContainers { get; set; } public static ConfigEntry<bool> SwapImpossibleContainers { get; set; }
public static ConfigEntry<bool> ReorderGrids { get; set; } public static ConfigEntry<bool> ReorderGrids { get; set; }
public static ConfigEntry<bool> SynchronizeStashScrolling { get; set; } public static ConfigEntry<bool> SynchronizeStashScrolling { get; set; }
@@ -473,14 +473,14 @@ internal class Settings
null, null,
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
// configEntries.Add(UnloadAmmoBoxInPlace = config.Bind( configEntries.Add(UnloadAmmoBoxInPlace = config.Bind(
// InventorySection, InventorySection,
// "Unload Ammo Boxes In-Place", "Unload Ammo Boxes In-Place",
// true, true,
// new ConfigDescription( new ConfigDescription(
// "Whether to unload ammo boxes in-place, otherwise there needs to be free space somewhere", "Whether to unload ammo boxes in-place, otherwise there needs to be free space somewhere",
// null, null,
// new ConfigurationManagerAttributes { IsAdvanced = true }))); new ConfigurationManagerAttributes { IsAdvanced = true })));
configEntries.Add(SwapImpossibleContainers = config.Bind( configEntries.Add(SwapImpossibleContainers = config.Bind(
InventorySection, InventorySection,