Merge branch 'fix-trol-log-weapon-inventory-crash' into main

Stops the Trol log weapon's out-of-bounds inventory slot from crashing
InventoryGrid every frame (console spam, frame stall, corrupted icons),
and adds a safety net for saves that already carry the bad item.
This commit is contained in:
kanym 2026-08-09 12:36:32 +02:00
commit 3067d6f6e2
2 changed files with 72 additions and 2 deletions

View file

@ -0,0 +1,55 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Safety net for TrolLogWeapon's old (99, 99) "hidden slot" trick: InventoryGrid.UpdateGui
/// maps every item in an inventory to a grid cell with y * width + x and no bounds check at
/// all, so any item already sitting at an out-of-range position (a save from before the fix,
/// or one handed off through a chest) throws ArgumentOutOfRangeException every frame that
/// inventory's screen is open - flooding the log, stalling the frame, and corrupting every
/// other slot's icon for the rest of that update.
///
/// This is a Prefix on InventoryGrid.UpdateGui itself rather than a one-time fix on
/// Player.OnSpawned, because the same crash happens opening a CHEST that holds the item, not
/// just a player's own pack - one check here covers every inventory the grid ever renders.
/// </summary>
[HarmonyPatch(typeof(InventoryGrid), "UpdateGui")]
public static class InventoryGridSafetyPatch
{
public static void Prefix(InventoryGrid __instance)
{
Inventory inventory = Traverse.Create(__instance).Field<Inventory>("m_inventory").Value;
if (inventory == null) return;
int width = inventory.GetWidth();
int height = inventory.GetHeight();
List<ItemDrop.ItemData> items = inventory.GetAllItems();
foreach (ItemDrop.ItemData item in items)
{
Vector2i pos = item.m_gridPos;
if (pos.x >= 0 && pos.x < width && pos.y >= 0 && pos.y < height) continue;
Vector2i fixedPos = Traverse.Create(inventory).Method("FindEmptySlot", false).GetValue<Vector2i>();
if (fixedPos.x == -1)
{
// No room even for a swap - dropping it is the only option that does not
// still crash the grid the moment this method runs again.
inventory.RemoveItem(item);
Jotunn.Logger.LogWarning(
$"[InventoryGridSafety] '{item.m_shared.m_name}' was at invalid grid position " +
$"{pos} with no empty slot to move it to; removed to stop the inventory crashing.");
continue;
}
Jotunn.Logger.LogWarning(
$"[InventoryGridSafety] '{item.m_shared.m_name}' was at invalid grid position " +
$"{pos} (grid is {width}x{height}); moved to {fixedPos}.");
item.m_gridPos = fixedPos;
}
}
}
}

View file

@ -64,11 +64,26 @@ namespace RacesOfValheim
ItemDrop.ItemData currentLeft = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_leftItem").Value; ItemDrop.ItemData currentLeft = HarmonyLib.Traverse.Create(user).Field<ItemDrop.ItemData>("m_leftItem").Value;
if (currentLeft != null) user.UnequipItem(currentLeft); 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 wepDrop = weaponPrefab.GetComponent<ItemDrop>();
ItemDrop.ItemData newItem = wepDrop.m_itemData.Clone(); ItemDrop.ItemData newItem = wepDrop.m_itemData.Clone();
newItem.m_dropPrefab = weaponPrefab; 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(); newItem.m_durability = newItem.GetMaxDurability();
var m_inventory = HarmonyLib.Traverse.Create(user.GetInventory()).Field<List<ItemDrop.ItemData>>("m_inventory").Value; var m_inventory = HarmonyLib.Traverse.Create(user.GetInventory()).Field<List<ItemDrop.ItemData>>("m_inventory").Value;