Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] FieldAddress attribute to jump to address read and jump back to previous position #178

Open
sn4k3 opened this issue Dec 4, 2021 · 0 comments

Comments

@sn4k3
Copy link

sn4k3 commented Dec 4, 2021

I often deal with jumps on structures and need to create a new class rather than one and seek in between of serializations, sometimes is usefull to have the field address rather than sequential enforced

My proposal:

[FieldAddress(long offset|nameof(field), SeekOrigin origin = SeekOrigin.Begin, bool returnToCurrentPosition = true)]
[FieldOrder(0)] uint MyField1;
[FieldOrder(1)] uint MyField2;
[FieldOrder(2)] uint MyField3;
example 1: [FieldAddress(40)] uint MyField1;
stream.Position; // eg. 20
(Serialize MyField1) // at stream.Position 40
stream.Position = 20; // Because of returnToCurrentPosition = true
(Serialize MyField2) // at stream.Position 20
(Serialize MyField3) // at stream.Position 24
example 2: [FieldAddress(nameof(MyField2Address), returnToCurrentPosition = false)] uint MyField1;
stream.Position; // eg. 20
(Serialize MyField1) // at stream.Position 40
(Serialize MyField2) // at stream.Position 44
(Serialize MyField3) // at stream.Position 48
// Does not return to old position and continue from here

This will allow complex jumping, dazy chain, and unify classes.
Another use case:

class Header:

[FieldAddress(-4, SeekOrigin.End, true)]
[FieldOrder(0)] uint Checksum; // Last uint on the file
[FieldAddress(0, SeekOrigin.Begin, true)] // Make sure we are on position 0
[FieldOrder(1)] uint MyHeader1; // stream.position 0
[FieldOrder(2)] uint MyHeader2; // stream.position 4
[FieldOrder(3)] uint MyHeader3; // stream.position 8
[FieldOrder(4)] uint MyHeader4; // stream.position 12

This way i dont need to deserialize all file first in order to validate my checksum
Of course this will raise other problem, on create file from scrath, in this case we have to write header as last operation or SetLength of file before hand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant