jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
290 stars 62 forks source link

Improve the FieldOffset #193

Open sn4k3 opened 2 years ago

sn4k3 commented 2 years ago

This pull request implements #178 which allows to jump to addresses and make use of stream.Seek() with SeekOrigin. The default behaviour of FieldOffset has not changed and then it will rewind by default.

Since i have files pointing to tables this will help having a master class rather that multiple classes and jump in between.

Arguments

Example:

public class FileSpec
{
    [FieldOrder(0)]
    [FieldLength(12)]
    [SerializeAs(SerializedType.TerminatedString)]
    public string FileVersion { get; set; } = "FileMarking";

    [FieldOrder(1)]
    public uint FileVersion { get; set; }

    [FieldOrder(2)]
    public uint HeaderAddress { get; set; }

    [FieldOrder(3)]
    public uint SettingsAddress { get; set; }

    [FieldOrder(4)]
    public uint PreviewAddress { get; set; }

    [FieldOrder(5)]
    public uint LayersAddress { get; set; }

    [FieldOrder(6)]
    [FieldOffset(nameof(HeaderAddress), false)]
    public uint HeaderField1 { get; set; }

    [FieldOrder(7)]
    public uint HeaderField2 { get; set; }

    [FieldOrder(8)]
    public uint HeaderField3 { get; set; }

    [FieldOrder(9)]
    [FieldOffset(nameof(SettingsAddress), false)]
    public uint SettingsField1 { get; set; }

    [FieldOrder(10)]
    public uint SettingsField2 { get; set; }

    [FieldOrder(11)]
    [FieldOffset(nameof(PreviewAddress), false)]
    public uint PreviewSize { get; set; }

    [FieldOrder(12)]
    [FieldCount(nameof(PreviewSize))]
    public byte[] PreviewData {get; set; }

    [FieldOrder(13)]
    [FieldOffset(nameof(LayersAddress), false)]
    public uint LayerCount { get; set; }

    [FieldOrder(14)]
    [FieldCount(nameof(LayerCount))]
    public Layer[] Layers { get; set; }

    [FieldOrder(15)]
    [FieldOffset(4, SeekOrigin.Current, false)] // Skip unused 4 bytes first, same as declaring uint Padding
    public uint FileChecksum { get; set; }
}
stumpy1029 commented 3 months ago

Any updates on this feature? I have a chunk file format (that I cannot change) who uses absolute file offsets for each chunk.