//------------------------------------------------------------------------------------- SyncEntityStreamTemplateHandler::SyncEntityStreamTemplateHandler(Network::NetworkInterface & networkInterface): Task(), networkInterface_(networkInterface) { networkInterface.dispatcher().addTask(this); MemoryStream accountDefMemoryStream; ENGINE_COMPONENT_INFO& dbcfg = g_kbeSrvConfig.getDBMgr(); ScriptDefModule* scriptModule = EntityDef::findScriptModule(dbcfg.dbAccountEntityScriptType); if(scriptModule != NULL) { ScriptDefModule::PROPERTYDESCRIPTION_MAP& propertyDescrs = scriptModule->getPersistentPropertyDescriptions(); ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = propertyDescrs.begin(); if(scriptModule->hasCell()) { Vector3 pos, dir; ADD_POSDIR_TO_STREAM(accountDefMemoryStream, pos, dir); } for(; iter != propertyDescrs.end(); ++iter) { PropertyDescription* propertyDescription = iter->second; accountDefMemoryStream << propertyDescription->getUType(); propertyDescription->addPersistentToStream(&accountDefMemoryStream, NULL); } } }
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; }
//------------------------------------------------------------------------------------- void EntityComponent::initProperty(bool isReload) { ScriptDefModule::PROPERTYDESCRIPTION_MAP* oldpropers = NULL; if (isReload) { ScriptDefModule* pOldScriptDefModule = EntityDef::findOldScriptModule(pComponentDescrs_->getName()); if (!pOldScriptDefModule) { ERROR_MSG(fmt::format("{}::initProperty: not found old_module!\n", pComponentDescrs_->getName())); KBE_ASSERT(false && "EntityComponent::initProperty: not found old_module"); } oldpropers = &pOldScriptDefModule->getPropertyDescrs(); } const ScriptDefModule::PROPERTYDESCRIPTION_MAP* pPropertyDescrs = pChildPropertyDescrs(); ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = pPropertyDescrs->begin(); for (; iter != pPropertyDescrs->end(); ++iter) { PropertyDescription* propertyDescription = iter->second; DataType* dataType = propertyDescription->getDataType(); if (oldpropers) { ScriptDefModule::PROPERTYDESCRIPTION_MAP::iterator olditer = oldpropers->find(iter->first); if (olditer != oldpropers->end()) { if (strcmp(olditer->second->getDataType()->getName(), propertyDescription->getDataType()->getName()) == 0 && strcmp(olditer->second->getDataType()->getName(), propertyDescription->getDataType()->getName()) == 0) continue; } } if (dataType) { PyObject* defObj = propertyDescription->newDefaultVal(); PyObject_SetAttrString(static_cast<PyObject*>(this), propertyDescription->getName(), defObj); Py_DECREF(defObj); /* DEBUG_MSG(fmt::format("EntityComponent::"#CLASS": added [{}] property ref={}.\n", propertyDescription->getName(), defObj->ob_refcnt));*/ } else { ERROR_MSG(fmt::format("EntityComponent::initProperty: {} dataType is NULL.\n", propertyDescription->getName())); } } }
//------------------------------------------------------------------------------------- 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; }
//------------------------------------------------------------------------------------- bool SyncEntityStreamTemplateHandler::process() { Components::COMPONENTS& cts = Components::getSingleton().getComponents(DBMGR_TYPE); Network::Channel* pChannel = NULL; if(cts.size() > 0) { Components::COMPONENTS::iterator ctiter = cts.begin(); if((*ctiter).pChannel == NULL) return true; pChannel = (*ctiter).pChannel; } if(pChannel == NULL) return true; MemoryStream accountDefMemoryStream; ENGINE_COMPONENT_INFO& dbcfg = g_kbeSrvConfig.getDBMgr(); ScriptDefModule* scriptModule = EntityDef::findScriptModule(dbcfg.dbAccountEntityScriptType); if(scriptModule == NULL) { delete this; return false; } ScriptDefModule::PROPERTYDESCRIPTION_MAP& propertyDescrs = scriptModule->getPersistentPropertyDescriptions(); ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = propertyDescrs.begin(); if(scriptModule->hasCell()) { Vector3 pos, dir; ADD_POSDIR_TO_STREAM(accountDefMemoryStream, pos, dir); } for(; iter != propertyDescrs.end(); ++iter) { PropertyDescription* propertyDescription = iter->second; accountDefMemoryStream << propertyDescription->getUType(); propertyDescription->addPersistentToStream(&accountDefMemoryStream, NULL); } Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(DbmgrInterface::syncEntityStreamTemplate); (*pBundle).append(accountDefMemoryStream); pChannel->send(pBundle); delete this; return false; }
//------------------------------------------------------------------------------------- bool EntityTables::load(DBInterface* dbi) { EntityDef::SCRIPT_MODULES smodules = EntityDef::getScriptModules(); EntityDef::SCRIPT_MODULES::const_iterator iter = smodules.begin(); for(; iter != smodules.end(); ++iter) { ScriptDefModule* pSM = (*iter).get(); EntityTable* pEtable = dbi->createEntityTable(); bool ret = pEtable->initialize(pSM, pSM->getName()); if(!ret) { delete pEtable; return false; } tables_[pEtable->tableName()].reset(pEtable); } return true; }
//------------------------------------------------------------------------------------- void ClientObjectBase::onEntityEnterWorld(Mercury::Channel * pChannel, ENTITY_ID eid, ENTITY_SCRIPT_UID scriptType, SPACE_ID spaceID) { client::Entity* entity = pEntities_->find(eid); if(entity == NULL) { BUFFEREDMESSAGE::iterator iter = bufferedCreateEntityMessage_.find(eid); if(iter != bufferedCreateEntityMessage_.end()) { ScriptDefModule* sm = EntityDef::findScriptModule(scriptType); KBE_ASSERT(sm); // 设置entity的cellMailbox EntityMailbox* mailbox = new EntityMailbox(EntityDef::findScriptModule(sm->getName()), NULL, appID(), eid, MAILBOX_TYPE_CELL); createEntityCommon(sm->getName(), NULL, true, eid, true, NULL, mailbox); this->onUpdatePropertys(pChannel, *iter->second.get()); bufferedCreateEntityMessage_.erase(iter); } else { ERROR_MSG(boost::format("ClientObjectBase::onEntityEnterWorld: not found entity(%1%).\n") % eid); } return; } DEBUG_MSG(boost::format("ClientObjectBase::onEntityEnterWorld: %1%.\n") % eid); EventData_EnterWorld eventdata; eventdata.spaceID = spaceID; eventdata.pEntity = entity->getAspect(); eventHandler_.fire(&eventdata); entity->onEnterWorld(); }