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.
55 lines
2.6 KiB
C#
55 lines
2.6 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|