1
0
Fork 0

Codechange: Additional type safety for saveload version variables.

pull/7150/head
Peter Nelson 2019-01-29 00:56:28 +00:00 committed by PeterN
parent 9de12521ec
commit 0f37a683a2
4 changed files with 18 additions and 18 deletions

View File

@ -23,13 +23,13 @@
#include "safeguards.h" #include "safeguards.h"
extern const uint16 SAVEGAME_VERSION; ///< current savegame version extern const SaveLoadVersion SAVEGAME_VERSION; ///< current savegame version
extern SavegameType _savegame_type; ///< type of savegame we are loading extern SavegameType _savegame_type; ///< type of savegame we are loading
extern uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) extern uint32 _ttdp_version; ///< version of TTDP savegame (if applicable)
extern uint16 _sl_version; ///< the major savegame version identifier extern SaveLoadVersion _sl_version; ///< the major savegame version identifier
extern byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! extern byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!
static GamelogActionType _gamelog_action_type = GLAT_NONE; ///< action to record if anything changes static GamelogActionType _gamelog_action_type = GLAT_NONE; ///< action to record if anything changes

View File

@ -223,7 +223,7 @@ static void ShowHelp()
static void WriteSavegameInfo(const char *name) static void WriteSavegameInfo(const char *name)
{ {
extern uint16 _sl_version; extern SaveLoadVersion _sl_version;
uint32 last_ottd_rev = 0; uint32 last_ottd_rev = 0;
byte ever_modified = 0; byte ever_modified = 0;
bool removed_newgrfs = false; bool removed_newgrfs = false;

View File

@ -53,16 +53,16 @@
#include "../safeguards.h" #include "../safeguards.h"
extern const uint16 SAVEGAME_VERSION = SL_MAX_VERSION - 1; ///< Current savegame version of OpenTTD. extern const SaveLoadVersion SAVEGAME_VERSION = (SaveLoadVersion)(SL_MAX_VERSION - 1); ///< Current savegame version of OpenTTD.
SavegameType _savegame_type; ///< type of savegame we are loading SavegameType _savegame_type; ///< type of savegame we are loading
FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop.
uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) uint32 _ttdp_version; ///< version of TTDP savegame (if applicable)
uint16 _sl_version; ///< the major savegame version identifier SaveLoadVersion _sl_version; ///< the major savegame version identifier
byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!
char _savegame_format[8]; ///< how to compress savegames char _savegame_format[8]; ///< how to compress savegames
bool _do_autosave; ///< are we doing an autosave at the moment? bool _do_autosave; ///< are we doing an autosave at the moment?
/** What are we currently doing? */ /** What are we currently doing? */
enum SaveLoadAction { enum SaveLoadAction {
@ -1915,7 +1915,7 @@ struct LZOLoadFilter : LoadFilter {
/* Check if size is bad */ /* Check if size is bad */
((uint32*)out)[0] = size = tmp[1]; ((uint32*)out)[0] = size = tmp[1];
if (_sl_version != 0) { if (_sl_version != SL_MIN_VERSION) {
tmp[0] = TO_BE32(tmp[0]); tmp[0] = TO_BE32(tmp[0]);
size = TO_BE32(size); size = TO_BE32(size);
} }
@ -2562,7 +2562,7 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
if (fmt == endof(_saveload_formats)) { if (fmt == endof(_saveload_formats)) {
DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format"); DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
_sl.lf->Reset(); _sl.lf->Reset();
_sl_version = 0; _sl_version = SL_MIN_VERSION;
_sl_minor_version = 0; _sl_minor_version = 0;
/* Try to find the LZO savegame format; it uses 'OTTD' as tag. */ /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
@ -2580,7 +2580,7 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
if (fmt->tag == hdr[0]) { if (fmt->tag == hdr[0]) {
/* check version number */ /* check version number */
_sl_version = TO_BE32(hdr[1]) >> 16; _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
/* Minor is not used anymore from version 18.0, but it is still needed /* Minor is not used anymore from version 18.0, but it is still needed
* in versions before that (4 cases) which can't be removed easy. * in versions before that (4 cases) which can't be removed easy.
* Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */ * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
@ -2721,7 +2721,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, Detaile
ClearGRFConfigList(&_grfconfig); ClearGRFConfigList(&_grfconfig);
GamelogReset(); GamelogReset();
if (!LoadOldSaveGame(filename)) return SL_REINIT; if (!LoadOldSaveGame(filename)) return SL_REINIT;
_sl_version = 0; _sl_version = SL_MIN_VERSION;
_sl_minor_version = 0; _sl_minor_version = 0;
GamelogStartAction(GLAT_LOAD); GamelogStartAction(GLAT_LOAD);
if (!AfterLoadGame()) { if (!AfterLoadGame()) {

View File

@ -750,8 +750,8 @@ typedef SaveLoad SaveLoadGlobVarList;
*/ */
static inline bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor = 0) static inline bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor = 0)
{ {
extern uint16 _sl_version; extern SaveLoadVersion _sl_version;
extern byte _sl_minor_version; extern byte _sl_minor_version;
return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor); return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
} }
@ -764,7 +764,7 @@ static inline bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor = 0
*/ */
static inline bool SlIsObjectCurrentlyValid(SaveLoadVersion version_from, SaveLoadVersion version_to) static inline bool SlIsObjectCurrentlyValid(SaveLoadVersion version_from, SaveLoadVersion version_to)
{ {
extern const uint16 SAVEGAME_VERSION; extern const SaveLoadVersion SAVEGAME_VERSION;
if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION >= version_to) return false; if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION >= version_to) return false;
return true; return true;