E* EntityApp<E>::createEntityCommon(const char* entityType, PyObject* params, bool isInitializeScript, ENTITY_ID eid, bool initProperty) { // 检查ID是否足够, 不足返回NULL if(eid <= 0 && idClient_.getSize() == 0) { PyErr_SetString(PyExc_SystemError, "EntityApp::createEntityCommon: is Failed. not enough entityIDs."); PyErr_PrintEx(0); return NULL; } ScriptDefModule* sm = EntityDef::findScriptModule(entityType); if(sm == NULL) { PyErr_Format(PyExc_TypeError, "EntityApp::createEntityCommon: entity [%s] not found.\n", entityType); PyErr_PrintEx(0); return NULL; } else if(componentType_ == CELLAPP_TYPE ? !sm->hasCell() : !sm->hasBase()) { PyErr_Format(PyExc_TypeError, "EntityApp::createEntityCommon: entity [%s] not found.\n", entityType); PyErr_PrintEx(0); return NULL; } PyObject* obj = sm->createObject(); // 判断是否要分配一个新的id ENTITY_ID id = eid; if(id <= 0) id = idClient_.alloc(); E* entity = onCreateEntityCommon(obj, sm, id); if(initProperty) entity->initProperty(); // 将entity加入entities pEntities_->add(id, entity); // 初始化脚本 if(isInitializeScript) entity->initializeEntity(params); SCRIPT_ERROR_CHECK(); if(g_debugEntity) { INFO_MSG(boost::format("EntityApp::createEntityCommon: new %1% (%2%) refc=%3%.\n") % entityType % id % obj->ob_refcnt); } else { INFO_MSG(boost::format("EntityApp::createEntityCommon: new %1% (%2%)\n") % entityType % id); } return entity; }
//------------------------------------------------------------------------------------- client::Entity* ClientObjectBase::createEntityCommon(const char* entityType, PyObject* params, bool isInitializeScript, ENTITY_ID eid, bool initProperty, EntityMailbox* base, EntityMailbox* cell) { KBE_ASSERT(eid > 0); ScriptDefModule* sm = EntityDef::findScriptModule(entityType); if(sm == NULL) { PyErr_Format(PyExc_TypeError, "ClientObjectBase::createEntityCommon: entity [%s] not found.\n", entityType); PyErr_PrintEx(0); return NULL; } else if(!sm->hasClient()) { PyErr_Format(PyExc_TypeError, "ClientObjectBase::createEntityCommon: entity [%s] not found.\n", entityType); PyErr_PrintEx(0); return NULL; } PyObject* obj = sm->createObject(); client::Entity* entity = new(obj) client::Entity(eid, sm, base, cell); entity->pClientApp(this); if(initProperty) entity->initProperty(); // 将entity加入entities pEntities_->add(eid, entity); // 初始化脚本 if(isInitializeScript) entity->initializeEntity(params); SCRIPT_ERROR_CHECK(); if(g_debugEntity) { INFO_MSG(boost::format("ClientObjectBase::createEntityCommon: new %1% (%2%) refc=%3%.\n") % entityType % eid % obj->ob_refcnt); } else { INFO_MSG(boost::format("ClientObjectBase::createEntityCommon: new %1% (%2%)\n") % entityType % eid); } EventData_CreatedEntity eventdata; eventdata.pEntity = entity->getAspect(); eventHandler_.fire(&eventdata); return entity; }