1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-15 00:05:02 +02:00
Files
.github
AI
CI
Mods
android
client
clientapp
cmake_modules
config
debian
docker
docs
include
ios
launcher
lib
battle
bonuses
campaign
constants
entities
events
filesystem
gameState
json
logging
mapObjectConstructors
mapObjects
mapping
minizip
modding
network
networkPacks
pathfinder
rewardable
rmg
serializer
BinaryDeserializer.cpp
BinaryDeserializer.h
BinarySerializer.cpp
BinarySerializer.h
CLoadFile.cpp
CLoadFile.h
CMemorySerializer.cpp
CMemorySerializer.h
CSaveFile.cpp
CSaveFile.h
CSerializer.cpp
CSerializer.h
CTypeList.cpp
CTypeList.h
Connection.cpp
Connection.h
ESerializationVersion.h
JsonDeserializer.cpp
JsonDeserializer.h
JsonSerializeFormat.cpp
JsonSerializeFormat.h
JsonSerializer.cpp
JsonSerializer.h
JsonTreeSerializer.h
JsonUpdater.cpp
JsonUpdater.h
RegisterTypes.h
Serializeable.h
SerializerReflection.cpp
SerializerReflection.h
spells
texts
vstd
AI_Base.h
ArtifactUtils.cpp
ArtifactUtils.h
AsyncRunner.h
BasicTypes.cpp
BattleFieldHandler.cpp
BattleFieldHandler.h
CAndroidVMHelper.cpp
CAndroidVMHelper.h
CArtHandler.cpp
CArtHandler.h
CArtifactInstance.cpp
CArtifactInstance.h
CBonusTypeHandler.cpp
CBonusTypeHandler.h
CConfigHandler.cpp
CConfigHandler.h
CConsoleHandler.cpp
CConsoleHandler.h
CCreatureHandler.cpp
CCreatureHandler.h
CCreatureSet.cpp
CCreatureSet.h
CGameInfoCallback.cpp
CGameInfoCallback.h
CGameInterface.cpp
CGameInterface.h
CMakeLists.txt
CPlayerState.cpp
CPlayerState.h
CRandomGenerator.cpp
CRandomGenerator.h
CScriptingModule.cpp
CScriptingModule.h
CSkillHandler.cpp
CSkillHandler.h
CSoundBase.h
CStack.cpp
CStack.h
CStopWatch.h
CThreadHelper.cpp
CThreadHelper.h
Color.h
ConditionalWait.h
ConstTransitivePtr.h
ExceptionsCommon.h
ExtraOptionsInfo.cpp
ExtraOptionsInfo.h
FunctionList.h
GameCallbackHolder.h
GameConstants.h
GameLibrary.cpp
GameLibrary.h
GameSettings.cpp
GameSettings.h
IBonusTypeHandler.h
IGameCallback.cpp
IGameCallback.h
IGameEventsReceiver.h
IGameSettings.h
IHandlerBase.cpp
IHandlerBase.h
LoadProgress.cpp
LoadProgress.h
LogicalExpression.cpp
LogicalExpression.h
ObstacleHandler.cpp
ObstacleHandler.h
Point.h
Rect.cpp
Rect.h
ResourceSet.cpp
ResourceSet.h
RiverHandler.cpp
RiverHandler.h
RoadHandler.cpp
RoadHandler.h
ScopeGuard.h
ScriptHandler.cpp
ScriptHandler.h
StartInfo.cpp
StartInfo.h
StdInc.cpp
StdInc.h
TerrainHandler.cpp
TerrainHandler.h
TurnTimerInfo.cpp
TurnTimerInfo.h
UnlockGuard.h
VCMIDirs.cpp
VCMIDirs.h
int3.h
vcmi_endian.h
lobby
mapeditor
osx
rpm
scripting
scripts
server
serverapp
test
vcmiqt
win
xcode
.editorconfig
.gitattributes
.gitignore
.gitmodules
.travis.yml
AUTHORS.h
CCallback.cpp
CCallback.h
CMakeLists.txt
CMakePresets.json
ChangeLog.md
Global.h
Version.cpp.in
Version.h
conanfile.py
fuzzylite.pc.in
license.txt
vcmibuilder
vcmi/lib/serializer/CSaveFile.cpp

73 lines
1.6 KiB
C++
Raw Normal View History

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
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