fix: Fixes allocating CompactInfo when changing the weight of an item (#2282)

### Summary

* Fixes extra allocations and computing weight values when changing the weight of an item.
This commit is contained in:
Kamron Batman 2025-11-29 12:28:27 -08:00 committed by GitHub
parent 5fb0e806c5
commit 6a7d49b679
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -474,25 +474,27 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
}
set
{
if (Weight != value)
var defaultWeight = DefaultWeight;
var info = LookupCompactInfo();
var oldWeight = info is { m_Weight: >= 0 } ? info.m_Weight : defaultWeight;
if (oldWeight == value)
{
var info = AcquireCompactInfo();
var oldPileWeight = PileWeight;
info.m_Weight = value;
if (info.m_Weight < 0)
{
VerifyCompactInfo();
}
var newPileWeight = PileWeight;
UpdateTotal(this, TotalType.Weight, newPileWeight - oldPileWeight);
InvalidateProperties();
return;
}
if (value < 0 || defaultWeight == value)
{
info?.m_Weight = -1;
VerifyCompactInfo();
}
else
{
(info ?? AcquireCompactInfo()).m_Weight = value;
}
UpdateTotal(this, TotalType.Weight, PileWeight - (int)Math.Ceiling(oldWeight * Amount));
InvalidateProperties();
}
}