2016-09-10 02:28:11 +02:00
|
|
|
/*
|
|
|
|
* CLoadIntegrityValidator.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CLoadIntegrityValidator.h"
|
2017-08-11 19:03:05 +02:00
|
|
|
#include "../filesystem/FileStream.h"
|
2017-07-13 10:26:03 +02:00
|
|
|
|
|
|
|
#include "../registerTypes/RegisterTypes.h"
|
2016-09-10 02:28:11 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CLoadIntegrityValidator::CLoadIntegrityValidator(const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion)
|
2016-09-10 02:28:11 +02:00
|
|
|
: serializer(this), foundDesync(false)
|
|
|
|
{
|
|
|
|
registerTypes(serializer);
|
2022-12-07 23:36:20 +02:00
|
|
|
primaryFile = std::make_unique<CLoadFile>(primaryFileName, minimalVersion);
|
|
|
|
controlFile = std::make_unique<CLoadFile>(controlFileName, minimalVersion);
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
assert(primaryFile->serializer.fileVersion == controlFile->serializer.fileVersion);
|
|
|
|
serializer.fileVersion = primaryFile->serializer.fileVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLoadIntegrityValidator::read( void * data, unsigned size )
|
|
|
|
{
|
|
|
|
assert(primaryFile);
|
|
|
|
assert(controlFile);
|
|
|
|
|
|
|
|
if(!size)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
std::vector<ui8> controlData(size);
|
|
|
|
auto ret = primaryFile->read(data, size);
|
|
|
|
|
|
|
|
if(!foundDesync)
|
|
|
|
{
|
|
|
|
controlFile->read(controlData.data(), size);
|
|
|
|
if(std::memcmp(data, controlData.data(), size))
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Desync found! Position: %d", primaryFile->sfile->tellg());
|
2016-09-10 02:28:11 +02:00
|
|
|
foundDesync = true;
|
|
|
|
//throw std::runtime_error("Savegame dsynchronized!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CLoadFile> CLoadIntegrityValidator::decay()
|
|
|
|
{
|
|
|
|
primaryFile->serializer.loadedPointers = this->serializer.loadedPointers;
|
|
|
|
primaryFile->serializer.loadedPointersTypes = this->serializer.loadedPointersTypes;
|
|
|
|
return std::move(primaryFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLoadIntegrityValidator::checkMagicBytes( const std::string &text )
|
|
|
|
{
|
|
|
|
assert(primaryFile);
|
|
|
|
assert(controlFile);
|
|
|
|
|
|
|
|
primaryFile->checkMagicBytes(text);
|
|
|
|
controlFile->checkMagicBytes(text);
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|