//------------------------------------------------------------------------------------- bool Script::install(const wchar_t* pythonHomeDir, std::wstring pyPaths, const char* moduleName, COMPONENT_TYPE componentType) { std::wstring pySysPaths = SCRIPT_PATH; wchar_t* pwpySysResPath = strutil::char2wchar(const_cast<char*>(Resmgr::getSingleton().getPySysResPath().c_str())); strutil::kbe_replace(pySysPaths, L"../../res/", pwpySysResPath); pyPaths += pySysPaths; free(pwpySysResPath); #if KBE_PLATFORM == PLATFORM_WIN32 Py_SetPythonHome(const_cast<wchar_t*>(pythonHomeDir)); // 先设置python的环境变量 #else std::wstring fs = L";"; std::wstring rs = L":"; size_t pos = 0; while(true) { pos = pyPaths.find(fs, pos); if (pos == std::wstring::npos) break; pyPaths.replace(pos, fs.length(), rs); } Py_SetPath(pyPaths.c_str()); char* tmpchar = strutil::wchar2char(const_cast<wchar_t*>(pyPaths.c_str())); DEBUG_MSG(boost::format("Script::install: paths=%1%.\n") % tmpchar); free(tmpchar); #endif // Initialise python // Py_VerboseFlag = 2; Py_FrozenFlag = 1; // Warn if tab and spaces are mixed in indentation. // Py_TabcheckFlag = 1; Py_NoSiteFlag = 1; Py_IgnoreEnvironmentFlag = 1; Py_Initialize(); // python解释器的初始化 if (!Py_IsInitialized()) { ERROR_MSG("Script::install::Py_Initialize is failed!\n"); return false; } #if KBE_PLATFORM == PLATFORM_WIN32 PySys_SetPath(pyPaths.c_str()); #endif PyObject *m = PyImport_AddModule("__main__"); module_ = PyImport_AddModule(moduleName); // 添加一个脚本基础模块 if (module_ == NULL) return false; const char* componentName = COMPONENT_NAME_EX(componentType); if (PyModule_AddStringConstant(module_, "component", componentName)) { ERROR_MSG(boost::format("Script::init: Unable to set KBEngine.component to %1%\n") % componentName ); return false; } // 注册产生uuid方法到py APPEND_SCRIPT_MODULE_METHOD(module_, genUUID64, __py_genUUID64, METH_VARARGS, 0); if(!install_py_dlls()) { ERROR_MSG("Script::init: install_py_dlls() is failed!\n"); return false; } #ifndef KBE_SINGLE_THREADED s_pOurInitTimeModules = PyDict_Copy( PySys_GetObject( "modules" ) ); s_pMainThreadState = PyThreadState_Get(); s_defaultContext = s_pMainThreadState; PyEval_InitThreads(); KBEConcurrency::setMainThreadIdleFunctions( &Script::releaseLock, &Script::acquireLock ); #endif ScriptStdOutErr::installScript(NULL); // 安装py重定向模块 ScriptStdOutErrHook::installScript(NULL); static struct PyModuleDef moduleDesc = { PyModuleDef_HEAD_INIT, moduleName, "This module is created by KBEngine!", -1, NULL }; PyModule_Create(&moduleDesc); // 初始化基础模块 PyObject_SetAttrString(m, moduleName, module_); // 将模块对象加入main pyStdouterr_ = new ScriptStdOutErr(); // 重定向python输出 pyStdouterrHook_ = new ScriptStdOutErrHook(); if(!pyStdouterr_->install()){ // 安装py重定向脚本模块 ERROR_MSG("Script::install::pyStdouterr_->install() is failed!\n"); SCRIPT_ERROR_CHECK(); return false; } Pickler::initialize(); PyProfile::initialize(this); PyStruct::initialize(); Copy::initialize(); SCRIPT_ERROR_CHECK(); math::installModule("Math"); INFO_MSG("Script::install is successfully!\n"); return installExtraModule("KBExtra"); }
//------------------------------------------------------------------------------------- bool Script::install(const wchar_t* pythonHomeDir, std::wstring pyPaths, const char* moduleName, COMPONENT_TYPE componentType) { std::wstring pySysPaths = SCRIPT_PATH; wchar_t* pwpySysResPath = strutil::char2wchar(const_cast<char*>(Resmgr::getSingleton().getPySysResPath().c_str())); strutil::kbe_replace(pySysPaths, L"../../res/", pwpySysResPath); pyPaths += pySysPaths; free(pwpySysResPath); // 先设置python的环境变量 Py_SetPythonHome(const_cast<wchar_t*>(pythonHomeDir)); #if KBE_PLATFORM != PLATFORM_WIN32 std::wstring fs = L";"; std::wstring rs = L":"; size_t pos = 0; while(true) { pos = pyPaths.find(fs, pos); if (pos == std::wstring::npos) break; pyPaths.replace(pos, fs.length(), rs); } char* tmpchar = strutil::wchar2char(const_cast<wchar_t*>(pyPaths.c_str())); DEBUG_MSG(fmt::format("Script::install(): paths={}.\n", tmpchar)); free(tmpchar); #endif // Initialise python // Py_VerboseFlag = 2; Py_FrozenFlag = 1; // Warn if tab and spaces are mixed in indentation. // Py_TabcheckFlag = 1; Py_NoSiteFlag = 1; Py_IgnoreEnvironmentFlag = 1; Py_SetPath(pyPaths.c_str()); // python解释器的初始化 Py_Initialize(); if (!Py_IsInitialized()) { ERROR_MSG("Script::install(): Py_Initialize is failed!\n"); return false; } PyObject *m = PyImport_AddModule("__main__"); // 添加一个脚本基础模块 module_ = PyImport_AddModule(moduleName); if (module_ == NULL) return false; const char* componentName = COMPONENT_NAME_EX(componentType); if (PyModule_AddStringConstant(module_, "component", componentName)) { ERROR_MSG(fmt::format("Script::install(): Unable to set KBEngine.component to {}\n", componentName)); return false; } // 注册产生uuid方法到py APPEND_SCRIPT_MODULE_METHOD(module_, genUUID64, __py_genUUID64, METH_VARARGS, 0); if(!install_py_dlls()) { ERROR_MSG("Script::install(): install_py_dlls() is failed!\n"); return false; } // 安装py重定向模块 ScriptStdOut::installScript(NULL); ScriptStdErr::installScript(NULL); /* static struct PyModuleDef moduleDesc = { PyModuleDef_HEAD_INIT, moduleName, "This module is created by KBEngine!", -1, NULL }; // 初始化基础模块 PyModule_Create(&moduleDesc); */ // 将模块对象加入main PyObject_SetAttrString(m, moduleName, module_); PyObject_SetAttrString(module_, "__doc__", PyUnicode_FromString("This module is created by KBEngine!")); // 重定向python输出 pyStdouterr_ = new ScriptStdOutErr(); // 安装py重定向脚本模块 if(!pyStdouterr_->install()){ ERROR_MSG("Script::install::pyStdouterr_->install() is failed!\n"); delete pyStdouterr_; SCRIPT_ERROR_CHECK(); return false; } PyGC::initialize(); Pickler::initialize(); PyProfile::initialize(this); PyStruct::initialize(); Copy::initialize(); SCRIPT_ERROR_CHECK(); math::installModule("Math"); INFO_MSG(fmt::format("Script::install(): is successfully, Python=({})!\n", Py_GetVersion())); return installExtraModule("KBExtra"); }