using System;
using System.Collections.Generic;
using System.Linq;
namespace com.clusterrr.Famicom.Containers
{
///
/// File data FDS block (block type 4)
///
public class FdsBlockFileData : IFdsBlock, IEquatable
{
private byte blockType = 4;
///
/// Valid block type ID
///
public byte ValidTypeID { get => 4; }
///
/// True if block type ID is valid
///
public bool IsValid { get => blockType == ValidTypeID; }
private byte[] data = Array.Empty();
///
/// File data
///
public IEnumerable Data
{
get => Array.AsReadOnly(data);
set => data = value.ToArray();
}
///
/// Length of the block
///
public uint Length => (uint)(data.Length + 1);
///
/// Create FdsBlockFileData object from raw data
///
/// Data
/// Offset
/// Length
/// FdsBlockFileData object
public static FdsBlockFileData FromBytes(byte[] data, int offset = 0, int length = -1)
{
var retobj = new FdsBlockFileData
{
blockType = data[offset],
data = new byte[length < 0 ? data.Length - offset - 1 : length - 1]
};
Array.Copy(data, offset + 1, retobj.data, 0, retobj.data.Length);
return retobj;
}
///
/// Returns raw data
///
/// Data
public byte[] ToBytes() => Enumerable.Concat(new[] { blockType }, data).ToArray();
///
/// String representation
///
/// Number of bytes as string
public override string ToString() => $"{data.Length} bytes";
///
/// Equality comparer
///
/// Other FdsBlockFileData object
/// True if objects are equal
public bool Equals(FdsBlockFileData other)
{
return Enumerable.SequenceEqual(this.ToBytes(), other.ToBytes());
}
}
}