NHSE/NHSE.Core/Structures/Item/ItemArrayEditor.cs
Kurt a9810c7d03 Merge Field Item back into Item
Different means of expression for the 8 byte item structure's FreeParam

Will be expanding the ItemEditor so that it has a checkmark to toggle Extension Item editing behavior.

Removes IHeldItem as there's no need to abstract it.
2020-05-08 12:20:24 -07:00

63 lines
2.0 KiB
C#

using System.Collections.Generic;
namespace NHSE.Core
{
public class ItemArrayEditor<T> where T : Item, ICopyableItem<T>
{
public readonly IReadOnlyList<T> Items;
public ItemArrayEditor(IReadOnlyList<T> items) => Items = items;
public int ItemSize => Items[0].Size;
public int TotalSize => Items.Count * ItemSize;
public byte[] Write() => Items.SetArray(ItemSize);
public void ImportItemDataX(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++;
}
}
}
}