Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validations of "getPlayer" lua function #1907

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/lua/functions/core/game/bank_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int BankFunctions::luaBankCredit(lua_State* L) {
// Bank.credit(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -19,7 +19,7 @@ int BankFunctions::luaBankDebit(lua_State* L) {
// Bank.debit(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -31,7 +31,7 @@ int BankFunctions::luaBankBalance(lua_State* L) {
// Bank.balance(playerOrGuild[, amount]])
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
if (lua_gettop(L) == 1) {
Expand All @@ -47,7 +47,7 @@ int BankFunctions::luaBankHasBalance(lua_State* L) {
// Bank.hasBalance(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -60,13 +60,13 @@ int BankFunctions::luaBankTransfer(lua_State* L) {
auto source = getBank(L, 1);
if (source == nullptr) {
g_logger().debug("BankFunctions::luaBankTransfer: source is null");
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
std::shared_ptr<Bank> destination = getBank(L, 2);
if (destination == nullptr) {
g_logger().debug("BankFunctions::luaBankTransfer: destination is null");
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 3);
Expand All @@ -78,12 +78,12 @@ int BankFunctions::luaBankTransferToGuild(lua_State* L) {
// Bank.transfer(fromPlayerOrGuild, toGuild, amount)
auto source = getBank(L, 1);
if (source == nullptr) {
lua_pushnil(L);
reportErrorFunc("Source is nullptr");
return 1;
}
std::shared_ptr<Bank> destination = getBank(L, 2, true /* isGuild */);
if (destination == nullptr) {
lua_pushnil(L);
reportErrorFunc("Destination is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 3);
Expand All @@ -94,6 +94,11 @@ int BankFunctions::luaBankTransferToGuild(lua_State* L) {
int BankFunctions::luaBankWithdraw(lua_State* L) {
// Bank.withdraw(player, amount[, source = player])
auto player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

uint64_t amount = getNumber<uint64_t>(L, 2);
if (lua_gettop(L) == 2) {
if (!player) {
Expand All @@ -106,7 +111,7 @@ int BankFunctions::luaBankWithdraw(lua_State* L) {
}
auto source = getBank(L, 3);
if (source == nullptr) {
lua_pushnil(L);
reportErrorFunc("Source is nullptr");
return 1;
}
pushBoolean(L, source->withdraw(player, amount));
Expand All @@ -117,6 +122,7 @@ int BankFunctions::luaBankDeposit(lua_State* L) {
// Bank.deposit(player, amount[, destination = player])
auto player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}
const auto bank = std::make_shared<Bank>(player);
Expand All @@ -134,7 +140,7 @@ int BankFunctions::luaBankDeposit(lua_State* L) {
}
auto destination = getBank(L, 3);
if (destination == nullptr) {
lua_pushnil(L);
reportErrorFunc("Destination is nullptr");
return 1;
}
pushBoolean(L, g_game().removeMoney(player, amount) && destination->credit(amount));
Expand Down
8 changes: 4 additions & 4 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,16 @@ int GlobalFunctions::luaDebugPrint(lua_State* L) {

int GlobalFunctions::luaIsInWar(lua_State* L) {
// isInWar(cid, target)
std::shared_ptr<Player> player = getPlayer(L, 1);
const auto &player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
reportErrorFunc(fmt::format("{} - Player", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)));
pushBoolean(L, false);
return 1;
}

std::shared_ptr<Player> targetPlayer = getPlayer(L, 2);
const auto &targetPlayer = getPlayer(L, 2);
if (!targetPlayer) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
reportErrorFunc(fmt::format("{} - TargetPlayer", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)));
pushBoolean(L, false);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/core/game/modal_window_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ int ModalWindowFunctions::luaModalWindowSetPriority(lua_State* L) {

int ModalWindowFunctions::luaModalWindowSendToPlayer(lua_State* L) {
// modalWindow:sendToPlayer(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
lua_pushnil(L);
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

Expand Down
10 changes: 5 additions & 5 deletions src/lua/functions/core/network/network_message_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ int NetworkMessageFunctions::luaNetworkMessageSendToPlayer(lua_State* L) {
}

const auto &player = getPlayer(L, 2);
if (player) {
player->sendNetworkMessage(*message);
pushBoolean(L, true);
} else {
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushnil(L);
return 1;
}

player->sendNetworkMessage(*message);
pushBoolean(L, true);
return 1;
}
14 changes: 10 additions & 4 deletions src/lua/functions/creatures/npc/npc_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int NpcFunctions::luaNpcOpenShopWindow(lua_State* L) {
return 1;
}

std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand All @@ -360,7 +360,7 @@ int NpcFunctions::luaNpcOpenShopWindow(lua_State* L) {

int NpcFunctions::luaNpcCloseShopWindow(lua_State* L) {
// npc:closeShopWindow(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand Down Expand Up @@ -452,7 +452,13 @@ int NpcFunctions::luaNpcFollow(lua_State* L) {
return 1;
}

pushBoolean(L, npc->setFollowCreature(getPlayer(L, 2)));
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

pushBoolean(L, npc->setFollowCreature(player));
return 1;
}

Expand All @@ -478,7 +484,7 @@ int NpcFunctions::luaNpcSellItem(lua_State* L) {
return 1;
}

std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand Down
35 changes: 30 additions & 5 deletions src/lua/functions/creatures/player/party_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ int PartyFunctions::luaPartyGetLeader(lua_State* L) {

int PartyFunctions::luaPartySetLeader(lua_State* L) {
// party:setLeader(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->passPartyLeadership(player));
Expand Down Expand Up @@ -139,7 +144,12 @@ int PartyFunctions::luaPartyGetInviteeCount(lua_State* L) {

int PartyFunctions::luaPartyAddInvite(lua_State* L) {
// party:addInvite(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->invitePlayer(player));
Expand All @@ -151,7 +161,12 @@ int PartyFunctions::luaPartyAddInvite(lua_State* L) {

int PartyFunctions::luaPartyRemoveInvite(lua_State* L) {
// party:removeInvite(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->removeInvite(player));
Expand All @@ -163,7 +178,12 @@ int PartyFunctions::luaPartyRemoveInvite(lua_State* L) {

int PartyFunctions::luaPartyAddMember(lua_State* L) {
// party:addMember(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->joinParty(player));
Expand All @@ -175,7 +195,12 @@ int PartyFunctions::luaPartyAddMember(lua_State* L) {

int PartyFunctions::luaPartyRemoveMember(lua_State* L) {
// party:removeMember(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->leaveParty(player));
Expand Down
19 changes: 10 additions & 9 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,16 +983,17 @@ int PlayerFunctions::luaPlayerGetMaxMana(lua_State* L) {

int PlayerFunctions::luaPlayerSetMaxMana(lua_State* L) {
// player:setMaxMana(maxMana)
std::shared_ptr<Player> player = getPlayer(L, 1);
if (player) {
player->manaMax = getNumber<int32_t>(L, 2);
player->mana = std::min<int32_t>(player->mana, player->manaMax);
g_game().addPlayerMana(player);
player->sendStats();
pushBoolean(L, true);
} else {
lua_pushnil(L);
auto player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

player->manaMax = getNumber<int32_t>(L, 2);
player->mana = std::min<int32_t>(player->mana, player->manaMax);
g_game().addPlayerMana(player);
player->sendStats();
pushBoolean(L, true);
return 1;
}

Expand Down
25 changes: 21 additions & 4 deletions src/lua/functions/map/house_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,12 @@ int HouseFunctions::luaHouseCanEditAccessList(lua_State* L) {
}

uint32_t listId = getNumber<uint32_t>(L, 2);
std::shared_ptr<Player> player = getPlayer(L, 3);

auto player = getPlayer(L, 3);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

pushBoolean(L, house->canEditAccessList(listId, player));
return 1;
Expand Down Expand Up @@ -415,7 +420,19 @@ int HouseFunctions::luaHouseKickPlayer(lua_State* L) {
return 1;
}

pushBoolean(L, house->kickPlayer(getPlayer(L, 2), getPlayer(L, 3)));
auto player = getPlayer(L, 2);
if (!player) {
reportErrorFunc("Player is nullptr");
return 1;
}

auto targetPlayer = getPlayer(L, 3);
if (!targetPlayer) {
reportErrorFunc("Target player is nullptr");
return 1;
}

pushBoolean(L, house->kickPlayer(player, targetPlayer));
return 1;
}

Expand All @@ -427,9 +444,9 @@ int HouseFunctions::luaHouseIsInvited(lua_State* L) {
return 1;
}

std::shared_ptr<Player> player = getPlayer(L, 2);
auto player = getPlayer(L, 2);
if (!player) {
lua_pushnil(L);
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

Expand Down
24 changes: 18 additions & 6 deletions src/lua/functions/map/position_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ int PositionFunctions::luaPositionSendMagicEffect(lua_State* L) {
// position:sendMagicEffect(magicEffect[, player = nullptr])
CreatureVector spectators;
if (lua_gettop(L) >= 3) {
if (const auto &player = getPlayer(L, 3)) {
spectators.emplace_back(player);
const auto &player = getPlayer(L, 3);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

spectators.emplace_back(player);
}

MagicEffectClasses magicEffect = getNumber<MagicEffectClasses>(L, 2);
Expand All @@ -177,9 +181,13 @@ int PositionFunctions::luaPositionRemoveMagicEffect(lua_State* L) {
// position:removeMagicEffect(magicEffect[, player = nullptr])
CreatureVector spectators;
if (lua_gettop(L) >= 3) {
if (const auto &player = getPlayer(L, 3)) {
spectators.emplace_back(player);
const auto &player = getPlayer(L, 3);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

spectators.emplace_back(player);
}

MagicEffectClasses magicEffect = getNumber<MagicEffectClasses>(L, 2);
Expand All @@ -204,9 +212,13 @@ int PositionFunctions::luaPositionSendDistanceEffect(lua_State* L) {
// position:sendDistanceEffect(positionEx, distanceEffect[, player = nullptr])
CreatureVector spectators;
if (lua_gettop(L) >= 4) {
if (const auto &player = getPlayer(L, 4)) {
spectators.emplace_back(player);
const auto &player = getPlayer(L, 4);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

spectators.emplace_back(player);
}

ShootType_t distanceEffect = getNumber<ShootType_t>(L, 3);
Expand Down
Loading