Mod weapons in hands/in raid; Move add offer setting

This commit is contained in:
Tyfon
2024-08-21 15:39:27 -07:00
parent 0d7ee4de68
commit 7bdd98ecd1
5 changed files with 389 additions and 122 deletions

22
Extensions.cs Normal file
View File

@@ -0,0 +1,22 @@
using System.Linq;
using EFT.InventoryLogic;
namespace UIFixes;
public static class Extensions
{
public static Item GetRootItemNotEquipment(this Item item)
{
return item.GetAllParentItemsAndSelf(true).LastOrDefault(i => i is not EquipmentClass) ?? item;
}
public static Item GetRootItemNotEquipment(this ItemAddress itemAddress)
{
if (itemAddress.Container == null || itemAddress.Container.ParentItem == null)
{
return null;
}
return itemAddress.Container.ParentItem.GetRootItemNotEquipment();
}
}

View File

@@ -39,6 +39,9 @@ global using NoPossibleActionsError = GClass3317;
global using CannotSortError = GClass3325; global using CannotSortError = GClass3325;
global using FailedToSortError = GClass3326; global using FailedToSortError = GClass3326;
global using MoveSameSpaceError = InteractionsHandlerClass.GClass3353; global using MoveSameSpaceError = InteractionsHandlerClass.GClass3353;
global using NotModdableInRaidError = GClass3321;
global using MultitoolNeededError = GClass3322;
global using ModVitalPartInRaidError = GClass3323;
// Operations // Operations
global using ItemOperation = GStruct413; global using ItemOperation = GStruct413;

View File

@@ -263,11 +263,6 @@ public static class TacticalBindsPatches
Quickbind.SetType(index, Quickbind.ItemType.Other); Quickbind.SetType(index, Quickbind.ItemType.Other);
} }
private static Item GetRootItemNotEquipment(this Item item)
{
return item.GetAllParentItemsAndSelf(true).LastOrDefault(i => i is not EquipmentClass) ?? item;
}
} }
public static class Quickbind public static class Quickbind

View File

@@ -1,16 +1,23 @@
using Diz.LanguageExtensions; using Comfort.Common;
using EFT;
using EFT.InventoryLogic; using EFT.InventoryLogic;
using EFT.UI.DragAndDrop;
using HarmonyLib; using HarmonyLib;
using SPT.Reflection.Patching; using SPT.Reflection.Patching;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEngine;
namespace UIFixes; namespace UIFixes;
public static class WeaponModdingPatches public static class WeaponModdingPatches
{ {
private const string MultitoolId = "544fb5454bdc2df8738b456a";
private static readonly string[] EquippedSlots = ["FirstPrimaryWeapon", "SecondPrimaryWeapon", "Holster"];
public static void Enable() public static void Enable()
{ {
new ResizePatch().Enable(); new ResizePatch().Enable();
@@ -18,8 +25,13 @@ public static class WeaponModdingPatches
new ResizeOperationRollbackPatch().Enable(); new ResizeOperationRollbackPatch().Enable();
new MoveBeforeNetworkTransactionPatch().Enable(); new MoveBeforeNetworkTransactionPatch().Enable();
//new ModdingMoveToSortingTablePatch().Enable(); new ModEquippedPatch().Enable();
//new PresetMoveToSortingTablePatch().Enable(); new InspectLockedPatch().Enable();
new ModCanBeMovedPatch().Enable();
new ModCanDetachPatch().Enable();
new ModCanApplyPatch().Enable();
new ModRaidModdablePatch().Enable();
new EmptyVitalPartsPatch().Enable();
} }
public class ResizePatch : ModulePatch public class ResizePatch : ModulePatch
@@ -192,124 +204,332 @@ public static class WeaponModdingPatches
} }
} }
// public class ModdingMoveToSortingTablePatch : ModulePatch public class ModEquippedPatch : ModulePatch
// { {
// private static bool InPatch = false; protected override MethodBase GetTargetMethod()
{
// protected override MethodBase GetTargetMethod() return AccessTools.Method(R.ContextMenuHelper.Type, "IsInteractive");
// { }
// return AccessTools.DeclaredMethod(typeof(GClass2848), nameof(GClass2848.Select));
// } // Enable/disable options in the context menu
[PatchPostfix]
// [PatchPostfix] public static void Postfix(EItemInfoButton button, ref IResult __result, Item ___item_0)
// public static void Postfix(GClass2848 __instance, Item item, ItemAddress itemAddress, bool simulate, ref Error error, ref bool __result) {
// { // These two are only visible out of raid, enable them
// if (!Settings.MoveBuildsToSortingTable.Value || InPatch || __result || error is not InteractionsHandlerClass.GClass3363) if (Settings.ModifyEquippedWeapons.Value && (button == EItemInfoButton.Modding || button == EItemInfoButton.EditBuild))
// { {
// return; if (__result.Succeed || !Singleton<BonusController>.Instance.HasBonus(EBonusType.UnlockWeaponModification))
// } {
return;
// // get top level item (weapon) }
// Item rootItem = itemAddress.Container.ParentItem.GetRootMergedItem();
// if (rootItem == null) __result = SuccessfulResult.New;
// { return;
// return; }
// }
// This is surprisingly active in raid? Only enable out of raid.
// // move it to sorting table if (button == EItemInfoButton.Disassemble)
// SortingTableClass sortingTable = __instance.InventoryControllerClass.Inventory.SortingTable; {
// if (sortingTable == null) if (!Plugin.InRaid() && Settings.ModifyEquippedWeapons.Value)
// { {
// return; __result = SuccessfulResult.New;
// } return;
}
// ItemAddressClass sortingTableAddress = sortingTable.Grid.FindLocationForItem(rootItem); }
// if (sortingTableAddress == null)
// { // These are on mods; normally the context menu is disabled so these are individually not disabled
// return; // Need to do the disabling as appropriate
// } if (___item_0 is Mod mod && (button == EItemInfoButton.Uninstall || button == EItemInfoButton.Discard))
{
// var sortingTableMove = InteractionsHandlerClass.Move(rootItem, sortingTableAddress, __instance.InventoryControllerClass, simulate); if (!CanModify(mod, out string error))
// if (sortingTableMove.Failed || sortingTableMove.Value == null) {
// { __result = new FailedResult(error);
// return; return;
// } }
}
// if (simulate) }
// { }
// // Just testing, and it was moveable to sorting table, so assume everything is fine.
// error = null; public class InspectLockedPatch : ModulePatch
// __result = true; {
// return; protected override MethodBase GetTargetMethod()
// } {
return AccessTools.Method(typeof(ModSlotView), nameof(ModSlotView.method_14));
// // Actually selecting it, so do it and then redo the select }
// __instance.InventoryControllerClass.RunNetworkTransaction(sortingTableMove.Value);
// Enable context menu on normally unmoddable slots, maybe keep them gray
// InPatch = true; [PatchPostfix]
// __result = __instance.Select(item, itemAddress, simulate, out error); public static void Postfix(ModSlotView __instance, ref bool ___bool_1, CanvasGroup ____canvasGroup)
// InPatch = false; {
// } // Keep it grayed out and warning text if its not draggable, even if context menu is enabled
// } if (__instance.Slot.ContainedItem is Mod mod && CanModify(mod, out string error))
{
// public class PresetMoveToSortingTablePatch : ModulePatch ___bool_1 = false;
// { ____canvasGroup.alpha = 1f;
// private static bool InPatch = false; }
// protected override MethodBase GetTargetMethod() ____canvasGroup.blocksRaycasts = true;
// { ____canvasGroup.interactable = true;
// return AccessTools.Method(typeof(GClass2849), nameof(GClass2849.method_2)); }
// } }
// [PatchPostfix] public class ModCanBeMovedPatch : ModulePatch
// public static void Postfix( {
// GClass2849 __instance, protected override MethodBase GetTargetMethod()
// Item item, {
// List<GClass2849.Class2174> modsWithSlots, return AccessTools.Method(typeof(Mod), nameof(Mod.CanBeMoved));
// TraderControllerClass itemController, }
// bool simulate,
// ref GStruct416<List<GStruct414<GClass2802>>> __result) // As far as I can tell this never gets called, but hey
// { [PatchPostfix]
// if (!Settings.MoveBuildsToSortingTable.Value || public static void Postfix(Mod __instance, IContainer toContainer, ref GStruct416<bool> __result)
// InPatch || {
// __result.Succeeded || if (__result.Succeeded)
// __result.Error is not InteractionsHandlerClass.GClass3363 || {
// itemController is not InventoryControllerClass inventoryController) return;
// { }
// return;
// } if (!CanModify(__instance, out string itemError))
{
// // move it to sorting table return;
// SortingTableClass sortingTable = inventoryController.Inventory.SortingTable; }
// if (sortingTable == null)
// { if (toContainer is not Slot toSlot || !CanModify(R.SlotItemAddress.Create(toSlot), out string slotError))
// return; {
// } return;
}
// ItemAddressClass sortingTableAddress = sortingTable.Grid.FindLocationForItem(item);
// if (sortingTableAddress == null) __result = true;
// { }
// return; }
// }
public class ModCanDetachPatch : ModulePatch
// var sortingTableMove = InteractionsHandlerClass.Move(item, sortingTableAddress, inventoryController, simulate); // only called with simulate = false {
// if (sortingTableMove.Failed || sortingTableMove.Value == null) private static Type TargetMethodReturnType;
// {
// return; protected override MethodBase GetTargetMethod()
// } {
MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_1));
// InPatch = true; TargetMethodReturnType = method.ReturnType;
// __result = __instance.method_2(item, modsWithSlots, itemController, simulate); return method;
// InPatch = false; }
// if (__result.Succeeded) // This gets invoked when dragging items around between slots
// { [PatchPostfix]
// __result.Value.Prepend(sortingTableMove); public static void Postfix(Item item, ItemAddress to, TraderControllerClass itemController, ref GStruct416<GClass3372> __result)
// } {
// else if (item is not Mod mod)
// { {
// sortingTableMove.Value.RollBack(); return;
// } }
// }
// } if (Plugin.InRaid() && __result.Succeeded)
{
// In raid successes are all fine
return;
}
bool canModify = CanModify(mod, out string error) && CanModify(to, out error);
if (canModify == __result.Succeeded)
{
// In agreement, just check the error is best to show
if (Settings.ModifyRaidWeapons.Value == ModRaidWeapon.WithTool &&
(__result.Error is NotModdableInRaidError || __result.Error is ModVitalPartInRaidError))
{
// Double check this is an unequipped weapon
Weapon weapon = item.GetRootItemNotEquipment() as Weapon ?? to.GetRootItemNotEquipment() as Weapon;
if (weapon != null && !EquippedSlots.Contains(weapon.Parent.Container.ID))
{
__result = new MultitoolNeededError(item);
}
}
}
if (__result.Failed && canModify)
{
// Override result with success if DestinationCheck passes
var destinationCheck = InteractionsHandlerClass.DestinationCheck(item.Parent, to, itemController.OwnerType);
if (destinationCheck.Failed)
{
return;
}
__result = default;
}
else if (__result.Succeeded && !canModify)
{
// Out of raid, likely dragging a mod that was previously non-interactive, need to actually block
__result = new VitalPartInHandsError();
}
}
private class VitalPartInHandsError : InventoryError
{
public override string GetLocalizedDescription()
{
return "Vital mod weapon in hands".Localized();
}
public override string ToString()
{
return "Vital mod weapon in hands";
}
}
}
public class ModCanApplyPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(LootItemClass), nameof(LootItemClass.Apply));
}
// Gets called when dropping mods on top of weapons
[PatchPrefix]
public static void Prefix(LootItemClass __instance, Item item)
{
if (!Plugin.InRaid())
{
return;
}
if (__instance is not Weapon weapon || item is not Mod mod || EquippedSlots.Contains(weapon.Parent.Container.ID))
{
return;
}
if (CanModify(mod, out string error))
{
ModRaidModdablePatch.Override = true;
EmptyVitalPartsPatch.Override = true;
}
}
[PatchPostfix]
public static void Postfix(LootItemClass __instance, ref ItemOperation __result)
{
ModRaidModdablePatch.Override = false;
EmptyVitalPartsPatch.Override = false;
// If setting is multitool, may need to change some errors
if (Settings.ModifyRaidWeapons.Value == ModRaidWeapon.WithTool)
{
if (__instance is not Weapon weapon || EquippedSlots.Contains(weapon.Parent.Container.ID))
{
return;
}
if (__result.Error is NotModdableInRaidError || __result.Error is ModVitalPartInRaidError)
{
__result = new MultitoolNeededError(__instance);
}
}
}
}
public class ModRaidModdablePatch : ModulePatch
{
public static bool Override = false;
protected override MethodBase GetTargetMethod()
{
return AccessTools.Property(typeof(Mod), nameof(Mod.RaidModdable)).GetMethod;
}
[PatchPostfix]
public static void Postfix(ref bool __result)
{
__result = __result || Override;
}
}
public class EmptyVitalPartsPatch : ModulePatch
{
public static bool Override = false;
protected override MethodBase GetTargetMethod()
{
return AccessTools.Property(typeof(LootItemClass), nameof(LootItemClass.VitalParts)).GetMethod;
}
[PatchPrefix]
public static bool Prefix(ref IEnumerable<Slot> __result)
{
if (Override)
{
__result = [];
return false;
}
return true;
}
}
private static bool CanModify(Mod item, out string error)
{
return CanModify(item, item?.Parent, out error);
}
private static bool CanModify(ItemAddress itemAddress, out string error)
{
return CanModify(null, itemAddress, out error);
}
private static bool CanModify(Mod item, ItemAddress itemAddress, out string error)
{
error = null;
// If it's raidmoddable and not in a vital slot, then it's all good
if ((item == null || item.RaidModdable) &&
(!R.SlotItemAddress.Type.IsAssignableFrom(itemAddress.GetType()) || !new R.SlotItemAddress(itemAddress).Slot.Required))
{
return true;
}
Item rootItem = itemAddress.GetRootItemNotEquipment();
if (rootItem is not Weapon weapon)
{
return true;
}
// Can't modify weapon in hands
if (EquippedSlots.Contains(weapon.Parent.Container.ID))
{
if (Plugin.InRaid())
{
error = "Inventory Errors/Not moddable in raid";
return false;
}
if (!Settings.ModifyEquippedWeapons.Value)
{
error = "Vital mod weapon in hands";
return false;
}
}
// Not in raid, not in hands: anything is possible
if (!Plugin.InRaid())
{
return true;
}
if (Settings.ModifyRaidWeapons.Value == ModRaidWeapon.Never)
{
error = "Inventory Errors/Not moddable in raid";
return false;
}
Player player = Singleton<GameWorld>.Instance.MainPlayer;
bool hasMultitool = player.Equipment.GetAllItems().Any(i => i.TemplateId == MultitoolId);
if (Settings.ModifyRaidWeapons.Value == ModRaidWeapon.WithTool && !hasMultitool)
{
error = "Inventory Errors/Not moddable without multitool";
return false;
}
return true;
}
} }

View File

@@ -52,6 +52,14 @@ internal enum TacticalBindModifier
Alt Alt
} }
internal enum ModRaidWeapon
{
Never,
[Description("With Multitool")]
WithTool,
Always
}
internal class Settings internal class Settings
{ {
// Categories // Categories
@@ -94,7 +102,6 @@ internal class Settings
public static ConfigEntry<KeyboardShortcut> UnpackKeyBind { get; set; } public static ConfigEntry<KeyboardShortcut> UnpackKeyBind { get; set; }
public static ConfigEntry<KeyboardShortcut> FilterByKeyBind { get; set; } public static ConfigEntry<KeyboardShortcut> FilterByKeyBind { get; set; }
public static ConfigEntry<KeyboardShortcut> LinkedSearchKeyBind { get; set; } public static ConfigEntry<KeyboardShortcut> LinkedSearchKeyBind { get; set; }
public static ConfigEntry<bool> AddOfferContextMenu { get; set; } // Advanced
public static ConfigEntry<KeyboardShortcut> AddOfferKeyBind { get; set; } public static ConfigEntry<KeyboardShortcut> AddOfferKeyBind { get; set; }
public static ConfigEntry<KeyboardShortcut> SortingTableKeyBind { get; set; } public static ConfigEntry<KeyboardShortcut> SortingTableKeyBind { get; set; }
public static ConfigEntry<bool> LimitNonstandardDrags { get; set; } // Advanced public static ConfigEntry<bool> LimitNonstandardDrags { get; set; } // Advanced
@@ -112,6 +119,8 @@ internal class Settings
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> ModifyEquippedWeapons { get; set; } // Advanced
public static ConfigEntry<ModRaidWeapon> ModifyRaidWeapons { 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; }
public static ConfigEntry<bool> GreedyStackMove { get; set; } public static ConfigEntry<bool> GreedyStackMove { get; set; }
@@ -122,6 +131,7 @@ internal class Settings
public static ConfigEntry<bool> AutoOpenSortingTable { get; set; } public static ConfigEntry<bool> AutoOpenSortingTable { get; set; }
public static ConfigEntry<bool> DefaultSortingTableBind { get; set; } // Advanced public static ConfigEntry<bool> DefaultSortingTableBind { get; set; } // Advanced
public static ConfigEntry<bool> ContextMenuOnRight { get; set; } public static ConfigEntry<bool> ContextMenuOnRight { get; set; }
public static ConfigEntry<bool> AddOfferContextMenu { get; set; }
public static ConfigEntry<bool> ShowGPCurrency { get; set; } public static ConfigEntry<bool> ShowGPCurrency { get; set; }
public static ConfigEntry<bool> ShowOutOfStockCheckbox { get; set; } public static ConfigEntry<bool> ShowOutOfStockCheckbox { get; set; }
public static ConfigEntry<SortingTableDisplay> SortingTableButton { get; set; } public static ConfigEntry<SortingTableDisplay> SortingTableButton { get; set; }
@@ -424,15 +434,6 @@ internal class Settings
null, null,
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
configEntries.Add(AddOfferContextMenu = config.Bind(
InputSection,
"Add Offer Context Menu",
true,
new ConfigDescription(
"Add a context menu to list the item on the flea market",
null,
new ConfigurationManagerAttributes { IsAdvanced = true })));
configEntries.Add(AddOfferKeyBind = config.Bind( configEntries.Add(AddOfferKeyBind = config.Bind(
InputSection, InputSection,
"Linked Search Shortcut", "Linked Search Shortcut",
@@ -569,6 +570,24 @@ internal class Settings
null, null,
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
configEntries.Add(ModifyEquippedWeapons = config.Bind(
InventorySection,
"Modify Equipped Weapons",
true,
new ConfigDescription(
"Enable the modification of equipped weapons, including vital parts, out of raid",
null,
new ConfigurationManagerAttributes { IsAdvanced = true })));
configEntries.Add(ModifyRaidWeapons = config.Bind(
InventorySection,
"Modify Weapons In Raid",
ModRaidWeapon.Never,
new ConfigDescription(
"When to enable the modification of vital parts of unequipped weapons, in raid",
null,
new ConfigurationManagerAttributes { })));
configEntries.Add(ReorderGrids = config.Bind( configEntries.Add(ReorderGrids = config.Bind(
InventorySection, InventorySection,
"Standardize Grid Order", "Standardize Grid Order",
@@ -659,6 +678,15 @@ internal class Settings
null, null,
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
configEntries.Add(AddOfferContextMenu = config.Bind(
InputSection,
"Add Offer Context Menu",
true,
new ConfigDescription(
"Add a context menu to list the item on the flea market",
null,
new ConfigurationManagerAttributes { })));
configEntries.Add(ShowGPCurrency = config.Bind( configEntries.Add(ShowGPCurrency = config.Bind(
InventorySection, InventorySection,
"Show GP Coins in Currency", "Show GP Coins in Currency",
@@ -916,7 +944,6 @@ internal class Settings
RecalcOrder(configEntries); RecalcOrder(configEntries);
MakeDependent(EnableMultiSelect, EnableMultiSelectInRaid); MakeDependent(EnableMultiSelect, EnableMultiSelectInRaid);
MakeDependent(EnableMultiSelect, ShowMultiSelectDebug, false); MakeDependent(EnableMultiSelect, ShowMultiSelectDebug, false);
MakeDependent(EnableMultiSelect, EnableMultiClick); MakeDependent(EnableMultiSelect, EnableMultiClick);