libcmo21/LibCmo/CK2/CKFileOthers.cpp

298 lines
7.4 KiB
C++
Raw Normal View History

2023-08-25 17:35:45 +08:00
#include "CKFile.hpp"
#include "CKStateChunk.hpp"
2023-08-28 17:04:28 +08:00
#include "ObjImpls/CKObject.hpp"
2023-08-25 17:35:45 +08:00
#include <cstdarg>
namespace LibCmo::CK2 {
#pragma region CKFileObject
// CKObject is managed by CKMinContext,
// so we do not considering its memory leak
// however, we need process CKStateChunk.
CKFileObject::CKFileObject() :
ObjectId(0u), CreatedObjectId(0u), ObjectCid(CK_CLASSID::CKCID_OBJECT),
ObjPtr(nullptr), Name(), Data(nullptr), Options(CK_FO_OPTIONS::CK_FO_DEFAULT),
2023-08-28 17:04:28 +08:00
FileIndex(0u), SaveFlags(CK_STATESAVE_ALL), PackSize(0u) {}
2023-08-25 17:35:45 +08:00
CKFileObject::CKFileObject(const CKFileObject& rhs) :
ObjectId(rhs.ObjectId), CreatedObjectId(rhs.CreatedObjectId), ObjectCid(rhs.ObjectCid),
ObjPtr(rhs.ObjPtr), Name(rhs.Name), Data(rhs.Data), Options(rhs.Options),
2023-08-28 17:04:28 +08:00
FileIndex(rhs.FileIndex), SaveFlags(rhs.SaveFlags), PackSize(rhs.PackSize) {
2023-08-25 17:35:45 +08:00
if (this->Data != nullptr) {
this->Data = new CKStateChunk(*(rhs.Data));
}
}
CKFileObject::CKFileObject(CKFileObject&& rhs) :
ObjectId(rhs.ObjectId), CreatedObjectId(rhs.CreatedObjectId), ObjectCid(rhs.ObjectCid),
ObjPtr(rhs.ObjPtr), Name(rhs.Name), Data(rhs.Data), Options(rhs.Options),
2023-08-28 17:04:28 +08:00
FileIndex(rhs.FileIndex), SaveFlags(rhs.SaveFlags), PackSize(rhs.PackSize) {
2023-08-25 17:35:45 +08:00
rhs.Data = nullptr;
}
CKFileObject& CKFileObject::operator=(const CKFileObject& rhs) {
this->ObjectId = rhs.ObjectId;
this->CreatedObjectId = rhs.CreatedObjectId;
this->ObjectCid = rhs.ObjectCid;
this->ObjPtr = rhs.ObjPtr;
this->Name = rhs.Name;
this->Data = rhs.Data;
if (this->Data != nullptr) {
this->Data = new CKStateChunk(*(rhs.Data));
}
2023-08-28 17:04:28 +08:00
this->PackSize = rhs.PackSize;
2023-08-25 17:35:45 +08:00
this->Options = rhs.Options;
this->FileIndex = rhs.FileIndex;
this->SaveFlags = rhs.SaveFlags;
return *this;
}
CKFileObject& CKFileObject::operator=(CKFileObject&& rhs) {
this->ObjectId = rhs.ObjectId;
this->CreatedObjectId = rhs.CreatedObjectId;
this->ObjectCid = rhs.ObjectCid;
this->ObjPtr = rhs.ObjPtr;
this->Name = rhs.Name;
this->Data = rhs.Data;
rhs.Data = nullptr;
2023-08-28 17:04:28 +08:00
this->PackSize = rhs.PackSize;
2023-08-25 17:35:45 +08:00
this->Options = rhs.Options;
this->FileIndex = rhs.FileIndex;
this->SaveFlags = rhs.SaveFlags;
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
CKFileObject::~CKFileObject() {
if (Data != nullptr) delete Data;
}
#pragma endregion
#pragma region CKFileManagerData
CKFileManagerData::CKFileManagerData() :
Data(nullptr), Manager(0u, 0u) {}
CKFileManagerData::CKFileManagerData(const CKFileManagerData& rhs) :
Data(rhs.Data), Manager(rhs.Manager) {
if (this->Data != nullptr) {
this->Data = new CKStateChunk(*(rhs.Data));
}
}
CKFileManagerData::CKFileManagerData(CKFileManagerData&& rhs) :
Data(rhs.Data), Manager(rhs.Manager) {
rhs.Data = nullptr;
}
CKFileManagerData& CKFileManagerData::operator=(const CKFileManagerData& rhs) {
this->Manager = rhs.Manager;
this->Data = rhs.Data;
if (this->Data != nullptr) {
this->Data = new CKStateChunk(*(rhs.Data));
}
return *this;
}
CKFileManagerData& CKFileManagerData::operator=(CKFileManagerData&& rhs) {
this->Manager = rhs.Manager;
this->Data = rhs.Data;
rhs.Data = nullptr;
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
CKFileManagerData::~CKFileManagerData() {
if (Data != nullptr) delete Data;
}
#pragma endregion
2023-08-25 21:57:22 +08:00
#pragma region CKFileReader
2023-08-25 17:35:45 +08:00
2023-08-25 21:57:22 +08:00
CKFileReader::CKFileReader(CKContext* ctx) :
m_Ctx(ctx), m_Visitor(this),
m_Done(false),
m_SaveIDMax(0),
m_FileObjects(), m_ManagersData(), m_PluginsDep(), m_IncludedFiles(),
m_FileInfo() {}
2023-08-25 17:35:45 +08:00
2023-08-25 21:57:22 +08:00
CKFileReader::~CKFileReader() {}
2023-08-25 17:35:45 +08:00
2023-08-26 20:34:51 +08:00
CKINT CKFileReader::GetSaveIdMax() {
return m_SaveIDMax;
}
2023-08-25 21:57:22 +08:00
const XContainer::XArray<CKFileObject>& CKFileReader::GetFileObjects() {
return m_FileObjects;
2023-08-25 17:35:45 +08:00
}
2023-08-26 20:34:51 +08:00
const XContainer::XArray<CKFileManagerData>& CKFileReader::GetManagersData() {
return m_ManagersData;
}
const XContainer::XArray<CKFilePluginDependencies>& CKFileReader::GetPluginsDep() {
return m_PluginsDep;
}
2023-08-25 21:57:22 +08:00
const XContainer::XArray<XContainer::XString>& CKFileReader::GetIncludedFiles() {
return m_IncludedFiles;
}
2023-08-25 17:35:45 +08:00
2023-08-26 20:34:51 +08:00
const CKFileInfo CKFileReader::GetFileInfo() {
return m_FileInfo;
}
2023-08-25 17:35:45 +08:00
#pragma endregion
2023-08-25 21:57:22 +08:00
#pragma region CKFileWriter
CKFileWriter::CKFileWriter(CKContext* ctx) :
2023-08-28 14:18:58 +08:00
m_Ctx(ctx), m_Visitor(this),
m_Done(false), m_IsCopyFromReader(false),
2023-08-28 17:04:28 +08:00
m_SaveIDMax(0),
2023-08-28 21:21:40 +08:00
m_FileObjects(), m_ManagersData(), m_PluginsDep(), m_IncludedFiles(),
m_FileInfo()
2023-08-28 14:18:58 +08:00
{}
2023-08-25 21:57:22 +08:00
CKFileWriter::CKFileWriter(CKContext* ctx, CKFileReader* reader) :
2023-08-28 14:18:58 +08:00
m_Ctx(ctx), m_Visitor(this),
m_Done(false), m_IsCopyFromReader(true),
2023-08-28 17:04:28 +08:00
m_SaveIDMax(0),
2023-08-28 21:21:40 +08:00
m_FileObjects(), m_ManagersData(), m_PluginsDep(), m_IncludedFiles(),
m_FileInfo()
2023-08-28 14:18:58 +08:00
{
2023-08-28 17:04:28 +08:00
// sync save id max
this->m_SaveIDMax = reader->GetSaveIdMax();
2023-08-28 14:18:58 +08:00
// copy objects
for (const auto& item : reader->GetFileObjects()) {
CKFileObject obj;
// copy CKObject pointer
obj.ObjPtr = item.ObjPtr;
// and use ctor to copy CKStateChunk
if (item.Data == nullptr) {
obj.Data = nullptr;
} else {
obj.Data = new CKStateChunk(*item.Data);
}
2023-08-28 17:04:28 +08:00
// copy other data
obj.ObjectId = item.ObjectId;
obj.ObjectCid = item.ObjectCid;
2023-08-28 14:18:58 +08:00
obj.SaveFlags = item.SaveFlags;
2023-08-28 17:04:28 +08:00
obj.Name = item.Name;
2023-08-28 14:18:58 +08:00
// insert
m_FileObjects.emplace_back(std::move(obj));
}
// copy managers
for (const auto& item : reader->GetManagersData()) {
CKFileManagerData mgr;
// copy guid
mgr.Manager = item.Manager;
// copy chunk
if (item.Data == nullptr) {
mgr.Data = nullptr;
} else {
mgr.Data = new CKStateChunk(*item.Data);
}
// insert
m_ManagersData.emplace_back(std::move(mgr));
}
// copy plugin dep
for (const auto& item : reader->GetPluginsDep()) {
// direct copy
m_PluginsDep.emplace_back(item);
}
// copy included file
for (const auto& item : reader->GetIncludedFiles()) {
// direct copy
m_IncludedFiles.emplace_back(item);
}
}
2023-08-25 17:35:45 +08:00
2023-08-25 21:57:22 +08:00
CKFileWriter::~CKFileWriter() {}
CKBOOL CKFileWriter::AddSavedObject(ObjImpls::CKObject* obj, CKDWORD flags) {
2023-08-28 14:18:58 +08:00
if (m_Done || m_IsCopyFromReader) return CKFALSE;
2023-08-25 21:57:22 +08:00
return CKFALSE;
2023-08-25 17:35:45 +08:00
}
2023-08-25 21:57:22 +08:00
CKBOOL CKFileWriter::AddSavedObjects(CKObjectArray* objarray, CKDWORD flags) {
2023-08-28 14:18:58 +08:00
if (m_Done || m_IsCopyFromReader) return CKFALSE;
2023-08-25 21:57:22 +08:00
return CKFALSE;
}
2023-08-25 17:35:45 +08:00
2023-08-25 21:57:22 +08:00
CKBOOL CKFileWriter::AddSavedFile(CKSTRING u8FileName) {
2023-08-28 14:18:58 +08:00
if (m_Done || m_IsCopyFromReader) return CKFALSE;
2023-08-25 21:57:22 +08:00
return CKFALSE;
}
2023-08-25 17:35:45 +08:00
#pragma endregion
2023-08-25 21:57:22 +08:00
#pragma region CKFileVisitor
CKFileVisitor::CKFileVisitor(CKFileReader* reader) :
m_IsReader(CKTRUE), m_Reader(reader), m_Writer(nullptr) {
if (reader == nullptr) LIBPANIC("Reader is nullptr.");
}
CKFileVisitor::CKFileVisitor(CKFileWriter* writer) :
m_IsReader(false), m_Reader(nullptr), m_Writer(nullptr) {
if (writer == nullptr) LIBPANIC("Writer is nullptr.");
}
CKFileVisitor::CKFileVisitor(const CKFileVisitor& rhs) :
m_IsReader(rhs.m_IsReader), m_Reader(rhs.m_Reader), m_Writer(rhs.m_Writer) {}
CKFileVisitor::CKFileVisitor(CKFileVisitor&& rhs) :
m_IsReader(rhs.m_IsReader), m_Reader(rhs.m_Reader), m_Writer(rhs.m_Writer) {}
CKFileVisitor& CKFileVisitor::operator=(const CKFileVisitor& rhs) {
this->m_IsReader = rhs.m_IsReader;
this->m_Reader = rhs.m_Reader;
this->m_Writer = rhs.m_Writer;
return *this;
}
CKFileVisitor& CKFileVisitor::operator=(CKFileVisitor&& rhs) {
this->m_IsReader = rhs.m_IsReader;
this->m_Reader = rhs.m_Reader;
this->m_Writer = rhs.m_Writer;
return *this;
}
const CKFileObject* CKFileVisitor::GetFileObjectByIndex(size_t index) {
if (m_IsReader) {
return &m_Reader->m_FileObjects[index];
} else {
return nullptr;
}
}
#pragma endregion
2023-08-25 17:35:45 +08:00
}