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

@@ -122,8 +122,21 @@ public:
SLE_CONDVAR(TransportedCargoStat<uint32>, new_act, SLE_UINT32, SLV_165, SL_MAX_VERSION),
};
/**
* Get the number of cargoes used by this savegame version.
* @return The number of cargoes used by this savegame version.
*/
size_t GetNumCargo() const
{
if (IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES)) return 32;
if (IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH)) return NUM_CARGO;
/* Read from the savegame how long the list is. */
return SlGetStructListLength(NUM_CARGO);
}
void Save(Town *t) const override
{
SlSetStructListLength(NUM_CARGO);
for (CargoID i = 0; i < NUM_CARGO; i++) {
SlObject(&t->supplied[i], this->GetDescription());
}
@@ -131,7 +144,7 @@ public:
void Load(Town *t) const override
{
uint num_cargo = IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO;
size_t num_cargo = this->GetNumCargo();
for (CargoID i = 0; i < num_cargo; i++) {
SlObject(&t->supplied[i], this->GetDescription());
}
@@ -149,14 +162,16 @@ public:
void Save(Town *t) const override
{
for (int i = TE_BEGIN; i < TE_END; i++) {
SlSetStructListLength(NUM_TE);
for (size_t i = TE_BEGIN; i < TE_END; i++) {
SlObject(&t->received[i], this->GetDescription());
}
}
void Load(Town *t) const override
{
for (int i = TE_BEGIN; i < TE_END; i++) {
size_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? (size_t)TE_END : SlGetStructListLength(TE_END);
for (size_t i = 0; i < length; i++) {
SlObject(&t->received[i], this->GetDescription());
}
}