lighten load on gc from inventory apis

This commit is contained in:
DrPerkyLegit 2026-04-03 00:26:25 -04:00
parent 0ea9dfb0ce
commit 85580fac4e
2 changed files with 60 additions and 40 deletions

View file

@ -60,8 +60,17 @@ public class Inventory : IEnumerable<ItemStack>
_items[i]?.UnbindFromInventory(); _items[i]?.UnbindFromInventory();
if (id > 0 && count > 0) if (id > 0 && count > 0)
{ {
_items[i] = new ItemStack(id, count, (short)aux); if (_items[i] == null)
_items[i]!.BindToInventory(this, i); {
_items[i] = new ItemStack(id, count, (short)aux);
}
else
{
_items[i]!.setTypeId(id);
_items[i]!.setAmount(count);
_items[i]!.setDurability((short)aux);
}
_items[i]!.BindToInventory(this, i); //should we unbind and rebind or just keep the bind?
} }
else else
{ {

View file

@ -19,9 +19,14 @@ public class PlayerInventory : Inventory
private int _heldItemSlot; private int _heldItemSlot;
internal HumanEntity? _holder; internal HumanEntity? _holder;
private int[] syncBuffer;
private GCHandle syncBufferHandle;
internal PlayerInventory() internal PlayerInventory()
: base("Player", InventoryType.PLAYER, INVENTORY_SIZE) : base("Player", InventoryType.PLAYER, INVENTORY_SIZE)
{ {
this.syncBuffer = new int[121];
this.syncBufferHandle = GCHandle.Alloc(this.syncBuffer, GCHandleType.Pinned);
} }
protected internal override void EnsureSynced() protected internal override void EnsureSynced()
@ -30,22 +35,17 @@ public class PlayerInventory : Inventory
return; return;
int entityId = _holder.getEntityId(); int entityId = _holder.getEntityId();
int[] buf = new int[121];
var gh = GCHandle.Alloc(buf, GCHandleType.Pinned); NativeBridge.GetPlayerInventory(entityId, this.syncBufferHandle.AddrOfPinnedObject());
try
{ byte[]? metadataBuffer = null;
NativeBridge.GetPlayerInventory(entityId, gh.AddrOfPinnedObject()); GCHandle? metadataBufferHandle = null;
}
finally
{
gh.Free();
}
for (int i = 0; i < INVENTORY_SIZE; i++) for (int i = 0; i < INVENTORY_SIZE; i++)
{ {
int id = buf[i * 3 + 0]; int id = this.syncBuffer[i * 3 + 0];
int aux = buf[i * 3 + 1]; int aux = this.syncBuffer[i * 3 + 1];
int packed = buf[i * 3 + 2]; int packed = this.syncBuffer[i * 3 + 2];
ushort count = (ushort)((packed >> 8) & 0xFFFF); ushort count = (ushort)((packed >> 8) & 0xFFFF);
@ -55,22 +55,37 @@ public class PlayerInventory : Inventory
_items[i]?.UnbindFromInventory(); _items[i]?.UnbindFromInventory();
if (id > 0 && count > 0) if (id > 0 && count > 0)
{ {
var stack = new ItemStack(id, count, (short)aux); if (_items[i] == null)
{
_items[i] = new ItemStack(id, count, (short)aux);
}
else
{
_items[i]!.setTypeId(id);
_items[i]!.setAmount(count);
_items[i]!.setDurability((short)aux);
}
if (hasMetadata) if (hasMetadata)
{ {
var meta = ReadMetaFromNative(entityId, i); var meta = ReadMetaFromNative(entityId, i, metadataBuffer, metadataBufferHandle);
if (meta != null) if (meta != null)
stack.setItemMetaInternal(meta); {
_items[i]!.setItemMetaInternal(meta);
}
} }
_items[i] = stack; _items[i]!.BindToInventory(this, i);
stack.BindToInventory(this, i);
} }
else else
{ {
_items[i] = null; _items[i] = null;
} }
} }
_heldItemSlot = buf[120]; _heldItemSlot = this.syncBuffer[120];
if (metadataBufferHandle.HasValue)
metadataBufferHandle.Value.Free();
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -176,7 +191,7 @@ public class PlayerInventory : Inventory
/// <param name="stack">The ItemStack to set.</param> /// <param name="stack">The ItemStack to set.</param>
public void setItemInHand(ItemStack? stack) public void setItemInHand(ItemStack? stack)
{ {
EnsureSynced(); EnsureSynced(); //we need to sync the current held slot, hate doing this here during a set call
setItem(_heldItemSlot, stack); setItem(_heldItemSlot, stack);
} }
@ -233,41 +248,37 @@ public class PlayerInventory : Inventory
/// <returns>The HumanEntity that owns this inventory.</returns> /// <returns>The HumanEntity that owns this inventory.</returns>
public HumanEntity? getHolder() => _holder; public HumanEntity? getHolder() => _holder;
private static ItemMeta? ReadMetaFromNative(int entityId, int slot) private static ItemMeta? ReadMetaFromNative(int entityId, int slot, byte[]? buffer, GCHandle? bufferHandle)
{ {
if (NativeBridge.GetItemMeta == null) if (NativeBridge.GetItemMeta == null)
return null; return null;
byte[] buf = new byte[4096]; if (buffer == null)
int bytesWritten;
var gh = GCHandle.Alloc(buf, GCHandleType.Pinned);
try
{ {
bytesWritten = NativeBridge.GetItemMeta(entityId, slot, gh.AddrOfPinnedObject(), buf.Length); buffer = new byte[4096];
} bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
finally
{
gh.Free();
} }
int bytesWritten = NativeBridge.GetItemMeta(entityId, slot, bufferHandle.GetValueOrDefault().AddrOfPinnedObject(), buffer!.Length);
if (bytesWritten <= 0) if (bytesWritten <= 0)
return null; return null;
int offset = 0; int offset = 0;
int nameLen = BitConverter.ToInt32(buf, offset); int nameLen = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
string? displayName = null; string? displayName = null;
if (nameLen > 0) if (nameLen > 0)
{ {
displayName = Encoding.UTF8.GetString(buf, offset, nameLen); displayName = Encoding.UTF8.GetString(buffer, offset, nameLen);
offset += nameLen; offset += nameLen;
} }
int loreCount = 0; int loreCount = 0;
if (offset + 4 <= bytesWritten) if (offset + 4 <= bytesWritten)
{ {
loreCount = BitConverter.ToInt32(buf, offset); loreCount = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
} }
@ -278,11 +289,11 @@ public class PlayerInventory : Inventory
for (int i = 0; i < loreCount; i++) for (int i = 0; i < loreCount; i++)
{ {
if (offset + 4 > bytesWritten) break; if (offset + 4 > bytesWritten) break;
int lineLen = BitConverter.ToInt32(buf, offset); int lineLen = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
if (lineLen > 0 && offset + lineLen <= bytesWritten) if (lineLen > 0 && offset + lineLen <= bytesWritten)
{ {
lore.Add(Encoding.UTF8.GetString(buf, offset, lineLen)); lore.Add(Encoding.UTF8.GetString(buffer, offset, lineLen));
offset += lineLen; offset += lineLen;
} }
else else
@ -295,7 +306,7 @@ public class PlayerInventory : Inventory
int enchantCount = 0; int enchantCount = 0;
if (offset + 4 <= bytesWritten) if (offset + 4 <= bytesWritten)
{ {
enchantCount = BitConverter.ToInt32(buf, offset); enchantCount = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
} }
@ -308,10 +319,10 @@ public class PlayerInventory : Inventory
if (offset + (4 + 4) > bytesWritten) if (offset + (4 + 4) > bytesWritten)
break; break;
int type = BitConverter.ToInt32(buf, offset); int type = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
int level = BitConverter.ToInt32(buf, offset); int level = BitConverter.ToInt32(buffer, offset);
offset += 4; offset += 4;
enchants.Add((EnchantmentType)type, level); enchants.Add((EnchantmentType)type, level);