Skip to content

Commit

Permalink
Add test to reproduce the Ware deserialization failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire authored and Flow86 committed Aug 19, 2020
1 parent da8fdd9 commit e38020a
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 195 deletions.
2 changes: 1 addition & 1 deletion libs/s25main/Replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ bool Replay::LoadGameData(MapInfo& mapInfo)
case MAPTYPE_SAVEGAME:
// Load savegame
mapInfo.savegame = std::make_unique<Savegame>();
if(!mapInfo.savegame->Load(file, true, true))
if(!mapInfo.savegame->Load(file, SaveGameDataToLoad::All))
{
lastErrorMsg = std::string(_("Savegame error: ")) + mapInfo.savegame->GetLastErrorMsg();
return false;
Expand Down
14 changes: 8 additions & 6 deletions libs/s25main/Savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ bool Savegame::Save(BinaryFile& file, const std::string& mapName)
return true;
}

bool Savegame::Load(const std::string& filePath, bool loadSettings, bool loadGameData)
bool Savegame::Load(const std::string& filePath, const SaveGameDataToLoad what)
{
BinaryFile file;

return file.Open(filePath, OFM_READ) && Load(file, loadSettings, loadGameData);
return file.Open(filePath, OFM_READ) && Load(file, what);
}

bool Savegame::Load(BinaryFile& file, bool loadSettings, bool loadGameData)
bool Savegame::Load(BinaryFile& file, const SaveGameDataToLoad what)
{
try
{
Expand All @@ -68,14 +68,16 @@ bool Savegame::Load(BinaryFile& file, bool loadSettings, bool loadGameData)
if(!ReadAllHeaderData(file))
return false;

if(!loadSettings)
if(what == SaveGameDataToLoad::Header)
return true;

ReadPlayerData(file);
ReadGGS(file);

if(loadGameData)
ReadGameData(file);
if(what == SaveGameDataToLoad::HeaderAndSettings)
return true;

ReadGameData(file);
} catch(std::runtime_error& e)
{
lastErrorMsg = e.what();
Expand Down
11 changes: 9 additions & 2 deletions libs/s25main/Savegame.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
#include "SerializedGameData.h"
class BinaryFile;

enum class SaveGameDataToLoad
{
Header,
HeaderAndSettings,
All
};

class Savegame : public SavedFile
{
public:
Expand All @@ -37,8 +44,8 @@ class Savegame : public SavedFile
bool Save(BinaryFile& file, const std::string& mapName);

/// Lädt Savegame oder Teile davon
bool Load(const std::string& filePath, bool loadSettings, bool loadGameData);
bool Load(BinaryFile& file, bool loadSettings, bool loadGameData);
bool Load(const std::string& filePath, SaveGameDataToLoad what);
bool Load(BinaryFile& file, SaveGameDataToLoad what);

void WriteExtHeader(BinaryFile& file, const std::string& mapName) override;
bool ReadExtHeader(BinaryFile& file) override;
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/desktops/dskSinglePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void dskSinglePlayer::Msg_ButtonClick(const unsigned ctrl_id)
Savegame save;

// Datei öffnen
if(!save.Load(savFile.string(), false, false))
if(!save.Load(savFile.string(), SaveGameDataToLoad::Header))
continue;

if(save.GetSaveTime() > recent)
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/ingameWindows/iwSave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void iwSaveLoad::RefreshTable()
Savegame save;

// Datei öffnen
if(!save.Load(saveFile.string(), false, false))
if(!save.Load(saveFile.string(), SaveGameDataToLoad::Header))
{
// Show errors only first time this is loaded
if(!loadedOnce)
Expand Down
4 changes: 2 additions & 2 deletions libs/s25main/network/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void GameClient::StartGame(const unsigned random_init)
// Random-Generator initialisieren
RANDOM.Init(random_init);

if(!IsReplayModeOn() && mapinfo.savegame && !mapinfo.savegame->Load(mapinfo.filepath, true, true))
if(!IsReplayModeOn() && mapinfo.savegame && !mapinfo.savegame->Load(mapinfo.filepath, SaveGameDataToLoad::All))
{
OnError(CE_INVALID_MAP);
return;
Expand Down Expand Up @@ -999,7 +999,7 @@ bool GameClient::CreateLobby()
break;
case MAPTYPE_SAVEGAME:
mapinfo.savegame = std::make_unique<Savegame>();
if(!mapinfo.savegame->Load(mapinfo.filepath, true, false))
if(!mapinfo.savegame->Load(mapinfo.filepath, SaveGameDataToLoad::HeaderAndSettings))
return false;

numPlayers = mapinfo.savegame->GetNumPlayers();
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/network/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool GameServer::Start(const CreateServerInfo& csi, const std::string& map_path,
{
Savegame save;

if(!save.Load(mapinfo.filepath, true, false))
if(!save.Load(mapinfo.filepath, SaveGameDataToLoad::HeaderAndSettings))
return false;

// Spieleranzahl
Expand Down
12 changes: 3 additions & 9 deletions libs/s25main/nodeObjs/noFlag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

noFlag::noFlag(const MapPoint pos, const unsigned char player) : noRoadNode(NOP_FLAG, pos, player), ani_offset(rand() % 20000)
{
for(auto& ware : wares)
ware = nullptr;
wares = {};

// BWUs nullen
for(auto& bwu : bwus)
Expand Down Expand Up @@ -129,7 +128,7 @@ void noFlag::Draw(DrawPoint drawPt)
LOADER.flag_cache[gwg->GetPlayer(player).nation][flagtype][ani_step].draw(drawPt, 0xFFFFFFFF, gwg->GetPlayer(player).color);

// Waren (von hinten anfangen zu zeichnen)
for(unsigned i = 8; i; --i)
for(unsigned i = wares.size(); i; --i)
{
if(wares[i - 1])
LOADER.GetMapImageN(2200 + wares[i - 1]->type)->DrawFull(drawPt + WARES_POS[i - 1]);
Expand Down Expand Up @@ -171,12 +170,7 @@ void noFlag::AddWare(Ware*& ware)
*/
unsigned noFlag::GetNumWares() const
{
unsigned count = 0;
for(auto ware : wares)
if(ware)
++count;

return count;
return static_cast<unsigned>(std::count_if(wares.begin(), wares.end(), [](const auto* ware) { return ware; }));
}

/**
Expand Down
Loading

0 comments on commit e38020a

Please sign in to comment.