Repository navigation
Expand file tree
/
Copy pathpython_app.cpp
More file actions
766 lines (639 loc) · 20.9 KB
/
Copy pathpython_app.cpp
File metadata and controls
766 lines (639 loc) · 20.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
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 "python_app.h"
#include "server/py_file_descriptor.h"
namespace KBEngine{
KBEngine::ScriptTimers KBEngine::PythonApp::scriptTimers_;
/**
内部定时器处理类
*/
class ScriptTimerHandler : public TimerHandler
{
public:
ScriptTimerHandler(ScriptTimers* scriptTimers, PyObject * callback) :
pyCallback_(callback),
scriptTimers_(scriptTimers)
{
}
~ScriptTimerHandler()
{
Py_DECREF(pyCallback_);
}
private:
virtual void handleTimeout(TimerHandle handle, void * pUser)
{
int id = ScriptTimersUtil::getIDForHandle(scriptTimers_, handle);
PyObject *pyRet = PyObject_CallFunction(pyCallback_, "i", id);
if (pyRet == NULL)
{
SCRIPT_ERROR_CHECK();
return;
}
return;
}
virtual void onRelease(TimerHandle handle, void * /*pUser*/)
{
scriptTimers_->releaseTimer(handle);
delete this;
}
PyObject* pyCallback_;
ScriptTimers* scriptTimers_;
};
//-------------------------------------------------------------------------------------
PythonApp::PythonApp(Network::EventDispatcher& dispatcher,
Network::NetworkInterface& ninterface,
COMPONENT_TYPE componentType,
COMPONENT_ID componentID):
ServerApp(dispatcher, ninterface, componentType, componentID),
script_(),
entryScript_()
{
ScriptTimers::initialize(*this);
}
//-------------------------------------------------------------------------------------
PythonApp::~PythonApp()
{
}
//-------------------------------------------------------------------------------------
bool PythonApp::inInitialize()
{
if(!installPyScript())
return false;
if(!installPyModules())
return false;
return true;
}
//-------------------------------------------------------------------------------------
bool PythonApp::initializeEnd()
{
gameTickTimerHandle_ = this->dispatcher().addTimer(1000000 / g_kbeSrvConfig.gameUpdateHertz(), this,
reinterpret_cast<void *>(TIMEOUT_GAME_TICK));
return true;
}
//-------------------------------------------------------------------------------------
void PythonApp::onShutdownBegin()
{
ServerApp::onShutdownBegin();
}
//-------------------------------------------------------------------------------------
void PythonApp::onShutdownEnd()
{
ServerApp::onShutdownEnd();
}
//-------------------------------------------------------------------------------------
void PythonApp::finalise(void)
{
gameTickTimerHandle_.cancel();
scriptTimers_.cancelAll();
ScriptTimers::finalise(*this);
uninstallPyScript();
ServerApp::finalise();
}
//-------------------------------------------------------------------------------------
void PythonApp::handleTimeout(TimerHandle handle, void * arg)
{
ServerApp::handleTimeout(handle, arg);
switch (reinterpret_cast<uintptr>(arg))
{
case TIMEOUT_GAME_TICK:
++g_kbetime;
handleTimers();
break;
default:
break;
}
}
//-------------------------------------------------------------------------------------
int PythonApp::registerPyObjectToScript(const char* attrName, PyObject* pyObj)
{
return script_.registerToModule(attrName, pyObj);
}
//-------------------------------------------------------------------------------------
int PythonApp::unregisterPyObjectToScript(const char* attrName)
{
return script_.unregisterToModule(attrName);
}
//-------------------------------------------------------------------------------------
bool PythonApp::installPyScript()
{
if(Resmgr::getSingleton().respaths().size() <= 0 ||
Resmgr::getSingleton().getPyUserResPath().size() == 0 ||
Resmgr::getSingleton().getPySysResPath().size() == 0 ||
Resmgr::getSingleton().getPyUserScriptsPath().size() == 0)
{
KBE_ASSERT(false && "PythonApp::installPyScript: KBE_RES_PATH is error!\n");
return false;
}
std::wstring user_scripts_path = L"";
wchar_t* tbuf = KBEngine::strutil::char2wchar(const_cast<char*>(Resmgr::getSingleton().getPyUserScriptsPath().c_str()));
if(tbuf != NULL)
{
user_scripts_path += tbuf;
free(tbuf);
}
else
{
KBE_ASSERT(false && "PythonApp::installPyScript: KBE_RES_PATH error[char2wchar]!\n");
return false;
}
std::wstring pyPaths = user_scripts_path + L"common;";
pyPaths += user_scripts_path + L"data;";
pyPaths += user_scripts_path + L"user_type;";
switch(componentType_)
{
case BASEAPP_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"base;";
break;
case CELLAPP_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"cell;";
break;
case DBMGR_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"db;";
break;
case INTERFACES_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"interface;";
break;
case LOGINAPP_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"login;";
break;
case LOGGER_TYPE:
pyPaths += user_scripts_path + L"server_common;";
pyPaths += user_scripts_path + L"logger;";
break;
default:
pyPaths += user_scripts_path + L"client;";
break;
};
std::string kbe_res_path = Resmgr::getSingleton().getPySysResPath();
kbe_res_path += "scripts/common";
tbuf = KBEngine::strutil::char2wchar(const_cast<char*>(kbe_res_path.c_str()));
bool ret = getScript().install(tbuf, pyPaths, "KBEngine", componentType_);
free(tbuf);
return ret;
}
//-------------------------------------------------------------------------------------
bool PythonApp::uninstallPyScript()
{
return uninstallPyModules() && getScript().uninstall();
}
//-------------------------------------------------------------------------------------
bool PythonApp::installPyModules()
{
// 安装入口模块
PyObject *entryScriptFileName = NULL;
if(componentType() == BASEAPP_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getBaseApp();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else if(componentType() == CELLAPP_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getCellApp();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else if(componentType() == INTERFACES_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getInterfaces();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else if (componentType() == LOGINAPP_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getLoginApp();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else if (componentType() == DBMGR_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getDBMgr();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else if (componentType() == LOGGER_TYPE)
{
ENGINE_COMPONENT_INFO& info = g_kbeSrvConfig.getLogger();
entryScriptFileName = PyUnicode_FromString(info.entryScriptFile);
}
else
{
ERROR_MSG("PythonApp::installPyModules: Unsupported script!\n");
}
PyObject * module = getScript().getModule();
// 注册创建entity的方法到py
// 向脚本注册app发布状态
APPEND_SCRIPT_MODULE_METHOD(module, publish, __py_getAppPublish, METH_VARARGS, 0);
// 注册设置脚本输出类型
APPEND_SCRIPT_MODULE_METHOD(module, scriptLogType, __py_setScriptLogType, METH_VARARGS, 0);
// 获得资源全路径
APPEND_SCRIPT_MODULE_METHOD(module, getResFullPath, __py_getResFullPath, METH_VARARGS, 0);
// 是否存在某个资源
APPEND_SCRIPT_MODULE_METHOD(module, hasRes, __py_hasRes, METH_VARARGS, 0);
// 打开一个文件
APPEND_SCRIPT_MODULE_METHOD(module, open, __py_kbeOpen, METH_VARARGS, 0);
// 列出目录下所有文件
APPEND_SCRIPT_MODULE_METHOD(module, listPathRes, __py_listPathRes, METH_VARARGS, 0);
// 匹配相对路径获得全路径
APPEND_SCRIPT_MODULE_METHOD(module, matchPath, __py_matchPath, METH_VARARGS, 0);
// debug追踪kbe封装的py对象计数
APPEND_SCRIPT_MODULE_METHOD(module, debugTracing, script::PyGC::__py_debugTracing, METH_VARARGS, 0);
if (PyModule_AddIntConstant(module, "LOG_TYPE_NORMAL", log4cxx::ScriptLevel::SCRIPT_INT))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.LOG_TYPE_NORMAL.\n");
}
if (PyModule_AddIntConstant(module, "LOG_TYPE_INFO", log4cxx::ScriptLevel::SCRIPT_INFO))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.LOG_TYPE_INFO.\n");
}
if (PyModule_AddIntConstant(module, "LOG_TYPE_ERR", log4cxx::ScriptLevel::SCRIPT_ERR))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.LOG_TYPE_ERR.\n");
}
if (PyModule_AddIntConstant(module, "LOG_TYPE_DBG", log4cxx::ScriptLevel::SCRIPT_DBG))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.LOG_TYPE_DBG.\n");
}
if (PyModule_AddIntConstant(module, "LOG_TYPE_WAR", log4cxx::ScriptLevel::SCRIPT_WAR))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.LOG_TYPE_WAR.\n");
}
if (PyModule_AddIntConstant(module, "NEXT_ONLY", KBE_NEXT_ONLY))
{
ERROR_MSG( "PythonApp::installPyModules: Unable to set KBEngine.NEXT_ONLY.\n");
}
// 注册所有pythonApp都要用到的通用接口
APPEND_SCRIPT_MODULE_METHOD(module, addTimer, __py_addTimer, METH_VARARGS, 0);
APPEND_SCRIPT_MODULE_METHOD(module, delTimer, __py_delTimer, METH_VARARGS, 0);
APPEND_SCRIPT_MODULE_METHOD(module, registerReadFileDescriptor, PyFileDescriptor::__py_registerReadFileDescriptor, METH_VARARGS, 0);
APPEND_SCRIPT_MODULE_METHOD(module, registerWriteFileDescriptor, PyFileDescriptor::__py_registerWriteFileDescriptor, METH_VARARGS, 0);
APPEND_SCRIPT_MODULE_METHOD(module, deregisterReadFileDescriptor, PyFileDescriptor::__py_deregisterReadFileDescriptor, METH_VARARGS, 0);
APPEND_SCRIPT_MODULE_METHOD(module, deregisterWriteFileDescriptor, PyFileDescriptor::__py_deregisterWriteFileDescriptor, METH_VARARGS, 0);
onInstallPyModules();
if (entryScriptFileName != NULL)
{
entryScript_ = PyImport_Import(entryScriptFileName);
SCRIPT_ERROR_CHECK();
S_RELEASE(entryScriptFileName);
if(entryScript_.get() == NULL)
{
return false;
}
}
return true;
}
//-------------------------------------------------------------------------------------
bool PythonApp::uninstallPyModules()
{
// script::PyGC::set_debug(script::PyGC::DEBUG_STATS|script::PyGC::DEBUG_LEAK);
// script::PyGC::collect();
script::PyGC::debugTracing();
return true;
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_getAppPublish(PyObject* self, PyObject* args)
{
return PyLong_FromLong(g_appPublish);
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_setScriptLogType(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount != 1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::scriptLogType(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
int type = -1;
if(PyArg_ParseTuple(args, "i", &type) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::scriptLogType(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
DebugHelper::getSingleton().setScriptMsgType(type);
S_Return;
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_getResFullPath(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount != 1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::getResFullPath(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
char* respath = NULL;
if(PyArg_ParseTuple(args, "s", &respath) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::getResFullPath(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
if(!Resmgr::getSingleton().hasRes(respath))
return PyUnicode_FromString("");
std::string fullpath = Resmgr::getSingleton().matchRes(respath);
return PyUnicode_FromString(fullpath.c_str());
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_hasRes(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount != 1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::hasRes(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
char* respath = NULL;
if(PyArg_ParseTuple(args, "s", &respath) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::hasRes(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
return PyBool_FromLong(Resmgr::getSingleton().hasRes(respath));
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_kbeOpen(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount != 2)
{
PyErr_Format(PyExc_TypeError, "KBEngine::open(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
char* respath = NULL;
char* fargs = NULL;
if(PyArg_ParseTuple(args, "s|s", &respath, &fargs) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::open(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
std::string sfullpath = Resmgr::getSingleton().matchRes(respath);
PyObject *ioMod = PyImport_ImportModule("io");
// SCOPED_PROFILE(SCRIPTCALL_PROFILE);
PyObject *openedFile = PyObject_CallMethod(ioMod, const_cast<char*>("open"),
const_cast<char*>("ss"),
const_cast<char*>(sfullpath.c_str()),
fargs);
Py_DECREF(ioMod);
return openedFile;
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_matchPath(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount != 1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::matchPath(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
char* respath = NULL;
if(PyArg_ParseTuple(args, "s", &respath) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::matchPath(): args is error!");
PyErr_PrintEx(0);
S_Return;
}
std::string path = Resmgr::getSingleton().matchPath(respath);
return PyUnicode_FromStringAndSize(path.c_str(), path.size());
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_listPathRes(PyObject* self, PyObject* args)
{
int argCount = PyTuple_Size(args);
if(argCount < 1 || argCount > 2)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path, pathargs=\'*.*\'] is error!");
PyErr_PrintEx(0);
S_Return;
}
std::wstring wExtendName = L"*";
PyObject* pathobj = NULL;
PyObject* path_argsobj = NULL;
if(argCount == 1)
{
if(PyArg_ParseTuple(args, "O", &pathobj) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path] is error!");
PyErr_PrintEx(0);
S_Return;
}
}
else
{
if(PyArg_ParseTuple(args, "O|O", &pathobj, &path_argsobj) == -1)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path, pathargs=\'*.*\'] is error!");
PyErr_PrintEx(0);
S_Return;
}
if(PyUnicode_Check(path_argsobj))
{
wchar_t* fargs = NULL;
fargs = PyUnicode_AsWideCharString(path_argsobj, NULL);
wExtendName = fargs;
PyMem_Free(fargs);
}
else
{
if(PySequence_Check(path_argsobj))
{
wExtendName = L"";
Py_ssize_t size = PySequence_Size(path_argsobj);
for(int i=0; i<size; ++i)
{
PyObject* pyobj = PySequence_GetItem(path_argsobj, i);
if(!PyUnicode_Check(pyobj))
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path, pathargs=\'*.*\'] is error!");
PyErr_PrintEx(0);
S_Return;
}
wchar_t* wtemp = NULL;
wtemp = PyUnicode_AsWideCharString(pyobj, NULL);
wExtendName += wtemp;
wExtendName += L"|";
PyMem_Free(wtemp);
}
}
else
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[pathargs] is error!");
PyErr_PrintEx(0);
S_Return;
}
}
}
if(!PyUnicode_Check(pathobj))
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path] is error!");
PyErr_PrintEx(0);
S_Return;
}
if(PyUnicode_GET_LENGTH(pathobj) == 0)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path] is NULL!");
PyErr_PrintEx(0);
S_Return;
}
if(wExtendName.size() == 0)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[pathargs] is NULL!");
PyErr_PrintEx(0);
S_Return;
}
if(wExtendName[0] == '.')
wExtendName.erase(wExtendName.begin());
if(wExtendName.size() == 0)
wExtendName = L"*";
wchar_t* respath = PyUnicode_AsWideCharString(pathobj, NULL);
if(respath == NULL)
{
PyErr_Format(PyExc_TypeError, "KBEngine::listPathRes(): args[path] is NULL!");
PyErr_PrintEx(0);
S_Return;
}
char* cpath = strutil::wchar2char(respath);
std::string foundPath = Resmgr::getSingleton().matchPath(cpath);
free(cpath);
PyMem_Free(respath);
respath = strutil::char2wchar(foundPath.c_str());
std::vector<std::wstring> results;
Resmgr::getSingleton().listPathRes(respath, wExtendName, results);
PyObject* pyresults = PyTuple_New(results.size());
std::vector<std::wstring>::iterator iter = results.begin();
int i = 0;
for(; iter != results.end(); ++iter)
{
PyTuple_SET_ITEM(pyresults, i++, PyUnicode_FromWideChar((*iter).c_str(), (*iter).size()));
}
free(respath);
return pyresults;
}
//-------------------------------------------------------------------------------------
void PythonApp::startProfile_(Network::Channel* pChannel, std::string profileName, int8 profileType, uint32 timelen)
{
switch(profileType)
{
case 0: // pyprofile
new PyProfileHandler(this->networkInterface(), timelen, profileName, pChannel->addr());
return;
default:
break;
};
ServerApp::startProfile_(pChannel, profileName, profileType, timelen);
}
//-------------------------------------------------------------------------------------
void PythonApp::onExecScriptCommand(Network::Channel* pChannel, KBEngine::MemoryStream& s)
{
std::string cmd;
s.readBlob(cmd);
PyObject* pycmd = PyUnicode_DecodeUTF8(cmd.data(), cmd.size(), NULL);
if(pycmd == NULL)
{
SCRIPT_ERROR_CHECK();
return;
}
DEBUG_MSG(fmt::format("PythonApp::onExecScriptCommand: size({}), command={}.\n",
cmd.size(), cmd));
std::string retbuf = "";
PyObject* pycmd1 = PyUnicode_AsEncodedString(pycmd, "utf-8", NULL);
script_.run_simpleString(PyBytes_AsString(pycmd1), &retbuf);
if(retbuf.size() == 0)
{
retbuf = "\r\n";
}
// 将结果返回给客户端
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
ConsoleInterface::ConsoleExecCommandCBMessageHandler msgHandler;
(*pBundle).newMessage(msgHandler);
ConsoleInterface::ConsoleExecCommandCBMessageHandlerArgs1::staticAddToBundle((*pBundle), retbuf);
pChannel->send(pBundle);
Py_DECREF(pycmd);
Py_DECREF(pycmd1);
}
//-------------------------------------------------------------------------------------
void PythonApp::onReloadScript(bool fullReload)
{
}
//-------------------------------------------------------------------------------------
void PythonApp::reloadScript(bool fullReload)
{
onReloadScript(fullReload);
// SCOPED_PROFILE(SCRIPTCALL_PROFILE);
// 所有脚本都加载完毕
PyObject* pyResult = PyObject_CallMethod(getEntryScript().get(),
const_cast<char*>("onInit"),
const_cast<char*>("i"),
1);
if(pyResult != NULL)
Py_DECREF(pyResult);
else
SCRIPT_ERROR_CHECK();
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_addTimer(PyObject* self, PyObject* args)
{
float interval, repeat;
PyObject *pyCallback;
if (!PyArg_ParseTuple(args, "ffO", &interval, &repeat, &pyCallback))
S_Return;
if (!PyCallable_Check(pyCallback))
{
PyErr_Format(PyExc_TypeError, "KBEngine::addTimer: '%.200s' object is not callable", (pyCallback ? pyCallback->ob_type->tp_name : "NULL"));
PyErr_PrintEx(0);
S_Return;
}
ScriptTimers * pTimers = &scriptTimers();
ScriptTimerHandler *handler = new ScriptTimerHandler(pTimers, pyCallback);
ScriptID id = ScriptTimersUtil::addTimer(&pTimers, interval, repeat, 0, handler);
if (id == 0)
{
delete handler;
PyErr_SetString(PyExc_ValueError, "Unable to add timer");
PyErr_PrintEx(0);
S_Return;
}
Py_INCREF(pyCallback);
return PyLong_FromLong(id);
}
//-------------------------------------------------------------------------------------
PyObject* PythonApp::__py_delTimer(PyObject* self, PyObject* args)
{
ScriptID timerID;
if (!PyArg_ParseTuple(args, "i", &timerID))
{
PyErr_Format(PyExc_TypeError, "KBEngine::delTimer: args error!");
PyErr_PrintEx(0);
S_Return;
}
if (!ScriptTimersUtil::delTimer(&scriptTimers(), timerID))
{
PyErr_Format(PyExc_TypeError, "KBEngine::delTimer: error!");
PyErr_PrintEx(0);
return PyLong_FromLong(-1);
}
return PyLong_FromLong(timerID);
}
//-------------------------------------------------------------------------------------
}