Skip to content

Commit cff1b82

Browse files
committed
修正账号实体加组件出错
kbengine#635
1 parent 47ef0ce commit cff1b82

File tree

11 files changed

+194
-45
lines changed

11 files changed

+194
-45
lines changed

kbe/src/lib/db_mysql/entity_table_mysql.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "remove_entity_helper.h"
88
#include "entitydef/scriptdef_module.h"
99
#include "entitydef/property.h"
10+
#include "entitydef/entitydef.h"
1011
#include "db_interface/db_interface.h"
1112
#include "db_interface/entity_table.h"
1213
#include "network/fixed_messages.h"
@@ -138,6 +139,13 @@ bool EntityTableMysql::initialize(ScriptDefModule* sm, std::string name)
138139
{
139140
PropertyDescription* pdescrs = iter->second;
140141

142+
// 如果某个实体没有cell部分, 而组件属性没有base部分则忽略
143+
if (!sm->hasCell())
144+
{
145+
if (pdescrs->getDataType()->type() == DATA_TYPE_ENTITY_COMPONENT && !pdescrs->hasBase())
146+
continue;
147+
}
148+
141149
EntityTableItem* pETItem = this->createItem(pdescrs->getDataType()->getName(), pdescrs->getDefaultValStr());
142150

143151
pETItem->pParentTable(this);
@@ -1500,13 +1508,20 @@ bool EntityTableItemMysql_Component::initialize(const PropertyDescription* pProp
15001508
pTable->tableName(tableName);
15011509
pTable->isChild(true);
15021510

1511+
ScriptDefModule* pScriptDefModule = EntityDef::findScriptModule(pparentTable->tableName(), false);
1512+
15031513
ScriptDefModule::PROPERTYDESCRIPTION_MAP& pdescrsMap = pEntityComponentScriptDefModule->getPersistentPropertyDescriptions();
15041514
ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = pdescrsMap.begin();
15051515

15061516
for (; iter != pdescrsMap.end(); ++iter)
15071517
{
15081518
PropertyDescription* pdescrs = iter->second;
15091519

1520+
if (!pScriptDefModule->hasCell() && pdescrs->hasCell() && !pdescrs->hasBase())
1521+
{
1522+
continue;
1523+
}
1524+
15101525
EntityTableItem* pETItem = pparentTable->createItem(pdescrs->getDataType()->getName(), pdescrs->getDefaultValStr());
15111526

15121527
pETItem->pParentTable(pparentTable);

kbe/src/lib/entitydef/datatype.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,7 @@ void EntityComponentType::addPersistentToStream(MemoryStream* mstream, PyObject*
24692469
propertyDescription->getName(), pScriptDefModule_ ? pScriptDefModule_->getName() : "", pScriptDefModule_ ? pScriptDefModule_->getUType() : 0,
24702470
COMPONENT_NAME_EX(CELLAPP_TYPE)));
24712471

2472-
propertyDescription->addToStream(mstream, NULL);
2472+
propertyDescription->addPersistentToStream(mstream, NULL);
24732473
}
24742474
}
24752475

@@ -2480,6 +2480,41 @@ void EntityComponentType::addPersistentToStream(MemoryStream* mstream, PyObject*
24802480
pEntityComponent->addPersistentToStream(mstream, pyValue);
24812481
}
24822482

2483+
//-------------------------------------------------------------------------------------
2484+
void EntityComponentType::addPersistentToStream(MemoryStream* mstream)
2485+
{
2486+
ScriptDefModule::PROPERTYDESCRIPTION_MAP& propertyDescrs = pScriptDefModule_->getPersistentPropertyDescriptions();
2487+
ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = propertyDescrs.begin();
2488+
2489+
for (; iter != propertyDescrs.end(); ++iter)
2490+
{
2491+
PropertyDescription* propertyDescription = iter->second;
2492+
2493+
PyObject* pyDefVal = propertyDescription->newDefaultVal();
2494+
propertyDescription->getDataType()->addToStream(mstream, pyDefVal);
2495+
Py_DECREF(pyDefVal);
2496+
}
2497+
}
2498+
//-------------------------------------------------------------------------------------
2499+
void EntityComponentType::addPersistentToStreamTemplates(ScriptDefModule* pScriptModule, MemoryStream* mstream)
2500+
{
2501+
ScriptDefModule::PROPERTYDESCRIPTION_MAP& propertyDescrs = pScriptDefModule_->getPersistentPropertyDescriptions();
2502+
ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = propertyDescrs.begin();
2503+
2504+
for (; iter != propertyDescrs.end(); ++iter)
2505+
{
2506+
PropertyDescription* propertyDescription = iter->second;
2507+
2508+
if (propertyDescription->hasCell())
2509+
{
2510+
// 一些实体没有cell部分, 因此cell属性忽略
2511+
if (!pScriptModule->hasCell())
2512+
continue;
2513+
}
2514+
2515+
propertyDescription->addPersistentToStream(mstream, NULL);
2516+
}
2517+
}
24832518
//-------------------------------------------------------------------------------------
24842519
void EntityComponentType::addCellDataToStream(MemoryStream* mstream, uint32 flags, PyObject* pyValue,
24852520
ENTITY_ID ownerID, PropertyDescription* parentPropertyDescription, COMPONENT_TYPE sendtoComponentType, bool checkValue)
@@ -2609,7 +2644,7 @@ PyObject* EntityComponentType::createFromStream(MemoryStream* mstream)
26092644
}
26102645

26112646
//-------------------------------------------------------------------------------------
2612-
PyObject* EntityComponentType::createFromPersistentStream(MemoryStream* mstream)
2647+
PyObject* EntityComponentType::createFromPersistentStream(ScriptDefModule* pScriptDefModule, MemoryStream* mstream)
26132648
{
26142649
KBE_ASSERT(EntityDef::context().currEntityID > 0);
26152650

@@ -2619,7 +2654,7 @@ PyObject* EntityComponentType::createFromPersistentStream(MemoryStream* mstream)
26192654
PyObject* pyEntityComponent = new(pyobj) EntityComponent(EntityDef::context().currEntityID, pScriptDefModule_, EntityDef::context().currComponentType);
26202655

26212656
EntityComponent* pEntityComponent = static_cast<EntityComponent*>(pyEntityComponent);
2622-
return pEntityComponent->createFromPersistentStream(mstream);
2657+
return pEntityComponent->createFromPersistentStream(pScriptDefModule, mstream);
26232658
}
26242659

26252660
//-------------------------------------------------------------------------------------

kbe/src/lib/entitydef/datatype.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,14 @@ class EntityComponentType : public DataType
769769

770770
void addToStream(MemoryStream* mstream, PyObject* pyValue);
771771
void addPersistentToStream(MemoryStream* mstream, PyObject* pyValue);
772+
void addPersistentToStream(MemoryStream* mstream);
773+
void addPersistentToStreamTemplates(ScriptDefModule* pScriptModule, MemoryStream* mstream);
772774
void addCellDataToStream(MemoryStream* mstream, uint32 flags, PyObject* pyValue,
773775
ENTITY_ID ownerID, PropertyDescription* parentPropertyDescription, COMPONENT_TYPE sendtoComponentType, bool checkValue);
774776

775777
PyObject* createFromStream(MemoryStream* mstream);
776-
PyObject* createFromPersistentStream(MemoryStream* mstream);
778+
PyObject* createFromPersistentStream(ScriptDefModule* pScriptDefModule, MemoryStream* mstream);
779+
777780
PyObject* createCellData();
778781
PyObject* createCellDataFromPersistentStream(MemoryStream* mstream);
779782
PyObject* createCellDataFromStream(MemoryStream* mstream);

kbe/src/lib/entitydef/entity_component.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ PyObject* EntityComponent::pyGetOwnerID()
125125
//-------------------------------------------------------------------------------------
126126
PyObject* EntityComponent::owner(bool attempt)
127127
{
128-
if (ownerID_ == 0)
129-
{
130-
S_Return;
131-
}
132-
133128
if (!owner_)
134129
{
130+
if (ownerID_ == 0)
131+
{
132+
return NULL;
133+
}
134+
135135
if (clientappID_ > 0)
136136
{
137137
owner_ = EntityDef::tryGetEntity(clientappID_, ownerID_);
@@ -153,10 +153,13 @@ PyObject* EntityComponent::owner(bool attempt)
153153
//-------------------------------------------------------------------------------------
154154
void EntityComponent::updateOwner(ENTITY_ID id, PyObject* pOwner)
155155
{
156-
if (pOwner == owner_)
156+
if (ownerID_ == id)
157157
{
158-
KBE_ASSERT(ownerID_ == id);
159-
return;
158+
if (owner_)
159+
{
160+
KBE_ASSERT(pOwner == owner_);
161+
return;
162+
}
160163
}
161164

162165
ownerID_ = id;
@@ -743,6 +746,10 @@ bool EntityComponent::isSamePersistentType(PyObject* pyValue)
743746
PyObject* pyVal = NULL;
744747
if (propertyDescription->hasCell())
745748
{
749+
// 一些实体没有cell部分, 因此cell属性忽略
750+
if (!cellComponentPart)
751+
continue;
752+
746753
pyVal = PyDict_GetItemString(cellComponentPart, propertyDescription->getName());
747754
Py_XINCREF(pyVal);
748755
}
@@ -789,7 +796,7 @@ bool EntityComponent::isSamePersistentType(PyObject* pyValue)
789796
}
790797

791798
//-------------------------------------------------------------------------------------
792-
PyObject* EntityComponent::createFromPersistentStream(MemoryStream* mstream)
799+
PyObject* EntityComponent::createFromPersistentStream(ScriptDefModule* pScriptModule, MemoryStream* mstream)
793800
{
794801
KBE_ASSERT(g_componentType == BASEAPP_TYPE);
795802

@@ -806,6 +813,11 @@ PyObject* EntityComponent::createFromPersistentStream(MemoryStream* mstream)
806813
{
807814
PropertyDescription* propertyDescription = iter->second;
808815

816+
if (pScriptModule && !pScriptModule->hasCell() && !propertyDescription->hasBase())
817+
{
818+
continue;
819+
}
820+
809821
PyObject* pyobj = propertyDescription->createFromStream(mstream);
810822

811823
if (propertyDescription->hasCell() && !cellDataDict)
@@ -906,6 +918,10 @@ void EntityComponent::addPersistentToStream(MemoryStream* mstream, PyObject* pyV
906918
PyObject* pyVal = NULL;
907919
if (propertyDescription->hasCell())
908920
{
921+
// 一些实体没有cell部分, 因此cell属性忽略
922+
if (!cellComponentPart)
923+
continue;
924+
909925
pyVal = PyDict_GetItemString(cellComponentPart, propertyDescription->getName());
910926
Py_XINCREF(pyVal);
911927
}
@@ -927,7 +943,7 @@ void EntityComponent::addPersistentToStream(MemoryStream* mstream, PyObject* pyV
927943
propertyDescription->getName(), pComponentDescrs_ ? pComponentDescrs_->getName() : "", pComponentDescrs_ ? pComponentDescrs_->getUType() : 0,
928944
owner()->ob_type->tp_name, ownerID(), COMPONENT_NAME_EX(componentType())));
929945

930-
propertyDescription->addToStream(mstream, NULL);
946+
propertyDescription->addPersistentToStream(mstream, NULL);
931947
}
932948
}
933949

@@ -995,7 +1011,9 @@ void EntityComponent::addToServerStream(MemoryStream* mstream, PyObject* pyValue
9951011
(*mstream) << pPropertyDescription_->getUType();
9961012
(*mstream) << propertyDescription->getUType();
9971013

998-
propertyDescription->addToStream(mstream, NULL);
1014+
PyObject* pyDefVal = propertyDescription->newDefaultVal();
1015+
propertyDescription->addToStream(mstream, pyDefVal);
1016+
Py_DECREF(pyDefVal);
9991017
}
10001018
}
10011019
}
@@ -1105,7 +1123,9 @@ void EntityComponent::addToClientStream(MemoryStream* mstream, PyObject* pyValue
11051123
(*mstream) << propertyDescription->getUType();
11061124
}
11071125

1108-
propertyDescription->addToStream(mstream, NULL);
1126+
PyObject* pyDefVal = propertyDescription->newDefaultVal();
1127+
propertyDescription->addToStream(mstream, pyDefVal);
1128+
Py_DECREF(pyDefVal);
11091129
}
11101130
}
11111131
}
@@ -1283,7 +1303,7 @@ void EntityComponent::createFromDict(PyObject* pyDict)
12831303
}
12841304

12851305
//-------------------------------------------------------------------------------------
1286-
void EntityComponent::updateFromDict(PyObject* pyDict)
1306+
void EntityComponent::updateFromDict(PyObject* pOwner, PyObject* pyDict)
12871307
{
12881308
// 设置为-1, 避免onScriptSetAttribute中尝试广播属性
12891309
ENTITY_ID oid = ownerID_;
@@ -1296,15 +1316,18 @@ void EntityComponent::updateFromDict(PyObject* pyDict)
12961316

12971317
PyObject* pyCellData = NULL;
12981318

1299-
PyObject* cellDataDict = PyObject_GetAttrString(owner(), "cellData");
1300-
if (!cellDataDict)
1319+
if (pOwner)
13011320
{
1302-
PyErr_Clear();
1303-
}
1304-
else
1305-
{
1306-
pyCellData = PyDict_GetItemString(cellDataDict, pPropertyDescription_->getName());
1307-
Py_DECREF(cellDataDict);
1321+
PyObject* cellDataDict = PyObject_GetAttrString(pOwner, "cellData");
1322+
if (!cellDataDict)
1323+
{
1324+
PyErr_Clear();
1325+
}
1326+
else
1327+
{
1328+
pyCellData = PyDict_GetItemString(cellDataDict, pPropertyDescription_->getName());
1329+
Py_DECREF(cellDataDict);
1330+
}
13081331
}
13091332

13101333
const ScriptDefModule::PROPERTYDESCRIPTION_MAP* pPropertyDescrs = pChildPropertyDescrs();
@@ -1325,7 +1348,7 @@ void EntityComponent::updateFromDict(PyObject* pyDict)
13251348
CRITICAL_MSG(fmt::format("EntityComponent::updateFromDict: {} type(curr_py: {} != {}) error! name={}, utype={}, owner={}, ownerID={}, domain={}.\n",
13261349
propertyDescription->getName(), (value ? value->ob_type->tp_name : "unknown"), propertyDescription->getDataType()->getName(),
13271350
pComponentDescrs_ ? pComponentDescrs_->getName() : "", pComponentDescrs_ ? pComponentDescrs_->getUType() : 0,
1328-
owner()->ob_type->tp_name, ownerID(), COMPONENT_NAME_EX(componentType())));
1351+
(pOwner ? pOwner->ob_type->tp_name : "unknown"), ownerID(), COMPONENT_NAME_EX(componentType())));
13291352
}
13301353
else
13311354
{
@@ -1368,6 +1391,9 @@ void EntityComponent::convertDictDataToEntityComponent(ENTITY_ID entityID, PyObj
13681391
ScriptDefModule::COMPONENTDESCRIPTION_MAP::iterator comps_iter = componentDescrs.begin();
13691392
for (; comps_iter != componentDescrs.end(); ++comps_iter)
13701393
{
1394+
if (!comps_iter->second->getScriptType())
1395+
continue;
1396+
13711397
PyObject* pyObj = PyDict_GetItemString(cellData, comps_iter->first.c_str());
13721398
if (!pyObj || !PyDict_Check(pyObj))
13731399
{
@@ -1387,7 +1413,7 @@ void EntityComponent::convertDictDataToEntityComponent(ENTITY_ID entityID, PyObj
13871413

13881414
PyObject* pyobj = comps_iter->second->createObject();
13891415

1390-
// 执行Entity的构造函数
1416+
// 执行Entity组件的构造函数
13911417
PyObject* pyEntityComponent = new(pyobj) EntityComponent(entityID, comps_iter->second, g_componentType);
13921418

13931419
EntityComponent* pEntityComponent = static_cast<EntityComponent*>(pyEntityComponent);

kbe/src/lib/entitydef/entity_component.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class EntityComponent : public script::ScriptObject
149149

150150
bool isSamePersistentType(PyObject* pyValue);
151151
void addPersistentToStream(MemoryStream* mstream, PyObject* pyValue);
152-
PyObject* createFromPersistentStream(MemoryStream* mstream);
152+
void addPersistentToStreamTemplates(ScriptDefModule* pScriptModule, MemoryStream* mstream);
153+
PyObject* createFromPersistentStream(ScriptDefModule* pScriptModule, MemoryStream* mstream);
153154

154155
PropertyDescription* getProperty(ENTITY_PROPERTY_UID child_uid);
155156

@@ -184,7 +185,7 @@ class EntityComponent : public script::ScriptObject
184185
PyObject* createCellData();
185186

186187
void createFromDict(PyObject* pyDict);
187-
void updateFromDict(PyObject* pyDict);
188+
void updateFromDict(PyObject* pOwner, PyObject* pyDict);
188189

189190
static void convertDictDataToEntityComponent(ENTITY_ID entityID, PyObject* pEntity, ScriptDefModule* pEntityScriptDescrs, PyObject* cellData);
190191
static std::vector<EntityComponent*> getComponents(const std::string& name, PyObject* pEntity, ScriptDefModule* pEntityScriptDescrs);

kbe/src/lib/entitydef/entity_macro.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public: \
437437
if(PyDict_Check(value) /* createDictDataFromPersistentStream 流程导致非字典 */) \
438438
{ \
439439
EntityComponent* pEntityComponent = (EntityComponent*)PyObject_GetAttr(this, key); \
440-
pEntityComponent->updateFromDict(value); \
440+
pEntityComponent->updateFromDict(this, value); \
441441
Py_DECREF(pEntityComponent); \
442442
} \
443443
else \
@@ -471,7 +471,25 @@ public: \
471471
} \
472472
} \
473473
else \
474+
{ \
475+
wchar_t* PyUnicode_AsWideCharStringRet0 = PyUnicode_AsWideCharString(key, NULL); \
476+
char* ccattr = strutil::wchar2char(PyUnicode_AsWideCharStringRet0); \
477+
PyMem_Free(PyUnicode_AsWideCharStringRet0); \
478+
\
479+
PropertyDescription* pCompPropertyDescription = \
480+
pScriptModule_->findComponentPropertyDescription(ccattr); \
481+
\
482+
free(ccattr); \
483+
\
484+
if (pCompPropertyDescription) \
485+
{ \
486+
/* 一般在base上可能放在cellData中是字典,而没有cell的实体需要pass这个设置 */ \
487+
if(PyDict_Check(value)) \
488+
continue; \
489+
} \
490+
\
474491
PyObject_SetAttr(this, key, value); \
492+
} \
475493
} \
476494
\
477495
SCRIPT_ERROR_CHECK(); \

kbe/src/lib/entitydef/property.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,24 @@ void EntityComponentDescription::addPersistentToStream(MemoryStream* mstream, Py
495495
((EntityComponentType*)dataType_)->addPersistentToStream(mstream, pyValue);
496496
}
497497

498+
//-------------------------------------------------------------------------------------
499+
void EntityComponentDescription::addPersistentToStreamTemplates(ScriptDefModule* pScriptModule, MemoryStream* mstream)
500+
{
501+
((EntityComponentType*)dataType_)->addPersistentToStreamTemplates(pScriptModule, mstream);
502+
}
503+
498504
//-------------------------------------------------------------------------------------
499505
PyObject* EntityComponentDescription::createFromPersistentStream(MemoryStream* mstream)
500506
{
501-
EntityComponent* pEntityComponent = static_cast<EntityComponent*>(static_cast<EntityComponentType*>(dataType_)->createFromPersistentStream(mstream));
507+
EntityComponent* pEntityComponent = static_cast<EntityComponent*>(static_cast<EntityComponentType*>(dataType_)->createFromPersistentStream(NULL, mstream));
508+
pEntityComponent->pPropertyDescription(this);
509+
return pEntityComponent;
510+
}
511+
512+
//-------------------------------------------------------------------------------------
513+
PyObject* EntityComponentDescription::createFromPersistentStream(ScriptDefModule* pScriptModule, MemoryStream* mstream)
514+
{
515+
EntityComponent* pEntityComponent = static_cast<EntityComponent*>(static_cast<EntityComponentType*>(dataType_)->createFromPersistentStream(pScriptModule, mstream));
502516
pEntityComponent->pPropertyDescription(this);
503517
return pEntityComponent;
504518
}

kbe/src/lib/entitydef/property.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ class EntityComponentDescription : public PropertyDescription
274274

275275
virtual bool isSamePersistentType(PyObject* pyValue);
276276
virtual void addPersistentToStream(MemoryStream* mstream, PyObject* pyValue);
277+
void addPersistentToStreamTemplates(ScriptDefModule* pScriptModule, MemoryStream* mstream);
277278
virtual PyObject* createFromPersistentStream(MemoryStream* mstream);
279+
PyObject* createFromPersistentStream(ScriptDefModule* pScriptModule, MemoryStream* mstream);
278280

279281
virtual PyObject* createFromStream(MemoryStream* mstream);
280282

0 commit comments

Comments
 (0)