Skip to content

Commit

Permalink
Replaced the extern version comparisons with the new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 29, 2024
1 parent cc333cc commit 350f284
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 122 deletions.
18 changes: 11 additions & 7 deletions src/common/GameValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "GameMode.h"
#include "GlobalConstants.h"
#include "MapList.h" // req. only by WriteConfig
#include "Version.h"
#include "sfx.h"

#include <stdio.h>
Expand All @@ -27,9 +28,6 @@ extern SoundsList *soundpacklist;

extern short joystickcount;

extern int32_t g_iVersion[];
extern bool VersionIsEqual(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild);


//[Keyboard/Joystick][Game/Menu][NumPlayers][NumKeys] left, right, jump, down, turbo, powerup, start, cancel
SDL_KEYTYPE controlkeys[2][2][4][NUM_KEYS] = { { { {SDLK_LEFT, SDLK_RIGHT, SDLK_UP, SDLK_DOWN, SDLK_RCTRL, SDLK_RSHIFT, SDLK_RETURN, SDLK_ESCAPE},
Expand Down Expand Up @@ -303,10 +301,13 @@ void CGameConfig::ReadBinaryConfig() {
if (!options.is_open())
throw std::runtime_error("Could not open " + options_path);

int32_t version[4];
options.read_i32_array(version, 4);
Version version;
version.major = options.read_i32();
version.minor = options.read_i32();
version.patch = options.read_i32();
version.build = options.read_i32();

if (!VersionIsEqual(g_iVersion, version[0], version[1], version[2], version[3])) {
if (GAME_VERSION != version) {
printf("Old options.bin detected. Skipped reading it.\n");
return;
}
Expand Down Expand Up @@ -449,7 +450,10 @@ void CGameConfig::WriteConfig() const
if (!options.is_open())
throw std::runtime_error("Could not open " + options_path);

options.write_raw(g_iVersion, sizeof(int) * 4);
options.write_i32(GAME_VERSION.major);
options.write_i32(GAME_VERSION.minor);
options.write_i32(GAME_VERSION.patch);
options.write_i32(GAME_VERSION.build);

options.write_u8(static_cast<uint8_t>(spawnstyle));
options.write_u8(static_cast<uint8_t>(awardstyle));
Expand Down
5 changes: 2 additions & 3 deletions src/common/MapList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#include "GameValues.h"
#include "linfunc.h"
#include "map.h"
#include "Version.h"

#include <cstdlib>
#include <cstring>
#include <stdexcept>

extern int32_t g_iVersion[];

extern CMap* g_map;
extern FiltersList* filterslist;
extern CGameValues game_values;
Expand Down Expand Up @@ -433,7 +432,7 @@ void MapList::WriteFilters()
continue;

fprintf(fp, "#Version\n");
fprintf(fp, "%d.%d.%d.%d\n\n", g_iVersion[0], g_iVersion[1], g_iVersion[2], g_iVersion[3]);
fprintf(fp, "%d.%d.%d.%d\n\n", GAME_VERSION.major, GAME_VERSION.minor, GAME_VERSION.patch, GAME_VERSION.build);

fprintf(fp, "#Icon\n");
fprintf(fp, "%d\n\n", game_values.piFilterIcons[iFilter + NUM_AUTO_FILTERS]);
Expand Down
60 changes: 0 additions & 60 deletions src/common/global.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "FileList.h"
#include "GameValues.h"
#include "Game.h"
#include "gfx.h"
#include "map.h"
#include "MapList.h"
Expand All @@ -11,15 +10,6 @@
#include <algorithm>
#include <array>

//1.8.0.0 == Release to staff
//1.8.0.1 == Second release to staff
//1.8.0.2 == beta1
//1.8.0.3 == beta2
//1.8.0.4 == final
//1.9.0.0 == neagix work-in-progress, not released
//2.0.0.0 == fluffypillow netplay code
int32_t g_iVersion[] = {2, 0, 0, 0};

// main game directory, read from command line argument
#ifdef __ANDROID__
std::string RootDataDirectory = GetHomeDirectory() + "data";
Expand All @@ -28,56 +18,6 @@ std::string RootDataDirectory = GetRootDirectory() + "data";
#endif

CResourceManager *rm;

bool VersionIsEqual(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild)
{
return iVersion[0] == iMajor && iVersion[1] == iMinor && iVersion[2] == iMicro && iVersion[3] == iBuild;
}

bool VersionIsEqualOrBefore(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild)
{
if (iVersion[0] < iMajor)
return true;

if (iVersion[0] == iMajor) {
if (iVersion[1] < iMinor)
return true;

if (iVersion[1] == iMinor) {
if (iVersion[2] < iMicro)
return true;

if (iVersion[2] == iMicro) {
return iVersion[3] <= iBuild;
}
}
}

return false;
}

bool VersionIsEqualOrAfter(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild)
{
if (iVersion[0] > iMajor)
return true;

if (iVersion[0] == iMajor) {
if (iVersion[1] > iMinor)
return true;

if (iVersion[1] == iMinor) {
if (iVersion[2] > iMicro)
return true;

if (iVersion[2] == iMicro) {
return iVersion[3] >= iBuild;
}
}
}

return false;
}

CGameValues game_values;

FiltersList *filterslist; //Filters list must be initiallized before maps list because it is used in maplist constructor
Expand Down
23 changes: 8 additions & 15 deletions src/common/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "RandomNumberGenerator.h"
#include "ResourceManager.h"
#include "TilesetManager.h"
#include "Version.h"

#include "SDL_image.h"
#include "sdl12wrapper.h"
Expand Down Expand Up @@ -39,11 +40,6 @@ using std::endl;


extern gfxSprite spr_frontmap[2];
// extern int32_t g_iVersion[];

extern bool VersionIsEqual(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild);
extern bool VersionIsEqualOrBefore(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild);
extern bool VersionIsEqualOrAfter(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild);

//Converts the tile type into the flags that this tile carries (solid + ice + death, etc)
short g_iTileTypeConversion[NUMTILETYPES] = {0, 1, 2, 5, 121, 9, 17, 33, 65, 6, 21, 37, 69, 3961, 265, 529, 1057, 2113, 4096};
Expand Down Expand Up @@ -581,23 +577,20 @@ void CMap::loadMap(const std::string& file, ReadType iReadType)
return;
}

//Load version number
int32_t version[4];
//version[0] = mapfile.read_i32(); //Major
//version[1] = mapfile.read_i32(); //Minor
//version[2] = mapfile.read_i32(); //Micro
//version[3] = mapfile.read_i32(); //Build
mapfile.read_i32_array(version, 4);
Version version;
version.major = mapfile.read_i32();
version.minor = mapfile.read_i32();
version.patch = mapfile.read_i32();
version.build = mapfile.read_i32();

if (iReadType != read_type_summary) {
cout << "loading map " << file;

if (iReadType == read_type_preview)
cout << " (preview)";

if (VersionIsEqualOrAfter(version, 1, 6, 0, 0)) {
cout << " [v" << version[0] << '.' << version[1] << '.'
<< version[2] << '.' << version[3] << "]";
if (version >= Version {1, 6, 0, 0}) {
printf(" [v%d.%d.%d.%d]", version.major, version.minor, version.patch, version.build);
}
else
cout << " [v1.5]";
Expand Down
23 changes: 12 additions & 11 deletions src/common/map/MapReader.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
#include "map/MapReader.h"

extern bool VersionIsEqualOrAfter(int32_t iVersion[], short iMajor, short iMinor, short iMicro, short iBuild);
#include "Version.h"

MapReader* MapReader::getLoaderByVersion(int32_t (&mapversion)[4])

MapReader* MapReader::getLoaderByVersion(const Version& mapversion)
{
/* v1.8.x readers */

if (VersionIsEqualOrAfter(mapversion, 1, 8, 0, 2))
if (mapversion >= Version {1, 8, 0, 2})
return new MapReader1802();

if (VersionIsEqualOrAfter(mapversion, 1, 8, 0, 1))
if (mapversion >= Version {1, 8, 0, 1})
return new MapReader1801();

if (VersionIsEqualOrAfter(mapversion, 1, 8, 0, 0))
if (mapversion >= Version {1, 8, 0, 0})
return new MapReader1800();

/* v1.7.x readers */

if (VersionIsEqualOrAfter(mapversion, 1, 7, 0, 2))
if (mapversion >= Version {1, 7, 0, 2})
return new MapReader1702();

if (VersionIsEqualOrAfter(mapversion, 1, 7, 0, 1))
if (mapversion >= Version {1, 7, 0, 1})
return new MapReader1701();

if (VersionIsEqualOrAfter(mapversion, 1, 7, 0, 0))
if (mapversion >= Version {1, 7, 0, 0})
return new MapReader1700();

/* 1.6.x readers */

if (VersionIsEqualOrAfter(mapversion, 1, 6, 1, 0))
if (mapversion >= Version {1, 6, 1, 0})
return new MapReader1610();

if (VersionIsEqualOrAfter(mapversion, 1, 6, 0, 10))
if (mapversion >= Version {1, 6, 0, 10})
return new MapReader160A();

if (VersionIsEqualOrAfter(mapversion, 1, 6, 0, 0))
if (mapversion >= Version {1, 6, 0, 0})
return new MapReader1600();

/* Return 1.5 reader */
Expand Down
3 changes: 2 additions & 1 deletion src/common/map/MapReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CMap;
class MovingPlatformPath;
struct TilesetTile;
struct MapTile;
struct Version;
enum ReadType: unsigned char;

class MapReader {
Expand All @@ -16,7 +17,7 @@ class MapReader {

virtual bool load(CMap&, BinaryFile&, ReadType) = 0;

static MapReader* getLoaderByVersion(int32_t (&mapversion)[4]);
static MapReader* getLoaderByVersion(const Version& mapversion);
};

/*
Expand Down
17 changes: 9 additions & 8 deletions src/smw/GSMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ui/MI_TourStop.h"
#include "ui/MI_World.h"
#include "world.h"
#include "Version.h"

#include "menu/BonusWheelMenu.h"
#include "menu/GameSettingsMenu.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ extern WorldMap g_worldmap;

extern void LoadCurrentMapBackground();

extern TourStop * ParseTourStopLine(char * buffer, int32_t iVersion[4], bool fIsWorld);
extern TourStop * ParseTourStopLine(char * buffer, const Version& version, bool fIsWorld);

extern CMap* g_map;

Expand Down Expand Up @@ -1476,7 +1477,7 @@ bool MenuState::ReadTourFile()

char buffer[256];
bool fReadVersion = false;
int32_t iVersion[4] = {0, 0, 0, 0};
Version version {0, 0, 0, 0};
while (fgets(buffer, 256, fp) && game_values.tourstoptotal < 10) {
if (strchr(ignorable_leads, buffer[0]))
continue;
Expand All @@ -1486,24 +1487,24 @@ bool MenuState::ReadTourFile()

char * psz = strtok(buffer, ".\n");
if (psz)
iVersion[0] = atoi(psz);
version.major = atoi(psz);

psz = strtok(NULL, ".\n");
if (psz)
iVersion[1] = atoi(psz);
version.minor = atoi(psz);

psz = strtok(NULL, ".\n");
if (psz)
iVersion[2] = atoi(psz);
version.patch = atoi(psz);

psz = strtok(NULL, ".\n");
if (psz)
iVersion[3] = atoi(psz);
version.build = atoi(psz);

continue;
}

TourStop * ts = ParseTourStopLine(buffer, iVersion, false);
TourStop * ts = ParseTourStopLine(buffer, version, false);

game_values.tourstops.push_back(ts);
game_values.tourstoptotal++;
Expand All @@ -1513,7 +1514,7 @@ bool MenuState::ReadTourFile()
mTourStopMenu->miTourStop->Refresh(game_values.tourstopcurrent);

//For old tours, turn on the bonus wheel at the end
if (iVersion[0] == 1 && iVersion[1] == 7 && iVersion[2] == 0 && iVersion[3] <= 1)
if (version.major == 1 && version.minor == 7 && version.patch == 0 && version.build <= 1)
game_values.tourstops[game_values.tourstoptotal - 1]->iBonusType = 1;
}

Expand Down
Loading

0 comments on commit 350f284

Please sign in to comment.