Changeset - r25612:9facf21c639c
[Not reviewed]
master
0 2 0
Patric Stout - 3 years ago 2021-06-06 12:01:42
truebrain@openttd.org
Codechange: rework CHTS-chunk save/load to be more like the others
2 files changed with 40 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/cheat_type.h
Show inline comments
 
@@ -28,12 +28,9 @@ struct Cheats {
 
	Cheat switch_company;   ///< change to another company
 
	Cheat money;            ///< get rich or poor
 
	Cheat crossing_tunnels; ///< allow tunnels that cross each other
 
	Cheat dummy1;           ///< empty cheat (build while in pause mode)
 
	Cheat no_jetcrash;      ///< no jet will crash on small airports anymore
 
	Cheat dummy2;           ///< empty cheat (change the climate of the map)
 
	Cheat change_date;      ///< changes date ingame
 
	Cheat setup_prod;       ///< setup raw-material production in game
 
	Cheat dummy3;           ///< empty cheat (enable running el-engines on normal rail)
 
	Cheat edit_max_hl;      ///< edit the maximum heightlevel; this is a cheat because of the fact that it needs to reset NewGRF game state and doing so as a simple configuration breaks the expectation of many
 
};
 

	
src/saveload/cheat_sl.cpp
Show inline comments
 
@@ -14,21 +14,38 @@
 

	
 
#include "../safeguards.h"
 

	
 
static const SaveLoad _cheats_desc[] = {
 
	SLE_VAR(Cheats, magic_bulldozer.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, magic_bulldozer.value, SLE_BOOL),
 
	SLE_VAR(Cheats, switch_company.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, switch_company.value, SLE_BOOL),
 
	SLE_VAR(Cheats, money.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, money.value, SLE_BOOL),
 
	SLE_VAR(Cheats, crossing_tunnels.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, crossing_tunnels.value, SLE_BOOL),
 
	SLE_NULL(1),
 
	SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
 
	SLE_VAR(Cheats, no_jetcrash.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, no_jetcrash.value, SLE_BOOL),
 
	SLE_NULL(1),
 
	SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
 
	SLE_VAR(Cheats, change_date.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, change_date.value, SLE_BOOL),
 
	SLE_VAR(Cheats, setup_prod.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, setup_prod.value, SLE_BOOL),
 
	SLE_NULL(1),
 
	SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
 
	SLE_VAR(Cheats, edit_max_hl.been_used, SLE_BOOL),
 
	SLE_VAR(Cheats, edit_max_hl.value, SLE_BOOL),
 
};
 

	
 
/**
 
 * Save the cheat values.
 
 */
 
static void Save_CHTS()
 
{
 
	/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
 
	byte count = sizeof(_cheats) / sizeof(Cheat);
 
	Cheat *cht = (Cheat*) &_cheats;
 
	Cheat *cht_last = &cht[count];
 

	
 
	SlSetLength(count * 2);
 
	for (; cht != cht_last; cht++) {
 
		SlWriteByte(cht->been_used);
 
		SlWriteByte(cht->value);
 
	}
 
	SlSetLength(std::size(_cheats_desc));
 
	SlObject(&_cheats, _cheats_desc);
 
}
 

	
 
/**
 
@@ -36,15 +53,21 @@ static void Save_CHTS()
 
 */
 
static void Load_CHTS()
 
{
 
	Cheat *cht = (Cheat*)&_cheats;
 
	size_t count = SlGetFieldLength() / 2;
 
	/* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
 
	if (count > sizeof(_cheats) / sizeof(Cheat)) SlErrorCorrupt("Too many cheat values");
 
	size_t count = SlGetFieldLength();
 
	std::vector<SaveLoad> slt;
 

	
 
	for (uint i = 0; i < count; i++) {
 
		cht[i].been_used = (SlReadByte() != 0);
 
		cht[i].value     = (SlReadByte() != 0);
 
	/* Cheats were added over the years without a savegame bump. They are
 
	 * stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
 
	 * are stored for this savegame. So read only "count" SLE_BOOLs (and in
 
	 * result "count / 2" cheats). */
 
	for (auto &sld : _cheats_desc) {
 
		count--;
 
		slt.push_back(sld);
 

	
 
		if (count == 0) break;
 
	}
 

	
 
	SlObject(&_cheats, slt);
 
}
 

	
 
/** Chunk handlers related to cheats. */
0 comments (0 inline, 0 general)