1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-24 05:37:45 +00:00
sapphire/src/servers/Server_Zone/DebugCommand/DebugCommandHandler.cpp

523 lines
18 KiB
C++
Raw Normal View History

2017-08-08 13:53:47 +02:00
#include <boost/lexical_cast.hpp>
2017-08-19 00:18:40 +02:00
#include <src/servers/Server_Common/Common.h>
2017-09-14 17:07:58 +02:00
#include <src/servers/Server_Common/Version.h>
2017-08-19 00:18:40 +02:00
#include <src/servers/Server_Common/Database/Database.h>
#include <src/servers/Server_Common/Network/GamePacketNew.h>
#include <src/servers/Server_Common/Network/CommonNetwork.h>
#include <src/servers/Server_Common/Util/UtilMath.h>
#include <src/servers/Server_Common/Network/PacketContainer.h>
#include <src/servers/Server_Common/Logging/Logger.h>
#include <src/servers/Server_Common/Exd/ExdData.h>
2017-08-08 13:53:47 +02:00
#include "DebugCommand.h"
#include "DebugCommandHandler.h"
2017-08-08 13:53:47 +02:00
#include "src/servers/Server_Zone/Network/PacketWrappers/ServerNoticePacket.h"
#include "src/servers/Server_Zone/Network/PacketWrappers/ActorControlPacket142.h"
#include "src/servers/Server_Zone/Network/PacketWrappers/ActorControlPacket143.h"
#include "src/servers/Server_Zone/Network/PacketWrappers/InitUIPacket.h"
#include "src/servers/Server_Zone/Network/GameConnection.h"
#include "src/servers/Server_Zone/Script/ScriptManager.h"
2017-08-08 13:53:47 +02:00
#include "src/servers/Server_Zone/Actor/Player.h"
#include "src/servers/Server_Zone/Actor/BattleNpc.h"
2017-08-08 13:53:47 +02:00
#include "src/servers/Server_Zone/Zone/Zone.h"
2017-08-08 13:53:47 +02:00
#include "src/servers/Server_Zone/ServerZone.h"
2017-08-08 13:53:47 +02:00
#include "src/servers/Server_Zone/StatusEffect/StatusEffect.h"
#include "src/servers/Server_Zone/Session.h"
2017-08-08 13:53:47 +02:00
#include <boost/make_shared.hpp>
2017-08-17 18:12:29 +02:00
extern Core::Db::Database g_database;
extern Core::Scripting::ScriptManager g_scriptMgr;
extern Core::Data::ExdData g_exdData;
extern Core::Logger g_log;
extern Core::ServerZone g_serverZone;
2017-08-08 13:53:47 +02:00
// instanciate and initialize commands
Core::DebugCommandHandler::DebugCommandHandler()
2017-08-08 13:53:47 +02:00
{
// Push all commands onto the register map ( command name - function - description - required GM level )
registerCommand( "set", &DebugCommandHandler::set, "Loads and injects a premade Packet.", 1 );
registerCommand( "get", &DebugCommandHandler::get, "Loads and injects a premade Packet.", 1 );
registerCommand( "add", &DebugCommandHandler::add, "Loads and injects a premade Packet.", 1 );
2017-10-21 23:41:09 +11:00
registerCommand( "inject", &DebugCommandHandler::injectPacket, "Loads and injects a premade packet.", 1 );
registerCommand( "injectc", &DebugCommandHandler::injectChatPacket, "Loads and injects a premade chat packet.", 1 );
registerCommand( "script_reload", &DebugCommandHandler::scriptReload, "Reload all server scripts", 1 );
registerCommand( "nudge", &DebugCommandHandler::nudge, "Nudges you forward/up/down", 1 );
registerCommand( "info", &DebugCommandHandler::serverInfo, "Send server info", 0 );
2017-10-21 23:41:09 +11:00
registerCommand( "unlock", &DebugCommandHandler::unlockCharacter, "Unlock character", 1 );
registerCommand( "help", &DebugCommandHandler::help, "Shows registered commands", 0 );
2017-08-08 13:53:47 +02:00
}
// clear all loaded commands
Core::DebugCommandHandler::~DebugCommandHandler()
2017-08-08 13:53:47 +02:00
{
for( auto it = m_commandMap.begin(); it != m_commandMap.end(); ++it )
( *it ).second.reset();
}
// add a command set to the register map
void Core::DebugCommandHandler::registerCommand( const std::string& n, Core::DebugCommand::pFunc functionPtr,
const std::string& hText, uint8_t uLevel )
2017-08-08 13:53:47 +02:00
{
m_commandMap[std::string( n )] = boost::make_shared<DebugCommand>( n, functionPtr, hText, uLevel );
2017-08-08 13:53:47 +02:00
}
// try to retrieve the command in question, execute if found
void Core::DebugCommandHandler::execCommand( char * data, Core::Entity::PlayerPtr pPlayer )
2017-08-08 13:53:47 +02:00
{
// define callback pointer
void ( DebugCommandHandler::*pf )( char *, Entity::PlayerPtr, boost::shared_ptr< DebugCommand > );
2017-08-08 13:53:47 +02:00
std::string commandString;
// check if the command has parameters
std::string tmpCommand = std::string( data );
std::size_t pos = tmpCommand.find_first_of( " " );
if( pos != std::string::npos )
// command has parameters, grab the first part
commandString = tmpCommand.substr( 0, pos );
else
// no parameters, just get the command
commandString = tmpCommand;
// try to retrieve the command
2017-08-08 13:53:47 +02:00
auto it = m_commandMap.find( commandString );
if( it == m_commandMap.end() )
// no command found, do something... or not
2017-08-10 16:31:48 +02:00
pPlayer->sendUrgent( "Command not found." );
2017-08-08 13:53:47 +02:00
else
{
if( pPlayer->getGmRank() < it->second->getRequiredGmLevel() )
{
pPlayer->sendUrgent( "You are not allowed to use this command." );
return;
}
2017-08-08 13:53:47 +02:00
// command found, call the callback function and pass parameters if present.
pf = ( *it ).second->m_pFunc;
( this->*pf )( data, pPlayer, ( *it ).second );
return;
}
}
///////////////////////////////////////////////////////////////////////////////////////
// Definition of the commands
///////////////////////////////////////////////////////////////////////////////////////
void Core::DebugCommandHandler::scriptReload( char * data, Core::Entity::PlayerPtr pPlayer,
boost::shared_ptr<Core::DebugCommand> command )
2017-08-08 13:53:47 +02:00
{
g_scriptMgr.reload();
2017-10-09 20:09:49 +02:00
pPlayer->sendDebug( "Scripts reloaded." );
2017-08-08 13:53:47 +02:00
}
2017-10-21 23:41:09 +11:00
void Core::DebugCommandHandler::help( char* data, Entity::PlayerPtr pPlayer, boost::shared_ptr< Core::DebugCommand > command )
{
pPlayer->sendDebug( "Registered debug commands:" );
for ( auto cmd : m_commandMap )
{
if ( pPlayer->getGmRank( ) >= cmd.second->m_gmLevel )
{
pPlayer->sendDebug( " - " + cmd.first + " - " + cmd.second->getHelpText( ) );
}
}
}
void Core::DebugCommandHandler::set( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr<Core::DebugCommand> command )
2017-08-08 13:53:47 +02:00
{
std::string subCommand = "";
std::string params = "";
// check if the command has parameters
std::string tmpCommand = std::string( data + command->getName().length() + 1 );
std::size_t pos = tmpCommand.find_first_of( " " );
if( pos != std::string::npos )
// command has parameters, grab the first part
subCommand = tmpCommand.substr( 0, pos );
else
// no subcommand given
subCommand = tmpCommand;
if( command->getName().length() + 1 + pos + 1 < strlen( data ) )
params = std::string( data + command->getName().length() + 1 + pos + 1 );
g_log.debug( "[" + std::to_string( pPlayer->getId() ) + "] " +
"subCommand " + subCommand + " params: " + params );
if( ( ( subCommand == "pos" ) || ( subCommand == "posr" ) ) && ( params != "" ) )
{
int32_t posX;
int32_t posY;
int32_t posZ;
sscanf( params.c_str(), "%d %d %d", &posX, &posY, &posZ );
if( ( posX == 0xcccccccc ) || ( posY == 0xcccccccc ) || ( posZ == 0xcccccccc ) )
{
pPlayer->sendUrgent( "Syntaxerror." );
return;
}
if( subCommand == "pos" )
pPlayer->setPosition( static_cast< float >( posX ),
static_cast< float >( posY ),
static_cast< float >( posZ ) );
else
pPlayer->setPosition( pPlayer->getPos().x + static_cast< float >( posX ),
pPlayer->getPos().y + static_cast< float >( posY ),
pPlayer->getPos().z + static_cast< float >( posZ ) );
Network::Packets::GamePacketNew< Network::Packets::Server::FFXIVIpcActorSetPos, Network::Packets::ServerZoneIpcType >
2017-08-08 13:53:47 +02:00
setActorPosPacket( pPlayer->getId() );
setActorPosPacket.data().x = pPlayer->getPos().x;
setActorPosPacket.data().y = pPlayer->getPos().y;
setActorPosPacket.data().z = pPlayer->getPos().z;
pPlayer->queuePacket( setActorPosPacket );
}
else if( ( subCommand == "tele" ) && ( params != "" ) )
{
int32_t aetheryteId;
sscanf( params.c_str(), "%i", &aetheryteId );
pPlayer->teleport( aetheryteId );
}
else if( ( subCommand == "discovery" ) && ( params != "" ) )
{
int32_t map_id;
int32_t discover_id;
sscanf( params.c_str(), "%i %i", &map_id, &discover_id );
Network::Packets::GamePacketNew< Network::Packets::Server::FFXIVIpcDiscovery, Network::Packets::ServerZoneIpcType > discoveryPacket( pPlayer->getId() );
2017-08-08 13:53:47 +02:00
discoveryPacket.data().map_id = map_id;
discoveryPacket.data().map_part_id = discover_id;
pPlayer->queuePacket( discoveryPacket );
}
else if( ( subCommand == "discovery_pos" ) && ( params != "" ) )
{
int32_t map_id;
int32_t discover_id;
int32_t pos_id;
sscanf( params.c_str(), "%i %i %i", &pos_id, &map_id, &discover_id );
std::string query2 = "UPDATE IGNORE `discoveryinfo` SET `discover_id` = '" + std::to_string( discover_id ) +
"' WHERE `discoveryinfo`.`id` = " + std::to_string( pos_id ) + ";";
2017-08-08 13:53:47 +02:00
std::string query1 = "INSERT IGNORE INTO `discoveryinfo` (`id`, `map_id`, `discover_id`) VALUES ('" + std::to_string( pos_id ) +
2017-08-08 13:53:47 +02:00
"', '" + std::to_string( map_id ) +
"', '" + std::to_string( discover_id ) + "')";
g_database.execute( query1 );
g_database.execute( query2 );
2017-08-08 13:53:47 +02:00
}
else if( subCommand == "discovery_reset" )
{
pPlayer->resetDiscovery();
pPlayer->queuePacket( Network::Packets::Server::InitUIPacket( pPlayer ) );
}
2017-08-09 14:38:46 +02:00
else if( subCommand == "classjob" )
{
int32_t id;
sscanf( params.c_str(), "%d", &id );
if( pPlayer->getLevelForClass( static_cast<Core::Common::ClassJob> ( id ) ) == 0 )
{
pPlayer->setLevelForClass( 1, static_cast<Core::Common::ClassJob> ( id ) );
pPlayer->setClassJob( static_cast<Core::Common::ClassJob> ( id ) );
}
else
pPlayer->setClassJob( static_cast<Core::Common::ClassJob> ( id ) );
}
else if ( subCommand == "cfpenalty" )
{
int32_t minutes;
sscanf( params.c_str(), "%d", &minutes );
pPlayer->setCFPenaltyMinutes( minutes );
}
else if ( subCommand == "eorzeatime" )
{
uint64_t timestamp;
2017-09-22 15:38:32 +02:00
sscanf( params.c_str(), "%llu", &timestamp );
pPlayer->setEorzeaTimeOffset( timestamp );
pPlayer->sendNotice( "Eorzea time offset: " + std::to_string( timestamp ) );
}
2017-10-02 16:00:26 +02:00
else if ( subCommand == "model" )
{
uint32_t slot;
uint32_t val;
2017-10-02 16:02:28 +02:00
sscanf( params.c_str(), "%d %d", &slot, &val );
2017-10-02 16:00:26 +02:00
pPlayer->setModelForSlot( static_cast<Inventory::EquipSlot>( slot ), val );
pPlayer->sendModel();
pPlayer->sendDebug( "Model updated" );
}
2017-10-18 18:06:25 +02:00
else if ( subCommand == "mount" )
{
int32_t id;
sscanf( params.c_str(), "%d", &id );
pPlayer->dismount();
pPlayer->mount( id );
}
2017-10-02 16:00:26 +02:00
else
{
pPlayer->sendUrgent( subCommand + " is not a valid SET command." );
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::add( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr<Core::DebugCommand> command )
2017-08-08 13:53:47 +02:00
{
std::string subCommand;
std::string params = "";
// check if the command has parameters
std::string tmpCommand = std::string( data + command->getName().length() + 1 );
std::size_t pos = tmpCommand.find_first_of( " " );
if( pos != std::string::npos )
// command has parameters, grab the first part
subCommand = tmpCommand.substr( 0, pos );
else
{
// no subcommand given
subCommand = tmpCommand;
return;
}
if( command->getName().length() + 1 + pos + 1 < strlen( data ) )
params = std::string( data + command->getName().length() + 1 + pos + 1 );
g_log.debug( "[" + std::to_string( pPlayer->getId() ) + "] " +
"subCommand " + subCommand + " params: " + params );
if( subCommand == "status" )
2017-08-08 13:53:47 +02:00
{
int32_t id;
int32_t duration;
uint16_t param;
2017-08-08 13:53:47 +02:00
2017-10-09 10:45:18 -03:00
sscanf( params.c_str(), "%d %d %hu", &id, &duration, &param );
2017-08-08 13:53:47 +02:00
StatusEffect::StatusEffectPtr effect( new StatusEffect::StatusEffect( id, pPlayer, pPlayer, duration, 3000 ) );
effect->setParam( param );
2017-08-08 13:53:47 +02:00
pPlayer->addStatusEffect( effect );
}
2017-10-09 00:31:31 -03:00
else if ( subCommand == "title" )
{
uint32_t titleId;
2017-10-09 10:45:18 -03:00
sscanf( params.c_str(), "%u", &titleId );
2017-10-09 00:31:31 -03:00
pPlayer->addTitle( titleId );
2017-10-09 02:15:57 -03:00
pPlayer->sendNotice( "Added title (ID: " + std::to_string( titleId ) + ")" );
2017-10-09 00:31:31 -03:00
}
2017-08-08 13:53:47 +02:00
else if( subCommand == "spawn" )
{
int32_t model, name;
sscanf( params.c_str(), "%d %d", &model, &name );
Entity::BattleNpcPtr pBNpc( new Entity::BattleNpc( model, name, pPlayer->getPos() ) );
auto pZone = pPlayer->getCurrentZone();
pBNpc->setCurrentZone( pZone );
pZone->pushActor( pBNpc );
}
else if( subCommand == "op" )
{
// temporary research packet
int32_t opcode;
sscanf( params.c_str(), "%x", &opcode );
Network::Packets::GamePacketPtr pPe( new Network::Packets::GamePacket( opcode, 0x30, pPlayer->getId(), pPlayer->getId() ) );
pPlayer->queuePacket( pPe );
}
else if( subCommand == "actrl" )
{
// temporary research packet
int32_t opcode;
int32_t param1;
int32_t param2;
int32_t param3;
int32_t param4;
int32_t param5;
int32_t param6;
int32_t playerId;
sscanf( params.c_str(), "%x %x %x %x %x %x %x %x", &opcode, &param1, &param2, &param3, &param4, &param5, &param6, &playerId );
pPlayer->sendNotice( "Injecting ACTOR_CONTROL " + std::to_string( opcode ) );
Network::Packets::GamePacketNew< Network::Packets::Server::FFXIVIpcActorControl143, Network::Packets::ServerZoneIpcType > actorControl( playerId, pPlayer->getId() );
2017-08-08 13:53:47 +02:00
actorControl.data().category = opcode;
actorControl.data().param1 = param1;
actorControl.data().param2 = param2;
actorControl.data().param3 = param3;
actorControl.data().param4 = param4;
actorControl.data().param5 = param5;
actorControl.data().param6 = param6;
pPlayer->queuePacket( actorControl );
/*sscanf(params.c_str(), "%x %x %x %x %x %x %x", &opcode, &param1, &param2, &param3, &param4, &param5, &param6, &playerId);
Network::Packets::Server::ServerNoticePacket noticePacket(pPlayer, "Injecting ACTOR_CONTROL " + std::to_string(opcode));
pPlayer->queuePacket(noticePacket);
Network::Packets::Server::ActorControlPacket143 controlPacket(pPlayer, opcode,
param1, param2, param3, param4, param5, param6, playerId);
pPlayer->queuePacket(controlPacket);*/
}
2017-10-02 16:00:26 +02:00
else
{
pPlayer->sendUrgent( subCommand + " is not a valid ADD command." );
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::get( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr<Core::DebugCommand> command )
2017-08-08 13:53:47 +02:00
{
std::string subCommand;
std::string params = "";
// check if the command has parameters
std::string tmpCommand = std::string( data + command->getName().length() + 1 );
std::size_t pos = tmpCommand.find_first_of( " " );
if( pos != std::string::npos )
// command has parameters, grab the first part
subCommand = tmpCommand.substr( 0, pos );
else
// no subcommand given
subCommand = tmpCommand;
if( command->getName().length() + 1 + pos + 1 < strlen( data ) )
params = std::string( data + command->getName().length() + 1 + pos + 1 );
g_log.debug( "[" + std::to_string( pPlayer->getId() ) + "] " +
"subCommand " + subCommand + " params: " + params );
if( ( subCommand == "pos" ) )
{
int16_t map_id = g_exdData.m_zoneInfoMap[pPlayer->getCurrentZone()->getId()].map_id;
pPlayer->sendNotice( "Pos:\n" +
std::to_string( pPlayer->getPos().x ) + "\n" +
std::to_string( pPlayer->getPos().y ) + "\n" +
std::to_string( pPlayer->getPos().z ) + "\n" +
std::to_string( pPlayer->getRotation() ) + "\nMapId: " +
std::to_string( map_id ) + "\nZoneID: " +
std::to_string( pPlayer->getCurrentZone()->getId() ) + "\n" );
}
2017-10-02 16:00:26 +02:00
else
{
pPlayer->sendUrgent( subCommand + " is not a valid GET command." );
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::injectPacket( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr< Core::DebugCommand > command )
2017-08-08 13:53:47 +02:00
{
auto pSession = g_serverZone.getSession( pPlayer->getId() );
if( pSession )
pSession->getZoneConnection()->injectPacket( data + 7, pPlayer );
}
void Core::DebugCommandHandler::injectChatPacket( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr< Core::DebugCommand > command )
{
auto pSession = g_serverZone.getSession( pPlayer->getId() );
if( pSession )
pSession->getChatConnection()->injectPacket( data + 8, pPlayer );
}
void Core::DebugCommandHandler::nudge( char * data, Entity::PlayerPtr pPlayer, boost::shared_ptr<DebugCommand> command )
2017-08-08 13:53:47 +02:00
{
std::string subCommand;
// check if the command has parameters
std::string tmpCommand = std::string( data + command->getName().length() + 1 );
std::size_t spos = tmpCommand.find_first_of( " " );
auto& pos = pPlayer->getPos();
int32_t offset = 0;
char direction[20];
memset( direction, 0, 20 );
sscanf( tmpCommand.c_str(), "%d %s", &offset, direction );
if( direction[0] == 'u' || direction[0] == '+' )
{
pos.y += offset;
pPlayer->sendNotice( "nudge: Placing up " + std::to_string( offset ) + " yalms" );
}
else if( direction[0] == 'd' || direction[0] == '-' )
{
pos.y -= offset;
pPlayer->sendNotice( "nudge: Placing down " + std::to_string( offset ) + " yalms" );
}
else
{
float angle = pPlayer->getRotation() + ( PI / 2 );
pos.x -= offset * cos( angle );
pos.z += offset * sin( angle );
pPlayer->sendNotice( "nudge: Placing forward " + std::to_string( offset ) + " yalms" );
}
if( offset != 0 )
{
Network::Packets::GamePacketNew< Network::Packets::Server::FFXIVIpcActorSetPos, Network::Packets::ServerZoneIpcType >
2017-08-08 13:53:47 +02:00
setActorPosPacket( pPlayer->getId() );
setActorPosPacket.data().x = pPlayer->getPos().x;
setActorPosPacket.data().y = pPlayer->getPos().y;
setActorPosPacket.data().z = pPlayer->getPos().z;
setActorPosPacket.data().r16 = Math::Util::floatToUInt16Rot( pPlayer->getRotation() );
pPlayer->queuePacket( setActorPosPacket );
}
}
2017-09-14 17:07:58 +02:00
2017-09-14 17:19:44 +02:00
void Core::DebugCommandHandler::serverInfo( char * data, Core::Entity::PlayerPtr pPlayer, boost::shared_ptr< Core::DebugCommand > command )
2017-09-14 17:07:58 +02:00
{
2017-09-25 18:06:17 +02:00
pPlayer->sendDebug( "SapphireServer " + Version::VERSION + "\nRev: " + Version::GIT_HASH );
pPlayer->sendDebug( "Compiled: " __DATE__ " " __TIME__ );
2017-09-14 17:07:58 +02:00
pPlayer->sendDebug( "Sessions: " + std::to_string( g_serverZone.getSessionCount() ) );
}
2017-10-21 23:41:09 +11:00
void Core::DebugCommandHandler::unlockCharacter( char* data, Entity::PlayerPtr pPlayer, boost::shared_ptr< Core::DebugCommand > command )
{
pPlayer->unlock( );
}