Files @ r25751:3154638283de
Branch filter:

Location: cpp/openttd-patchpack/source/src/saveload/cheat_sl.cpp

Patric Stout
Feature: framework to make savegames self-descriptive

We won't be able to make it fully self-descriptive (looking at you
MAP-chunks), but anything else can. With this framework, we can
add headers for each chunk explaining how each chunk looks like
in detail.

They also will all be tables, making it a lot easier to read in
external tooling, and opening the way to consider a database
(like SQLite) to use as savegame format.

Lastly, with the headers in the savegame, you can freely add
fields without needing a savegame version bump; older versions
of OpenTTD will simply ignore the new field. This also means
we can remove all the SLE_CONDNULL, as they are irrelevant.

The next few commits will start using this framework.
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file cheat_sl.cpp Code handling saving and loading of cheats */

#include "../stdafx.h"
#include "../cheat_type.h"

#include "saveload.h"

#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()
{
	SlSetArrayIndex(0);

	SlSetLength(std::size(_cheats_desc));
	SlObject(&_cheats, _cheats_desc);
}

/**
 * Load the cheat values.
 */
static void Load_CHTS()
{
	size_t count = SlGetFieldLength();
	std::vector<SaveLoad> slt;

	/* 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;
	}

	if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
	SlObject(&_cheats, slt);
	if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries");
}

static const ChunkHandler cheat_chunk_handlers[] = {
	{ 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_ARRAY },
};

extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers);