From ad9d0cadce350fdeb27c86e2c9c9ab45ce1c5abf Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 23 Aug 2023 16:04:58 +0800 Subject: [PATCH] refactor(2/?) --- LibCmo/CK2/CKContext.cpp | 204 ++++++------- LibCmo/CK2/CKContext.hpp | 54 +++- LibCmo/CK2/CKDefines.hpp | 55 +++- LibCmo/CK2/CKEnums.hpp | 34 ++- LibCmo/CK2/CKGlobals.cpp | 189 +++++++++++- LibCmo/CK2/CKGlobals.hpp | 21 +- LibCmo/CK2/CKIdentifiers.hpp | 43 +-- .../CKBaseManager.hpp | 0 LibCmo/CK2/CKObject.hpp | 244 ---------------- LibCmo/CK2/CKObjectImplements/CKObject.cpp | 207 ++++++------- LibCmo/CK2/CKObjectImplements/CKObject.hpp | 275 ++++++++++++++++++ LibCmo/CK2/CKTypes.hpp | 4 +- LibCmo/LibCmo.vcxproj | 12 +- LibCmo/LibCmo.vcxproj.filters | 4 +- LibCmo/VTUtils.cpp | 4 + LibCmo/VTUtils.hpp | 73 ++--- 16 files changed, 857 insertions(+), 566 deletions(-) rename LibCmo/CK2/{ => CKManagerImplements}/CKBaseManager.hpp (100%) delete mode 100644 LibCmo/CK2/CKObject.hpp create mode 100644 LibCmo/CK2/CKObjectImplements/CKObject.hpp diff --git a/LibCmo/CK2/CKContext.cpp b/LibCmo/CK2/CKContext.cpp index 2839d15..7339096 100644 --- a/LibCmo/CK2/CKContext.cpp +++ b/LibCmo/CK2/CKContext.cpp @@ -1,4 +1,5 @@ #include "CKContext.hpp" +#include "CKObjectImplements/CKObject.hpp" #include namespace LibCmo::CK2 { @@ -9,59 +10,112 @@ namespace LibCmo::CK2 { static char g_UniqueFolder[] = "LibCmo"; #endif +#pragma region Objects Management + + CKObject* CKContext::CreateCKObject(CK_CLASSID cls, CKSTRING name, + CK_OBJECTCREATION_OPTIONS options = CK_OBJECTCREATION_OPTIONS::CK_OBJECTCREATION_NONAMECHECK, + CK_CREATIONMODE* res = nullptr) { + // todo: Process paramter options and res + + // get description first + const CKClassDesc* desc = CKGetClassDesc(cls); + if (desc == nullptr) return nullptr; + + // allocate a CK_ID first + CK_ID decided_id; + if (this->m_ReturnedObjectIds.empty()) { + // create new CK_ID. + decided_id = m_ObjectsList.size(); + m_ObjectsList.resize(decided_id + 1); + } else { + // use returned CK_ID. + decided_id = m_ReturnedObjectIds.back(); + m_ReturnedObjectIds.pop_back(); + } + + // create one + CKObject* obj = desc->CreationFct(this, decided_id, name); + + // put into slot + m_ObjectsList[decided_id] = obj; + } + + CKObject* CKContext::GetCKObject(CK_ID id) { + if (id >= m_ObjectsList.size()) return nullptr; + return m_ObjectsList[id]; + } + + /** + * @brief The real CKObject destroy worker shared by CKContext::DestroyCKObject and CKContext::~CKContext + * @param[in] ctx The CKContext + * @param[in] obj The CKObject need to be free. + */ + static void InternalDestroy(CKContext* ctx, CKObject* obj) { + // find desc by classid + // if really we can not find it, we only can delete it directly. + const CKClassDesc* desc = CKGetClassDesc(obj->GetClassID()); + if (desc == nullptr) { + delete obj; + return; + } + + // free it and return its value + desc->ReleaseFct(ctx, obj); + } + void CKContext::DestroyCKObject(CK_ID id) { + if (id >= m_ObjectsList.size()) return; + + // get object and free it + CKObject* obj = m_ObjectsList[id]; + if (obj == nullptr) return; + InternalDestroy(this, obj); + + // return its allocated id. + m_ObjectsList[id] = nullptr; + m_ReturnedObjectIds.emplace_back(id); + + } + +#pragma endregion + #pragma region Ctor Dtor - CKMinContext::CKMinContext() : + CKContext::CKContext() : + m_ObjectsList(), m_ReturnedObjectIds(), m_NameEncoding(), m_TempFolder(), - m_PrintCallback(nullptr), - m_CKObjectMaxID(0u), - // register CKObjects - m_ObjectsCreationMap{ - {CK_CLASSID::CKCID_OBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKObject(ctx, id, name); })}, - {CK_CLASSID::CKCID_SCENEOBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKSceneObject(ctx, id, name); })}, - {CK_CLASSID::CKCID_BEOBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBeObject(ctx, id, name); })}, - - {CK_CLASSID::CKCID_GROUP, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKGroup(ctx, id, name); })}, - {CK_CLASSID::CKCID_MESH, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMesh(ctx, id, name); })}, - {CK_CLASSID::CKCID_TEXTURE, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKTexture(ctx, id, name); })}, - {CK_CLASSID::CKCID_MATERIAL, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMaterial(ctx, id, name); })}, - {CK_CLASSID::CKCID_RENDEROBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKRenderObject(ctx, id, name); })}, - {CK_CLASSID::CKCID_3DENTITY, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CK3dEntity(ctx, id, name); })}, - - {CK_CLASSID::CKCID_PARAMETERIN, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterIn(ctx, id, name); })}, - {CK_CLASSID::CKCID_PARAMETER, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameter(ctx, id, name); })}, - {CK_CLASSID::CKCID_PARAMETEROUT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterOut(ctx, id, name); })}, - {CK_CLASSID::CKCID_PARAMETERLOCAL, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterLocal(ctx, id, name); })}, - {CK_CLASSID::CKCID_PARAMETEROPERATION, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterOperation(ctx, id, name); })}, - {CK_CLASSID::CKCID_BEHAVIORLINK, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehaviorLink(ctx, id, name); })}, - {CK_CLASSID::CKCID_BEHAVIORIO, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehaviorLink(ctx, id, name); })}, - {CK_CLASSID::CKCID_BEHAVIOR, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehavior(ctx, id, name); })} - }, - // register CKBaseManagers - m_ManagersCreationMap{ - {ATTRIBUTE_MANAGER_GUID, ([](CKMinContext* ctx, CK_ID id) ->CKManagerImplements::CKBaseManager* { return new(std::nothrow) CKManagerImplements::CKAttributeManager(ctx, id); })}, - } + m_OutputCallback(nullptr) { // preset for temp folder + // todo: add current CKContext pointer as the part of temp path. + // thus multiple CKContext can work. m_TempFolder = std::filesystem::temp_directory_path(); m_TempFolder /= g_UniqueFolder; std::filesystem::create_directory(m_TempFolder); } - CKMinContext::~CKMinContext() { + + CKContext::~CKContext() { // free all created objects - for (const auto& [key, value] : this->m_ObjectsList) { - delete value; + for (auto& ptr : m_ObjectsList) { + if (ptr != nullptr) { + InternalDestroy(this, ptr); + } } + // todo: free all created managers } #pragma endregion -#pragma region Print +#pragma region Output utilities - void CKMinContext::Printf(CKSTRING fmt, ...) { - if (m_PrintCallback == nullptr) return; + void CKContext::OutputToConsole(CKSTRING str) { + if (m_OutputCallback == nullptr) return; + m_OutputCallback(str); + } + + void CKContext::OutputToConsoleEx(CKSTRING fmt, ...) { + if (m_OutputCallback == nullptr) return; va_list argptr; va_start(argptr, fmt); @@ -75,69 +129,34 @@ namespace LibCmo::CK2 { va_end(argptr); - m_PrintCallback(result); + m_OutputCallback(result.c_str()); } - void CKMinContext::SetPrintCallback(PrintCallback cb) { - m_PrintCallback = cb; + void CKContext::SetOutputCallback(OutputCallback cb) { + m_OutputCallback = cb; } #pragma endregion -#pragma region Objects +#pragma region Temp IO utilities - CKObjectImplements::CKObject* CKMinContext::CreateCKObject(CK_ID id, CK_CLASSID cls, CKSTRING name) { - // pick creation function - const auto& creation = m_ObjectsCreationMap.find(cls); - if (creation == m_ObjectsCreationMap.end()) return nullptr; - - // check ckid - if (m_ObjectsList.contains(id)) return nullptr; - - // create it - return creation->second(this, id, name); + void CKContext::SetTempPath(CKSTRING u8_temp) { + EncodingHelper::SetStdPathFromU8Path(this->m_TempFolder, u8_temp); } - CKObjectImplements::CKObject* CKMinContext::GetCKObject(CK_ID id) { - const auto& probe = m_ObjectsList.find(id); - if (probe == m_ObjectsList.end()) return nullptr; - else return probe->second; - } + FILE* CKContext::OpenTempFile(CKSTRING u8_filename, CKBOOL is_read) { + std::filesystem::path stdfilename; + EncodingHelper::SetStdPathFromU8Path(stdfilename, u8_filename); - void CKMinContext::DestroyCKObject(CK_ID id) { - const auto& probe = m_ObjectsList.find(id); - if (probe != m_ObjectsList.end()) { - delete (probe->second); - m_ObjectsList.erase(probe); - } - } - - void CKMinContext::ClearCKObject(void) { - // free all created objects - for (const auto& [key, value] : this->m_ObjectsList) { - delete value; - } - // clear list - this->m_ObjectsList.clear(); - } - - CK_ID CKMinContext::GetObjectMaxID(void) { - return this->m_CKObjectMaxID; - } - - void CKMinContext::SetObjectMaxID(CK_ID id) { - this->m_CKObjectMaxID = id; + auto realfile = this->m_TempFolder / stdfilename; + return EncodingHelper::OpenStdPathFile(realfile, is_read); } #pragma endregion -#pragma region Managers +#pragma region Encoding utilities -#pragma endregion - -#pragma region Misc Funcs - - void CKMinContext::GetUtf8String(const std::string& native_name, std::string& u8_name) { + void CKContext::GetUtf8String(const std::string& native_name, std::string& u8_name) { bool success = false; for (const auto& token : this->m_NameEncoding) { success = LibCmo::EncodingHelper::GetUtf8VirtoolsName(native_name, u8_name, token); @@ -147,11 +166,11 @@ namespace LibCmo::CK2 { // fallback if (!success) { u8_name = native_name; - this->Printf("Error when converting to UTF8 string."); + this->OutputToConsole("Error when converting to UTF8 string."); } } - void CKMinContext::GetNativeString(const std::string& u8_name, std::string& native_name) { + void CKContext::GetNativeString(const std::string& u8_name, std::string& native_name) { bool success = false; for (const auto& token : this->m_NameEncoding) { success = LibCmo::EncodingHelper::GetNativeVirtoolsName(u8_name, native_name, token); @@ -161,11 +180,11 @@ namespace LibCmo::CK2 { // fallback if (!success) { native_name = u8_name; - this->Printf("Error when converting to native string."); + this->OutputToConsole("Error when converting to native string."); } } - void CKMinContext::SetEncoding(const std::vector encoding_series) { + void CKContext::SetEncoding(const std::vector encoding_series) { // free all current series for (const auto& encoding : this->m_NameEncoding) { LibCmo::EncodingHelper::DestroyEncodingToken(encoding); @@ -178,17 +197,6 @@ namespace LibCmo::CK2 { } } - void CKMinContext::SetTempPath(CKSTRING u8_temp) { - EncodingHelper::SetStdPathFromU8Path(this->m_TempFolder, u8_temp); - } - - FILE* CKMinContext::OpenTempFile(CKSTRING u8_filename, bool is_read) { - std::filesystem::path stdfilename; - EncodingHelper::SetStdPathFromU8Path(stdfilename, u8_filename); - - auto realfile = this->m_TempFolder / stdfilename; - return EncodingHelper::OpenStdPathFile(realfile, is_read); - } #pragma endregion diff --git a/LibCmo/CK2/CKContext.hpp b/LibCmo/CK2/CKContext.hpp index ff54628..67a4f52 100644 --- a/LibCmo/CK2/CKContext.hpp +++ b/LibCmo/CK2/CKContext.hpp @@ -3,6 +3,7 @@ #include "../VTAll.hpp" #include #include +#include #include namespace LibCmo::CK2 { @@ -24,49 +25,72 @@ namespace LibCmo::CK2 { CKContext& operator=(const CKContext&) = delete; ~CKContext(); - using PrintCallback = std::function; - void Printf(CKSTRING fmt, ...); - void SetPrintCallback(PrintCallback cb); + // ========== Objects Management ========== /** * @brief Creates a CKObject or derived class instance. * @param[in] cls Class Identifier (CK_CLASSID) of the object to create. * @param[in] name The name of this object. + * @param[in] options Tell CKContext how to create this object when conflict happended. + * @param[out] res The value indicate the real method how this object created. * @return A pointer to the newly created object. * @remark CKObjects must be destroy with the DestroyObject method. * @see CKObject, DestroyObject */ - CKObject* CreateCKObject(CK_CLASSID cls, CKSTRING name); + CKObject* CreateCKObject(CK_CLASSID cls, CKSTRING name, + CK_OBJECTCREATION_OPTIONS options = CK_OBJECTCREATION_OPTIONS::CK_OBJECTCREATION_NONAMECHECK, + CK_CREATIONMODE* res = nullptr); CKObject* GetCKObject(CK_ID id); void DestroyCKObject(CK_ID id); + // ========== Object Access ========== + //CKManagerImplements::CKBaseManager* CreateCKManager(CKGUID guid); //CKManagerImplements::CKBaseManager* GetCKManager(CK_ID guid); //void DestroyCKManager(CKManagerImplements::CKBaseManager* mgr); - CK_ID GetObjectMaxID(void); - void SetObjectMaxID(CK_ID id); + //CKObject* GetObjectByName(CKSTRING name, CKObject* previous = NULL); + //CKObject* GetObjectByNameAndClass(CKSTRING name, CK_CLASSID cid, CKObject* previous = NULL); + //CKObject* GetObjectByNameAndParentClass(CKSTRING name, CK_CLASSID pcid, CKObject* previous); + //const XContainer::XObjectPointerArray GetObjectListByType(CK_CLASSID cid, CKBOOL derived); + //CKINT GetObjectsCountByClassID(CK_CLASSID cid); + //CK_ID* GetObjectsListByClassID(CK_CLASSID cid); + + // ========== Encoding utilities ========== void GetUtf8String(const std::string& native_name, std::string& u8_name); void GetNativeString(const std::string& u8_name, std::string& native_name); - void SetEncoding(const std::vector encoding_series); + + // ========== Temp IO utilities ========== + void SetTempPath(CKSTRING u8_temp); + FILE* OpenTempFile(CKSTRING u8_filename, CKBOOL is_read); - FILE* OpenTempFile(CKSTRING u8_filename, bool is_read); + // ========== Print utilities ========== - public: - std::map m_ObjectsList; - std::map m_ManagersList; + using OutputCallback = std::function; + void OutputToConsole(CKSTRING str); + void OutputToConsoleEx(CKSTRING fmt, ...); + void SetOutputCallback(OutputCallback cb); - std::map> m_ObjectsCreationMap; - std::map> m_ManagersCreationMap; + protected: + // ========== Objects Management ========== - CK_ID m_CKObjectMaxID; + XContainer::XArray m_ObjectsList; + std::deque m_ReturnedObjectIds; + + // ========== Encoding utilities ========== std::vector m_NameEncoding; + + // ========== Temp IO utilities ========== + std::filesystem::path m_TempFolder; - PrintCallback m_PrintCallback; + + // ========== Print utilities ========== + + OutputCallback m_OutputCallback; }; } diff --git a/LibCmo/CK2/CKDefines.hpp b/LibCmo/CK2/CKDefines.hpp index 5325704..7f182a9 100644 --- a/LibCmo/CK2/CKDefines.hpp +++ b/LibCmo/CK2/CKDefines.hpp @@ -1,10 +1,12 @@ #pragma once +#include "CKTypes.hpp" +#include "../XContainer/XTypes.hpp" #include #include #include #include -#include "CKTypes.hpp" +#include namespace LibCmo::CK2 { /** @@ -16,4 +18,55 @@ namespace LibCmo::CK2 { */ constexpr const CKDWORD DEVVERSION = 0x02050000u; constexpr const CKGUID VIRTOOLS_GUID = CKGUID(0x56495254u, 0x4f4f4c53u); + + // ========== Class registration utilities ========== + + //using CKClassRegisterFct = std::function; + using CKClassCreationFct = std::function; + using CKClassReleaseFct = std::function; + using CKClassNameFct = std::function; + //using CKClassDependenciesFct = std::function; + //using CKClassDependenciesCountFct = std::function; + + struct CKClassDesc { + CKBOOL Done; + // Initialized upon class registration + CK_CLASSID Self; + CK_CLASSID Parent; // Class Identifier of parent class + //CKClassRegisterFct RegisterFct; // Pointer to Class Specific Registration function + CKClassCreationFct CreationFct; // Pointer to Class instance creation function + CKClassReleaseFct ReleaseFct; // Pointer to Class instance release function + CKClassNameFct NameFct; // Pointer to Class name function + //CKClassDependenciesFct DependsFct; // Pointer to Class dependencies function (Copy,delete,replace...) + //CKClassDependenciesCountFct DependsCountFct; // Pointer to Class dependencies Count function (Copy,delete,replace...) + + //// Initialized by class specific registration function + //CKDWORD DefaultOptions; // Default options for this class + //CKDWORD DefaultCopyDependencies; + //CKDWORD DefaultDeleteDependencies; + //CKDWORD DefaultReplaceDependencies; + //CKDWORD DefaultSaveDependencies; + //CKGUID Parameter; // Associated parameter GUID + + // Initialized when building class hierarchy table + CKINT DerivationLevel; // O => CKObject , etc.. + XContainer::XBitArray Parents; // Bit Mask of parents classes + XContainer::XBitArray Children; // Bit Mask of children classes + //XContainer::XBitArray ToBeNotify; // Mask for Classes that should warn the objects of this class when they are deleted + //XContainer::XBitArray CommonToBeNotify; // idem but merged with sub classes masks + //XContainer::XSArray ToNotify; // List of ClassID to notify when an object of this class is deleted (inverse of ToBeNotify) + + CKClassDesc() : + Done(CKFALSE), + Self(CK_CLASSID::CKCID_OBJECT), Parent(CK_CLASSID::CKCID_OBJECT), + CreationFct(nullptr), ReleaseFct(nullptr), NameFct(nullptr), + DerivationLevel(0), + Parents(), Children() + {} + CKClassDesc(const CKClassDesc& rhs) = default; + CKClassDesc(CKClassDesc&& rhs) = default; + CKClassDesc& operator=(const CKClassDesc& rhs) = default; + CKClassDesc& operator=(CKClassDesc& rhs) = default; + }; + } diff --git a/LibCmo/CK2/CKEnums.hpp b/LibCmo/CK2/CKEnums.hpp index 3db508c..665c89d 100644 --- a/LibCmo/CK2/CKEnums.hpp +++ b/LibCmo/CK2/CKEnums.hpp @@ -64,7 +64,6 @@ namespace LibCmo::CK2 { CKFILE_FORVIEWER = 4, /**< Don't save Interface Data within the file, the level won't be editable anymore in the interface */ CKFILE_WHOLECOMPRESSED = 8, /**< Compress the whole file */ }; - LIBCMO_BITFLAG_OPERATORS(CK_FILE_WRITEMODE); /** Load Options. @remark @@ -88,7 +87,6 @@ namespace LibCmo::CK2 { CK_LOAD_CHECKDEPENDENCIES = 1 << 7, /**< Check if every plugins needed are availables */ CK_LOAD_ONLYBEHAVIORS = 1 << 8, /**< */ }; - LIBCMO_BITFLAG_OPERATORS(CK_LOAD_FLAGS); /** Options that will be used to create this object... */ @@ -99,6 +97,36 @@ namespace LibCmo::CK2 { CK_FO_DONTLOADOBJECT, /**< Object chunk will not be read either because it is a reference or because the loaded object already exist in the current level and the user choose to keep the existing one. */ }; /** + Summary: Specify the way an object just loaded should be handled when it already exists in the level. + */ + enum class CK_LOADMODE : int32_t { + CKLOAD_INVALID = -1, /**< Use the existing object instead of loading */ + CKLOAD_OK = 0, /**< Ignore ( Name unicity is broken ) */ + CKLOAD_REPLACE = 1, /**< Replace the existing object (Not yet implemented) */ + CKLOAD_RENAME = 2, /**< Rename the loaded object */ + CKLOAD_USECURRENT = 3,/**< Use the existing object instead of loading */ + }; + using CK_CREATIONMODE = CK_LOADMODE; + /** + Specify the way an object is created through CKCreateObject. + @remark + + These flag controls the way an object is created, the most important of these flags + being CK_OBJECTCREATION_DYNAMIC which, if set in CKCreateObject, make the newly created object + dynamic. + @see CKContext::CreateObject + */ + enum class CK_OBJECTCREATION_OPTIONS : uint32_t { + CK_OBJECTCREATION_NONAMECHECK = 0, /**< Do not test for name unicity (may be overriden in special case) */ + CK_OBJECTCREATION_REPLACE = 1, /**< Replace the current object by the object being loaded */ + CK_OBJECTCREATION_RENAME = 2, /**< Rename the created object to ensure its uniqueness */ + CK_OBJECTCREATION_USECURRENT = 3, /**< Do not create a new object, use the one with the same name instead */ + CK_OBJECTCREATION_ASK = 4, /**< If a duplicate name if found, opens a dialog box to ask the useror use automatic load mode if any. */ + CK_OBJECTCREATION_FLAGSMASK = 0x0000000F, /**< Mask for previous values */ + CK_OBJECTCREATION_DYNAMIC = 0x00000010, /**< The object must be created dynamic */ + CK_OBJECTCREATION_ACTIVATE = 0x00000020, /**< The object will be copied/created active */ + CK_OBJECTCREATION_NONAMECOPY = 0x00000040 /**< The object will take control of the string given to it directly, without copying it */ + }; + /** Type identifier for a Virtools plugin. @remark + Each plugin must be given a type. @@ -130,7 +158,6 @@ namespace LibCmo::CK2 { CHNK_DONTDELETE_PTR = 0x40, /**< Data buffer stored in m_Buffer is not owned by CKStateChunk , it must not be deleted... */ CHNK_DONTDELETE_PARSER = 0x80, /**< m_Parser Ptr is not owned by CKStateChunk , it must not be deleted... */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATECHUNK_CHUNKOPTIONS); enum class CK_STATECHUNK_CHUNKVERSION : uint32_t { CHUNK_VERSIONBASE = 0, CHUNK_VERSION1 = 4, /**< equal to file version : WriteObjectID => table */ @@ -196,6 +223,5 @@ namespace LibCmo::CK2 { CKBEHAVIORLINK_ACTIVATEDLASTFRAME = 0x20000000, /**< This link had been activated last frame */ CK_OBJECT_BEHAVIORLINKMASK = 0x30000000, }; - LIBCMO_BITFLAG_OPERATORS(CK_OBJECT_FLAGS); } diff --git a/LibCmo/CK2/CKGlobals.cpp b/LibCmo/CK2/CKGlobals.cpp index 10a34b6..3f6640d 100644 --- a/LibCmo/CK2/CKGlobals.cpp +++ b/LibCmo/CK2/CKGlobals.cpp @@ -1,13 +1,19 @@ -#include "VTUtils.hpp" +#include "../VTUtils.hpp" #if defined(LIBCMO_OS_WIN32) #define ZLIB_WINAPI #endif #include +#include "../VTEncoding.hpp" #include "CKGlobals.hpp" +#include +#include #include namespace LibCmo::CK2 { + +#pragma region Compression utilities + void* CKPackData(const void* Data, CKINT size, CKINT& NewSize, CKINT compressionlevel) { uLong boundary = compressBound(static_cast(size)); char* DestBuffer = new char[boundary]; @@ -40,7 +46,7 @@ namespace LibCmo::CK2 { return DestBuffer; } - CKDWORD CKComputeDataCRC(const void* data, size_t size, CKDWORD PreviousCRC) { + CKDWORD CKComputeDataCRC(const void* data, CKINT size, CKDWORD PreviousCRC) { return static_cast(adler32( static_cast(PreviousCRC), reinterpret_cast(data), @@ -48,4 +54,183 @@ namespace LibCmo::CK2 { )); } +#pragma endregion + +#pragma region CKClass Registration + + static XContainer::XArray g_CKClassInfo; + static std::map g_CKClassInfoId2Idx; + static CK_CLASSID g_CKClassInfoMaxID = static_cast(0); + + static void ComputeParentsTable(CKClassDesc& desc) { + // if it has done, do not process it again. + if (desc.Done) return; + + // find direct parent + auto finder = g_CKClassInfoId2Idx.find(desc.Parent); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + CKClassDesc& parent = g_CKClassInfo[finder->second]; + + // if it is not self inheritance, call recursively + if (desc.Self != desc.Parent) { + ComputeParentsTable(parent); + } + + // copy parent's parents + desc.Parents = parent.Parents; + // and set self as its parent + finder = g_CKClassInfoId2Idx.find(desc.Self); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + desc.Parents[finder->second] = true; + + // set derivation level + desc.DerivationLevel = parent.DerivationLevel + 1; + + // set done + desc.Done = CKTRUE; + } + static void CKBuildClassHierarchyTable() { + // set Done to false and resize all XBitArray + size_t classCount = g_CKClassInfo.size(); + for (auto& item : g_CKClassInfo) { + item.Done = CKFALSE; + item.Parents.resize(classCount, false); + item.Children.resize(classCount, false); + } + // compute parents + for (auto& item : g_CKClassInfo) { + ComputeParentsTable(item); + } + // compute children by parents table + // iterate CKClassDesc::Parents and register it self to gotten parents + for (auto& item : g_CKClassInfo) { + auto finder = g_CKClassInfoId2Idx.find(item.Self); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + size_t selfidx = finder->second; + + for (size_t idx = 0; idx < classCount; ++idx) { + // if this idx is its parent, + // add self to parent. + if (item.Parents[idx]) { + g_CKClassInfo[idx].Children[selfidx] = true; + } + } + } + } + void CKClassRegister(CK_CLASSID cid, CK_CLASSID parentCid, + CKClassCreationFct createFct, CKClassReleaseFct relFct, CKClassNameFct nameFct) { + + CKClassDesc desc; + desc.Self = cid; + desc.Parent = parentCid; + desc.CreationFct = createFct; + desc.ReleaseFct = relFct; + desc.NameFct = nameFct; + g_CKClassInfoId2Idx.emplace(cid, g_CKClassInfo.size()); + g_CKClassInfo.emplace_back(std::move(desc)); + g_CKClassInfoMaxID = std::max(g_CKClassInfoMaxID, cid); + } + +#pragma endregion + +#pragma region Class Hierarchy Management + + CKINT CKGetClassCount() { + return g_CKClassInfo.size(); + } + + const CKClassDesc* CKGetClassDesc(CK_CLASSID cid) { + auto finder = g_CKClassInfoId2Idx.find(cid); + if (finder == g_CKClassInfoId2Idx.end()) return nullptr; + return g_CKClassInfo.data() + finder->second; + } + + CKSTRING CKClassIDToString(CK_CLASSID cid) { + auto finder = g_CKClassInfoId2Idx.find(cid); + if (finder == g_CKClassInfoId2Idx.end() || g_CKClassInfo[finder->second].NameFct == nullptr) return "Invalid Class Identifier"; + return g_CKClassInfo[finder->second].NameFct(); + } + + CKBOOL CKIsChildClassOf(CK_CLASSID child, CK_CLASSID parent) { + // get corresponding index first + auto finder = g_CKClassInfoId2Idx.find(child); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + size_t child_idx = finder->second; + finder = g_CKClassInfoId2Idx.find(parent); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + size_t parent_idx = finder->second; + + return g_CKClassInfo[child_idx].Parents[parent_idx]; + } + + CK_CLASSID CKGetParentClassID(CK_CLASSID child) { + auto finder = g_CKClassInfoId2Idx.find(child); + if (finder == g_CKClassInfoId2Idx.end()) std::abort(); + return g_CKClassInfo[finder->second].Parent; + } + + CK_CLASSID CKGetCommonParent(CK_CLASSID cid1, CK_CLASSID cid2) { + // I don't know algorithm, I just copy the decompiled code. + while (true) { + if (CKIsChildClassOf(cid1, cid2)) return cid2; + if (CKIsChildClassOf(cid2, cid1)) break; + + cid2 = CKGetParentClassID(cid1); + cid1 = cid2; + } + + return cid1; + } + +#pragma endregion + +#pragma region Initializations functions + + CKERROR CKStartUp() { + // todo: add class type registrations + + /* + // register CKObjects + m_ObjectsCreationMap{ + {CK_CLASSID::CKCID_OBJECT, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKObject(ctx, id, name); })}, + {CK_CLASSID::CKCID_SCENEOBJECT, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKSceneObject(ctx, id, name); })}, + {CK_CLASSID::CKCID_BEOBJECT, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBeObject(ctx, id, name); })}, + + {CK_CLASSID::CKCID_GROUP, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKGroup(ctx, id, name); })}, + {CK_CLASSID::CKCID_MESH, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMesh(ctx, id, name); })}, + {CK_CLASSID::CKCID_TEXTURE, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKTexture(ctx, id, name); })}, + {CK_CLASSID::CKCID_MATERIAL, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMaterial(ctx, id, name); })}, + {CK_CLASSID::CKCID_RENDEROBJECT, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKRenderObject(ctx, id, name); })}, + {CK_CLASSID::CKCID_3DENTITY, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CK3dEntity(ctx, id, name); })}, + + {CK_CLASSID::CKCID_PARAMETERIN, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterIn(ctx, id, name); })}, + {CK_CLASSID::CKCID_PARAMETER, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameter(ctx, id, name); })}, + {CK_CLASSID::CKCID_PARAMETEROUT, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterOut(ctx, id, name); })}, + {CK_CLASSID::CKCID_PARAMETERLOCAL, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterLocal(ctx, id, name); })}, + {CK_CLASSID::CKCID_PARAMETEROPERATION, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKParameterOperation(ctx, id, name); })}, + {CK_CLASSID::CKCID_BEHAVIORLINK, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehaviorLink(ctx, id, name); })}, + {CK_CLASSID::CKCID_BEHAVIORIO, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehaviorLink(ctx, id, name); })}, + {CK_CLASSID::CKCID_BEHAVIOR, ([](CKContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBehavior(ctx, id, name); })} + }, + // register CKBaseManagers + m_ManagersCreationMap{ + {ATTRIBUTE_MANAGER_GUID, ([](CKContext* ctx, CK_ID id) ->CKManagerImplements::CKBaseManager* { return new(std::nothrow) CKManagerImplements::CKAttributeManager(ctx, id); })}, + } + */ + + return CKERROR::CKERR_OK; + } + + CKERROR CKShutdown() { + // free class indo + g_CKClassInfo.clear(); + g_CKClassInfoId2Idx.clear(); + g_CKClassInfoMaxID = static_cast(0); + + return CKERROR::CKERR_OK; + } + +#pragma endregion + + } diff --git a/LibCmo/CK2/CKGlobals.hpp b/LibCmo/CK2/CKGlobals.hpp index 942dd75..6a2aae3 100644 --- a/LibCmo/CK2/CKGlobals.hpp +++ b/LibCmo/CK2/CKGlobals.hpp @@ -1,6 +1,6 @@ #pragma once -#include "CKTypes.hpp" +#include "CkDefines.hpp" namespace LibCmo::CK2 { @@ -43,6 +43,23 @@ namespace LibCmo::CK2 { * @return CRC of the buffer. * @see CKPackData, CKUnPackData */ - CKDWORD CKComputeDataCRC(const void* data, size_t size, CKDWORD PreviousCRC = 0); + CKDWORD CKComputeDataCRC(const void* data, CKINT size, CKDWORD PreviousCRC = 0); + + // ========== CKClass Registration ========== + void CKClassRegister(CK_CLASSID cid, CK_CLASSID parentCid, + CKClassCreationFct createFct, CKClassReleaseFct relFct, CKClassNameFct nameFct); + + // ========== Class Hierarchy Management ========== + CKINT CKGetClassCount(); + const CKClassDesc* CKGetClassDesc(CK_CLASSID cid); + CKSTRING CKClassIDToString(CK_CLASSID cid); + + CKBOOL CKIsChildClassOf(CK_CLASSID child, CK_CLASSID parent); + CK_CLASSID CKGetParentClassID(CK_CLASSID child); + CK_CLASSID CKGetCommonParent(CK_CLASSID cid1, CK_CLASSID cid2); + + // ========== Initializations functions ========== + CKERROR CKStartUp(); + CKERROR CKShutdown(); } diff --git a/LibCmo/CK2/CKIdentifiers.hpp b/LibCmo/CK2/CKIdentifiers.hpp index 8fc245d..0af0b4e 100644 --- a/LibCmo/CK2/CKIdentifiers.hpp +++ b/LibCmo/CK2/CKIdentifiers.hpp @@ -22,7 +22,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_OBJECTHIERAHIDDEN = 0x00000018, /**< The object is hidden hierarchically */ CK_STATESAVE_OBJECTALL = 0x0000000F, }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_OBJECT); /** Be Object */ @@ -39,7 +38,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_BEOBJECTONLY = 0x00000FF0, /**< Save only BeObject specific datas */ CK_STATESAVE_BEOBJECTALL = 0x00000FFF, /**< Save All datas */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_BEOBJECT); /** 3dEntity */ @@ -57,7 +55,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_3DENTITYONLY = 0x003FF000, /**< Save only 3dEntity specific datas */ CK_STATESAVE_3DENTITYALL = 0x003FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_3DENTITY); /** Light */ @@ -77,7 +74,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_TLIGHTONLY = 0xF0000000, /**< Save only Target Light specific datas */ CK_STATESAVE_TLIGHTALL = 0xFFFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_LIGHT); /** Camera */ @@ -96,7 +92,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_TCAMERAONLY = 0x70000000, /**< Save only Target camera specific datas */ CK_STATESAVE_TCAMERAALL = 0x7FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_CAMERA); /** Sprite3D */ @@ -110,7 +105,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SPRITE3DONLY = 0x0FC00000, /**< Save only Sprite3D specific datas */ CK_STATESAVE_SPRITE3DALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SPRITE3D); /** Object 3D */ @@ -120,7 +114,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_3DOBJECTRONLY = 0x00C00000, /**< Save only 3dObject specific datas */ CK_STATESAVE_3DOBJECTALL = 0x03FFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_3DOBJECT); /** BodyPart */ @@ -135,7 +128,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_BODYPARTONLY = 0x7F000000, /**< Save only bodypart specific datas */ CK_STATESAVE_BODYPARTALL = 0x7FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_BODYPART); /** Character */ @@ -153,7 +145,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_CHARACTERONLY = 0xFFC00000, /**< Save only character specific datas */ CK_STATESAVE_CHARACTERALL = 0xFFFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_CHARACTER); /** CURVE && Curve Point */ @@ -172,7 +163,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_CURVEONLY = 0xFFC00000, /**< Save only curve specific data */ CK_STATESAVE_CURVEALL = 0xFFFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_CURVE); /** 2dEntity */ @@ -187,7 +177,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_2DENTITYHIERARCHY = 0x00400000, /**< Save Material */ CK_STATESAVE_2DENTITYALL = 0x0070FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_2DENTITY); /** Sprite */ @@ -208,7 +197,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SPRITEONLY = 0xF0EF0000, /**< Save only sprite specific data */ CK_STATESAVE_SPRITEALL = 0x70FFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SPRITE); /** Sprite Text */ @@ -222,7 +210,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SPRITETEXTONLY = 0x0F000000, /**< Save only SpriteText specific data */ CK_STATESAVE_SPRITETEXTALL = 0x3FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SPRITETEXT); /** Sound */ @@ -238,7 +225,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SOUNDONLY = 0x000FF000, /**< Save only Sound specific data */ CK_STATESAVE_SOUNDALL = 0x000FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SOUND); /** Wave Sound */ @@ -254,7 +240,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_WAVSOUNDONLY = 0x0FF00000, /**< Save All datas for sub-classes */ CK_STATESAVE_WAVSOUNDALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_WAVSOUND); /** Wave Sound */ @@ -270,7 +255,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_MIDISOUNDONLY = 0x0FF00000, CK_STATESAVE_MIDISOUNDALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_MIDISOUND); /** Place */ @@ -281,7 +265,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_PLACELEVEL = 0x00008000, /**< Save level using the place */ CK_STATESAVE_PLACEALL = 0x0000FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_PLACE); /** Level CKSaveObjectState will not save any data */ @@ -293,7 +276,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_LEVELSCENE = 0x80000000, /**< Default and active scene */ CK_STATESAVE_LEVELALL = 0xFFFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_LEVEL); /** GROUP */ @@ -308,7 +290,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_GROUPRESERVED7 = 0x00080000, /**< Reserved for future use */ CK_STATESAVE_GROUPALL = 0x000FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_GROUP); /** MESH CKSaveOjectSave will save all data and does not take flags into account */ @@ -328,7 +309,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_MESHONLY = 0x00FFF000, /**< Save All datas for sub-classes */ CK_STATESAVE_MESHALL = 0x00FFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_MESH); /** PATCH MESH CKSaveOjectSave will save all data and does not take flags into account */ @@ -341,7 +321,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_PATCHMESHONLY = 0x0FF00000, /**< Save All datas for sub-classes */ CK_STATESAVE_PATCHMESHALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_PATCHMESH); /** Material */ @@ -357,7 +336,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_MATERIALONLY = 0x000FF000, /**< Save All datas for sub-classes */ CK_STATESAVE_MATERIALALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_MATERIAL); /** Texture CKSaveOjectSave will save all relevant data and does not take flags into account */ @@ -378,7 +356,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_TEXONLY = 0x00FFF000, /**< Save Only Texture Data (Dot NOT MODIFY ! Texture loading/saving relies on this value) */ CK_STATESAVE_TEXALL = 0x002FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_TEXTURE); /** 2d CURVE && 2d Curve Point */ @@ -400,7 +377,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_2DCURVESAVEPOINTS = 0x0003F800, /**< Obsolete */ CK_STATESAVE_2DCURVEALL = 0x0007FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_2DCURVE); /** Kinematic Chain */ @@ -411,7 +387,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_KINEMATICCHAINRESERVED3 = 0x00000080, /**< Reserved for future use */ CK_STATESAVE_KINEMATICCHAINALL = 0x000000FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_KINEMATICCHAIN); /** Animation */ @@ -426,7 +401,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_ANIMATIONRESERVED6 = 0x00000800, /**< Reserved for future use */ CK_STATESAVE_ANIMATIONALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_ANIMATION); /** Keyed Anim */ @@ -446,7 +420,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_KEYEDANIMRESERVED2 = 0x01000000, /**< Reserved for future use */ CK_STATESAVE_KEYEDANIMRESERVED3 = 0x02000000, /**< Reserved for future use */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_KEYEDANIMATION); /** Object Animation CKSaveOjectSave will save all relevant data and does not take flags into account */ @@ -471,7 +444,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_KEYEDANIMONLY = 0x03FFF000, /**< Save All datas for sub-classes */ CK_STATESAVE_KEYEDANIMALL = 0x03FFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_OBJECTANIMATION); /** IK Animation */ @@ -487,7 +459,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_IKANIMATIONRESERVED9 = 0x00200000, /**< Reserved for future use */ CK_STATESAVE_IKANIMATIONALL = 0x003FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_IKANIMATION); /** BehaviorLink */ @@ -501,7 +472,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_BEHAV_LINKONLY = 0x000000F0, /**< */ CK_STATESAVE_BEHAV_LINKALL = 0x000000FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_BEHAV_LINK); /** BehaviorIO */ @@ -514,7 +484,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_BEHAVIOONLY = 0x000000F0, /**< */ CK_STATESAVE_BEHAVIOALL = 0x000000FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_BEHAV_IO); /** BehaviorPrototype */ @@ -537,7 +506,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_PROTORESERVED14 = 0x00080000, /**< Reserved for future use */ CK_STATESAVE_PROTOALL = 0x000FFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_PROTOTYPE); /** Behavior */ @@ -566,7 +534,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_BEHAVIORONLY = 0x01FFFFF0, CK_STATESAVE_BEHAVIORALL = 0x01FFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_BEHAVIOR); /** SCENE CKSaveOjectSave will save all relevant data and does not take flags into account */ @@ -589,7 +556,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SCENERESERVED14 = 0x08000000, /**< Reserved for future use */ CK_STATESAVE_SCENEALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SCENE); /** ParameterIn */ @@ -606,7 +572,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_PARAMETERIN_DISABLED = 0x00002000, /**< The parameter was disabled */ CK_STATESAVE_PARAMETERIN_ALL = 0x0000FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_PARAMETERIN); /** ParameterLocal et ParameterOut */ @@ -619,7 +584,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_PARAMETEROUT_ISSETTING = 0x00000400, /**< Reserved for future use */ CK_STATESAVE_PARAMETEROUT_ALL = 0x0000FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_PARAMETEROUT); /** Parameter Operation */ @@ -633,7 +597,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_OPERATIONNEWDATA = 0x00000400, CK_STATESAVE_OPERATIONALL = 0x000007FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_OPERATION); /** Synchro Object CKSaveOjectSave will save all relevant data and does not take flags into account */ @@ -645,7 +608,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SYNCHRORESERVED3 = 0x00000200, /**< Reserved for future use */ CK_STATESAVE_SYNCHRONALL = 0x000003FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SYNCHRO); /** Grid */ @@ -659,7 +621,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_GRIDONLY = 0x0FC00000, /**< */ CK_STATESAVE_GRIDALL = 0x0FFFFFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_GRID); /** Layer (For Grids) */ @@ -673,7 +634,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_LAYERONLY = 0x000003F0, /**< */ CK_STATESAVE_LAYERALL = 0x000003FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_LAYER); /** DataArray CKSaveOjectSave will save all relevant data and does not take flags into account */ @@ -683,7 +643,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_DATAARRAYMEMBERS = 0x00004000, /**< Save members */ CK_STATESAVE_DATAARRAYALL = 0x0000FFFF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_DATAARRAY); /** SceneObjectDesc */ @@ -694,6 +653,6 @@ namespace LibCmo::CK2 { CK_STATESAVE_SCENEOBJECTRES3 = 0x00000080, /**< Reserved for future use */ CK_STATESAVE_SCENEOBJECTDESCALL = 0x000000FF, /**< Save All datas for sub-classes */ }; - LIBCMO_BITFLAG_OPERATORS(CK_STATESAVEFLAGS_SCENEOBJECTDESC); + } \ No newline at end of file diff --git a/LibCmo/CK2/CKBaseManager.hpp b/LibCmo/CK2/CKManagerImplements/CKBaseManager.hpp similarity index 100% rename from LibCmo/CK2/CKBaseManager.hpp rename to LibCmo/CK2/CKManagerImplements/CKBaseManager.hpp diff --git a/LibCmo/CK2/CKObject.hpp b/LibCmo/CK2/CKObject.hpp deleted file mode 100644 index 8762d7c..0000000 --- a/LibCmo/CK2/CKObject.hpp +++ /dev/null @@ -1,244 +0,0 @@ -#pragma once - -#include "CKDefines.hpp" -#include "CKEnums.hpp" - -namespace LibCmo::CK2::CKObjectImplements { - - class CKObject { - public: - CK_ID m_ID; - std::string m_Name; - CK_OBJECT_FLAGS m_ObjectFlags; - CKMinContext* m_Context; - - CKObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKObject(const CKObject&) = delete; - CKObject& operator=(const CKObject&) = delete; - virtual ~CKObject(); - - CK_ID GetID(void) { return this->m_ID; } - CKSTRING GetName(void) { return (this->m_Name.empty() ? nullptr : this->m_Name.c_str()); } - void SetName(CKSTRING u8_name) { this->m_Name = (u8_name == nullptr ? "" : u8_name); } - CK_OBJECT_FLAGS GetObjectFlags(void) { return this->m_ObjectFlags; } - void SetObjectFlags(CK_OBJECT_FLAGS flags) { this->m_ObjectFlags = flags; } - - virtual CK_CLASSID GetClassID(void) { return CK_CLASSID::CKCID_OBJECT; } - virtual bool Load(CKStateChunk* chunk, const CKFileDocument* doc); - virtual CKStateChunk* Save(const CKFileDocument* doc); - }; - - class CKSceneObject : public CKObject { - public: - CKSceneObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKSceneObject(const CKSceneObject&) = delete; - CKSceneObject& operator=(const CKSceneObject&) = delete; - virtual ~CKSceneObject(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_SCENEOBJECT; } - protected: - XBitArray m_Scenes; - }; - - class CKBeObject : public CKSceneObject { - public: - CKBeObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKBeObject(const CKBeObject&) = delete; - CKBeObject& operator=(const CKBeObject&) = delete; - virtual ~CKBeObject(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEOBJECT; } - protected: - - }; - -#pragma region Map Related - - class CKGroup : public CKBeObject { - public: - CKGroup(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKGroup(const CKGroup&) = delete; - CKGroup& operator=(const CKGroup&) = delete; - virtual ~CKGroup(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_GROUP; } - protected: - - }; - - class CKMesh : public CKBeObject { - public: - CKMesh(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKMesh(const CKMesh&) = delete; - CKMesh& operator=(const CKMesh&) = delete; - virtual ~CKMesh(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_MESH; } - protected: - - }; - - class CKTexture : public CKBeObject { - public: - CKTexture(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKTexture(const CKTexture&) = delete; - CKTexture& operator=(const CKTexture&) = delete; - virtual ~CKTexture(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_TEXTURE; } - protected: - - }; - - class CKMaterial : public CKBeObject { - public: - CKMaterial(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKMaterial(const CKMaterial&) = delete; - CKMaterial& operator=(const CKMaterial&) = delete; - virtual ~CKMaterial(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_MATERIAL; } - protected: - - }; - - class CKRenderObject : public CKBeObject { - public: - CKRenderObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKRenderObject(const CKRenderObject&) = delete; - CKRenderObject& operator=(const CKRenderObject&) = delete; - ~CKRenderObject(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_RENDEROBJECT; } - protected: - - }; - - class CK3dEntity : public CKRenderObject { - public: - CK3dEntity(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CK3dEntity(const CK3dEntity&) = delete; - CK3dEntity& operator=(const CK3dEntity&) = delete; - virtual ~CK3dEntity(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_3DENTITY; } - protected: - - }; - - class CK3dObject :public CK3dEntity { - public: - CK3dObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CK3dObject(const CK3dObject&) = delete; - CK3dObject& operator=(const CK3dObject&) = delete; - ~CK3dObject(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_3DOBJECT; } - protected: - - }; - -#pragma endregion - -#pragma region Behavior Related - - class CKParameterIn :public CKObject { - public: - CKParameterIn(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKParameterIn(const CKParameterIn&) = delete; - CKParameterIn& operator=(const CKParameterIn&) = delete; - ~CKParameterIn(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETERIN; } - protected: - - }; - - class CKParameter :public CKObject { - public: - CKParameter(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKParameter(const CKParameter&) = delete; - CKParameter& operator=(const CKParameter&) = delete; - ~CKParameter(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETER; } - protected: - - }; - - class CKParameterOut :public CKParameter { - public: - CKParameterOut(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKParameterOut(const CKParameterOut&) = delete; - CKParameterOut& operator=(const CKParameterOut&) = delete; - ~CKParameterOut(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETEROUT; } - protected: - - }; - - class CKParameterLocal :public CKParameter { - public: - CKParameterLocal(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKParameterLocal(const CKParameterLocal&) = delete; - CKParameterLocal& operator=(const CKParameterLocal&) = delete; - ~CKParameterLocal(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETERLOCAL; } - protected: - - }; - - class CKParameterOperation :public CKObject { - public: - CKParameterOperation(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKParameterOperation(const CKParameterOperation&) = delete; - CKParameterOperation& operator=(const CKParameterOperation&) = delete; - ~CKParameterOperation(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETEROPERATION; } - protected: - - }; - - class CKBehaviorLink :public CKObject { - public: - CKBehaviorLink(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKBehaviorLink(const CKBehaviorLink&) = delete; - CKBehaviorLink& operator=(const CKBehaviorLink&) = delete; - ~CKBehaviorLink(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIORLINK; } - protected: - - }; - - class CKBehaviorIO :public CKObject { - public: - CKBehaviorIO(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKBehaviorIO(const CKBehaviorIO&) = delete; - CKBehaviorIO& operator=(const CKBehaviorIO&) = delete; - ~CKBehaviorIO(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIORIO; } - protected: - - }; - - class CKBehavior :public CKSceneObject { - public: - CKBehavior(CKMinContext* ctx, CK_ID ckid, CKSTRING name); - CKBehavior(const CKBehavior&) = delete; - CKBehavior& operator=(const CKBehavior&) = delete; - ~CKBehavior(); - - virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIOR; } - protected: - - }; - -#pragma endregion - - -} diff --git a/LibCmo/CK2/CKObjectImplements/CKObject.cpp b/LibCmo/CK2/CKObjectImplements/CKObject.cpp index 0d03bcc..35bd0f1 100644 --- a/LibCmo/CK2/CKObjectImplements/CKObject.cpp +++ b/LibCmo/CK2/CKObjectImplements/CKObject.cpp @@ -1,138 +1,123 @@ -#include "../CKObjects.hpp" +#include "CKObject.hpp" #include "../CKStateChunk.hpp" -#include "../CKIdentifiers.hpp" -#include "../VTUtils.hpp" -namespace LibCmo::CK2::CKObjectImplements { +namespace LibCmo::CK2 { - CKObject::CKObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : - m_ID(ckid), m_Context(ctx), - m_Name(name == nullptr ? "" : name), - m_ObjectFlags(CK_OBJECT_FLAGS::CK_PARAMETERIN_DISABLED) { - ; + + void CKObject::PreSave(CKFile* file, CKDWORD flags) {} + + CKStateChunk* CKObject::Save(CKFile* file, CKDWORD flags) { + return nullptr; } - CKObject::~CKObject() { - - } - - bool CKObject::Load(CKStateChunk* chunk, const CKFileDocument* doc) { - if (chunk->SeekIdentifier(Identifiers::CK_STATESAVEFLAGS_OBJECT::CK_STATESAVE_OBJECTHIDDEN)) { - EnumsHelper::FlagEnumRm(this->m_ObjectFlags, - { CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE, - CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE } - ); + bool CKObject::Load(CKStateChunk* chunk, CKFile* file) { + if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_OBJECT::CK_STATESAVE_OBJECTHIDDEN)) { + EnumsHelper::Rm(this->m_ObjectFlags, + EnumsHelper::Merge({ CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE, CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE })); } else { - if (chunk->SeekIdentifier(Identifiers::CK_STATESAVEFLAGS_OBJECT::CK_STATESAVE_OBJECTHIERAHIDDEN)) { + if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_OBJECT::CK_STATESAVE_OBJECTHIERAHIDDEN)) { // != 0 - EnumsHelper::FlagEnumRm(this->m_ObjectFlags, - { CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE, } - ); - EnumsHelper::FlagEnumAdd(this->m_ObjectFlags, - { CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE, } - ); + EnumsHelper::Rm(this->m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE); + EnumsHelper::Add(this->m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE); } else { // == 0 - EnumsHelper::FlagEnumAdd(this->m_ObjectFlags, - { CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE, } - ); - EnumsHelper::FlagEnumRm(this->m_ObjectFlags, - { CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE, } - ); + EnumsHelper::Add(this->m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE); + EnumsHelper::Rm(this->m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_HIERACHICALHIDE); + } } return true; } - CKStateChunk* CKObject::Save(const CKFileDocument* doc) { - return nullptr; - } - CKSceneObject::CKSceneObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKSceneObject::~CKSceneObject() { - } - - CKBeObject::CKBeObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKSceneObject(ctx, ckid, name) { - } - CKBeObject::~CKBeObject() { - } - - CKGroup::CKGroup(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { - } - CKGroup::~CKGroup() { - } - - CKMesh::CKMesh(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { - } - CKMesh::~CKMesh() { - } - - CKTexture::CKTexture(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { - } - CKTexture::~CKTexture() { - } - - CKMaterial::CKMaterial(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { - } - CKMaterial::~CKMaterial() { - } - - CKRenderObject::CKRenderObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { - } - CKRenderObject::~CKRenderObject() { - } - - CK3dEntity::CK3dEntity(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKRenderObject(ctx, ckid, name) { - } - CK3dEntity::~CK3dEntity() { - } - - CK3dObject::CK3dObject(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CK3dEntity(ctx, ckid, name) { - } - CK3dObject::~CK3dObject() { - } + void CKObject::PostLoad() {} - CKParameterIn::CKParameterIn(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKParameterIn::~CKParameterIn() { - } + //CKSceneObject::CKSceneObject(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKSceneObject::~CKSceneObject() { + //} - CKParameter::CKParameter(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKParameter::~CKParameter() { - } + //CKBeObject::CKBeObject(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKSceneObject(ctx, ckid, name) { + //} + //CKBeObject::~CKBeObject() { + //} - CKParameterOut::CKParameterOut(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKParameter(ctx, ckid, name) { - } - CKParameterOut::~CKParameterOut() { - } + //CKGroup::CKGroup(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { + //} + //CKGroup::~CKGroup() { + //} - CKParameterLocal::CKParameterLocal(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKParameter(ctx, ckid, name) { - } - CKParameterLocal::~CKParameterLocal() { - } + //CKMesh::CKMesh(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { + //} + //CKMesh::~CKMesh() { + //} - CKParameterOperation::CKParameterOperation(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKParameterOperation::~CKParameterOperation() { - } + //CKTexture::CKTexture(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { + //} + //CKTexture::~CKTexture() { + //} - CKBehaviorLink::CKBehaviorLink(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKBehaviorLink::~CKBehaviorLink() { - } + //CKMaterial::CKMaterial(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { + //} + //CKMaterial::~CKMaterial() { + //} - CKBehaviorIO::CKBehaviorIO(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { - } - CKBehaviorIO::~CKBehaviorIO() { - } + //CKRenderObject::CKRenderObject(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKBeObject(ctx, ckid, name) { + //} + //CKRenderObject::~CKRenderObject() { + //} - CKBehavior::CKBehavior(CKMinContext* ctx, CK_ID ckid, CKSTRING name) : CKSceneObject(ctx, ckid, name) { - } - CKBehavior::~CKBehavior() { - } + //CK3dEntity::CK3dEntity(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKRenderObject(ctx, ckid, name) { + //} + //CK3dEntity::~CK3dEntity() { + //} + + //CK3dObject::CK3dObject(CKContext* ctx, CK_ID ckid, CKSTRING name) : CK3dEntity(ctx, ckid, name) { + //} + //CK3dObject::~CK3dObject() { + //} + + + //CKParameterIn::CKParameterIn(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKParameterIn::~CKParameterIn() { + //} + + //CKParameter::CKParameter(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKParameter::~CKParameter() { + //} + + //CKParameterOut::CKParameterOut(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKParameter(ctx, ckid, name) { + //} + //CKParameterOut::~CKParameterOut() { + //} + + //CKParameterLocal::CKParameterLocal(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKParameter(ctx, ckid, name) { + //} + //CKParameterLocal::~CKParameterLocal() { + //} + + //CKParameterOperation::CKParameterOperation(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKParameterOperation::~CKParameterOperation() { + //} + + //CKBehaviorLink::CKBehaviorLink(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKBehaviorLink::~CKBehaviorLink() { + //} + + //CKBehaviorIO::CKBehaviorIO(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKObject(ctx, ckid, name) { + //} + //CKBehaviorIO::~CKBehaviorIO() { + //} + + //CKBehavior::CKBehavior(CKContext* ctx, CK_ID ckid, CKSTRING name) : CKSceneObject(ctx, ckid, name) { + //} + //CKBehavior::~CKBehavior() { + //} } \ No newline at end of file diff --git a/LibCmo/CK2/CKObjectImplements/CKObject.hpp b/LibCmo/CK2/CKObjectImplements/CKObject.hpp new file mode 100644 index 0000000..8913870 --- /dev/null +++ b/LibCmo/CK2/CKObjectImplements/CKObject.hpp @@ -0,0 +1,275 @@ +#pragma once + +#include "../../VTUtils.hpp" +#include "../CKContext.hpp" + +namespace LibCmo::CK2 { + + class CKObject { + public: + CKObject(CKContext* ctx, CK_ID ckid, CKSTRING name) : + m_ID(ckid), + m_HasName(name != nullptr ? CKTRUE : CKFALSE), m_Name(name != nullptr ? name : ""), + m_Context(ctx), + m_ObjectFlags(CK_OBJECT_FLAGS::CK_PARAMETERIN_DISABLED) + {} + virtual ~CKObject() {} + LIBCMO_DISABLE_COPY_MOVE(CKObject); + + CK_ID GetID(void) { + return m_ID; + } + CKSTRING GetName(void) { + if (m_HasName) return m_Name.c_str(); + else return nullptr; + } + void SetName(CKSTRING u8_name) { + if (u8_name == nullptr) { + m_HasName = false; + m_Name.clear(); + } else { + m_HasName = true; + m_Name = u8_name; + } + } + CK_OBJECT_FLAGS GetObjectFlags(void) { + return m_ObjectFlags; + } + void SetObjectFlags(CK_OBJECT_FLAGS flags) { + m_ObjectFlags = flags; + } + CKContext* GetCKContext() { + return m_Context; + } + + virtual CK_CLASSID GetClassID(void) { + return CK_CLASSID::CKCID_OBJECT; + } + + virtual void PreSave(CKFile* file, CKDWORD flags); + virtual CKStateChunk* Save(CKFile* file, CKDWORD flags); + virtual bool Load(CKStateChunk* chunk, CKFile* file); + virtual void PostLoad(); + + private: + CK_ID m_ID; + CKBOOL m_HasName; + std::string m_Name; + CK_OBJECT_FLAGS m_ObjectFlags; + CKContext* m_Context; + }; + + //class CKSceneObject : public CKObject { + //public: + // CKSceneObject(CKContext* ctx, CK_ID ckid, CKSTRING name); + // CKSceneObject(const CKSceneObject&) = delete; + // CKSceneObject& operator=(const CKSceneObject&) = delete; + // virtual ~CKSceneObject(); + + // virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_SCENEOBJECT; } + //protected: + // XBitArray m_Scenes; + //}; + + //class CKBeObject : public CKSceneObject { + //public: + // CKBeObject(CKContext* ctx, CK_ID ckid, CKSTRING name); + // CKBeObject(const CKBeObject&) = delete; + // CKBeObject& operator=(const CKBeObject&) = delete; + // virtual ~CKBeObject(); + + // virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEOBJECT; } + //protected: + + //}; +// +//#pragma region Map Related +// +// class CKGroup : public CKBeObject { +// public: +// CKGroup(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKGroup(const CKGroup&) = delete; +// CKGroup& operator=(const CKGroup&) = delete; +// virtual ~CKGroup(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_GROUP; } +// protected: +// +// }; +// +// class CKMesh : public CKBeObject { +// public: +// CKMesh(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKMesh(const CKMesh&) = delete; +// CKMesh& operator=(const CKMesh&) = delete; +// virtual ~CKMesh(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_MESH; } +// protected: +// +// }; +// +// class CKTexture : public CKBeObject { +// public: +// CKTexture(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKTexture(const CKTexture&) = delete; +// CKTexture& operator=(const CKTexture&) = delete; +// virtual ~CKTexture(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_TEXTURE; } +// protected: +// +// }; +// +// class CKMaterial : public CKBeObject { +// public: +// CKMaterial(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKMaterial(const CKMaterial&) = delete; +// CKMaterial& operator=(const CKMaterial&) = delete; +// virtual ~CKMaterial(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_MATERIAL; } +// protected: +// +// }; +// +// class CKRenderObject : public CKBeObject { +// public: +// CKRenderObject(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKRenderObject(const CKRenderObject&) = delete; +// CKRenderObject& operator=(const CKRenderObject&) = delete; +// ~CKRenderObject(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_RENDEROBJECT; } +// protected: +// +// }; +// +// class CK3dEntity : public CKRenderObject { +// public: +// CK3dEntity(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CK3dEntity(const CK3dEntity&) = delete; +// CK3dEntity& operator=(const CK3dEntity&) = delete; +// virtual ~CK3dEntity(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_3DENTITY; } +// protected: +// +// }; +// +// class CK3dObject :public CK3dEntity { +// public: +// CK3dObject(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CK3dObject(const CK3dObject&) = delete; +// CK3dObject& operator=(const CK3dObject&) = delete; +// ~CK3dObject(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_3DOBJECT; } +// protected: +// +// }; +// +//#pragma endregion +// +//#pragma region Behavior Related +// +// class CKParameterIn :public CKObject { +// public: +// CKParameterIn(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKParameterIn(const CKParameterIn&) = delete; +// CKParameterIn& operator=(const CKParameterIn&) = delete; +// ~CKParameterIn(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETERIN; } +// protected: +// +// }; +// +// class CKParameter :public CKObject { +// public: +// CKParameter(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKParameter(const CKParameter&) = delete; +// CKParameter& operator=(const CKParameter&) = delete; +// ~CKParameter(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETER; } +// protected: +// +// }; +// +// class CKParameterOut :public CKParameter { +// public: +// CKParameterOut(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKParameterOut(const CKParameterOut&) = delete; +// CKParameterOut& operator=(const CKParameterOut&) = delete; +// ~CKParameterOut(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETEROUT; } +// protected: +// +// }; +// +// class CKParameterLocal :public CKParameter { +// public: +// CKParameterLocal(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKParameterLocal(const CKParameterLocal&) = delete; +// CKParameterLocal& operator=(const CKParameterLocal&) = delete; +// ~CKParameterLocal(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETERLOCAL; } +// protected: +// +// }; +// +// class CKParameterOperation :public CKObject { +// public: +// CKParameterOperation(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKParameterOperation(const CKParameterOperation&) = delete; +// CKParameterOperation& operator=(const CKParameterOperation&) = delete; +// ~CKParameterOperation(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_PARAMETEROPERATION; } +// protected: +// +// }; +// +// class CKBehaviorLink :public CKObject { +// public: +// CKBehaviorLink(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKBehaviorLink(const CKBehaviorLink&) = delete; +// CKBehaviorLink& operator=(const CKBehaviorLink&) = delete; +// ~CKBehaviorLink(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIORLINK; } +// protected: +// +// }; +// +// class CKBehaviorIO :public CKObject { +// public: +// CKBehaviorIO(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKBehaviorIO(const CKBehaviorIO&) = delete; +// CKBehaviorIO& operator=(const CKBehaviorIO&) = delete; +// ~CKBehaviorIO(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIORIO; } +// protected: +// +// }; +// +// class CKBehavior :public CKSceneObject { +// public: +// CKBehavior(CKContext* ctx, CK_ID ckid, CKSTRING name); +// CKBehavior(const CKBehavior&) = delete; +// CKBehavior& operator=(const CKBehavior&) = delete; +// ~CKBehavior(); +// +// virtual CK_CLASSID GetClassID(void) override { return CK_CLASSID::CKCID_BEHAVIOR; } +// protected: +// +// }; +// +//#pragma endregion + + +} diff --git a/LibCmo/CK2/CKTypes.hpp b/LibCmo/CK2/CKTypes.hpp index 3094a2e..79d9f41 100644 --- a/LibCmo/CK2/CKTypes.hpp +++ b/LibCmo/CK2/CKTypes.hpp @@ -166,12 +166,14 @@ namespace LibCmo::CK2 { using CKSTRING = const char*; using CKCHAR = char; - using CKBOOL = int32_t; + using CKBOOL = bool; using CKBYTE = uint8_t; using CKDWORD = uint32_t; using CKWORD = uint16_t; using CKINT = int32_t; + constexpr CKBOOL CKFALSE = false; + constexpr CKBOOL CKTRUE = true; using CKParameterType = int32_t; using CKOperationType = int32_t; diff --git a/LibCmo/LibCmo.vcxproj b/LibCmo/LibCmo.vcxproj index fe9d84d..18396ef 100644 --- a/LibCmo/LibCmo.vcxproj +++ b/LibCmo/LibCmo.vcxproj @@ -104,8 +104,6 @@ stdcpp20 /utf-8 %(AdditionalOptions) true - Create - VTAll.hpp Console @@ -125,8 +123,6 @@ stdcpp20 /utf-8 %(AdditionalOptions) true - Create - VTAll.hpp Console @@ -146,8 +142,6 @@ stdcpp20 /utf-8 %(AdditionalOptions) true - Create - VTAll.hpp Console @@ -169,8 +163,6 @@ stdcpp20 /utf-8 %(AdditionalOptions) true - Create - VTAll.hpp Console @@ -201,13 +193,13 @@ - + - + diff --git a/LibCmo/LibCmo.vcxproj.filters b/LibCmo/LibCmo.vcxproj.filters index 16a997b..390b3e3 100644 --- a/LibCmo/LibCmo.vcxproj.filters +++ b/LibCmo/LibCmo.vcxproj.filters @@ -122,10 +122,10 @@ Headers - + Headers\CK2\CKObjectImplements - + Headers\CK2\CKManagerImplements diff --git a/LibCmo/VTUtils.cpp b/LibCmo/VTUtils.cpp index 8eb2035..846e974 100644 --- a/LibCmo/VTUtils.cpp +++ b/LibCmo/VTUtils.cpp @@ -1,6 +1,10 @@ #include "VTUtils.hpp" namespace LibCmo { + + void SystemPanic(const char* reason) { + } + namespace StreamHelper { static constexpr const size_t CHUNK_SIZE = 10240; diff --git a/LibCmo/VTUtils.hpp b/LibCmo/VTUtils.hpp index 1a354a2..4667129 100644 --- a/LibCmo/VTUtils.hpp +++ b/LibCmo/VTUtils.hpp @@ -39,6 +39,8 @@ #if defined(_WIN32) #define LIBCMO_OS_WIN32 +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX #endif #include @@ -47,46 +49,49 @@ #include #include -#pragma region Enum Helper +#pragma region Batch Ctor operator= Operations -#define LIBCMO_BITFLAG_OPERATORS_BITWISE(OP, ENUM_TYPE) \ - constexpr ENUM_TYPE operator OP(ENUM_TYPE lhs, ENUM_TYPE rhs) noexcept { \ - typedef std::underlying_type_t underlying; \ - return static_cast(static_cast(lhs) OP static_cast(rhs)); \ - } \ - constexpr ENUM_TYPE& operator OP ## = (ENUM_TYPE& lhs, ENUM_TYPE rhs) noexcept { \ - return (lhs = lhs OP rhs); \ - } - -#define LIBCMO_BITFLAG_OPERATORS_BOOLEAN(OP, ENUM_TYPE) \ - constexpr bool operator OP(ENUM_TYPE lhs, std::underlying_type_t rhs) noexcept { \ - return static_cast>(lhs) OP rhs; \ - } \ - constexpr bool operator OP(std::underlying_type_t lhs, ENUM_TYPE rhs) noexcept { \ - return lhs OP static_cast>(rhs); \ - } - -#define LIBCMO_BITFLAG_OPERATORS(ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BITWISE(|, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BITWISE(&, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BITWISE(^, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(==, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(!=, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(<, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(>, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(>=, ENUM_TYPE) \ - LIBCMO_BITFLAG_OPERATORS_BOOLEAN(<=, ENUM_TYPE) \ - constexpr ENUM_TYPE operator~(ENUM_TYPE e) noexcept { \ - return static_cast(~static_cast>(e)); \ - } \ - constexpr bool operator!(ENUM_TYPE e) noexcept { \ - return static_cast(static_cast>(e)); \ - } +#define LIBCMO_DISABLE_COPY_MOVE(CLSNAME) \ + CLSNAME(const CLSNAME&) = delete; \ + CLSNAME(CLSNAME&&) = delete; \ + CLSNAME& operator=(const CLSNAME&) = delete; \ + CLSNAME& operator=(CLSNAME&&) = delete; #pragma endregion namespace LibCmo { + namespace EnumsHelper { + template, int> = 0> + inline TEnum Merge(std::initializer_list il) { + std::underlying_type_t result = 0; + for (auto it = il.begin(); it != il.end(); ++it) { + result |= static_cast>(*it); + } + return static_cast(result); + } + + template, int> = 0> + inline TEnum Inv(TEnum e) { + return static_cast(~(static_cast>(e))); + } + + template, int> = 0> + inline TEnum Rm(TEnum& e1, TEnum e2) { + e1 = static_cast(static_cast>(e1) & static_cast>(Inv(e2))); + } + + template, int> = 0> + inline TEnum Add(TEnum& e1, TEnum e2) { + e1 = static_cast(static_cast>(e1) | static_cast>(e2)); + } + + template, int> = 0> + inline bool Has(TEnum e, TEnum probe) { + return static_cast(static_cast>(e) & static_cast>(probe)); + } + } + namespace StreamHelper { void CopyStream(const void* src, FILE* dest, size_t len);