(svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)

This commit is contained in:
rubidium
2009-10-06 17:23:15 +00:00
parent fd38bbe706
commit ec3a0dc064
9 changed files with 118 additions and 51 deletions

View File

@@ -14,21 +14,30 @@
#include "saveload.h"
static const SaveLoad _cargopacket_desc[] = {
SLE_VAR(CargoPacket, source, SLE_UINT16),
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
SLE_VAR(CargoPacket, count, SLE_UINT16),
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, 125, SL_MAX_VERSION),
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, 125, SL_MAX_VERSION),
/**
* Wrapper function to get the CargoPacket's internal structure while
* some of the variables itself are private.
* @return the saveload description for CargoPackets.
*/
const SaveLoad *GetCargoPacketDesc()
{
static const SaveLoad _cargopacket_desc[] = {
SLE_VAR(CargoPacket, source, SLE_UINT16),
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
SLE_VAR(CargoPacket, count, SLE_UINT16),
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, 125, SL_MAX_VERSION),
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, 125, SL_MAX_VERSION),
/* Used to be paid_for, but that got changed. */
SLE_CONDNULL(1, 0, 120),
/* Used to be paid_for, but that got changed. */
SLE_CONDNULL(1, 0, 120),
SLE_END()
};
SLE_END()
};
return _cargopacket_desc;
}
static void Save_CAPA()
{
@@ -36,7 +45,7 @@ static void Save_CAPA()
FOR_ALL_CARGOPACKETS(cp) {
SlSetArrayIndex(cp->index);
SlObject(cp, _cargopacket_desc);
SlObject(cp, GetCargoPacketDesc());
}
}
@@ -46,7 +55,7 @@ static void Load_CAPA()
while ((index = SlIterateArray()) != -1) {
CargoPacket *cp = new (index) CargoPacket();
SlObject(cp, _cargopacket_desc);
SlObject(cp, GetCargoPacketDesc());
}
}

View File

@@ -698,10 +698,8 @@ static bool LoadOldGood(LoadgameState *ls, int num)
SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, _cargo_source != 0xFF);
if (GB(_waiting_acceptance, 0, 12) != 0) {
CargoPacket *cp = new CargoPacket();
cp->source = (_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
cp->count = GB(_waiting_acceptance, 0, 12);
cp->days_in_transit = _cargo_days;
CargoPacket *cp = new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_days);
cp->source = (_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
ge->cargo.Append(cp);
}
@@ -1332,8 +1330,12 @@ bool LoadOldVehicle(LoadgameState *ls, int num)
v->next = (Vehicle *)(size_t)_old_next_ptr;
if (_cargo_count != 0) {
CargoPacket *cp = new CargoPacket((_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source, _cargo_count);
cp->days_in_transit = _cargo_days;
CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days);
cp->source = (_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
cp->source_xy = (cp->source != INVALID_STATION) ? Station::Get(cp->source)->xy : 0;
cp->loaded_at_xy = cp->source_xy;
cp->source_type = ST_INDUSTRY;
cp->source_id = INVALID_SOURCE;
v->cargo.Append(cp);
}
}

View File

@@ -239,15 +239,10 @@ static void Load_STNS()
SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
if (GB(_waiting_acceptance, 0, 12) != 0) {
/* Don't construct the packet with station here, because that'll fail with old savegames */
CargoPacket *cp = new CargoPacket();
CargoPacket *cp = new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_days, _cargo_feeder_share);
/* In old versions, enroute_from used 0xFF as INVALID_STATION */
cp->source = (CheckSavegameVersion(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
cp->count = GB(_waiting_acceptance, 0, 12);
cp->days_in_transit = _cargo_days;
cp->feeder_share = _cargo_feeder_share;
cp->source_xy = _cargo_source_xy;
cp->days_in_transit = _cargo_days;
cp->feeder_share = _cargo_feeder_share;
SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, 1);
ge->cargo.Append(cp);
}

View File

@@ -719,12 +719,9 @@ void Load_VEHS()
if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v)) {
/* Don't construct the packet with station here, because that'll fail with old savegames */
CargoPacket *cp = new CargoPacket();
CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_feeder_share);
cp->source = _cargo_source;
cp->source_xy = _cargo_source_xy;
cp->count = _cargo_count;
cp->days_in_transit = _cargo_days;
cp->feeder_share = _cargo_feeder_share;
cp->loaded_at_xy = _cargo_loaded_at_xy;
v->cargo.Append(cp);
}