From 0fcbf2479e02211ccddf816afb703c83d9870c44 Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:35:11 -0700 Subject: [PATCH] Allow insure on inner items --- Patches/ContextMenuPatches.cs | 33 ++++++++++++++++++++++++++++++++ Patches/LoadAmmoInRaidPatches.cs | 5 +---- R.cs | 19 ++++++++++++++++-- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Patches/ContextMenuPatches.cs b/Patches/ContextMenuPatches.cs index 04d66e5..b500f9e 100644 --- a/Patches/ContextMenuPatches.cs +++ b/Patches/ContextMenuPatches.cs @@ -1,5 +1,6 @@ using Aki.Reflection.Patching; using Aki.Reflection.Utils; +using Comfort.Common; using EFT.InventoryLogic; using EFT.UI; using HarmonyLib; @@ -70,6 +71,8 @@ namespace UIFixes new SniffInteractionButtonCreationPatch().Enable(); new ChangeInteractionButtonCreationPatch().Enable(); + + new EnableInsureInnerItemsPatch().Enable(); } public class DeclareSubInteractionsInventoryPatch : ModulePatch @@ -243,5 +246,35 @@ namespace UIFixes CreatedButtonInteractionId = null; } } + + public class EnableInsureInnerItemsPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(R.ContextMenuHelper.Type, "IsInteractive"); + } + + [PatchPrefix] + public static bool Prefix(object __instance, EItemInfoButton button, ref IResult __result, Item ___item_0) + { + if (button != EItemInfoButton.Insure) + { + return true; + } + + InsuranceCompanyClass insurance = new R.ContextMenuHelper(__instance).InsuranceCompany; + ItemClass itemClass = ItemClass.FindOrCreate(___item_0); + IEnumerable insurableItems = insurance.GetItemChildren(itemClass).Flatten(insurance.GetItemChildren).Concat([itemClass]) + .Where(i => insurance.ItemTypeAvailableForInsurance(i) && !insurance.InsuredItems.Contains(i)); + + if (insurableItems.Any()) + { + __result = SuccessfulResult.New; + return false; + } + + return true; + } + } } } diff --git a/Patches/LoadAmmoInRaidPatches.cs b/Patches/LoadAmmoInRaidPatches.cs index e1453a1..834ba16 100644 --- a/Patches/LoadAmmoInRaidPatches.cs +++ b/Patches/LoadAmmoInRaidPatches.cs @@ -1,10 +1,8 @@ using Aki.Reflection.Patching; -using Aki.Reflection.Utils; using EFT.InventoryLogic; using EFT.UI; using EFT.UI.DragAndDrop; using HarmonyLib; -using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -24,8 +22,7 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = PatchConstants.EftTypes.Single(t => t.GetProperty("IsOwnedByPlayer") != null); - return AccessTools.Method(type, "IsActive"); + return AccessTools.Method(R.ContextMenuHelper.Type, "IsActive"); } [PatchPrefix] diff --git a/R.cs b/R.cs index ffda21b..10a5d08 100644 --- a/R.cs +++ b/R.cs @@ -57,6 +57,7 @@ namespace UIFixes GridWindow.InitTypes(); GridSortPanel.InitTypes(); RepairStrategy.InitTypes(); + ContextMenuHelper.InitTypes(); } public abstract class Wrapper(object value) @@ -659,12 +660,26 @@ namespace UIFixes public bool BrokenItemError() => (bool)BrokenItemErrorMethod.Invoke(Value, []); public bool IsNoCorrespondingArea() => (bool)IsNoCorrespondingAreaMethod.Invoke(Value, []); } + + public class ContextMenuHelper(object value) : Wrapper(value) + { + public static Type Type { get; private set; } + private static FieldInfo InsuranceCompanyField; + + public static void InitTypes() + { + Type = PatchConstants.EftTypes.Single(t => t.GetProperty("IsOwnedByPlayer") != null); // GClass3052 + InsuranceCompanyField = AccessTools.GetDeclaredFields(Type).Single(f => f.FieldType == typeof(InsuranceCompanyClass)); + } + + public InsuranceCompanyClass InsuranceCompany { get { return (InsuranceCompanyClass)InsuranceCompanyField.GetValue(Value); } } + } } public static class RExtentensions { - public static R.UIElement R(this UIElement value) => new R.UIElement(value); - public static R.UIInputNode R(this UIInputNode value) => new R.UIInputNode(value); + public static R.UIElement R(this UIElement value) => new(value); + public static R.UIInputNode R(this UIInputNode value) => new(value); public static R.ProductionPanel R(this ProductionPanel value) => new(value); public static R.AreaScreenSubstrate R(this AreaScreenSubstrate value) => new(value); public static R.ItemSpecificationPanel R(this ItemSpecificationPanel value) => new(value);