fix(trol): stop the log weapon's hidden slot from crashing InventoryGrid

TrolLogWeapon.GrabLog gave the picked-up log an out-of-bounds grid
position ((99, 99)) to keep it off the visible inventory grid.
InventoryGrid.UpdateGui maps every item to a cell with y * width + x
and no bounds check, so that position threw ArgumentOutOfRangeException
every frame the inventory screen was open - for the player holding it,
or anyone else who ever had it in their own pack or a chest. Visible
as endless console spam, a frame stall/freeze, and corrupted icons on
every other slot for the rest of that update.

Now uses Inventory.FindEmptySlot like every other picked-up item, so
it renders in a real slot with the normal equipped indicator.

Also adds InventoryGridSafetyPatch as a general safety net: any item
already sitting at an invalid grid position (existing saves from
before this fix, or one handed off through a chest) gets moved to a
real slot - or dropped if there's no room - instead of crashing the
grid the next time it renders.
This commit is contained in:
kanym 2026-08-09 10:48:47 +02:00
commit 524b11e084
2 changed files with 72 additions and 2 deletions

View file

@ -64,11 +64,26 @@ namespace RacesOfValheim
ItemDrop.ItemData currentLeft = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_leftItem").Value;
if (currentLeft != null) user.UnequipItem(currentLeft);
// Add the real weapon to inventory in a virtual slot (99, 99) to hide it
// A grid position out of the visible grid's bounds ((99, 99), same idea as a "hidden"
// slot) used to be how this stayed out of the way - but InventoryGrid.UpdateGui maps
// every single item in the inventory to a grid cell with y * width + x and no bounds
// check at all, for every player who ever ends up holding this, equipped or not. A
// position that far out throws ArgumentOutOfRangeException every frame the inventory
// screen is open, which both floods the log (visible as a stutter/freeze) and aborts
// the render loop partway through, corrupting every other slot's icon until the item
// is gone. FindEmptySlot is what every other item picked up in Valheim actually uses -
// it shows up in the grid like any other equipped weapon instead, which is correct.
ItemDrop wepDrop = weaponPrefab.GetComponent<ItemDrop>();
ItemDrop.ItemData newItem = wepDrop.m_itemData.Clone();
newItem.m_dropPrefab = weaponPrefab;
newItem.m_gridPos = new Vector2i(99, 99);
Vector2i gridPos = HarmonyLib.Traverse.Create(user.GetInventory()).Method("FindEmptySlot", false).GetValue<Vector2i>();
if (gridPos.x == -1)
{
user.Message(MessageHud.MessageType.Center, "No room to lift it!");
return;
}
newItem.m_gridPos = gridPos;
newItem.m_durability = newItem.GetMaxDurability();
var m_inventory = HarmonyLib.Traverse.Create(user.GetInventory()).Field<List<ItemDrop.ItemData>>("m_inventory").Value;