Apply autostack FiR rules to Top Up action

This commit is contained in:
Tyfon
2024-05-28 14:56:54 -07:00
parent e5f18ec25d
commit cba5871671
2 changed files with 75 additions and 37 deletions

View File

@@ -8,55 +8,93 @@ using System.Reflection;
namespace UIFixes namespace UIFixes
{ {
public class StackFirItemsPatch : ModulePatch public static class StackFirItemsPatches
{ {
private static Type MergeableItemType; public static void Enable()
protected override MethodBase GetTargetMethod()
{ {
MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_0)); new ContainerStackPatch().Enable();
MergeableItemType = method.GetParameters()[2].ParameterType.GetElementType(); // parameter is a ref type, get underlying type new TopUpStackPatch().Enable();
return method;
} }
// Reimplementing this entire method to ignore SpawnedInSession for certain types public class ContainerStackPatch : ModulePatch
[PatchPrefix]
public static bool Prefix(IEnumerable<StashGridClass> gridsToPut, Item itemToMerge, ref object mergeableItem, int overrideCount, ref bool __result)
{ {
if (!MergeableItemType.IsInstanceOfType(itemToMerge)) private static Type MergeableItemType;
protected override MethodBase GetTargetMethod()
{ {
mergeableItem = null; MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_0));
__result = false; MergeableItemType = method.GetParameters()[2].ParameterType.GetElementType(); // parameter is a ref type, get underlying type
return method;
} }
if (overrideCount <= 0) // Reimplementing this entire method to ignore SpawnedInSession for certain types
[PatchPrefix]
public static bool Prefix(IEnumerable<StashGridClass> gridsToPut, Item itemToMerge, ref object mergeableItem, int overrideCount, ref bool __result)
{ {
overrideCount = itemToMerge.StackObjectsCount; if (!MergeableItemType.IsInstanceOfType(itemToMerge))
{
mergeableItem = null;
__result = false;
}
if (overrideCount <= 0)
{
overrideCount = itemToMerge.StackObjectsCount;
}
bool ignoreSpawnedInSession;
if (itemToMerge.Template is MoneyClass)
{
ignoreSpawnedInSession = Settings.MergeFIRMoney.Value;
}
else if (itemToMerge.Template is AmmoTemplate)
{
ignoreSpawnedInSession = Settings.MergeFIRAmmo.Value;
}
else
{
ignoreSpawnedInSession = Settings.MergeFIROther.Value;
}
mergeableItem = gridsToPut.SelectMany(x => x.Items).Where(x => MergeableItemType.IsInstanceOfType(x))
.Where(x => x != itemToMerge)
.Where(x => x.TemplateId == itemToMerge.TemplateId)
.Where(x => ignoreSpawnedInSession || x.SpawnedInSession == itemToMerge.SpawnedInSession)
.Where(x => x.StackObjectsCount < x.StackMaxSize)
.FirstOrDefault(x => overrideCount <= x.StackMaxSize - x.StackObjectsCount);
__result = mergeableItem != null;
return false;
}
}
public class TopUpStackPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(Item), nameof(Item.IsSameItem));
} }
bool ignoreSpawnedInSession; [PatchPrefix]
if (itemToMerge.Template is MoneyClass) public static bool Prefix(Item __instance, Item other, ref bool __result)
{ {
ignoreSpawnedInSession = Settings.MergeFIRMoney.Value; bool ignoreSpawnedInSession;
} if (__instance.Template is MoneyClass)
else if (itemToMerge.Template is AmmoTemplate) {
{ ignoreSpawnedInSession = Settings.MergeFIRMoney.Value;
ignoreSpawnedInSession = Settings.MergeFIRAmmo.Value; }
} else if (__instance.Template is AmmoTemplate)
else {
{ ignoreSpawnedInSession = Settings.MergeFIRAmmo.Value;
ignoreSpawnedInSession = Settings.MergeFIROther.Value; }
} else
{
ignoreSpawnedInSession = Settings.MergeFIROther.Value;
}
mergeableItem = gridsToPut.SelectMany(x => x.Items).Where(x => MergeableItemType.IsInstanceOfType(x)) __result = __instance.TemplateId == other.TemplateId && __instance.Id != other.Id && (ignoreSpawnedInSession || __instance.SpawnedInSession == other.SpawnedInSession);
.Where(x => x != itemToMerge) return false;
.Where(x => x.TemplateId == itemToMerge.TemplateId) }
.Where(x => ignoreSpawnedInSession || x.SpawnedInSession == itemToMerge.SpawnedInSession)
.Where(x => x.StackObjectsCount < x.StackMaxSize)
.FirstOrDefault(x => overrideCount <= x.StackMaxSize - x.StackObjectsCount);
__result = mergeableItem != null;
return false;
} }
} }
} }

View File

@@ -24,7 +24,7 @@ namespace UIFixes
InspectWindowStatsPatches.Enable(); InspectWindowStatsPatches.Enable();
new RemoveDoorActionsPatch().Enable(); new RemoveDoorActionsPatch().Enable();
ScrollPatches.Enable(); ScrollPatches.Enable();
new StackFirItemsPatch().Enable(); StackFirItemsPatches.Enable();
SwapPatches.Enable(); SwapPatches.Enable();
SyncScrollPositionPatches.Enable(); SyncScrollPositionPatches.Enable();
new TransferConfirmPatch().Enable(); new TransferConfirmPatch().Enable();