Scripts/Commands: New argument parsing methodology (PR #22363)

- Detect the arguments accepted by the command handler
- Tokenize out those arguments automatically and feed them to the handler
- Unmatched rest of the string can be accepted by trailing char const* or CommandArgs*
This commit is contained in:
Aokromes
2018-09-09 14:31:14 +02:00
parent 88e1b344cb
commit 6815c24be1
14 changed files with 1043 additions and 405 deletions

View File

@@ -34,8 +34,10 @@ EndScriptData */
#include "RBAC.h"
#include "TicketMgr.h"
#include "Transport.h"
#include "Util.h"
#include "WorldSession.h"
using namespace Trinity::ChatCommands;
class go_commandscript : public CommandScript
{
public:
@@ -43,14 +45,26 @@ public:
std::vector<ChatCommand> GetCommands() const override
{
static std::vector<ChatCommand> goCreatureCommandTable =
{
{ "id", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoCreatureCIdCommand, "" },
{ "", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoCreatureSpawnIdCommand, "" }
};
static std::vector<ChatCommand> goGameObjectCommandTable =
{
{ "id", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoGameObjectGOIdCommand, "" },
{ "", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoGameObjectSpawnIdCommand, "" }
};
static std::vector<ChatCommand> goCommandTable =
{
{ "creature", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoCreatureCommand, "" },
{ "creature", rbac::RBAC_PERM_COMMAND_GO, false, nullptr, "", goCreatureCommandTable },
{ "gameobject", rbac::RBAC_PERM_COMMAND_GO, false, nullptr, "", goGameObjectCommandTable },
{ "graveyard", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoGraveyardCommand, "" },
{ "grid", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoGridCommand, "" },
{ "object", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoObjectCommand, "" },
{ "taxinode", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoTaxinodeCommand, "" },
{ "trigger", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoTriggerCommand, "" },
{ "areatrigger", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoAreaTriggerCommand, "" },
{ "zonexy", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoZoneXYCommand, "" },
{ "xyz", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoXYZCommand, "" },
{ "ticket", rbac::RBAC_PERM_COMMAND_GO, false, &HandleGoTicketCommand, "" },
@@ -65,123 +79,115 @@ public:
return commandTable;
}
/** \brief Teleport the GM to the specified creature
*
* .gocreature <GUID> --> TP using creature.guid
* .gocreature azuregos --> TP player to the mob with this name
* Warning: If there is more than one mob with this name
* you will be teleported to the first one that is found.
* .gocreature id 6109 --> TP player to the mob, that has this creature_template.entry
* Warning: If there is more than one mob with this "id"
* you will be teleported to the first one that is found.
*/
//teleport to creature
static bool HandleGoCreatureCommand(ChatHandler* handler, char const* args)
static bool DoTeleport(ChatHandler* handler, WorldLocation loc)
{
if (!*args)
return false;
Player* player = handler->GetSession()->GetPlayer();
// "id" or number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
char* param1 = handler->extractKeyFromLink((char*)args, "Hcreature");
if (!param1)
return false;
std::ostringstream whereClause;
// User wants to teleport to the NPC's template entry
if (strcmp(param1, "id") == 0)
if (!MapManager::IsValidMapCoord(loc) || sObjectMgr->IsTransportMap(loc.GetMapId()))
{
// Get the "creature_template.entry"
// number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
char* tail = strtok(nullptr, "");
if (!tail)
return false;
char* id = handler->extractKeyFromLink(tail, "Hcreature_entry");
if (!id)
return false;
uint32 entry = atoul(id);
if (!entry)
return false;
whereClause << "WHERE id = '" << entry << '\'';
}
else
{
ObjectGuid::LowType guidLow = atoul(param1);
// Number is invalid - maybe the user specified the mob's name
if (!guidLow)
{
std::string name = param1;
WorldDatabase.EscapeString(name);
whereClause << ", creature_template WHERE creature.id = creature_template.entry AND creature_template.name LIKE '" << name << '\'';
}
else
whereClause << "WHERE guid = '" << guidLow << '\'';
}
QueryResult result = WorldDatabase.PQuery("SELECT position_x, position_y, position_z, orientation, map FROM creature %s", whereClause.str().c_str());
if (!result)
{
handler->SendSysMessage(LANG_COMMAND_GOCREATNOTFOUND);
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, loc.GetPositionX(), loc.GetPositionY(), loc.GetPositionZ());
handler->SetSentErrorMessage(true);
return false;
}
if (result->GetRowCount() > 1)
handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
Field* fields = result->Fetch();
float x = fields[0].GetFloat();
float y = fields[1].GetFloat();
float z = fields[2].GetFloat();
float o = fields[3].GetFloat();
uint32 mapId = fields[4].GetUInt16();
if (!MapManager::IsValidMapCoord(mapId, x, y, z, o) || sObjectMgr->IsTransportMap(mapId))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
handler->SetSentErrorMessage(true);
return false;
}
// stop flight if need
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(mapId, x, y, z, o);
player->SaveRecallPosition(); // save only in non-flight case
player->TeleportTo(loc);
return true;
}
static bool HandleGoGraveyardCommand(ChatHandler* handler, char const* args)
static bool HandleGoCreatureSpawnIdCommand(ChatHandler* handler, Variant<Hyperlink<creature>, ObjectGuid::LowType> spawnId)
{
Player* player = handler->GetSession()->GetPlayer();
if (!*args)
CreatureData const* spawnpoint = sObjectMgr->GetCreatureData(spawnId);
if (!spawnpoint)
{
handler->SendSysMessage(LANG_COMMAND_GOCREATNOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
char* gyId = strtok((char*)args, " ");
if (!gyId)
return DoTeleport(handler, spawnpoint->spawnPoint);
}
static bool HandleGoCreatureCIdCommand(ChatHandler* handler, Variant<Hyperlink<creature_entry>, uint32> cId)
{
CreatureData const* spawnpoint = nullptr;
for (auto const& pair : sObjectMgr->GetAllCreatureData())
{
if (pair.second.id != *cId)
continue;
if (!spawnpoint)
spawnpoint = &pair.second;
else
{
handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
break;
}
}
if (!spawnpoint)
{
handler->SendSysMessage(LANG_COMMAND_GOCREATNOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
uint32 graveyardId = atoul(gyId);
return DoTeleport(handler, spawnpoint->spawnPoint);
}
if (!graveyardId)
static bool HandleGoGameObjectSpawnIdCommand(ChatHandler* handler, uint32 spawnId)
{
GameObjectData const* spawnpoint = sObjectMgr->GetGameObjectData(spawnId);
if (!spawnpoint)
{
handler->SendSysMessage(LANG_COMMAND_GOOBJNOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
WorldSafeLocsEntry const* gy = sWorldSafeLocsStore.LookupEntry(graveyardId);
return DoTeleport(handler, spawnpoint->spawnPoint);
}
static bool HandleGoGameObjectGOIdCommand(ChatHandler* handler, uint32 goId)
{
GameObjectData const* spawnpoint = nullptr;
for (auto const& pair : sObjectMgr->GetAllGameObjectData())
{
if (pair.second.id != goId)
continue;
if (!spawnpoint)
spawnpoint = &pair.second;
else
{
handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
break;
}
}
if (!spawnpoint)
{
handler->SendSysMessage(LANG_COMMAND_GOOBJNOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
return DoTeleport(handler, spawnpoint->spawnPoint);
}
static bool HandleGoGraveyardCommand(ChatHandler* handler, uint32 gyId)
{
WorldSafeLocsEntry const* gy = sWorldSafeLocsStore.LookupEntry(gyId);
if (!gy)
{
handler->PSendSysMessage(LANG_COMMAND_GRAVEYARDNOEXIST, graveyardId);
handler->PSendSysMessage(LANG_COMMAND_GRAVEYARDNOEXIST, gyId);
handler->SetSentErrorMessage(true);
return false;
}
@@ -193,6 +199,7 @@ public:
return false;
}
Player* player = handler->GetSession()->GetPlayer();
// stop flight if need
if (player->IsInFlight())
{
@@ -208,25 +215,15 @@ public:
}
//teleport to grid
static bool HandleGoGridCommand(ChatHandler* handler, char const* args)
static bool HandleGoGridCommand(ChatHandler* handler, float gridX, float gridY, Optional<uint32> oMapId)
{
if (!*args)
return false;
Player* player = handler->GetSession()->GetPlayer();
char* gridX = strtok((char*)args, " ");
char* gridY = strtok(nullptr, " ");
char* id = strtok(nullptr, " ");
if (!gridX || !gridY)
return false;
uint32 mapId = id ? atoul(id) : player->GetMapId();
uint32 mapId = oMapId.get_value_or(player->GetMapId());
// center of grid
float x = ((float)atof(gridX) - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
float y = ((float)atof(gridY) - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
float x = (gridX - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
float y = (gridY - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
if (!MapManager::IsValidMapCoord(mapId, x, y))
{
@@ -252,68 +249,8 @@ public:
return true;
}
//teleport to gameobject
static bool HandleGoObjectCommand(ChatHandler* handler, char const* args)
static bool HandleGoTaxinodeCommand(ChatHandler* handler, Variant<Hyperlink<taxinode>, uint32> nodeId)
{
if (!*args)
return false;
Player* player = handler->GetSession()->GetPlayer();
// number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
if (!id)
return false;
ObjectGuid::LowType guidLow = atoul(id);
if (!guidLow)
return false;
// by DB guid
GameObjectData const* goData = sObjectMgr->GetGameObjectData(guidLow);
if (!goData)
{
handler->SendSysMessage(LANG_COMMAND_GOOBJNOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
if (!MapManager::IsValidMapCoord(goData->spawnPoint) || sObjectMgr->IsTransportMap(goData->spawnPoint.GetMapId()))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, goData->spawnPoint.GetPositionX(), goData->spawnPoint.GetPositionY(), goData->spawnPoint.GetMapId());
handler->SetSentErrorMessage(true);
return false;
}
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(goData->spawnPoint);
return true;
}
static bool HandleGoTaxinodeCommand(ChatHandler* handler, char const* args)
{
Player* player = handler->GetSession()->GetPlayer();
if (!*args)
return false;
char* id = handler->extractKeyFromLink((char*)args, "Htaxinode");
if (!id)
return false;
uint32 nodeId = atoul(id);
if (!nodeId)
return false;
TaxiNodesEntry const* node = sTaxiNodesStore.LookupEntry(nodeId);
if (!node)
{
@@ -322,44 +259,11 @@ public:
return false;
}
if ((node->x == 0.0f && node->y == 0.0f && node->z == 0.0f) ||
!MapManager::IsValidMapCoord(node->map_id, node->x, node->y, node->z))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, node->x, node->y, node->map_id);
handler->SetSentErrorMessage(true);
return false;
}
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(node->map_id, node->x, node->y, node->z, player->GetOrientation());
return true;
return DoTeleport(handler, { node->map_id, { node->x, node->y, node->z } });
}
static bool HandleGoTriggerCommand(ChatHandler* handler, char const* args)
static bool HandleGoAreaTriggerCommand(ChatHandler* handler, Variant<Hyperlink<areatrigger>, uint32> areaTriggerId)
{
Player* player = handler->GetSession()->GetPlayer();
if (!*args)
return false;
char* id = strtok((char*)args, " ");
if (!id)
return false;
uint32 areaTriggerId = atoul(id);
if (!areaTriggerId)
return false;
AreaTriggerEntry const* at = sAreaTriggerStore.LookupEntry(areaTriggerId);
if (!at)
{
@@ -368,25 +272,7 @@ public:
return false;
}
if (!MapManager::IsValidMapCoord(at->mapid, at->x, at->y, at->z))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, at->x, at->y, at->mapid);
handler->SetSentErrorMessage(true);
return false;
}
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(at->mapid, at->x, at->y, at->z, player->GetOrientation());
return true;
return DoTeleport(handler, { at->mapid, { at->x, at->y, at->z } });
}
//teleport at coordinates
@@ -463,32 +349,14 @@ public:
}
//teleport at coordinates, including Z and orientation
static bool HandleGoXYZCommand(ChatHandler* handler, char const* args)
static bool HandleGoXYZCommand(ChatHandler* handler, float x, float y, Optional<float> z, Optional<uint32> id, Optional<float> o)
{
if (!*args)
return false;
Player* player = handler->GetSession()->GetPlayer();
char* goX = strtok((char*)args, " ");
char* goY = strtok(nullptr, " ");
char* goZ = strtok(nullptr, " ");
char* id = strtok(nullptr, " ");
char* port = strtok(nullptr, " ");
if (!goX || !goY)
return false;
float x = (float)atof(goX);
float y = (float)atof(goY);
float z;
float ort = port ? (float)atof(port) : player->GetOrientation();
uint32 mapId = id ? atoul(id) : player->GetMapId();
if (goZ)
uint32 mapId = id.get_value_or(player->GetMapId());
if (z)
{
z = (float)atof(goZ);
if (!MapManager::IsValidMapCoord(mapId, x, y, z))
if (!MapManager::IsValidMapCoord(mapId, x, y, *z))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
handler->SetSentErrorMessage(true);
@@ -507,33 +375,11 @@ public:
z = std::max(map->GetHeight(PhasingHandler::GetEmptyPhaseShift(), x, y, MAX_HEIGHT), map->GetWaterLevel(PhasingHandler::GetEmptyPhaseShift(), x, y));
}
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(mapId, x, y, z, ort);
return true;
return DoTeleport(handler, { mapId, { x, y, *z, o.get_value_or(0.0f) } });
}
static bool HandleGoTicketCommand(ChatHandler* handler, char const* args)
static bool HandleGoTicketCommand(ChatHandler* handler, uint32 ticketId)
{
if (!*args)
return false;
char* id = strtok((char*)args, " ");
if (!id)
return false;
uint32 ticketId = atoul(id);
if (!ticketId)
return false;
GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
if (!ticket)
{
@@ -554,99 +400,47 @@ public:
return true;
}
static bool HandleGoOffsetCommand(ChatHandler* handler, char const* args)
static bool HandleGoOffsetCommand(ChatHandler* handler, float dX, Optional<float> dY, Optional<float> dZ, Optional<float> dO)
{
if (!*args)
return false;
WorldLocation loc = handler->GetSession()->GetPlayer()->GetWorldLocation();
loc.RelocateOffset({ dX, dY.get_value_or(0.0f), dZ.get_value_or(0.0f), dO.get_value_or(0.0f) });
Player* player = handler->GetSession()->GetPlayer();
char* goX = strtok((char*)args, " ");
char* goY = strtok(nullptr, " ");
char* goZ = strtok(nullptr, " ");
char* port = strtok(nullptr, " ");
float x, y, z, o;
player->GetPosition(x, y, z, o);
if (goX)
x += atof(goX);
if (goY)
y += atof(goY);
if (goZ)
z += atof(goZ);
if (port)
o += atof(port);
if (!Trinity::IsValidMapCoord(x, y, z, o))
{
handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, player->GetMapId());
handler->SetSentErrorMessage(true);
return false;
}
// stop flight if need
if (player->IsInFlight())
{
player->GetMotionMaster()->MovementExpired();
player->CleanupAfterTaxiFlight();
}
// save only in non-flight case
else
player->SaveRecallPosition();
player->TeleportTo(player->GetMapId(), x, y, z, o);
return true;
return DoTeleport(handler, loc);
}
static bool HandleGoInstanceCommand(ChatHandler* handler, char const* args)
static bool HandleGoInstanceCommand(ChatHandler* handler, std::vector<std::string> const& labels)
{
if (!*args)
return false;
char* pos = const_cast<char*>(args);
do *pos = tolower(*pos);
while (*(++pos));
Tokenizer labels(args, ' ');
uint32 mapid = 0;
if (labels.size() == 1)
std::multimap<uint32, std::pair<uint16, std::string>> matches;
for (auto const& pair : sObjectMgr->GetInstanceTemplates())
{
try { mapid = std::stoi(labels[0]); }
catch (...) {}
uint32 count = 0;
std::string const& scriptName = sObjectMgr->GetScriptName(pair.second.ScriptId);
for (auto const& label : labels)
if (StringContainsStringI(scriptName, label))
++count;
if (count)
matches.emplace(count, decltype(matches)::mapped_type({ pair.first, scriptName }));
}
if (!mapid)
if (matches.empty())
{
std::multimap<uint32, std::pair<uint16, std::string>> matches;
for (auto const& pair : sObjectMgr->GetInstanceTemplates())
{
uint32 count = 0;
std::string const& scriptName = sObjectMgr->GetScriptName(pair.second.ScriptId);
for (char const* label : labels)
if (scriptName.find(label) != std::string::npos)
++count;
if (count)
matches.emplace(count, decltype(matches)::mapped_type({ pair.first, scriptName }));
}
if (matches.empty())
{
handler->SendSysMessage(LANG_COMMAND_NO_INSTANCES_MATCH);
return false;
}
auto it = matches.rbegin();
uint32 maxCount = it->first;
mapid = it->second.first;
if (++it != matches.rend() && it->first == maxCount)
{
handler->SendSysMessage(LANG_COMMAND_MULTIPLE_INSTANCES_MATCH);
--it;
do
handler->PSendSysMessage(LANG_COMMAND_MULTIPLE_INSTANCES_ENTRY, it->second.first, it->second.second);
while (++it != matches.rend() && it->first == maxCount);
handler->SetSentErrorMessage(true);
return false;
}
handler->SendSysMessage(LANG_COMMAND_NO_INSTANCES_MATCH);
return false;
}
auto it = matches.rbegin();
uint32 maxCount = it->first;
mapid = it->second.first;
if (++it != matches.rend() && it->first == maxCount)
{
handler->SendSysMessage(LANG_COMMAND_MULTIPLE_INSTANCES_MATCH);
--it;
do
handler->PSendSysMessage(LANG_COMMAND_MULTIPLE_INSTANCES_ENTRY, it->second.first, it->second.second);
while (++it != matches.rend() && it->first == maxCount);
handler->SetSentErrorMessage(true);
return false;
}
ASSERT(mapid);

View File

@@ -36,6 +36,8 @@ EndScriptData */
#include "RBAC.h"
#include "WorldSession.h"
using namespace Trinity::ChatCommands;
class tele_commandscript : public CommandScript
{
public:
@@ -58,17 +60,12 @@ public:
return commandTable;
}
static bool HandleTeleAddCommand(ChatHandler* handler, char const* args)
static bool HandleTeleAddCommand(ChatHandler* handler, std::string const& name)
{
if (!*args)
return false;
Player* player = handler->GetSession()->GetPlayer();
if (!player)
return false;
std::string name = args;
if (sObjectMgr->GetGameTeleExactName(name))
{
handler->SendSysMessage(LANG_COMMAND_TP_ALREADYEXIST);
@@ -98,13 +95,8 @@ public:
return true;
}
static bool HandleTeleDelCommand(ChatHandler* handler, char const* args)
static bool HandleTeleDelCommand(ChatHandler* handler, GameTele const* tele)
{
if (!*args)
return false;
// id, or string, or [name] Shift-click form |color|Htele:id|h[name]|h|r
GameTele const* tele = handler->extractGameTeleFromLink((char*)args);
if (!tele)
{
handler->SendSysMessage(LANG_COMMAND_TELE_NOTFOUND);
@@ -215,10 +207,14 @@ public:
}
//Teleport group to given game_tele.entry
static bool HandleTeleGroupCommand(ChatHandler* handler, char const* args)
static bool HandleTeleGroupCommand(ChatHandler* handler, GameTele const* tele)
{
if (!*args)
if (!tele)
{
handler->SendSysMessage(LANG_COMMAND_TELE_NOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
Player* target = handler->getSelectedPlayer();
if (!target)
@@ -232,15 +228,6 @@ public:
if (handler->HasLowerSecurity(target, ObjectGuid::Empty))
return false;
// id, or string, or [name] Shift-click form |color|Htele:id|h[name]|h|r
GameTele const* tele = handler->extractGameTeleFromLink((char*)args);
if (!tele)
{
handler->SendSysMessage(LANG_COMMAND_TELE_NOTFOUND);
handler->SetSentErrorMessage(true);
return false;
}
MapEntry const* map = sMapStore.LookupEntry(tele->mapId);
if (!map || map->IsBattlegroundOrArena())
{
@@ -298,16 +285,8 @@ public:
return true;
}
static bool HandleTeleCommand(ChatHandler* handler, char const* args)
static bool HandleTeleCommand(ChatHandler* handler, GameTele const* tele)
{
if (!*args)
return false;
Player* me = handler->GetSession()->GetPlayer();
// id, or string, or [name] Shift-click form |color|Htele:id|h[name]|h|r
GameTele const* tele = handler->extractGameTeleFromLink((char*)args);
if (!tele)
{
handler->SendSysMessage(LANG_COMMAND_TELE_NOTFOUND);
@@ -315,6 +294,7 @@ public:
return false;
}
Player* me = handler->GetSession()->GetPlayer();
if (me->IsInCombat())
{
handler->SendSysMessage(LANG_YOU_IN_COMBAT);