@Wietlol you can do a field offset of one, but supposedly you should byte align data, but idk why... I havewn't really experienced any issues not doing so.
I might need to do a field offset of one anyways, because GameMaker (my client code) pulls data packets in as buffers with a default alignment of one, that cannot be changed.
The nice thing about structs is I can force explicit (with alignment) or sequential (without alignment) and do quick serialization using an unsafe byte[] in the background to populate the fields. Like Unions in C++.
Keeps you from having to write a serialization method for each packet type.
I mean, it lacks helper methods to diagnose the header values (for the sake of over engineering). Like method that returns/prints a section from the header, say it prints: "Error flag is SET"
@mr5 it's private purely to avoid direct unsafe access to it. So you call a method .Serialize(ref byte[] buff) which would just copy the unsafe byte data to the reference byte[]. Or other way around .Deserialize(ref byte[] buff) which would copy from the buffer to the unsafe byte[]. Pretty straight forward to avoid any weird usage cases.
Then you can access the struct's public fields as normal after you've serialized/deserialized it.
@nyconing it's my code, not using someone else's API.
Hmm. Also, the common pattern I encounter when I invoke deserialize/serialize method is that they are pure functions. That is, I would expect it to return a data.
@FatalSleep I would make another accessor to it such that it would be a readonly property. If you do: instance.header = something, then instance.bytes is now unsync.
in C# world, it's very uncommon to make the field publicly accessible.
Yep, and also take out the serializer/deserializer method out of the class and make it a helper methods instead.
// I picture you would end up something like this:
var instance = Serializer.Deserialize<YourStructHere>(bytes);
var bytes = Serializer.Serialize(instance);
Like... a lot. It's for sending/receiving packet data.
Network -> Received Data Packet -> Check Network Stream -> Deserialize Netwokr Stream Bytes to Struct. Struct Serialize to Bytes -> Send over Network Stream.
hmm. not really sure how much it would affect the performance. If you're not really comfortable with it, then just make those methods as static methods inside the struct itself.