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

606 lines
20 KiB
C++
Raw Normal View History

2017-08-08 13:53:47 +02:00
#include <boost/lexical_cast.hpp>
#include <Server_Common/Common.h>
#include <Server_Common/Version.h>
#include <Server_Common/Network/GamePacketNew.h>
#include <Server_Common/Network/CommonNetwork.h>
#include <Server_Common/Util/UtilMath.h>
#include <Server_Common/Network/PacketContainer.h>
#include <Server_Common/Logging/Logger.h>
#include <Server_Common/Exd/ExdData.h>
#include <Server_Common/Database/DatabaseDef.h>
2017-08-08 13:53:47 +02:00
#include "DebugCommand.h"
#include "DebugCommandHandler.h"
2017-08-08 13:53:47 +02:00
#include "Network/PacketWrappers/ServerNoticePacket.h"
#include "Network/PacketWrappers/ActorControlPacket142.h"
#include "Network/PacketWrappers/ActorControlPacket143.h"
#include "Network/PacketWrappers/InitUIPacket.h"
#include "Network/GameConnection.h"
#include "Script/ScriptManager.h"
#include "Script/NativeScript.h"
2017-08-08 13:53:47 +02:00
#include "Actor/Player.h"
#include "Actor/BattleNpc.h"
2017-08-08 13:53:47 +02:00
#include "Zone/Zone.h"
2017-08-08 13:53:47 +02:00
#include "ServerZone.h"
2017-08-08 13:53:47 +02:00
#include "StatusEffect/StatusEffect.h"
#include "Session.h"
2017-08-08 13:53:47 +02:00
#include <boost/make_shared.hpp>
2017-12-12 17:29:31 +11:00
#include <boost/format.hpp>
2017-08-08 13:53:47 +02:00
2017-10-26 15:22:58 +02:00
#include <cinttypes>
2017-08-17 18:12:29 +02:00
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( "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 );
registerCommand( "script", &DebugCommandHandler::script, "Server script utilities", 1 );
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, 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, Entity::Player& player )
2017-08-08 13:53:47 +02:00
{
// define callback pointer
void ( DebugCommandHandler::*pf )( char *, Entity::Player&, 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
player.sendUrgent( "Command not found." );
2017-08-08 13:53:47 +02:00
else
{
if( player.getGmRank() < it->second->getRequiredGmLevel() )
{
player.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, player, ( *it ).second );
2017-08-08 13:53:47 +02:00
return;
}
}
///////////////////////////////////////////////////////////////////////////////////////
// Definition of the commands
///////////////////////////////////////////////////////////////////////////////////////
void Core::DebugCommandHandler::help( char* data, Entity::Player& player, boost::shared_ptr< DebugCommand > command )
2017-10-21 23:41:09 +11:00
{
player.sendDebug( "Registered debug commands:" );
2017-10-21 23:41:09 +11:00
for ( auto cmd : m_commandMap )
{
if ( player.getGmRank( ) >= cmd.second->m_gmLevel )
2017-10-21 23:41:09 +11:00
{
player.sendDebug( " - " + cmd.first + " - " + cmd.second->getHelpText( ) );
2017-10-21 23:41:09 +11:00
}
}
}
void Core::DebugCommandHandler::set( char * data, Entity::Player& player, boost::shared_ptr< 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( player.getId() ) + "] " +
2017-08-08 13:53:47 +02:00
"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 ) )
{
player.sendUrgent( "Syntaxerror." );
2017-08-08 13:53:47 +02:00
return;
}
if( subCommand == "pos" )
player.setPosition( static_cast< float >( posX ),
static_cast< float >( posY ),
static_cast< float >( posZ ) );
2017-08-08 13:53:47 +02:00
else
player.setPosition( player.getPos().x + static_cast< float >( posX ),
player.getPos().y + static_cast< float >( posY ),
player.getPos().z + static_cast< float >( posZ ) );
2017-08-08 13:53:47 +02:00
2017-11-21 18:43:09 +01:00
Network::Packets::ZoneChannelPacket< Network::Packets::Server::FFXIVIpcActorSetPos >
setActorPosPacket( player.getId() );
setActorPosPacket.data().x = player.getPos().x;
setActorPosPacket.data().y = player.getPos().y;
setActorPosPacket.data().z = player.getPos().z;
player.queuePacket( setActorPosPacket );
2017-08-08 13:53:47 +02:00
}
else if( ( subCommand == "tele" ) && ( params != "" ) )
{
int32_t aetheryteId;
sscanf( params.c_str(), "%i", &aetheryteId );
player.teleport( aetheryteId );
2017-08-08 13:53:47 +02:00
}
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::ZoneChannelPacket< Network::Packets::Server::FFXIVIpcDiscovery > discoveryPacket( player.getId() );
2017-08-08 13:53:47 +02:00
discoveryPacket.data().map_id = map_id;
discoveryPacket.data().map_part_id = discover_id;
player.queuePacket( discoveryPacket );
2017-08-08 13:53:47 +02:00
}
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 ) + "')";
2017-10-26 15:22:58 +02:00
g_charaDb.execute( query1 );
g_charaDb.execute( query2 );
2017-08-08 13:53:47 +02:00
}
else if( subCommand == "discovery_reset" )
{
player.resetDiscovery();
player.queuePacket( Network::Packets::Server::InitUIPacket( player ) );
2017-08-08 13:53:47 +02:00
}
2017-08-09 14:38:46 +02:00
else if( subCommand == "classjob" )
{
int32_t id;
sscanf( params.c_str(), "%d", &id );
if( player.getLevelForClass( static_cast< Common::ClassJob > ( id ) ) == 0 )
2017-08-09 14:38:46 +02:00
{
player.setLevelForClass( 1, static_cast< Common::ClassJob > ( id ) );
player.setClassJob( static_cast< Common::ClassJob > ( id ) );
2017-08-09 14:38:46 +02:00
}
else
player.setClassJob( static_cast< Common::ClassJob > ( id ) );
2017-08-09 14:38:46 +02:00
}
else if ( subCommand == "cfpenalty" )
{
int32_t minutes;
sscanf( params.c_str(), "%d", &minutes );
player.setCFPenaltyMinutes( minutes );
}
else if ( subCommand == "eorzeatime" )
{
uint64_t timestamp;
sscanf( params.c_str(), "%" SCNu64, &timestamp );
player.setEorzeaTimeOffset( timestamp );
player.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
player.setModelForSlot( static_cast< Inventory::EquipSlot >( slot ), val );
player.sendModel();
player.sendDebug( "Model updated" );
2017-10-02 16:00:26 +02:00
}
2017-10-18 18:06:25 +02:00
else if ( subCommand == "mount" )
{
int32_t id;
sscanf( params.c_str(), "%d", &id );
player.dismount();
player.mount( id );
2017-10-18 18:06:25 +02:00
}
2017-10-02 16:00:26 +02:00
else
{
player.sendUrgent( subCommand + " is not a valid SET command." );
2017-10-02 16:00:26 +02:00
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::add( char * data, Entity::Player& player, boost::shared_ptr< 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( player.getId() ) + "] " +
2017-08-08 13:53:47 +02:00
"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, player.getAsPlayer(), player.getAsPlayer(), duration, 3000 ) );
effect->setParam( param );
player.addStatusEffect( effect );
2017-08-08 13:53:47 +02:00
}
else if( subCommand == "title" )
2017-10-09 00:31:31 -03:00
{
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
player.addTitle( titleId );
player.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, player.getPos() ) );
2017-08-08 13:53:47 +02:00
auto pZone = player.getCurrentZone();
2017-08-08 13:53:47 +02:00
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, player.getId(), player.getId() ) );
player.queuePacket( pPe );
2017-08-08 13:53:47 +02:00
}
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 );
player.sendNotice( "Injecting ACTOR_CONTROL " + std::to_string( opcode ) );
Network::Packets::ZoneChannelPacket< Network::Packets::Server::FFXIVIpcActorControl143 > actorControl( playerId, player.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;
player.queuePacket( actorControl );
2017-08-08 13:53:47 +02:00
/*sscanf(params.c_str(), "%x %x %x %x %x %x %x", &opcode, &param1, &param2, &param3, &param4, &param5, &param6, &playerId);
Network::Packets::Server::ServerNoticePacket noticePacket( player, "Injecting ACTOR_CONTROL " + std::to_string( opcode ) );
2017-08-08 13:53:47 +02:00
player.queuePacket( noticePacket );
2017-08-08 13:53:47 +02:00
Network::Packets::Server::ActorControlPacket143 controlPacket( player, opcode,
param1, param2, param3, param4, param5, param6, playerId );
player.queuePacket( controlPacket );*/
2017-08-08 13:53:47 +02:00
}
2017-10-02 16:00:26 +02:00
else
{
player.sendUrgent( subCommand + " is not a valid ADD command." );
2017-10-02 16:00:26 +02:00
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::get( char * data, Entity::Player& player, boost::shared_ptr< 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( player.getId() ) + "] " +
2017-08-08 13:53:47 +02:00
"subCommand " + subCommand + " params: " + params );
if( ( subCommand == "pos" ) )
{
int16_t map_id = g_exdData.m_zoneInfoMap[player.getCurrentZone()->getId()].map_id;
2017-08-08 13:53:47 +02:00
player.sendNotice( "Pos:\n" +
std::to_string( player.getPos().x ) + "\n" +
std::to_string( player.getPos().y ) + "\n" +
std::to_string( player.getPos().z ) + "\n" +
std::to_string( player.getRotation() ) + "\nMapId: " +
std::to_string( map_id ) + "\nZoneID: " +
std::to_string( player.getCurrentZone()->getId() ) + "\n" );
2017-08-08 13:53:47 +02:00
}
2017-10-02 16:00:26 +02:00
else
{
player.sendUrgent( subCommand + " is not a valid GET command." );
2017-10-02 16:00:26 +02:00
}
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::injectPacket( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command )
2017-08-08 13:53:47 +02:00
{
auto pSession = g_serverZone.getSession( player.getId() );
2017-08-08 13:53:47 +02:00
if( pSession )
pSession->getZoneConnection()->injectPacket( data + 7, player );
2017-08-08 13:53:47 +02:00
}
void Core::DebugCommandHandler::injectChatPacket( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command )
{
auto pSession = g_serverZone.getSession( player.getId() );
if( pSession )
pSession->getChatConnection()->injectPacket( data + 8, player );
}
void Core::DebugCommandHandler::nudge( char * data, Entity::Player& player, 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 = player.getPos();
2017-08-08 13:53:47 +02:00
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;
player.sendNotice( "nudge: Placing up " + std::to_string( offset ) + " yalms" );
2017-08-08 13:53:47 +02:00
}
else if( direction[0] == 'd' || direction[0] == '-' )
{
pos.y -= offset;
player.sendNotice( "nudge: Placing down " + std::to_string( offset ) + " yalms" );
2017-08-08 13:53:47 +02:00
}
else
{
float angle = player.getRotation() + ( PI / 2 );
2017-08-08 13:53:47 +02:00
pos.x -= offset * cos( angle );
pos.z += offset * sin( angle );
player.sendNotice( "nudge: Placing forward " + std::to_string( offset ) + " yalms" );
2017-08-08 13:53:47 +02:00
}
if( offset != 0 )
{
2017-11-21 18:43:09 +01:00
Network::Packets::ZoneChannelPacket< Network::Packets::Server::FFXIVIpcActorSetPos >
setActorPosPacket( player.getId() );
setActorPosPacket.data().x = player.getPos().x;
setActorPosPacket.data().y = player.getPos().y;
setActorPosPacket.data().z = player.getPos().z;
setActorPosPacket.data().r16 = Math::Util::floatToUInt16Rot( player.getRotation() );
player.queuePacket( setActorPosPacket );
2017-08-08 13:53:47 +02:00
}
}
2017-09-14 17:07:58 +02:00
void Core::DebugCommandHandler::serverInfo( char * data, Entity::Player& player, boost::shared_ptr< DebugCommand > command )
2017-09-14 17:07:58 +02:00
{
player.sendDebug( "SapphireServer " + Version::VERSION + "\nRev: " + Version::GIT_HASH );
player.sendDebug( "Compiled: " __DATE__ " " __TIME__ );
player.sendDebug( "Sessions: " + std::to_string( g_serverZone.getSessionCount() ) );
2017-09-14 17:07:58 +02:00
}
2017-10-21 23:41:09 +11:00
void Core::DebugCommandHandler::unlockCharacter( char* data, Entity::Player& player, boost::shared_ptr< DebugCommand > command )
2017-10-21 23:41:09 +11:00
{
player.unlock();
}
void Core::DebugCommandHandler::script( char* data, Entity::Player &player, boost::shared_ptr< DebugCommand > command )
{
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;
// todo: fix params so it's empty if there's no params
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( player.getId() ) + "] " +
"subCommand " + subCommand + " params: " + params );
if( subCommand == "unload" )
{
if ( subCommand == params )
player.sendDebug( "Command failed: requires name of script" );
else
if( g_scriptMgr.getNativeScriptHandler().unloadScript( params ) )
player.sendDebug( "Unloaded script successfully." );
else
player.sendDebug( "Failed to unload script: " + params );
}
else if( subCommand == "find" || subCommand == "f" )
{
if( subCommand == params )
player.sendDebug( "Because reasons of filling chat with nonsense, please enter a search term" );
else
{
2017-12-12 17:29:31 +11:00
std::set< Core::Scripting::ScriptInfo* > scripts;
g_scriptMgr.getNativeScriptHandler().findScripts( scripts, params );
if( scripts.size() > 0 )
{
player.sendDebug( "Found " + std::to_string( scripts.size() ) + " scripts" );
for( auto it = scripts.begin(); it != scripts.end(); ++it )
{
auto script = *it;
player.sendDebug( " - '" + script->script_name + "' loaded at @ 0x" +
boost::str( boost::format( "%|X|" ) % script->handle ) +
", script ptr: 0x" + boost::str( boost::format( "%|X|" ) % script->script ) );
}
}
else
player.sendDebug( "No scripts found with search term: " + params );
}
}
else if( subCommand == "load" || subCommand == "l" )
{
if( subCommand == params )
player.sendDebug( "Command failed: requires relative path to script" );
else
{
if ( g_scriptMgr.getNativeScriptHandler().loadScript( params ) )
player.sendDebug( "Loaded '" + params + "' successfully" );
else
player.sendDebug( "Failed to load '" + params + "'" );
}
}
else if( subCommand == "build" || subCommand == "b" )
{
if( subCommand == params )
player.sendDebug( "Command failed: requires name of cmake target" );
else
{
}
}
else
{
player.sendDebug( "Unknown script subcommand: " + subCommand );
}
2017-10-21 23:41:09 +11:00
}