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
{
public class StackFirItemsPatch : ModulePatch
public static class StackFirItemsPatches
{
private static Type MergeableItemType;
protected override MethodBase GetTargetMethod()
public static void Enable()
{
MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_0));
MergeableItemType = method.GetParameters()[2].ParameterType.GetElementType(); // parameter is a ref type, get underlying type
return method;
new ContainerStackPatch().Enable();
new TopUpStackPatch().Enable();
}
// 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)
public class ContainerStackPatch : ModulePatch
{
if (!MergeableItemType.IsInstanceOfType(itemToMerge))
private static Type MergeableItemType;
protected override MethodBase GetTargetMethod()
{
mergeableItem = null;
__result = false;
MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_0));
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;
if (itemToMerge.Template is MoneyClass)
[PatchPrefix]
public static bool Prefix(Item __instance, Item other, ref bool __result)
{
ignoreSpawnedInSession = Settings.MergeFIRMoney.Value;
}
else if (itemToMerge.Template is AmmoTemplate)
{
ignoreSpawnedInSession = Settings.MergeFIRAmmo.Value;
}
else
{
ignoreSpawnedInSession = Settings.MergeFIROther.Value;
}
bool ignoreSpawnedInSession;
if (__instance.Template is MoneyClass)
{
ignoreSpawnedInSession = Settings.MergeFIRMoney.Value;
}
else if (__instance.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;
__result = __instance.TemplateId == other.TemplateId && __instance.Id != other.Id && (ignoreSpawnedInSession || __instance.SpawnedInSession == other.SpawnedInSession);
return false;
}
}
}
}

View File

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