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"
|
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
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-11 17:13:21 +02:00
|
|
|
//must be instantiated in .cpp file for access to complete types of all member fields
|
|
|
|
CSaveFile::~CSaveFile() = default;
|
2016-09-10 02:28:11 +02:00
|
|
|
|
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
|
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
sfile = std::make_unique<FileStream>(fname, std::ios::out | std::ios::binary);
|
2016-09-10 02:28:11 +02:00
|
|
|
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(...)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("Failed to save to %s", fname.string());
|
2016-09-10 02:28:11 +02:00
|
|
|
clear();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
void CSaveFile::reportState(vstd::CLoggerBase * out)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
2017-08-10 18:39:27 +02:00
|
|
|
out->debug("CSaveFile");
|
2016-09-10 02:28:11 +02:00
|
|
|
if(sfile.get() && *sfile)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
out->debug("\tOpened %s \tPosition: %d", fName, sfile->tellp());
|
2016-09-10 02:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2023-02-11 17:13:21 +02:00
|
|
|
write(text.c_str(), static_cast<unsigned int>(text.length()));
|
2016-09-10 02:28:11 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|