jcrona / tamalib

A hardware agnostic Tamagotchi P1 emulation library
GNU General Public License v2.0
155 stars 16 forks source link

Feature request: State serialisation/deserialisation #10

Open DavidBuchanan314 opened 2 years ago

DavidBuchanan314 commented 2 years ago

For most concrete implementations (e.g. in tamatool https://github.com/jcrona/tamatool/blob/master/src/state.c ), there exists a need to serialise the state to an array of bytes, so that it may be saved in non-volatile storage etc.

IMHO this logic should be contained within tamalib itself, along with functions vaguely like

#define SERIALISED_STATE_SIZE [something]

void get_serialised_state(uint8_t *buf);
void set_serialised_state(uint8_t *buf);
ericlewis commented 2 years ago

While I agree it would be handy, it is not quite a universal function. If the buffer is larger than the memory + the rom or whatever in RAM, you will crash. The Arduino one already suffers from memory pressure & the way it is implemented in tamatool has a whole different level of abstractions over the file system which aren't easily replicated. It sucks to replicate but there are not too many options given the constraints of the platforms.

DavidBuchanan314 commented 2 years ago

It could be done with ~0 memory, using a streaming API, e.g.

void get_serialised_state(void (*callback_that_writes_one_byte)(uint8_t));
void set_serialised_state(uint8_t (*callback_that_reads_one_byte)(void));

(please excuse my possibly incorrect C function pointer syntax...)

This API should be sufficient to implement something like writing to an EEPROM on a resource-constrained Arduino, but also work nicely for implementations like tamatool.