void initRuntime() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths(); extern std::string getCurAppPath(); std::string appPath = getCurAppPath(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) appPath.append("/../../"); #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) appPath.append("/../../../"); #endif appPath = replaceAll(appPath, "\\", "/"); g_projectPath = appPath; // add project's root directory to search path searchPathArray.insert(searchPathArray.begin(), g_projectPath); // add writable path to search path searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath()); FileUtils::getInstance()->setSearchPaths(searchPathArray); #endif auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); register_runtime_override_function(engine->getLuaStack()->getLuaState()); ConsoleCommand::getShareInstance()->init(); }
void onPreGameStart() { LOGD("%s", s_CPSDKVersion); auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); tolua_gplay_open(L); }
void NBGNetManager::handle_lua_onConnection(bool success) { if (luaHandler_onConnection > 0) { auto engine = LuaEngine::getInstance(); auto stack = engine->getLuaStack(); stack->pushBoolean(success); stack->executeFunctionByHandler(luaHandler_onConnection, 1); stack->clean(); } }
void NBGNetManager::onResponse(void* pLuaValueDict) { if (luaHandler_onResponse > 0) { auto engine = LuaEngine::getInstance(); auto stack = engine->getLuaStack(); auto responseData = (LuaValueDict*)pLuaValueDict; stack->pushLuaValueDict(*responseData); stack->executeFunctionByHandler(luaHandler_onResponse, 1); stack->clean(); } }
bool ComponentLua::loadAndExecuteScript() { // register native functions auto engine = LuaEngine::getInstance(); lua_State *l = engine->getLuaStack()->getLuaState(); // load script auto fileUtils = FileUtils::getInstance(); std::string fullPathOfScript = fileUtils->fullPathForFilename(_scriptFileName); Data data = fileUtils->getDataFromFile(fullPathOfScript); int error = LUA_ERRFILE; if(data.getSize() > 0) error = engine->getLuaStack()->luaLoadBuffer(l, (const char*)data.getBytes(), (int)data.getSize(), fullPathOfScript.c_str()); if (error) { CCLOG("ComponentLua::loadAndExecuteScript: %s", lua_tostring(l, -1)); lua_pop(l, 1); return false; } // execute script error = lua_pcall(l, 0, 1, 0); if (error) { CCLOG("ComponentLua::loadAndExecuteScript: %s", lua_tostring(l, -1)); lua_pop(l, 1); return false; } // check the return value from lua script is a table int type = lua_type(l, -1); if (type != LUA_TTABLE) { CCLOG("%s should return a table, or the script component can not work currectly", _scriptFileName.c_str()); return false; } storeLuaTable(); return true; }
void startScript(std::string strDebugArg) { // register lua engine auto engine = LuaEngine::getInstance(); if (!strDebugArg.empty()) { // open debugger.lua module cocos2d::log("debug args = %s", strDebugArg.c_str()); luaopen_lua_debugger(engine->getLuaStack()->getLuaState()); engine->executeString(strDebugArg.c_str()); } std::string code("require \""); code.append(ConfigParser::getInstance()->getEntryFile().c_str()); code.append("\""); engine->executeString(code.c_str()); }
bool WelcomeScene::init() { if(!Layer::init()) { return false; } SpriteFrameCache::getInstance()->addSpriteFramesWithFile("res/res_1.plist", "res/res_1.png"); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("res/res_2.plist", "res/res_2.png"); Size VisiableSize = Director::getInstance()->getVisibleSize(); m_bgSprite = Sprite::create("res/splash_bg.jpg"); addChild(m_bgSprite); m_bgSprite->setPosition(VisiableSize/2); Sprite* normalStart = Sprite::createWithSpriteFrameName("play_nor.png"); Sprite* selectedStart = Sprite::createWithSpriteFrameName("play_click.png"); MenuItemSprite* startBtn = MenuItemSprite::create(normalStart, selectedStart, [](Ref* r){ auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); lua_module_register(L); register_all_Entity(L); register_all_Enemy(L); register_all_Hero(L); register_all_Enhance(L); if ((engine->executeScriptFile("src/main.lua"))) { CCLOG("failed to luanch game"); } // Director::getInstance()->replaceScene(GameScene::createScene()); }); Menu* m = Menu::create(startBtn,nullptr); addChild(m); m->setPosition(Vec2(VisiableSize.width/2,VisiableSize.height*0.4)); auto e = EventListenerTouchOneByOne::create(); e->onTouchBegan = [=](Touch* touch,Event* e) { return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(e,this); return true; }
//发送到lua void SMessageCommand::SendToLua(SMessage*_message) { auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); int isOpen = engine->executeScriptFile("src/app/NetCommand/NNetCommandHelper.lua"); if (isOpen != 0){ CCLOG("Open Lua Error: %i", isOpen); } lua_getglobal(L, "recevieFromC"); lua_pushstring(L, _message->srcString.c_str()); /* lua_call 第一个参数:函数的参数个数 第二个参数:函数返回值个数 */ lua_call(L, 1, 0); }