Change: store length of SL_STRUCTLIST in the savegame

This wasn't consistently done, and often variables were used that
were read by an earlier blob. By moving it next to the struct
itself, the code becomes a bit more self-contained and easier to
read.

Additionally, this allows for external tooling to know how many
structs to expect, instead of having to know where to find the
length-field or a hard-coded value that can change at any moment.
This commit is contained in:
Patric Stout
2021-06-06 23:23:12 +02:00
committed by Patric Stout
parent d0bcb9839a
commit a146bcfe93
7 changed files with 156 additions and 42 deletions

View File

@@ -1589,6 +1589,34 @@ static bool SlObjectMember(void *object, const SaveLoad &sld)
return true;
}
/**
* Set the length of this list.
* @param The length of the list.
*/
void SlSetStructListLength(size_t length)
{
/* Automatically calculate the length? */
if (_sl.need_length != NL_NONE) {
SlSetLength(SlGetArrayLength(length));
if (_sl.need_length == NL_CALCLENGTH) return;
}
SlWriteArrayLength(length);
}
/**
* Get the length of this list; if it exceeds the limit, error out.
* @param limit The maximum size the list can be.
* @return The length of the list.
*/
size_t SlGetStructListLength(size_t limit)
{
size_t length = SlReadArrayLength();
if (length > limit) SlErrorCorrupt("List exceeds storage size");
return length;
}
/**
* Main SaveLoad function.
* @param object The object that is being saved or loaded.