diff options
-rw-r--r-- | src/cargopacket.h | 4 | ||||
-rw-r--r-- | src/saveload/station_sl.cpp | 353 |
2 files changed, 203 insertions, 154 deletions
diff --git a/src/cargopacket.h b/src/cargopacket.h index a5166e959..0e24bc623 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -456,8 +456,8 @@ protected: public: /** The super class ought to know what it's doing. */ friend class CargoList<StationCargoList, StationCargoPacketMap>; - /** The stations, via GoodsEntry, have a CargoList. */ - friend SaveLoadTable GetGoodsDesc(); + /* So we can use private/protected variables in the saveload code */ + friend class SlStationGoods; friend class CargoLoad; friend class CargoTransfer; diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 9c07834e4..21989139f 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -220,11 +220,6 @@ static uint32 _cargo_source_xy; static uint8 _cargo_days; static Money _cargo_feeder_share; -static const SaveLoad _station_speclist_desc[] = { - SLE_CONDVAR(StationSpecList, grfid, SLE_UINT32, SLV_27, SL_MAX_VERSION), - SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8, SLV_27, SL_MAX_VERSION), -}; - std::list<CargoPacket *> _packets; uint32 _num_dests; @@ -236,21 +231,147 @@ struct FlowSaveLoad { bool restricted; }; -static const SaveLoad _flow_desc[] = { - SLE_VAR(FlowSaveLoad, source, SLE_UINT16), - SLE_VAR(FlowSaveLoad, via, SLE_UINT16), - SLE_VAR(FlowSaveLoad, share, SLE_UINT32), - SLE_CONDVAR(FlowSaveLoad, restricted, SLE_BOOL, SLV_187, SL_MAX_VERSION), -}; +typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair; + +static OldPersistentStorage _old_st_persistent_storage; /** - * Wrapper function to get the GoodsEntry's internal structure while - * some of the variables itself are private. - * @return the saveload description for GoodsEntry. + * Swap the temporary packets with the packets without specific destination in + * the given goods entry. Assert that at least one of those is empty. + * @param ge Goods entry to swap with. */ -SaveLoadTable GetGoodsDesc() +static void SwapPackets(GoodsEntry *ge) { - static const SaveLoad goods_desc[] = { + StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets()); + + if (_packets.empty()) { + std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(INVALID_STATION)); + if (it == ge_packets.end()) { + return; + } else { + it->second.swap(_packets); + } + } else { + assert(ge_packets[INVALID_STATION].empty()); + ge_packets[INVALID_STATION].swap(_packets); + } +} + +class SlStationSpecList : public DefaultSaveLoadHandler<SlStationSpecList, BaseStation> { +public: + inline static const SaveLoad description[] = { + SLE_CONDVAR(StationSpecList, grfid, SLE_UINT32, SLV_27, SL_MAX_VERSION), + SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8, SLV_27, SL_MAX_VERSION), + }; + + void Save(BaseStation *bst) const override + { + for (uint i = 0; i < bst->num_specs; i++) { + SlObject(&bst->speclist[i], this->GetDescription()); + } + } + + void Load(BaseStation *bst) const override + { + if (bst->num_specs != 0) { + /* Allocate speclist memory when loading a game */ + bst->speclist = CallocT<StationSpecList>(bst->num_specs); + for (uint i = 0; i < bst->num_specs; i++) { + SlObject(&bst->speclist[i], this->GetDescription()); + } + } + } +}; + +class SlStationCargo : public DefaultSaveLoadHandler<SlStationCargo, GoodsEntry> { +public: + inline static const SaveLoad description[] = { + SLE_VAR(StationCargoPair, first, SLE_UINT16), + SLE_REFLIST(StationCargoPair, second, REF_CARGO_PACKET), + }; + + void Save(GoodsEntry *ge) const override + { + for (StationCargoPacketMap::ConstMapIterator it(ge->cargo.Packets()->begin()); it != ge->cargo.Packets()->end(); ++it) { + SlObject(const_cast<StationCargoPacketMap::value_type *>(&(*it)), this->GetDescription()); + } + } + + void Load(GoodsEntry *ge) const override + { + StationCargoPair pair; + for (uint j = 0; j < _num_dests; ++j) { + SlObject(&pair, this->GetDescription()); + const_cast<StationCargoPacketMap &>(*(ge->cargo.Packets()))[pair.first].swap(pair.second); + assert(pair.second.empty()); + } + } + + void FixPointers(GoodsEntry *ge) const override + { + for (StationCargoPacketMap::ConstMapIterator it = ge->cargo.Packets()->begin(); it != ge->cargo.Packets()->end(); ++it) { + SlObject(const_cast<StationCargoPair *>(&(*it)), this->GetDescription()); + } + } +}; + +class SlStationFlow : public DefaultSaveLoadHandler<SlStationFlow, GoodsEntry> { +public: + inline static const SaveLoad description[] = { + SLE_VAR(FlowSaveLoad, source, SLE_UINT16), + SLE_VAR(FlowSaveLoad, via, SLE_UINT16), + SLE_VAR(FlowSaveLoad, share, SLE_UINT32), + SLE_CONDVAR(FlowSaveLoad, restricted, SLE_BOOL, SLV_187, SL_MAX_VERSION), + }; + + void Save(GoodsEntry *ge) const override + { + for (FlowStatMap::const_iterator outer_it(ge->flows.begin()); outer_it != ge->flows.end(); ++outer_it) { + const FlowStat::SharesMap *shares = outer_it->second.GetShares(); + uint32 sum_shares = 0; + FlowSaveLoad flow; + flow.source = outer_it->first; + for (FlowStat::SharesMap::const_iterator inner_it(shares->begin()); inner_it != shares->end(); ++inner_it) { + flow.via = inner_it->second; + flow.share = inner_it->first - sum_shares; + flow.restricted = inner_it->first > outer_it->second.GetUnrestricted(); + sum_shares = inner_it->first; + assert(flow.share > 0); + SlObject(&flow, this->GetDescription()); + } + } + } + + void Load(GoodsEntry *ge) const override + { + FlowSaveLoad flow; + FlowStat *fs = nullptr; + StationID prev_source = INVALID_STATION; + for (uint32 j = 0; j < _num_flows; ++j) { + SlObject(&flow, this->GetDescription()); + if (fs == nullptr || prev_source != flow.source) { + fs = &(ge->flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second); + } else { + fs->AppendShare(flow.via, flow.share, flow.restricted); + } + prev_source = flow.source; + } + } +}; + +class SlStationGoods : public DefaultSaveLoadHandler<SlStationGoods, BaseStation> { +public: +#if defined(_MSC_VER) && (_MSC_VER == 1915 || _MSC_VER == 1916) + /* This table access private members of other classes; they have this + * class as friend. For MSVC CL 19.15 and 19.16 this doesn't work for + * "inline static const", so we are forced to wrap the table in a + * function. CL 19.16 is the latest for VS2017. */ + inline static const SaveLoad description[] = {{}}; + SaveLoadTable GetDescription() const override { +#else + inline +#endif + static const SaveLoad description[] = { SLEG_CONDVAR( _waiting_acceptance, SLE_UINT16, SL_MIN_VERSION, SLV_68), SLE_CONDVAR(GoodsEntry, status, SLE_UINT8, SLV_68, SL_MAX_VERSION), SLE_CONDNULL(2, SLV_51, SLV_68), @@ -272,42 +393,71 @@ SaveLoadTable GetGoodsDesc() SLE_CONDVAR(GoodsEntry, node, SLE_UINT16, SLV_183, SL_MAX_VERSION), SLEG_CONDVAR( _num_flows, SLE_UINT32, SLV_183, SL_MAX_VERSION), SLE_CONDVAR(GoodsEntry, max_waiting_cargo, SLE_UINT32, SLV_183, SL_MAX_VERSION), + SLEG_CONDSTRUCTLIST(SlStationFlow, SLV_183, SL_MAX_VERSION), + SLEG_CONDSTRUCTLIST(SlStationCargo, SLV_183, SL_MAX_VERSION), }; +#if defined(_MSC_VER) && (_MSC_VER == 1915 || _MSC_VER == 1916) + return description; + } +#endif - return goods_desc; -} + void Save(BaseStation *bst) const override + { + Station *st = Station::From(bst); + for (CargoID i = 0; i < NUM_CARGO; i++) { + _num_dests = (uint32)st->goods[i].cargo.Packets()->MapSize(); + _num_flows = 0; + for (FlowStatMap::const_iterator it(st->goods[i].flows.begin()); it != st->goods[i].flows.end(); ++it) { + _num_flows += (uint32)it->second.GetShares()->size(); + } + SlObject(&st->goods[i], this->GetDescription()); + } + } -typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair; + void Load(BaseStation *bst) const override + { + Station *st = Station::From(bst); -static const SaveLoad _cargo_list_desc[] = { - SLE_VAR(StationCargoPair, first, SLE_UINT16), - SLE_REFLIST(StationCargoPair, second, REF_CARGO_PACKET), -}; + /* Before savegame version 161, persistent storages were not stored in a pool. */ + if (IsSavegameVersionBefore(SLV_161) && !IsSavegameVersionBefore(SLV_145) && st->facilities & FACIL_AIRPORT) { + /* Store the old persistent storage. The GRFID will be added later. */ + assert(PersistentStorage::CanAllocateItem()); + st->airport.psa = new PersistentStorage(0, 0, 0); + memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(_old_st_persistent_storage.storage)); + } -/** - * Swap the temporary packets with the packets without specific destination in - * the given goods entry. Assert that at least one of those is empty. - * @param ge Goods entry to swap with. - */ -static void SwapPackets(GoodsEntry *ge) -{ - StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets()); + uint num_cargo = IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO; + for (CargoID i = 0; i < num_cargo; i++) { + SlObject(&st->goods[i], this->GetDescription()); + if (IsSavegameVersionBefore(SLV_183)) { + SwapPackets(&st->goods[i]); + } + } + } - if (_packets.empty()) { - std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(INVALID_STATION)); - if (it == ge_packets.end()) { - return; - } else { - it->second.swap(_packets); + void FixPointers(BaseStation *bst) const override + { + Station *st = Station::From(bst); + + uint num_cargo = IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO; + for (CargoID i = 0; i < num_cargo; i++) { + GoodsEntry *ge = &st->goods[i]; + if (IsSavegameVersionBefore(SLV_183)) { + SwapPackets(ge); // We have to swap back again to be in the format pre-183 expects. + SlObject(ge, this->GetDescription()); + SwapPackets(ge); + } else { + SlObject(ge, this->GetDescription()); + } } - } else { - assert(ge_packets[INVALID_STATION].empty()); - ge_packets[INVALID_STATION].swap(_packets); } -} +}; static void Load_STNS() { + static const SlStationGoods station_goods; + static const SlStationSpecList station_spec_list; + _cargo_source_xy = 0; _cargo_days = 0; _cargo_feeder_share = 0; @@ -323,7 +473,7 @@ static void Load_STNS() for (CargoID i = 0; i < num_cargo; i++) { GoodsEntry *ge = &st->goods[i]; - SlObject(ge, GetGoodsDesc()); + SlObject(ge, station_goods.GetDescription()); SwapPackets(ge); if (IsSavegameVersionBefore(SLV_68)) { SB(ge->status, GoodsEntry::GES_ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15)); @@ -349,7 +499,7 @@ static void Load_STNS() /* Allocate speclist memory when loading a game */ st->speclist = CallocT<StationSpecList>(st->num_specs); for (uint i = 0; i < st->num_specs; i++) { - SlObject(&st->speclist[i], _station_speclist_desc); + SlObject(&st->speclist[i], station_spec_list.GetDescription()); } } } @@ -357,6 +507,8 @@ static void Load_STNS() static void Ptrs_STNS() { + static const SlStationGoods station_goods; + /* Don't run when savegame version is higher than or equal to 123. */ if (!IsSavegameVersionBefore(SLV_123)) return; @@ -366,7 +518,7 @@ static void Ptrs_STNS() for (CargoID i = 0; i < num_cargo; i++) { GoodsEntry *ge = &st->goods[i]; SwapPackets(ge); - SlObject(ge, GetGoodsDesc()); + SlObject(ge, station_goods.GetDescription()); SwapPackets(ge); } } @@ -374,8 +526,6 @@ static void Ptrs_STNS() } } -static OldPersistentStorage _old_st_persistent_storage; - /** * SaveLoad handler for the BaseStation, which all other stations / waypoints * make use of. @@ -447,6 +597,7 @@ public: SLE_REFLIST(Station, loading_vehicles, REF_VEHICLE), SLE_CONDVAR(Station, always_accepted, SLE_FILE_U32 | SLE_VAR_U64, SLV_127, SLV_EXTEND_CARGOTYPES), SLE_CONDVAR(Station, always_accepted, SLE_UINT64, SLV_EXTEND_CARGOTYPES, SL_MAX_VERSION), + SLEG_STRUCTLIST(SlStationGoods), }; void GenericSaveLoad(BaseStation *bst) const @@ -486,52 +637,15 @@ static const SaveLoad _station_desc[] = { SLE_SAVEBYTE(BaseStation, facilities), SLEG_STRUCT(SlStationNormal), SLEG_STRUCT(SlStationWaypoint), + SLEG_CONDSTRUCTLIST(SlStationSpecList, SLV_27, SL_MAX_VERSION), }; -static void RealSave_STNN(BaseStation *bst) -{ - SlObject(bst, _station_desc); - - if ((bst->facilities & FACIL_WAYPOINT) == 0) { - Station *st = Station::From(bst); - for (CargoID i = 0; i < NUM_CARGO; i++) { - _num_dests = (uint32)st->goods[i].cargo.Packets()->MapSize(); - _num_flows = 0; - for (FlowStatMap::const_iterator it(st->goods[i].flows.begin()); it != st->goods[i].flows.end(); ++it) { - _num_flows += (uint32)it->second.GetShares()->size(); - } - SlObject(&st->goods[i], GetGoodsDesc()); - for (FlowStatMap::const_iterator outer_it(st->goods[i].flows.begin()); outer_it != st->goods[i].flows.end(); ++outer_it) { - const FlowStat::SharesMap *shares = outer_it->second.GetShares(); - uint32 sum_shares = 0; - FlowSaveLoad flow; - flow.source = outer_it->first; - for (FlowStat::SharesMap::const_iterator inner_it(shares->begin()); inner_it != shares->end(); ++inner_it) { - flow.via = inner_it->second; - flow.share = inner_it->first - sum_shares; - flow.restricted = inner_it->first > outer_it->second.GetUnrestricted(); - sum_shares = inner_it->first; - assert(flow.share > 0); - SlObject(&flow, _flow_desc); - } - } - for (StationCargoPacketMap::ConstMapIterator it(st->goods[i].cargo.Packets()->begin()); it != st->goods[i].cargo.Packets()->end(); ++it) { - SlObject(const_cast<StationCargoPacketMap::value_type *>(&(*it)), _cargo_list_desc); - } - } - } - - for (uint i = 0; i < bst->num_specs; i++) { - SlObject(&bst->speclist[i], _station_speclist_desc); - } -} - static void Save_STNN() { /* Write the stations */ for (BaseStation *st : BaseStation::Iterate()) { SlSetArrayIndex(st->index); - SlAutolength((AutolengthProc*)RealSave_STNN, st); + SlObject(st, _station_desc); } } @@ -539,59 +653,12 @@ static void Load_STNN() { _num_flows = 0; - uint num_cargo = IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO; int index; while ((index = SlIterateArray()) != -1) { bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0; BaseStation *bst = waypoint ? (BaseStation *)new (index) Waypoint() : new (index) Station(); SlObject(bst, _station_desc); - - if (!waypoint) { - Station *st = Station::From(bst); - - /* Before savegame version 161, persistent storages were not stored in a pool. */ - if (IsSavegameVersionBefore(SLV_161) && !IsSavegameVersionBefore(SLV_145) && st->facilities & FACIL_AIRPORT) { - /* Store the old persistent storage. The GRFID will be added later. */ - assert(PersistentStorage::CanAllocateItem()); - st->airport.psa = new PersistentStorage(0, 0, 0); - memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(_old_st_persistent_storage.storage)); - } - - for (CargoID i = 0; i < num_cargo; i++) { - SlObject(&st->goods[i], GetGoodsDesc()); - FlowSaveLoad flow; - FlowStat *fs = nullptr; - StationID prev_source = INVALID_STATION; - for (uint32 j = 0; j < _num_flows; ++j) { - SlObject(&flow, _flow_desc); - if (fs == nullptr || prev_source != flow.source) { - fs = &(st->goods[i].flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second); - } else { - fs->AppendShare(flow.via, flow.share, flow.restricted); - } - prev_source = flow.source; - } - if (IsSavegameVersionBefore(SLV_183)) { - SwapPackets(&st->goods[i]); - } else { - StationCargoPair pair; - for (uint j = 0; j < _num_dests; ++j) { - SlObject(&pair, _cargo_list_desc); - const_cast<StationCargoPacketMap &>(*(st->goods[i].cargo.Packets()))[pair.first].swap(pair.second); - assert(pair.second.empty()); - } - } - } - } - - if (bst->num_specs != 0) { - /* Allocate speclist memory when loading a game */ - bst->speclist = CallocT<StationSpecList>(bst->num_specs); - for (uint i = 0; i < bst->num_specs; i++) { - SlObject(&bst->speclist[i], _station_speclist_desc); - } - } } } @@ -600,26 +667,8 @@ static void Ptrs_STNN() /* Don't run when savegame version lower than 123. */ if (IsSavegameVersionBefore(SLV_123)) return; - uint num_cargo = IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO; - for (Station *st : Station::Iterate()) { - for (CargoID i = 0; i < num_cargo; i++) { - GoodsEntry *ge = &st->goods[i]; - if (IsSavegameVersionBefore(SLV_183)) { - SwapPackets(ge); - SlObject(ge, GetGoodsDesc()); - SwapPackets(ge); - } else { - SlObject(ge, GetGoodsDesc()); - for (StationCargoPacketMap::ConstMapIterator it = ge->cargo.Packets()->begin(); it != ge->cargo.Packets()->end(); ++it) { - SlObject(const_cast<StationCargoPair *>(&(*it)), _cargo_list_desc); - } - } - } - SlObject(st, _station_desc); - } - - for (Waypoint *wp : Waypoint::Iterate()) { - SlObject(wp, _station_desc); + for (BaseStation *bst : BaseStation::Iterate()) { + SlObject(bst, _station_desc); } } |