libcmo21/LibCmo/CK2/MgrImpls/CKObjectManager.cpp

271 lines
7.6 KiB
C++
Raw Normal View History

2023-09-04 22:58:53 +08:00
#include "CKObjectManager.hpp"
#include "../CKContext.hpp"
2023-09-04 22:58:53 +08:00
#include "../ObjImpls/CKObject.hpp"
namespace LibCmo::CK2::MgrImpls {
CKObjectManager::CKObjectManager(CKContext* ctx) :
CKBaseManager(ctx, OBJECT_MANAGER_GUID, "Object Manager"),
m_ObjectsList(), m_ReturnedObjectOffsets(), m_ObjectCount(0),
m_GroupGlobalIndex(), m_SceneGlobalIndex(),
m_ObjectsListByClass(CKGetClassCount()) {}
CKObjectManager::~CKObjectManager() {
DestroyAllObjects();
}
ObjImpls::CKObject* CKObjectManager::CreateObject(CK_CLASSID cls, CKSTRING name,
CK_OBJECTCREATION_OPTIONS options, CK_CREATIONMODE* res) {
// todo: Process paramter options and res
// get description first
const CKClassDesc* desc = CKGetClassDesc(cls);
if (desc == nullptr) return nullptr;
// allocate a CK_ID first
CKDWORD decided_off;
if (this->m_ReturnedObjectOffsets.empty()) {
// create new CK_ID.
decided_off = static_cast<CKDWORD>(m_ObjectsList.size());
m_ObjectsList.resize(decided_off + 1);
} else {
// use returned CK_ID.
decided_off = m_ReturnedObjectOffsets.back();
m_ReturnedObjectOffsets.pop_back();
}
// create one
ObjImpls::CKObject* obj = desc->CreationFct(m_Context, Offset2Id(decided_off), name);
// put into slot and inc count
m_ObjectsList[decided_off] = obj;
++m_ObjectCount;
2023-09-16 22:38:21 +08:00
// add into classid indexed object list
m_ObjectsListByClass[static_cast<size_t>(cls)].push_back(Offset2Id(decided_off));
2023-09-04 22:58:53 +08:00
// set out variable
return obj;
}
ObjImpls::CKObject* CKObjectManager::GetObject(CK_ID id) {
CKDWORD off = Id2Offset(id);
if (off >= m_ObjectsList.size()) return nullptr;
return m_ObjectsList[off];
}
CKDWORD CKObjectManager::GetObjectCount() {
return m_ObjectCount;
}
void CKObjectManager::InternalDestroy(ObjImpls::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
desc->ReleaseFct(m_Context, obj);
}
void CKObjectManager::DestroyObjects(CK_ID* ids, CKDWORD count) {
// pre notice manager
m_Context->ExecuteManagersOnSequenceToBeDeleted(ids, count);
// collect object data
// remove invalid object first, set them to be deleted
// collection deleted object class ids
XContainer::XObjectArray validObjIds;
XContainer::XBitArray cids;
for (CKDWORD i = 0; i < count; ++i) {
CKDWORD off = Id2Offset(ids[i]);
if (off >= m_ObjectsList.size()) continue;
ObjImpls::CKObject* obj = m_ObjectsList[off];
if (obj == nullptr) continue;
2023-09-16 22:38:21 +08:00
// set to be deleted
CK_OBJECT_FLAGS objflag = obj->GetObjectFlags();
EnumsHelper::Add(objflag, CK_OBJECT_FLAGS::CK_OBJECT_TOBEDELETED);
obj->SetObjectFlags(objflag);
// collect class id
XContainer::NSXBitArray::Set(cids, static_cast<CKDWORD>(obj->GetClassID()));
// add into list
validObjIds.emplace_back(ids[i]);
}
// then remove deleted object from m_ObjectListByClass
// because we have set to be deleted flag.
for (size_t i = 0; i < m_ObjectsListByClass.size(); ++i) {
if (!XContainer::NSXBitArray::IsSet(cids, static_cast<CKDWORD>(i)))
continue;
XContainer::NSXObjectArray::PreDeletedCheck(m_ObjectsListByClass[i], m_Context);
}
// use collected cid to get all class ids which need receive notify
// and use m_ObjectListByClass to notify them
XContainer::XBitArray notifyCids = CKGetAllNotifyClassID(cids);
for (size_t i = 0; i < m_ObjectsListByClass.size(); ++i) {
if (!XContainer::NSXBitArray::IsSet(notifyCids, static_cast<CKDWORD>(i)))
continue;
for (auto& objid : m_ObjectsListByClass[i]) {
m_ObjectsList[Id2Offset(objid)]->CheckPreDeletion();
}
}
// calling PreDelete function for deleted objects
for (const auto& objid : validObjIds) {
m_ObjectsList[Id2Offset(objid)]->PreDelete();
}
// then free all valid object
for (const auto& objid : validObjIds) {
CKDWORD off = Id2Offset(objid);
ObjImpls::CKObject* obj = m_ObjectsList[off];
2023-09-16 22:38:21 +08:00
// free it
InternalDestroy(obj);
// return its allocated id.
// and dec count
m_ObjectsList[off] = nullptr;
m_ReturnedObjectOffsets.emplace_back(off);
--m_ObjectCount;
}
2023-09-04 22:58:53 +08:00
// run post deletion for notify object
for (size_t i = 0; i < m_ObjectsListByClass.size(); ++i) {
if (!XContainer::NSXBitArray::IsSet(notifyCids, static_cast<CKDWORD>(i)))
continue;
for (auto& objid : m_ObjectsListByClass[i]) {
m_ObjectsList[Id2Offset(objid)]->CheckPostDeletion();
}
}
2023-09-04 22:58:53 +08:00
// notice post
m_Context->ExecuteManagersOnSequenceDeleted(ids, count);
2023-09-04 22:58:53 +08:00
}
void CKObjectManager::DestroyAllObjects() {
// free all created objects
for (auto& ptr : m_ObjectsList) {
if (ptr != nullptr) {
InternalDestroy(ptr);
}
}
// restore returned object list
m_ReturnedObjectOffsets.clear();
// empty object list
m_ObjectsList.clear();
// reset count
m_ObjectCount = 0;
// clear obj by class list
for (auto& ls : m_ObjectsListByClass) {
ls.clear();
}
// clear group and scene global index at the same time
m_SceneGlobalIndex.clear();
m_GroupGlobalIndex.clear();
}
XContainer::XObjectPointerArray CKObjectManager::GetObjectByNameAndClass(CKSTRING name, CK_CLASSID cid, bool derived) {
XContainer::XObjectPointerArray result;
2023-09-04 22:58:53 +08:00
for (size_t i = 0; i < m_ObjectsListByClass.size(); ++i) {
// check class id first
if (derived) {
if (!CKIsChildClassOf(static_cast<CK_CLASSID>(i), cid)) continue;
} else {
if (static_cast<CK_CLASSID>(i) != cid) continue;
}
2023-09-04 22:58:53 +08:00
// iterate all sub object and check name
for (const auto& objoff : m_ObjectsListByClass[i]) {
ObjImpls::CKObject* obj = m_ObjectsList[objoff];
if (name == nullptr) {
// directly add
result.emplace_back(obj);
} else {
// check name
if (CKStrEqual(name, obj->GetName())) {
result.emplace_back(obj);
}
}
}
}
2023-09-06 10:42:23 +08:00
return result;
2023-09-04 22:58:53 +08:00
}
#pragma region Object Check
2023-09-16 22:38:21 +08:00
bool CKObjectManager::IsObjectSafe(CK_ID objid) {
CKDWORD off = Id2Offset(objid);
if (off >= m_ObjectsList.size()) return false;
return m_ObjectsList[off] != nullptr;
}
bool CKObjectManager::IsObjectPointerSafe(const ObjImpls::CKObject* objptr) {
if (objptr == nullptr) return false;
// iterate all object list to check
for (const auto& ptr : m_ObjectsList) {
if (ptr == objptr) return true;
2023-09-04 22:58:53 +08:00
}
2023-09-16 22:38:21 +08:00
return false;
}
#pragma endregion
#pragma region Special Functions
2023-09-16 22:38:21 +08:00
CKDWORD CKObjectManager::AllocateGroupGlobalIndex() {
// try find first non-true position
CKDWORD index;
if (!XContainer::NSXBitArray::GetUnsetBitPosition(m_GroupGlobalIndex, 0, index)) {
// failed. distribute new one
index = static_cast<CKDWORD>(m_GroupGlobalIndex.size());
2023-09-04 22:58:53 +08:00
m_GroupGlobalIndex.resize(m_GroupGlobalIndex.size() + 1);
}
2023-09-16 22:38:21 +08:00
2023-09-04 22:58:53 +08:00
// set to occupy
XContainer::NSXBitArray::Set(m_GroupGlobalIndex, index);
2023-09-04 22:58:53 +08:00
return index;
}
2023-09-16 22:38:21 +08:00
CKDWORD CKObjectManager::AllocateSceneGlobalIndex() {
2023-09-04 22:58:53 +08:00
// same as group
2023-09-16 22:38:21 +08:00
CKDWORD index;
if (!XContainer::NSXBitArray::GetUnsetBitPosition(m_SceneGlobalIndex, 0, index)) {
index = static_cast<CKDWORD>(m_SceneGlobalIndex.size());
2023-09-04 22:58:53 +08:00
m_SceneGlobalIndex.resize(m_SceneGlobalIndex.size() + 1);
}
2023-09-16 22:38:21 +08:00
XContainer::NSXBitArray::Set(m_SceneGlobalIndex, index);
2023-09-04 22:58:53 +08:00
return index;
}
void CKObjectManager::FreeGroupGlobalIndex(CKDWORD id) {
// check position
if (id >= m_GroupGlobalIndex.size()) return;
// set value
XContainer::NSXBitArray::Unset(m_GroupGlobalIndex, id);
2023-09-04 22:58:53 +08:00
}
void CKObjectManager::FreeSceneGlobalIndex(CKDWORD id) {
// same as group
if (id >= m_SceneGlobalIndex.size()) return;
XContainer::NSXBitArray::Unset(m_SceneGlobalIndex, id);
2023-09-04 22:58:53 +08:00
}
#pragma endregion
2023-09-04 22:58:53 +08:00
}