Skip to content

Commit

Permalink
more otpimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
blaxxun committed Nov 29, 2023
1 parent 1da1434 commit 0558a0d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
19 changes: 18 additions & 1 deletion Backpacks/AutoPickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,40 @@ private static class CheckAutoPickupActive
{
public static bool PickingUp = false;
private static void Prefix() => PickingUp = true;
private static void Finalizer() => PickingUp = false;
private static void Finalizer()
{
PickingUp = false;
AutoPickupItemsWithFullInventory.itemCache.Clear();
}
}

[HarmonyPatch(typeof(Inventory), nameof(Inventory.CanAddItem), typeof(ItemDrop.ItemData), typeof(int))]
private static class AutoPickupItemsWithFullInventory
{
public static readonly HashSet<int> itemCache = new();

private static void Postfix(Inventory __instance, ItemDrop.ItemData item, ref bool __result)
{
if (!__result && CheckAutoPickupActive.PickingUp && Backpacks.autoFillBackpacks.Value == Backpacks.Toggle.On)
{
int hash = ((5342 + item.m_stack) << 5 + item.m_shared.m_name.GetHashCode()) << 5 + item.m_quality << 5 + item.m_customData.GetHashCode();
if (itemCache.Contains(hash))
{
return;
}

foreach (ItemDrop.ItemData inventoryItem in __instance.m_inventory)
{
if (inventoryItem.Data().Get<ItemContainer>() is { } container && container.Inventory.CanAddItem(item) && container.CanAddItem(item) && container.MayAutoPickup(item))
{
__result = true;
}
}

if (!__result)
{
itemCache.Add(hash);
}
}
}
}
Expand Down
25 changes: 9 additions & 16 deletions Backpacks/Backpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ public void Awake()
preventTeleportation = config("2 - Backpack", "Backpack Teleportation Check", Toggle.On, new ConfigDescription("If off, portals do not check the content of a backpack upon teleportation."));
backpackCeption = config("2 - Backpack", "Backpacks in Backpacks", Toggle.Off, new ConfigDescription("If on, you can put backpacks into backpacks."));
backpackChests = config("2 - Backpack", "Backpacks in Chests", Toggle.Off, new ConfigDescription("If on, you can put backpacks that aren't empty into chests, to make the chests bigger on the inside."));
uniqueBackpack = config("2 - Backpack", "Unique Backpacks", Unique.Global, new ConfigDescription("Can be used to restrict the number of backpacks a player can have in their inventory.\nGlobal: Only one backpack.\nRestricted: No other backpacks without item restrictions allowed.\nType: Only one backpack of each type.\nBypass: As many backpacks as you want.\nNone: Same as bypass, but doesn't overrule every other flag."));
hiddenBackpack = config("2 - Backpack", "Hide Backpacks", Toggle.Off, new ConfigDescription("If on, the backpack visual is hidden."), false);
uniqueBackpack = config("2 - Backpack", "Unique Backpack", Unique.Global, new ConfigDescription("Can be used to restrict the number of backpacks a player can have in their inventory.\nGlobal: Only one backpack.\nRestricted: No other backpacks without item restrictions allowed.\nType: Only one backpack of each type.\nBypass: As many backpacks as you want.\nNone: Same as bypass, but doesn't overrule every other flag."));
hiddenBackpack = config("2 - Backpack", "Hide Backpack", Toggle.Off, new ConfigDescription("If on, the backpack visual is hidden."), false);
hiddenBackpack.SettingChanged += (_, _) =>
{
foreach (Visual visual in Visual.visuals.Values)
{
visual.forceSetBackpackEquipped(visual.currentBackpackItemHash);
}
};
autoOpenBackpack = config("2 - Backpack", "Auto Open Backpacks", Toggle.On, new ConfigDescription("If on, the first backpack found in your inventory is opened automatically, if the inventory is opened."), false);
autoOpenBackpack = config("2 - Backpack", "Auto Open Backpack", Toggle.On, new ConfigDescription("If on, the backpack in your backpack slot is opened automatically, if the inventory is opened."), false);
equipStatusEffect = config("2 - Backpack", "Equip Status Effect", "", new ConfigDescription("Name of a status effect that should be applied to the player, if the backpack is equipped."));
equipStatusEffect.SettingChanged += (_, _) => AddStatusEffectToBackpack.Postfix(ObjectDB.instance);
autoFillBackpacks = config("2 - Backpack", "Auto Fill Backpacks", Toggle.On, new ConfigDescription("If on, items you pick up are added to your backpack. Conditions apply."), false);
autoFillBackpacks = config("2 - Backpack", "Auto Fill Backpack", Toggle.On, new ConfigDescription("If on, items you pick up are added to your backpack. Conditions apply."), false);

ParseBackpackSize();

Expand Down Expand Up @@ -316,21 +316,14 @@ private static class OpenBackpackAutomatically
{
private static void Postfix(InventoryGui __instance, Container? container)
{
if (container is null && autoOpenBackpack.Value == Toggle.On)
if (container is null && autoOpenBackpack.Value == Toggle.On && Player.m_localPlayer && Visual.visuals.TryGetValue(Player.m_localPlayer.m_visEquipment, out Visual visual) && visual.equippedBackpackItem is {} backpack)
{
foreach (ItemDrop.ItemData inventoryItem in Player.m_localPlayer.GetInventory().m_inventory)
if (__instance.m_playerGrid.GetInventory() is null)
{
if (inventoryItem.Data().Get<ItemContainer>() is { } backpack)
{
if (__instance.m_playerGrid.GetInventory() is null)
{
__instance.m_playerGrid.m_inventory = Player.m_localPlayer.GetInventory();
}

CustomContainer.OpenFakeItemsContainer.Open(__instance, backpack.Item);
break;
}
__instance.m_playerGrid.m_inventory = Player.m_localPlayer.GetInventory();
}

CustomContainer.OpenFakeItemsContainer.Open(__instance, backpack);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions Backpacks/CustomContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static bool Prefix(InventoryGrid __instance, Inventory fromInventory, It

if (existingItem is not null && existingItem != item)
{
if (!OpenContainer.CanAddItem(existingItem))
if (!OpenContainer.CanAddItemManually(existingItem))
{
__result = false;
return false;
Expand Down Expand Up @@ -287,7 +287,7 @@ private static bool Prefix(InventoryGrid __instance, Inventory fromInventory, It
return false;
}

if (!OpenContainer.CanAddItem(item))
if (!OpenContainer.CanAddItemManually(item))
{
__result = false;
return false;
Expand Down Expand Up @@ -331,7 +331,7 @@ private static bool Prefix(InventoryGrid grid, ref ItemDrop.ItemData? item, Inve
else if (item is not null && mod == InventoryGrid.Modifier.Move && OpenContainer?.Inventory is { } inventory && inventory != grid.m_inventory)
{
// Moving into container
if (!OpenContainer.CanAddItem(item))
if (!OpenContainer.CanAddItemManually(item))
{
return false;
}
Expand Down Expand Up @@ -378,6 +378,7 @@ private static class AddPressKeyToOpenTooltip
{
private static bool Updated = false;

[HarmonyPriority(Priority.VeryLow)]
private static void Postfix(InventoryGui __instance)
{
if (!Updated)
Expand Down
2 changes: 2 additions & 0 deletions Backpacks/ItemContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public virtual bool CanAddItem(ItemDrop.ItemData item)
#endif
}

public virtual bool CanAddItemManually(ItemDrop.ItemData item) => CanAddItem(item);

public virtual bool CanRemoveItem(ItemDrop.ItemData item) => true;

public virtual bool RemoveItem(ItemDrop.ItemData item) => true;
Expand Down

0 comments on commit 0558a0d

Please sign in to comment.