Load Ammo context menu in raid
This commit is contained in:
		
							
								
								
									
										81
									
								
								Patches/LoadAmmoInRaidPatches.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								Patches/LoadAmmoInRaidPatches.cs
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
using BepInEx;
 | 
					using BepInEx;
 | 
				
			||||||
using Comfort.Common;
 | 
					using Comfort.Common;
 | 
				
			||||||
using EFT;
 | 
					using EFT;
 | 
				
			||||||
 | 
					using UIFixes.Patches;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace UIFixes
 | 
					namespace UIFixes
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -48,6 +49,7 @@ namespace UIFixes
 | 
				
			|||||||
            KeepWindowsOnScreenPatches.Enable();
 | 
					            KeepWindowsOnScreenPatches.Enable();
 | 
				
			||||||
            new ContextMenuShortcutPatch().Enable();
 | 
					            new ContextMenuShortcutPatch().Enable();
 | 
				
			||||||
            new OpenSortingTablePatch().Enable();
 | 
					            new OpenSortingTablePatch().Enable();
 | 
				
			||||||
 | 
					            LoadAmmoInRaidPatches.Enable();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static bool InRaid()
 | 
					        public static bool InRaid()
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								Settings.cs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Settings.cs
									
									
									
									
									
								
							@@ -72,6 +72,7 @@ namespace UIFixes
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // In Raid
 | 
					        // In Raid
 | 
				
			||||||
        public static ConfigEntry<bool> RemoveDisabledActions { get; set; }
 | 
					        public static ConfigEntry<bool> RemoveDisabledActions { get; set; }
 | 
				
			||||||
 | 
					        public static ConfigEntry<bool> EnableLoadAmmo { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Flea Market
 | 
					        // Flea Market
 | 
				
			||||||
        public static ConfigEntry<bool> EnableFleaHistory { get; set; }
 | 
					        public static ConfigEntry<bool> EnableFleaHistory { get; set; }
 | 
				
			||||||
@@ -388,6 +389,15 @@ namespace UIFixes
 | 
				
			|||||||
                    null,
 | 
					                    null,
 | 
				
			||||||
                    new ConfigurationManagerAttributes { })));
 | 
					                    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
 | 
					            // Flea Market
 | 
				
			||||||
            configEntries.Add(EnableFleaHistory = config.Bind(
 | 
					            configEntries.Add(EnableFleaHistory = config.Bind(
 | 
				
			||||||
                FleaMarketSection,
 | 
					                FleaMarketSection,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,6 +36,9 @@
 | 
				
			|||||||
	<Reference Include="Comfort.Unity">
 | 
						<Reference Include="Comfort.Unity">
 | 
				
			||||||
	  <HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\Comfort.Unity.dll</HintPath>
 | 
						  <HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\Comfort.Unity.dll</HintPath>
 | 
				
			||||||
	</Reference>
 | 
						</Reference>
 | 
				
			||||||
 | 
						<Reference Include="CommonExtensions">
 | 
				
			||||||
 | 
						  <HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\CommonExtensions.dll</HintPath>
 | 
				
			||||||
 | 
						</Reference>
 | 
				
			||||||
	<Reference Include="ItemComponent.Types">
 | 
						<Reference Include="ItemComponent.Types">
 | 
				
			||||||
	  <HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\ItemComponent.Types.dll</HintPath>
 | 
						  <HintPath>$(PathToSPT)\EscapeFromTarkov_Data\Managed\ItemComponent.Types.dll</HintPath>
 | 
				
			||||||
	</Reference>
 | 
						</Reference>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user