Load Ammo context menu in raid

This commit is contained in:
Tyfon
2024-06-10 02:21:45 -07:00
parent c643b12455
commit 613130f537
4 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
using Aki.Reflection.Patching;
using EFT.InventoryLogic;
using EFT.UI;
using EFT.UI.DragAndDrop;
using HarmonyLib;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace UIFixes.Patches
{
public class LoadAmmoInRaidPatches
{
public static void Enable()
{
new EnableContextMenuPatch().Enable();
new SlowLoadingPatch().Enable();
}
public class EnableContextMenuPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(GClass3052), nameof(GClass3052.IsActive));
}
[PatchPrefix]
public static bool Prefix(GClass3052 __instance, EItemInfoButton button, ref bool __result, Item ___item_0)
{
if (button != EItemInfoButton.LoadAmmo || !Plugin.InRaid() || !Settings.EnableLoadAmmo.Value)
{
return true;
}
__result = MagazineBuildClass.TryFindPresetSource(___item_0).Succeeded;
return false;
}
}
public class SlowLoadingPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.LoadAmmoByType));
}
// This code is a mix of ItemUiContext.LoadAmmoByType, but then switching over to GridView.AcceptItem
[PatchPrefix]
public static bool Prefix(ItemUiContext __instance, MagazineClass magazine, string ammoTemplateId, ref Task __result)
{
if (!Plugin.InRaid() || !Settings.EnableLoadAmmo.Value)
{
return true;
}
InventoryControllerClass inventoryController = __instance.R().InventoryController;
EquipmentClass equipment = inventoryController.Inventory.Equipment;
List<BulletClass> ammo = [];
equipment.GetAllAssembledItems(ammo);
// Just do the first stack
BulletClass bullets = ammo.Where(a => a.TemplateId == ammoTemplateId && a.Parent.Container is not Slot)
.OrderBy(a => a.SpawnedInSession)
.ThenBy(a => a.StackObjectsCount)
.FirstOrDefault();
if (bullets != null)
{
int count = GridView.smethod_0(magazine, bullets);
inventoryController.LoadMagazine(bullets, magazine, count, false).HandleExceptions();
}
// The calling code in this instance doesn't do anything with the task, but it does await it, so if we don't return a Task it nullrefs
__result = Task.CompletedTask;
return false;
}
}
}
}

View File

@@ -1,6 +1,7 @@
using BepInEx;
using Comfort.Common;
using EFT;
using UIFixes.Patches;
namespace UIFixes
{
@@ -48,6 +49,7 @@ namespace UIFixes
KeepWindowsOnScreenPatches.Enable();
new ContextMenuShortcutPatch().Enable();
new OpenSortingTablePatch().Enable();
LoadAmmoInRaidPatches.Enable();
}
public static bool InRaid()

View File

@@ -72,6 +72,7 @@ namespace UIFixes
// In Raid
public static ConfigEntry<bool> RemoveDisabledActions { get; set; }
public static ConfigEntry<bool> EnableLoadAmmo { get; set; }
// Flea Market
public static ConfigEntry<bool> EnableFleaHistory { get; set; }
@@ -388,6 +389,15 @@ namespace UIFixes
null,
new ConfigurationManagerAttributes { })));
configEntries.Add(EnableLoadAmmo = config.Bind(
InRaidSection,
"Enable Load Ammo Context Menu",
true,
new ConfigDescription(
"Allows ammo to be loaded through the magazine context menu",
null,
new ConfigurationManagerAttributes { })));
// Flea Market
configEntries.Add(EnableFleaHistory = config.Bind(
FleaMarketSection,

View File

@@ -36,6 +36,9 @@
<Reference Include="Comfort.Unity">
<HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\Comfort.Unity.dll</HintPath>
</Reference>
<Reference Include="CommonExtensions">
<HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\CommonExtensions.dll</HintPath>
</Reference>
<Reference Include="ItemComponent.Types">
<HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\ItemComponent.Types.dll</HintPath>
</Reference>