JujuAdams / SNAP

Data format converters for GameMaker LTS 2022
MIT License
88 stars 15 forks source link

new function SnapBufferMeasureBinary #37

Closed Grisgram closed 1 year ago

Grisgram commented 1 year ago

Hey

when writing a struct of unknown size (like a savegame) to a binary buffer it is useful to know, how large the buffer shall be allocated to avoid too many resizing operations. This function does not write to the buffer, it just counts, how much would be written, taking into account the null-terminator characters for strings, your marker bytes and all that stuff. it's more or less a copy of SnapBufferWriteBinary, where all write-operations are replaced by length-counting.

return value is an integer (number of bytes).

hope u like it, cheers, Gris

PS: take care with the project file - I seem to have a newer version (resource version 1.7 -- you have 1.6) -- maybe you want to pick just the script, its up to you!

JujuAdams commented 1 year ago

Not a good solution for the use case, sorry.

SnapBufferWriteBinary() moves the "write head" of the buffer so you can tell the exact size of the data you're saving by using buffer_tell(). You can then pass that value into the buffer saving function variants that take a size parameter.

Also you should use string_byte_length() to get the correct length of a string when encoded using UTF8.

Grisgram commented 1 year ago

thanks for your input - will look into that -- still, buffer_tell only works AFTER your function ran, so i might have had tons of reallocations to the buffer, and the new version in snap wants to receive a buffer, so i must create it before i call your function, not afterwards. but yeah, that's something one can figure out during development (how big savegames get) and preallocate a close-to-reality size.