2023-11-11 00:39:08 +02:00
|
|
|
/*
|
|
|
|
* CSaveFile.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 "CSaveFile.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
CSaveFile::CSaveFile(const boost::filesystem::path &fname)
|
|
|
|
: serializer(this)
|
|
|
|
{
|
|
|
|
openNextFile(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
//must be instantiated in .cpp file for access to complete types of all member fields
|
|
|
|
CSaveFile::~CSaveFile() = default;
|
|
|
|
|
2024-02-02 02:36:57 +02:00
|
|
|
int CSaveFile::write(const std::byte * data, unsigned size)
|
2023-11-11 00:39:08 +02:00
|
|
|
{
|
2024-02-02 02:36:57 +02:00
|
|
|
sfile->write(reinterpret_cast<const char *>(data), size);
|
2023-11-11 00:39:08 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::openNextFile(const boost::filesystem::path &fname)
|
|
|
|
{
|
|
|
|
fName = fname;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sfile = std::make_unique<std::fstream>(fname.c_str(), 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
|
2024-01-20 20:34:51 +02:00
|
|
|
serializer & ESerializationVersion::CURRENT; //write format version
|
2023-11-11 00:39:08 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
logGlobal->error("Failed to save to %s", fname.string());
|
|
|
|
clear();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::reportState(vstd::CLoggerBase * out)
|
|
|
|
{
|
|
|
|
out->debug("CSaveFile");
|
|
|
|
if(sfile.get() && *sfile)
|
|
|
|
{
|
|
|
|
out->debug("\tOpened %s \tPosition: %d", fName, sfile->tellp());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::clear()
|
|
|
|
{
|
|
|
|
fName.clear();
|
|
|
|
sfile = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::putMagicBytes(const std::string &text)
|
|
|
|
{
|
2024-02-02 02:36:57 +02:00
|
|
|
write(reinterpret_cast<const std::byte*>(text.c_str()), text.length());
|
2023-11-11 00:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|