NHSE/NHSE.Core/Structures/Item/ItemArrayEditor.cs
Kurt b88c518d5c
Update FieldItemEditor for 3.0.0 (#716)
Updates the Field Item Editor to render layers based on the entire map, and the per-patch positioning of each layer.
Import/export will gracefully handle upgrade/downgrade, and viewport import/export will gracefully update tiles rather than a per-acre basis.

Performance has also been slightly improved; no allocation is done anymore when updating the image.
2026-01-25 16:55:38 -06:00

61 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
namespace NHSE.Core;
public sealed record ItemArrayEditor<T>(IReadOnlyList<T> Items)
where T : Item, ICopyableItem<T>
{
public int ItemSize => Items[0].Size;
public int TotalSize => Items.Count * ItemSize;
public byte[] Write() => Items.SetArray(ItemSize);
public void ImportItemDataX(ReadOnlySpan<byte> data, bool skipOccupiedSlots, int start = 0)
{
int expect = ItemSize;
var import = data.GetArray<T>(expect);
ImportItems(import, start, skipOccupiedSlots);
}
private void ImportItems(IReadOnlyList<T> import, int start, bool skipOccupiedSlots)
{
if (skipOccupiedSlots)
ImportItemsSkip(import, start);
else
ImportItemsOverwrite(import, start);
}
private void ImportItemsOverwrite(IReadOnlyList<T> import, int destIndex, int importIndex = 0)
{
while (importIndex < import.Count && destIndex < Items.Count)
{
Items[destIndex].CopyFrom(import[importIndex]);
destIndex++; importIndex++;
}
}
private void ImportItemsSkip(IReadOnlyList<T> import, int destIndex, int importIndex = 0)
{
while (importIndex < import.Count && destIndex < Items.Count)
{
var importItem = import[importIndex];
if (importItem.ItemId == Item.NONE)
{
importIndex++;
continue;
}
var destItem = Items[destIndex];
if (destItem.ItemId != Item.NONE)
{
destIndex++;
continue;
}
destItem.CopyFrom(importItem);
importIndex++;
destIndex++;
}
}
}