diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -122,8 +122,21 @@ public: SLE_CONDVAR(TransportedCargoStat, 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()); } }