Skip to content

Commit 30cc51d

Browse files
committed
修正数据库加载实体组件异常
kbengine#621
1 parent b73bb27 commit 30cc51d

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

kbe/src/lib/db_mysql/entity_table_mysql.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,12 @@ void EntityTableItemMysql_Component::addToStream(MemoryStream* s, mysql::DBConte
15511551
{
15521552
std::vector<DBID>& dbids = iter->second->dbids[resultDBID];
15531553

1554-
if (dbids.size() > 0)
1554+
// 如果一个实体已经存档,开发中又对实体加入了组件,那么数据库中此时是没有数据的,加载实体时需要对这样的情况做一些判断
1555+
// 例如:实体加载时重新写入组件默认数据值
1556+
bool foundData = dbids.size() > 0;
1557+
(*s) << foundData;
1558+
1559+
if (foundData)
15551560
{
15561561
// 理论上一定是不会大于1个的
15571562
KBE_ASSERT(dbids.size() == 1);

kbe/src/lib/entitydef/datatype.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2710,7 +2710,10 @@ PyObject* EntityComponentType::createCellDataFromPersistentStream(MemoryStream*
27102710
if (!propertyDescription->hasCell())
27112711
continue;
27122712

2713-
PyObject* pyobj = propertyDescription->createFromStream(mstream);
2713+
PyObject* pyobj = NULL;
2714+
2715+
if(mstream)
2716+
propertyDescription->createFromStream(mstream);
27142717

27152718
if (pyobj == NULL)
27162719
{

kbe/src/lib/entitydef/scriptdef_module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class ScriptDefModule : public RefCountable
9191
PropertyDescription* findAliasPropertyDescription(ENTITY_DEF_ALIASID aliasID);
9292
MethodDescription* findAliasMethodDescription(ENTITY_DEF_ALIASID aliasID);
9393

94+
size_t numPropertys() {
95+
return getCellPropertyDescriptions().size() + getBasePropertyDescriptions().size();
96+
}
97+
9498
INLINE PROPERTYDESCRIPTION_MAP& getCellPropertyDescriptions();
9599
INLINE PROPERTYDESCRIPTION_MAP& getCellPropertyDescriptionsByDetailLevel(int8 detailLevel);
96100
INLINE PROPERTYDESCRIPTION_MAP& getBasePropertyDescriptions();

kbe/src/server/baseapp/baseapp.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,53 @@ PyObject* createDictDataFromPersistentStream(MemoryStream& s, const char* entity
6767
PyObject* pyVal = NULL;
6868
const char* attrname = propertyDescription->getName();
6969

70-
if (propertyDescription->getDataType()->type() == DATA_TYPE_ENTITY_COMPONENT && !propertyDescription->hasBase())
70+
if (propertyDescription->getDataType()->type() == DATA_TYPE_ENTITY_COMPONENT)
7171
{
72-
pyVal = ((EntityComponentType*)propertyDescription->getDataType())->createCellDataFromPersistentStream(&s);
72+
bool hasComponentData = false;
73+
EntityComponentType* pEntityComponentType = ((EntityComponentType*)propertyDescription->getDataType());
74+
75+
if(pEntityComponentType->pScriptDefModule()->numPropertys() > 0)
76+
s >> hasComponentData;
77+
78+
if (hasComponentData)
79+
{
80+
if (!propertyDescription->hasBase())
81+
{
82+
pyVal = pEntityComponentType->createCellDataFromPersistentStream(&s);
83+
}
84+
else
85+
{
86+
pyVal = propertyDescription->createFromPersistentStream(&s);
87+
88+
if (!propertyDescription->isSameType(pyVal))
89+
{
90+
if (pyVal)
91+
{
92+
Py_DECREF(pyVal);
93+
}
94+
95+
ERROR_MSG(fmt::format("Baseapp::createDictDataFromPersistentStream: {}.{} error, set to default!\n",
96+
entityType, attrname));
97+
98+
pyVal = propertyDescription->parseDefaultStr("");
99+
}
100+
}
101+
}
102+
else
103+
{
104+
if (!propertyDescription->hasBase())
105+
{
106+
pyVal = ((EntityComponentType*)propertyDescription->getDataType())->createCellDataFromPersistentStream(NULL);
107+
}
108+
else
109+
{
110+
111+
ERROR_MSG(fmt::format("Baseapp::createDictDataFromPersistentStream: {}.{} error, set to default!\n",
112+
entityType, attrname));
113+
114+
pyVal = propertyDescription->parseDefaultStr("");
115+
}
116+
}
73117
}
74118
else
75119
{

kbe/src/server/baseapp/entity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ void Entity::addPersistentsDataToStream(uint32 flags, MemoryStream* s)
414414
{
415415
// 由于存在一种情况, 组件def中没有内容, 但有cell脚本,此时baseapp上无法判断他是否有cell属性,所以写celldata时没有数据写入
416416
EntityComponentType* pEntityComponentType = (EntityComponentType*)propertyDescription->getDataType();
417-
if (pEntityComponentType->pScriptDefModule()->getPropertyDescrs().size() == 0 && pEntityComponentType->pScriptDefModule()->getCellPropertyDescriptions().size() == 0)
417+
if (pEntityComponentType->pScriptDefModule()->numPropertys() == 0)
418418
continue;
419419
}
420420

0 commit comments

Comments
 (0)