Repository navigation
Expand file tree
/
Copy pathmethod.cpp
More file actions
275 lines (229 loc) · 7.4 KB
/
Copy pathmethod.cpp
File metadata and controls
275 lines (229 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "method.h"
#include "entitydef.h"
#include "network/bundle.h"
#ifndef CODE_INLINE
#include "method.inl"
#endif
namespace KBEngine{
uint32 MethodDescription::methodDescriptionCount_ = 0;
//-------------------------------------------------------------------------------------
MethodDescription::MethodDescription(ENTITY_METHOD_UID utype, COMPONENT_ID domain,
std::string name,
bool isExposed):
methodDomain_(domain),
name_(name),
utype_(utype),
argTypes_(),
isExposed_(isExposed),
currCallerID_(0),
aliasID_(-1)
{
MethodDescription::methodDescriptionCount_++;
EntityDef::md5().append((void*)name_.c_str(), (int)name_.size());
EntityDef::md5().append((void*)&utype_, sizeof(ENTITY_METHOD_UID));
EntityDef::md5().append((void*)&isExposed_, sizeof(bool));
}
//-------------------------------------------------------------------------------------
MethodDescription::~MethodDescription()
{
std::vector<DataType*>::iterator iter = argTypes_.begin();
for(; iter != argTypes_.end(); ++iter)
(*iter)->decRef();
argTypes_.clear();
}
//-------------------------------------------------------------------------------------
void MethodDescription::setExposed(void)
{
isExposed_ = true;
EntityDef::md5().append((void*)&isExposed_, sizeof(bool));
}
//-------------------------------------------------------------------------------------
bool MethodDescription::pushArgType(DataType* dataType)
{
if(dataType == NULL)
{
ERROR_MSG("MethodDescription::pushArgType: dataType is NULL!\n");
return false;
}
dataType->incRef();
argTypes_.push_back(dataType);
DATATYPE_UID uid = dataType->id();
EntityDef::md5().append((void*)&uid, sizeof(DATATYPE_UID));
EntityDef::md5().append((void*)&isExposed_, sizeof(bool));
return true;
}
//-------------------------------------------------------------------------------------
bool MethodDescription::checkArgs(PyObject* args)
{
if (args == NULL || !PyTuple_Check(args))
{
PyErr_Format(PyExc_AssertionError, "Method::checkArgs: method[%s] args is not a tuple.\n",
getName());
PyErr_PrintEx(0);
return false;
}
int offset = (isExposed() == true && g_componentType == CELLAPP_TYPE && isCell()) ? 1 : 0;
uint8 argsSize = (uint8)argTypes_.size();
uint8 giveArgsSize = (uint8)PyTuple_Size(args);
if (giveArgsSize != argsSize + offset)
{
PyErr_Format(PyExc_AssertionError, "Method::checkArgs: method[%s] requires exactly %d argument%s%s; %d given",
getName(),
argsSize,
(offset > 0) ? " + exposed(1)" : "",
(argsSize == 1) ? "" : "s",
PyTuple_Size(args));
PyErr_PrintEx(0);
return false;
}
// 检查是否是一个exposed方法
if(offset > 0)
{
PyObject* pyExposed = PyTuple_GetItem(args, 0);
if (!PyLong_Check(pyExposed))
{
PyObject* pyeid = PyObject_GetAttrString(pyExposed, "id");
if (pyeid == NULL || !PyLong_Check(pyeid))
{
Py_XDECREF(pyeid);
PyErr_Format( PyExc_TypeError,
"Method::checkArgs: method[%s] requires None, an id, or an object with an "
"id as its first agument", getName());
PyErr_PrintEx(0);
return false;
}
Py_DECREF(pyeid);
}
}
for(uint8 i=0; i <argsSize; ++i)
{
PyObject* pyArg = PyTuple_GetItem(args, i + offset);
if (!argTypes_[i]->isSameType(pyArg))
{
PyObject* pExample = argTypes_[i]->parseDefaultStr("");
PyErr_Format(PyExc_AssertionError,
"Method::checkArgs: method[%s] argument %d: Expected %s, %s found",
getName(),
i+1,
pExample->ob_type->tp_name,
pyArg != NULL ? pyArg->ob_type->tp_name : "NULL");
PyErr_PrintEx(0);
Py_DECREF(pExample);
return false;
}
}
return true;
}
//-------------------------------------------------------------------------------------
void MethodDescription::addToStream(MemoryStream* mstream, PyObject* args)
{
uint8 argsSize = argTypes_.size();
int offset = 0;
// 将utype放进去,方便对端识别这个方法
// 这里如果aliasID_大于0则采用一个优化的办法, 使用1字节传输
if(aliasID_ < 0)
{
(*mstream) << utype_;
}
else
{
uint8 utype = (uint8)aliasID_;
(*mstream) << utype;
}
// 如果是exposed方法则先将entityID打包进去
if(isExposed() && g_componentType == CELLAPP_TYPE && isCell())
{
offset = 1;
ENTITY_ID eid = PyLong_AsLong(PyTuple_GetItem(args, 0));
(*mstream) << eid;
}
// 将每一个参数添加到流中
for(uint8 i=0; i <argsSize; ++i)
{
PyObject* pyArg = PyTuple_GetItem(args, i + offset);
argTypes_[i]->addToStream(mstream, pyArg);
}
}
//-------------------------------------------------------------------------------------
PyObject* MethodDescription::createFromStream(MemoryStream* mstream)
{
size_t argSize = getArgSize();
PyObject* pyArgsTuple = NULL;
int offset = 0;
if(isExposed() && g_componentType == CELLAPP_TYPE && isCell())
{
offset = 1;
pyArgsTuple = PyTuple_New(argSize + offset);
// 设置一个调用者ID提供给脚本判断来源是否正确
KBE_ASSERT(currCallerID_ > 0);
PyTuple_SET_ITEM(pyArgsTuple, 0, PyLong_FromLong(currCallerID_));
}
else
pyArgsTuple = PyTuple_New(argSize);
for(size_t index=0; index<argSize; ++index)
{
PyObject* pyitem = argTypes_[index]->createFromStream(mstream);
if(pyitem == NULL)
{
WARNING_MSG(fmt::format("MethodDescription::createFromStream: {} arg[{}][{}] is NULL.\n",
this->getName(), index, argTypes_[index]->getName()));
}
PyTuple_SET_ITEM(pyArgsTuple, index + offset, pyitem);
}
return pyArgsTuple;
}
//-------------------------------------------------------------------------------------
size_t MethodDescription::getArgSize(void)
{
return argTypes_.size();
}
//-------------------------------------------------------------------------------------
PyObject* MethodDescription::call(PyObject* func, PyObject* args)
{
PyObject* pyResult = NULL;
if (!PyCallable_Check(func))
{
PyErr_Format(PyExc_TypeError, "MethodDescription::call: method[%s] call attempted on a error object!",
getName());
}
else
{
if(args == NULL)
{
pyResult = PyObject_CallObject(func, NULL);
}
else
{
if(checkArgs(args))
pyResult = PyObject_CallObject(func, args);
}
}
if (PyErr_Occurred())
{
if (isExposed() && PyErr_ExceptionMatches(PyExc_TypeError))
{
WARNING_MSG(fmt::format("MethodDescription::call: {} is method of exposed, if there is a missing arguments error, "
"try adding exposedID, For example: \ndef func(msg): => def func(exposedID, msg):\n",
this->getName()));
}
PyErr_PrintEx(0);
}
return pyResult;
}
//-------------------------------------------------------------------------------------
}