1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/*
* CLoadFile.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
*
*/
#include "StdInc.h"
#include "CLoadFile.h"
VCMI_LIB_NAMESPACE_BEGIN
CLoadFile::CLoadFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
: serializer(this)
{
openNextFile(fname, minimalVersion);
}
//must be instantiated in .cpp file for access to complete types of all member fields
CLoadFile::~CLoadFile() = default;
int CLoadFile::read(std::byte * data, unsigned size)
{
sfile->read(reinterpret_cast<char *>(data), size);
return size;
}
void CLoadFile::openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
{
serializer.loadingGamestate = true;
assert(!serializer.reverseEndianness);
assert(minimalVersion <= ESerializationVersion::CURRENT);
try
{
fName = fname.string();
sfile = std::make_unique<std::fstream>(fname.c_str(), std::ios::in | std::ios::binary);
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
if(!(*sfile))
THROW_FORMAT("Error: cannot open to read %s!", fName);
//we can read
char buffer[4];
sfile->read(buffer, 4);
if(std::memcmp(buffer, "VCMI", 4) != 0)
THROW_FORMAT("Error: not a VCMI file(%s)!", fName);
serializer & serializer.version;
if(serializer.version < minimalVersion)
THROW_FORMAT("Error: too old file format (%s)!", fName);
if(serializer.version > ESerializationVersion::CURRENT)
{
logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", vstd::to_underlying(serializer.version), vstd::to_underlying(ESerializationVersion::CURRENT), fName);
auto * versionptr = reinterpret_cast<char *>(&serializer.version);
std::reverse(versionptr, versionptr + 4);
logGlobal->warn("Version number reversed is %x, checking...", vstd::to_underlying(serializer.version));
if(serializer.version == ESerializationVersion::CURRENT)
{
logGlobal->warn("%s seems to have different endianness! Entering reversing mode.", fname.string());
serializer.reverseEndianness = true;
}
else
THROW_FORMAT("Error: too new file format (%s)!", fName);
}
}
catch(...)
{
clear(); //if anything went wrong, we delete file and rethrow
throw;
}
}
void CLoadFile::reportState(vstd::CLoggerBase * out)
{
out->debug("CLoadFile");
if(!!sfile && *sfile)
out->debug("\tOpened %s Position: %d", fName, sfile->tellg());
}
void CLoadFile::clear()
{
sfile = nullptr;
fName.clear();
serializer.version = ESerializationVersion::NONE;
}
void CLoadFile::checkMagicBytes(const std::string &text)
{
std::string loaded = text;
read(reinterpret_cast<std::byte*>(loaded.data()), text.length());
if(loaded != text)
throw std::runtime_error("Magic bytes doesn't match!");
}
VCMI_LIB_NAMESPACE_END
|