2016-09-10 02:28:11 +02:00
|
|
|
/*
|
|
|
|
* BinarySerializer.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 "BinarySerializer.h"
|
|
|
|
|
|
|
|
#include "../registerTypes/RegisterTypes.h"
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
extern template void registerTypes<BinarySerializer>(BinarySerializer & s);
|
|
|
|
|
2016-10-30 14:14:59 +02:00
|
|
|
CSaveFile::CSaveFile(const boost::filesystem::path &fname)
|
|
|
|
: serializer(this)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
registerTypes(serializer);
|
|
|
|
openNextFile(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSaveFile::~CSaveFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-30 14:14:59 +02:00
|
|
|
int CSaveFile::write(const void * data, unsigned size)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
sfile->write((char *)data,size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::openNextFile(const boost::filesystem::path &fname)
|
|
|
|
{
|
|
|
|
fName = fname;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sfile = make_unique<FileStream>(fname, std::ios::out | std::ios::binary);
|
|
|
|
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
|
|
|
|
|
|
|
|
if(!(*sfile))
|
|
|
|
THROW_FORMAT("Error: cannot open to write %s!", fname);
|
|
|
|
|
|
|
|
sfile->write("VCMI",4); //write magic identifier
|
2016-10-29 18:52:19 +02:00
|
|
|
serializer & SERIALIZATION_VERSION; //write format version
|
2016-09-10 02:28:11 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << "Failed to save to " << fname;
|
|
|
|
clear();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::reportState(CLogger * out)
|
|
|
|
{
|
|
|
|
out->debugStream() << "CSaveFile";
|
|
|
|
if(sfile.get() && *sfile)
|
|
|
|
{
|
|
|
|
out->debugStream() << "\tOpened " << fName << "\n\tPosition: " << sfile->tellp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::clear()
|
|
|
|
{
|
|
|
|
fName.clear();
|
|
|
|
sfile = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-30 14:14:59 +02:00
|
|
|
void CSaveFile::putMagicBytes(const std::string &text)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
write(text.c_str(), text.length());
|
|
|
|
}
|