int LuaBinder::luaResultNext(lua_State* L) { DBResult_ptr res = LuaBinder::getDBResult(L); if (!res) { pushBoolean(L, false); return 1; } pushBoolean(L, res->next()); return 1; }
int NpcScriptInterface::luaActionFollow(lua_State* L) { //selfFollow(player) Npc* npc = getScriptEnv()->getNpc(); if (!npc) { pushBoolean(L, false); return 1; } pushBoolean(L, npc->setFollowCreature(getPlayer(L, 1))); return 1; }
int CCLuaEngine::pushCCLuaValue(const CCLuaValue& value) { const CCLuaValueType type = value.getType(); if (type == CCLuaValueTypeInt) { return pushInt(value.intValue()); } else if (type == CCLuaValueTypeFloat) { return pushFloat(value.floatValue()); } else if (type == CCLuaValueTypeBoolean) { return pushBoolean(value.booleanValue()); } else if (type == CCLuaValueTypeString) { return pushString(value.stringValue().c_str()); } else if (type == CCLuaValueTypeDict) { pushCCLuaValueDict(value.dictValue()); } else if (type == CCLuaValueTypeArray) { pushCCLuaValueArray(value.arrayValue()); } else if (type == CCLuaValueTypeCCObject) { pushCCObject(value.ccobjectValue(), value.getCCObjectTypename().c_str()); } return lua_gettop(m_state); }
void Stack::pushVariable(const Variable &var) { switch (var.getType()) { case kTypeNil: pushNil(); break; case kTypeBoolean: pushBoolean(var.getBool()); break; case kTypeNumber: pushFloat(var.getFloat()); break; case kTypeString: pushString(var.getString()); break; case kTypeTable: pushTable(var.getTable()); break; case kTypeFunction: pushFunction(var.getFunction()); break; case kTypeUserType: pushRawUserType(var.getRawUserType(), var.getExactType()); break; default: warning("Pushing a varible of type \"%s\" not supported", var.getExactType().c_str()); break; } }
void DBCCArmatureNode::registerMovementEventHandler(cocos2d::LUA_FUNCTION func) { unregisterMovementEventHandler(); _movementEventHandler = func; auto dispatcher = getCCEventDispatcher(); auto f = [this](cocos2d::EventCustom *event) { auto eventData = (dragonBones::EventData*)(event->getUserData()); auto type = (int) eventData->getType(); auto movementId = eventData->animationState->name; auto lastState = eventData->armature->getAnimation()->getLastAnimationState(); auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack(); stack->pushObject(this, "db.DBCCArmatureNode"); stack->pushInt(type); stack->pushString(movementId.c_str(), movementId.size()); stack->pushBoolean(lastState == eventData->animationState); stack->executeFunctionByHandler(_movementEventHandler, 4); }; dispatcher->addCustomEventListener(dragonBones::EventData::COMPLETE, f); dispatcher->addCustomEventListener(dragonBones::EventData::LOOP_COMPLETE, f); }
void LuaStack::pushLuaValue(const LuaValue& value) { const LuaValueType type = value.getType(); if (type == LuaValueTypeInt) { return pushInt(value.intValue()); } else if (type == LuaValueTypeFloat) { return pushFloat(value.floatValue()); } else if (type == LuaValueTypeBoolean) { return pushBoolean(value.booleanValue()); } else if (type == LuaValueTypeString) { return pushString(value.stringValue().c_str()); } else if (type == LuaValueTypeDict) { pushLuaValueDict(value.dictValue()); } else if (type == LuaValueTypeArray) { pushLuaValueArray(value.arrayValue()); } else if (type == LuaValueTypeObject) { pushObject(value.ccobjectValue(), value.getObjectTypename().c_str()); } }
int32_t NpcScriptInterface::luaCloseShopWindow(lua_State* L) { //closeShopWindow(cid) ScriptEnvironment* env = getScriptEnv(); Player* player = g_game.getPlayerByID(popNumber(L)); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } Npc* npc = env->getNpc(); if (!npc) { reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); pushBoolean(L, false); return 1; } int32_t buyCallback; int32_t sellCallback; Npc* merchant = player->getShopOwner(buyCallback, sellCallback); //Check if we actually have a shop window with this player. if (merchant == npc) { player->sendCloseShop(); if (buyCallback != -1) { luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); } if (sellCallback != -1) { luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); } player->setShopOwner(NULL, -1, -1); npc->removeShopPlayer(player); } pushBoolean(L, true); return 1; }
int32_t NpcScriptInterface::luaGetNpcName(lua_State* L) { //getNpcName() Npc* npc = getScriptEnv()->getNpc(); if (npc) { LuaScriptInterface::pushString(L, npc->getName()); } else { pushBoolean(L, false); } return 1; }
int32_t NpcScriptInterface::luaActionFollow(lua_State* L) { //selfFollow(cid) uint32_t cid = popNumber<uint32_t>(L); Player* player = g_game.getPlayerByID(cid); if (cid != 0 && !player) { pushBoolean(L, false); return 1; } Npc* npc = getScriptEnv()->getNpc(); if (!npc) { pushBoolean(L, false); return 1; } pushBoolean(L, npc->setFollowCreature(player)); return 1; }
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(); } }
int NpcScriptInterface::luaNpcSetFocus(lua_State* L) { // npc:setFocus(creature) Creature* creature = getCreature(L, 2); Npc* npc = getUserdata<Npc>(L, 1); if (npc) { npc->setCreatureFocus(creature); pushBoolean(L, true); } else { lua_pushnil(L); } return 1; }
int NpcScriptInterface::luaNpcCloseShopWindow(lua_State* L) { // npc:closeShopWindow(player) Player* player = getPlayer(L, 2); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } Npc* npc = getUserdata<Npc>(L, 1); if (!npc) { reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); pushBoolean(L, false); return 1; } int32_t buyCallback; int32_t sellCallback; Npc* merchant = player->getShopOwner(buyCallback, sellCallback); if (merchant == npc) { player->sendCloseShop(); if (buyCallback != -1) { luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); } if (sellCallback != -1) { luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); } player->setShopOwner(nullptr, -1, -1); npc->removeShopPlayer(player); } pushBoolean(L, true); return 1; }
int LuaBinder::luaDatabaseTableExists(lua_State* L) { // Database.tableExists(database, table) auto tableExists = [](String database, String tableName) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " << db->escapeString(database) << " AND `TABLE_NAME` = " << db->escapeString(tableName) << " LIMIT 1"; return db->storeQuery(query.str()).get() != nullptr; }; pushBoolean(L, tableExists(getString(L, 1), getString(L, 2))); return 1; }
int LuaBinder::luaResultGetStream(lua_State* L) { DBResult_ptr res = LuaBinder::getDBResult(L); if (!res) { pushBoolean(L, false); return 1; } unsigned long length; const char* stream = res->getStream(getString(L, 2), length); lua_pushlstring(L, stream, length); lua_pushnumber(L, length); return 2; }
int32_t NpcScriptInterface::luaActionFollow(lua_State* L) { //selfFollow(cid) uint32_t cid = popNumber(L); ScriptEnvironment* env = getScriptEnv(); Player* player = g_game.getPlayerByID(cid); if (cid != 0 && !player) { pushBoolean(L, false); return 1; } Npc* npc = env->getNpc(); if (!npc) { pushBoolean(L, false); return 1; } bool result = npc->setFollowCreature(player, true); pushBoolean(L, result); return 1; }
int NpcScriptInterface::luaDoSellItem(lua_State* L) { //doSellItem(cid, itemid, amount, <optional> subtype, <optional> actionid, <optional: default: 1> canDropOnMap) Player* player = getPlayer(L, 1); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } uint32_t sellCount = 0; uint32_t itemId = getNumber<uint32_t>(L, 2); uint32_t amount = getNumber<uint32_t>(L, 3); uint32_t subType; int32_t n = getNumber<int32_t>(L, 4, -1); if (n != -1) { subType = n; } else { subType = 1; } uint32_t actionId = getNumber<uint32_t>(L, 5, 0); bool canDropOnMap = getBoolean(L, 6, true); const ItemType& it = Item::items[itemId]; if (it.stackable) { while (amount > 0) { int32_t stackCount = std::min<int32_t>(100, amount); Item* item = Item::CreateItem(it.id, stackCount); if (item && actionId != 0) { item->setActionId(actionId); } if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) { delete item; lua_pushnumber(L, sellCount); return 1; } amount -= stackCount; sellCount += stackCount; } } else { for (uint32_t i = 0; i < amount; ++i) { Item* item = Item::CreateItem(it.id, subType); if (item && actionId != 0) { item->setActionId(actionId); } if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) { delete item; lua_pushnumber(L, sellCount); return 1; } ++sellCount; } } lua_pushnumber(L, sellCount); return 1; }
int NpcScriptInterface::luaOpenShopWindow(lua_State* L) { //openShopWindow(cid, items, onBuy callback, onSell callback) int32_t sellCallback; if (lua_isfunction(L, -1) == 0) { sellCallback = -1; lua_pop(L, 1); // skip it - use default value } else { sellCallback = popCallback(L); } int32_t buyCallback; if (lua_isfunction(L, -1) == 0) { buyCallback = -1; lua_pop(L, 1); // skip it - use default value } else { buyCallback = popCallback(L); } if (lua_istable(L, -1) == 0) { reportError(__FUNCTION__, "item list is not a table."); pushBoolean(L, false); return 1; } std::list<ShopInfo> items; lua_pushnil(L); while (lua_next(L, -2) != 0) { const auto tableIndex = lua_gettop(L); ShopInfo item; item.itemId = getField<uint32_t>(L, tableIndex, "id"); item.subType = getField<int32_t>(L, tableIndex, "subType"); if (item.subType == 0) { item.subType = getField<int32_t>(L, tableIndex, "subtype"); lua_pop(L, 1); } item.buyPrice = getField<uint32_t>(L, tableIndex, "buy"); item.sellPrice = getField<uint32_t>(L, tableIndex, "sell"); item.realName = getFieldString(L, tableIndex, "name"); items.push_back(item); lua_pop(L, 6); } lua_pop(L, 1); Player* player = getPlayer(L, -1); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } //Close any eventual other shop window currently open. player->closeShopWindow(false); Npc* npc = getScriptEnv()->getNpc(); if (!npc) { reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); pushBoolean(L, false); return 1; } npc->addShopPlayer(player); player->setShopOwner(npc, buyCallback, sellCallback); player->openShopWindow(npc, items); pushBoolean(L, true); return 1; }
int32_t NpcScriptInterface::luaDoSellItem(lua_State* L) { //doSellItem(cid, itemid, amount, <optional> subtype, <optional> actionid, <optional: default: 1> canDropOnMap) int32_t parameters = lua_gettop(L); bool canDropOnMap; if (parameters > 5) { canDropOnMap = popBoolean(L); } else { canDropOnMap = true; } uint32_t actionId = 0; if (parameters > 4) { actionId = popNumber(L); } uint32_t subType = 1; if (parameters > 3) { int32_t n = popNumber(L); if (n != -1) { subType = n; } } uint32_t amount = popNumber(L); uint32_t itemId = popNumber(L); Player* player = g_game.getPlayerByID(popNumber(L)); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } uint32_t sellCount = 0; const ItemType& it = Item::items[itemId]; if (it.stackable) { while (amount > 0) { int32_t stackCount = std::min<int32_t>(100, amount); Item* item = Item::CreateItem(it.id, stackCount); if (item && actionId != 0) { item->setActionId(actionId); } if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RET_NOERROR) { delete item; lua_pushnumber(L, sellCount); return 1; } amount = amount - stackCount; sellCount += stackCount; } } else { for (uint32_t i = 0; i < amount; ++i) { Item* item = Item::CreateItem(it.id, subType); if (item && actionId != 0) { item->setActionId(actionId); } if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RET_NOERROR) { delete item; lua_pushnumber(L, sellCount); return 1; } ++sellCount; } } lua_pushnumber(L, sellCount); return 1; }
int NpcScriptInterface::luaNpcOpenShopWindow(lua_State* L) { // npc:openShopWindow(cid, items, buyCallback, sellCallback) if (!isTable(L, 3)) { reportErrorFunc("item list is not a table."); pushBoolean(L, false); return 1; } Player* player = getPlayer(L, 2); if (!player) { reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)); pushBoolean(L, false); return 1; } Npc* npc = getUserdata<Npc>(L, 1); if (!npc) { reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); pushBoolean(L, false); return 1; } int32_t sellCallback = -1; if (LuaScriptInterface::isFunction(L, 5)) { sellCallback = luaL_ref(L, LUA_REGISTRYINDEX); } int32_t buyCallback = -1; if (LuaScriptInterface::isFunction(L, 4)) { buyCallback = luaL_ref(L, LUA_REGISTRYINDEX); } std::list<ShopInfo> items; lua_pushnil(L); while (lua_next(L, 3) != 0) { const auto tableIndex = lua_gettop(L); ShopInfo item; item.itemId = getField<uint32_t>(L, tableIndex, "id"); item.subType = getField<int32_t>(L, tableIndex, "subType"); if (item.subType == 0) { item.subType = getField<int32_t>(L, tableIndex, "subtype"); lua_pop(L, 1); } item.buyPrice = getField<uint32_t>(L, tableIndex, "buy"); item.sellPrice = getField<uint32_t>(L, tableIndex, "sell"); item.realName = getFieldString(L, tableIndex, "name"); items.push_back(item); lua_pop(L, 6); } lua_pop(L, 1); player->closeShopWindow(false); npc->addShopPlayer(player); player->setShopOwner(npc, buyCallback, sellCallback); player->openShopWindow(npc, items); pushBoolean(L, true); return 1; }
int LuaInterface::signalCall(int numArgs, int numRets) { int rets = 0; int funcIndex = -numArgs-1; try { // must be a function if(isFunction(funcIndex)) { rets = safeCall(numArgs); if(numRets != -1) { if(rets != numRets) throw LuaException("function call didn't return the expected number of results", 0); } } // can also calls table of functions else if(isTable(funcIndex)) { // loop through table values pushNil(); bool done = false; while(next(funcIndex-1)) { if(isFunction()) { // repush arguments for(int i=0;i<numArgs;++i) pushValue(-numArgs-2); int rets = safeCall(numArgs); if(rets == 1) { done = popBoolean(); if(done) { pop(); break; } } else if(rets != 0) throw LuaException("function call didn't return the expected number of results", 0); } else { throw LuaException("attempt to call a non function", 0); } } pop(numArgs + 1); // pops the table of function and arguments if(numRets == 1 || numRets == -1) { rets = 1; pushBoolean(done); } } // nil values are ignored else if(isNil(funcIndex)) { pop(numArgs + 1); // pops the function and arguments } // if not nil, warn else { throw LuaException("attempt to call a non function value", 0); } } catch(stdext::exception& e) { g_logger.error(stdext::format("protected lua call failed: %s", e.what())); } // pushes nil values if needed while(numRets != -1 && rets < numRets) { pushNil(); rets++; } // returns the number of results on the stack return rets; }