diff --git a/JotunnModStub/InventoryGridSafetyPatch.cs b/JotunnModStub/InventoryGridSafetyPatch.cs new file mode 100644 index 0000000..2874ef3 --- /dev/null +++ b/JotunnModStub/InventoryGridSafetyPatch.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using HarmonyLib; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// 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. + /// + [HarmonyPatch(typeof(InventoryGrid), "UpdateGui")] + public static class InventoryGridSafetyPatch + { + public static void Prefix(InventoryGrid __instance) + { + Inventory inventory = Traverse.Create(__instance).Field("m_inventory").Value; + if (inventory == null) return; + + int width = inventory.GetWidth(); + int height = inventory.GetHeight(); + + List 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(); + 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; + } + } + } +} diff --git a/JotunnModStub/Trol/TrolLogWeapon.cs b/JotunnModStub/Trol/TrolLogWeapon.cs index 34b55a4..fa42538 100644 --- a/JotunnModStub/Trol/TrolLogWeapon.cs +++ b/JotunnModStub/Trol/TrolLogWeapon.cs @@ -64,11 +64,26 @@ namespace RacesOfValheim ItemDrop.ItemData currentLeft = HarmonyLib.Traverse.Create(user).Field("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.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(); + 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>("m_inventory").Value;