From 3a2272594e7665f2ce2ded03fceb24fb1464491e Mon Sep 17 00:00:00 2001 From: NotAdam Date: Sat, 29 Feb 2020 22:30:10 +1100 Subject: [PATCH] start of new service locator impl --- src/api/main.cpp | 2 +- src/common/Service.h | 114 ++++++++++++++++++ src/dbm/main.cpp | 2 +- src/lobby/mainLobbyServer.cpp | 2 +- src/tools/event_object_parser/main.cpp | 2 +- src/tools/exd_struct_test/main.cpp | 2 +- src/tools/questbattle_bruteforce/main.cpp | 2 +- src/world/Actor/Player.cpp | 17 +-- src/world/Actor/PlayerEvent.cpp | 6 +- src/world/Manager/DebugCommandMgr.cpp | 31 +++-- src/world/Manager/HousingMgr.cpp | 10 +- src/world/Manager/TerritoryMgr.cpp | 4 +- src/world/Navi/NaviProvider.cpp | 4 +- src/world/Network/GameConnection.cpp | 9 +- .../Network/Handlers/ClientTriggerHandler.cpp | 4 +- .../Network/Handlers/GMCommandHandlers.cpp | 5 +- src/world/Network/Handlers/PacketHandlers.cpp | 12 +- src/world/Script/NativeScriptMgr.cpp | 6 +- src/world/Script/ScriptMgr.cpp | 11 +- src/world/ServerMgr.cpp | 29 ++--- src/world/Territory/Territory.cpp | 16 +-- src/world/mainGameServer.cpp | 6 +- 22 files changed, 223 insertions(+), 73 deletions(-) create mode 100644 src/common/Service.h diff --git a/src/api/main.cpp b/src/api/main.cpp index 32d4b794..011fb509 100644 --- a/src/api/main.cpp +++ b/src/api/main.cpp @@ -33,7 +33,7 @@ #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; Sapphire::Db::DbWorkerPool< Sapphire::Db::ZoneDbConnection > g_charaDb; Sapphire::Data::ExdDataGenerated g_exdDataGen; diff --git a/src/common/Service.h b/src/common/Service.h new file mode 100644 index 00000000..bcf3dc2f --- /dev/null +++ b/src/common/Service.h @@ -0,0 +1,114 @@ +#ifndef SAPPHIRE_SERVICE_H +#define SAPPHIRE_SERVICE_H + +#include +#include + +// stolen from: https://github.com/skypjack/entt/blob/master/src/entt/locator/locator.hpp + +namespace Sapphire::Common +{ + /** + * @brief Service locator, nothing more. + * + * A service locator can be used to do what it promises: locate services.
+ * Usually service locators are tightly bound to the services they expose and + * thus it's hard to define a general purpose class to do that. This template + * based implementation tries to fill the gap and to get rid of the burden of + * defining a different specific locator for each application. + * + * @tparam SvcType Type of service managed by the locator. + */ + template< typename SvcType > + struct Service + { + /*! @brief Type of service offered. */ + using ServiceType = SvcType; + + /*! @brief Default constructor, deleted on purpose. */ + Service() = delete; + + /*! @brief Default destructor, deleted on purpose. */ + ~Service() = delete; + + /** + * @brief Tests if a valid service implementation is set. + * @return True if the service is set, false otherwise. + */ + static bool empty() noexcept + { + return !static_cast< bool >( service ); + } + + /** + * @brief Returns a weak pointer to a service implementation, if any. + * + * Clients of a service shouldn't retain references to it. The recommended + * way is to retrieve the service implementation currently set each and + * every time the need of using it arises. Otherwise users can incur in + * unexpected behaviors. + * + * @return A reference to the service implementation currently set, if any. + */ + static std::weak_ptr< SvcType > get() noexcept + { + return service; + } + + /** + * @brief Returns a weak reference to a service implementation, if any. + * + * Clients of a service shouldn't retain references to it. The recommended + * way is to retrieve the service implementation currently set each and + * every time the need of using it arises. Otherwise users can incur in + * unexpected behaviors. + * + * @warning + * In case no service implementation has been set, a call to this function + * results in undefined behavior. + * + * @return A reference to the service implementation currently set, if any. + */ + static SvcType& ref() noexcept + { + return *service; + } + + /** + * @brief Sets or replaces a service. + * @tparam Impl Type of the new service to use. + * @tparam Args Types of arguments to use to construct the service. + * @param args Parameters to use to construct the service. + */ + template< typename Impl = SvcType, typename... Args > + static void set( Args&& ... args ) + { + service = std::make_shared< Impl >( std::forward< Args >( args )... ); + } + + /** + * @brief Sets or replaces a service. + * @param ptr Service to use to replace the current one. + */ + static void set( std::shared_ptr< SvcType > ptr ) + { + assert( static_cast< bool >( ptr ) ); + service = std::move( ptr ); + } + + /** + * @brief Resets a service. + * + * The service is no longer valid after a reset. + */ + static void reset() + { + service.reset(); + } + + private: + inline static std::shared_ptr< SvcType > service = nullptr; + }; +} + +#endif //SAPPHIRE_SERVICE_H diff --git a/src/dbm/main.cpp b/src/dbm/main.cpp index 990f500d..ed6c09d4 100644 --- a/src/dbm/main.cpp +++ b/src/dbm/main.cpp @@ -8,7 +8,7 @@ #include #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; namespace fs = std::filesystem; diff --git a/src/lobby/mainLobbyServer.cpp b/src/lobby/mainLobbyServer.cpp index eb3c7653..0b16ff26 100644 --- a/src/lobby/mainLobbyServer.cpp +++ b/src/lobby/mainLobbyServer.cpp @@ -2,7 +2,7 @@ #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; Sapphire::Lobby::ServerLobby g_serverLobby( "lobby.ini" ); diff --git a/src/tools/event_object_parser/main.cpp b/src/tools/event_object_parser/main.cpp index 8d86dbb7..f1529fcb 100644 --- a/src/tools/event_object_parser/main.cpp +++ b/src/tools/event_object_parser/main.cpp @@ -28,7 +28,7 @@ #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; Sapphire::Data::ExdDataGenerated g_exdData; using namespace Sapphire; diff --git a/src/tools/exd_struct_test/main.cpp b/src/tools/exd_struct_test/main.cpp index e2beba0e..cb997b6f 100644 --- a/src/tools/exd_struct_test/main.cpp +++ b/src/tools/exd_struct_test/main.cpp @@ -24,7 +24,7 @@ #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; Sapphire::Data::ExdDataGenerated g_exdData; xiv::dat::GameData* gameData = nullptr; diff --git a/src/tools/questbattle_bruteforce/main.cpp b/src/tools/questbattle_bruteforce/main.cpp index 7aa34fbd..d8c1c6d4 100644 --- a/src/tools/questbattle_bruteforce/main.cpp +++ b/src/tools/questbattle_bruteforce/main.cpp @@ -20,7 +20,7 @@ #include -Sapphire::Common::Util::CrashHandler crashHandler; +[[maybe_unused]] Sapphire::Common::Util::CrashHandler crashHandler; Sapphire::Data::ExdDataGenerated g_exdData; xiv::dat::GameData* gameData = nullptr; diff --git a/src/world/Actor/Player.cpp b/src/world/Actor/Player.cpp index 8677d001..8565e7a4 100644 --- a/src/world/Actor/Player.cpp +++ b/src/world/Actor/Player.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "Session.h" #include "Player.h" @@ -114,8 +115,8 @@ Sapphire::Entity::Player::~Player() void Sapphire::Entity::Player::injectPacket( const std::string& path ) { - auto pServerZone = m_pFw->get< World::ServerMgr >(); - auto session = pServerZone->getSession( getId() ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto session = serverMgr.getSession( getId() ); if( session ) session->getZoneConnection()->injectPacket( path, *this ); } @@ -1257,8 +1258,8 @@ const uint8_t* Sapphire::Entity::Player::getGcRankArray() const void Sapphire::Entity::Player::queuePacket( Network::Packets::FFXIVPacketBasePtr pPacket ) { - auto pServerZone = m_pFw->get< World::ServerMgr >(); - auto pSession = pServerZone->getSession( m_id ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto pSession = serverMgr.getSession( m_id ); if( !pSession ) return; @@ -1272,8 +1273,8 @@ void Sapphire::Entity::Player::queuePacket( Network::Packets::FFXIVPacketBasePtr void Sapphire::Entity::Player::queueChatPacket( Network::Packets::FFXIVPacketBasePtr pPacket ) { - auto pServerZone = m_pFw->get< World::ServerMgr >(); - auto pSession = pServerZone->getSession( m_id ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto pSession = serverMgr.getSession( m_id ); if( !pSession ) return; @@ -1689,8 +1690,8 @@ void Sapphire::Entity::Player::sendZonePackets() if( isLogin() ) { //Update player map in servermgr - in case player name has been changed - auto pServerMgr = m_pFw->get< World::ServerMgr >(); - pServerMgr->updatePlayerName( getId(), getName() ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + serverMgr.updatePlayerName( getId(), getName() ); } getCurrentTerritory()->onBeforePlayerZoneIn( *this ); diff --git a/src/world/Actor/PlayerEvent.cpp b/src/world/Actor/PlayerEvent.cpp index 08f830e4..e8d79f5a 100644 --- a/src/world/Actor/PlayerEvent.cpp +++ b/src/world/Actor/PlayerEvent.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "Network/GameConnection.h" #include "Network/PacketWrappers/ActorControlPacket.h" @@ -341,8 +342,9 @@ void Sapphire::Entity::Player::eventItemActionStart( uint32_t eventId, void Sapphire::Entity::Player::onLogin() { - auto pServerMgr = m_pFw->get< Sapphire::World::ServerMgr >(); - auto motd = pServerMgr->getConfig().motd; + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + + auto motd = serverMgr.getConfig().motd; std::istringstream ss( motd ); std::string msg; diff --git a/src/world/Manager/DebugCommandMgr.cpp b/src/world/Manager/DebugCommandMgr.cpp index bba8c523..98a7480e 100644 --- a/src/world/Manager/DebugCommandMgr.cpp +++ b/src/world/Manager/DebugCommandMgr.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "DebugCommand/DebugCommand.h" #include "DebugCommandMgr.h" @@ -372,6 +373,8 @@ void Sapphire::World::Manager::DebugCommandMgr::set( char* data, Entity::Player& void Sapphire::World::Manager::DebugCommandMgr::add( char* data, Entity::Player& player, std::shared_ptr< DebugCommand > command ) { + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + std::string subCommand; std::string params = ""; @@ -418,9 +421,7 @@ void Sapphire::World::Manager::DebugCommandMgr::add( char* data, Entity::Player& } else if( subCommand == "bnpc" ) { - auto serverZone = framework()->get< World::ServerMgr >(); - - auto bNpcTemplate = serverZone->getBNpcTemplate( params ); + auto bNpcTemplate = serverMgr.getBNpcTemplate( params ); if( !bNpcTemplate ) { @@ -577,8 +578,9 @@ void Sapphire::World::Manager::DebugCommandMgr::injectPacket( char* data, Entity::Player& player, std::shared_ptr< DebugCommand > command ) { - auto pServerZone = framework()->get< World::ServerMgr >(); - auto pSession = pServerZone->getSession( player.getId() ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + + auto pSession = serverMgr.getSession( player.getId() ); if( pSession ) pSession->getZoneConnection()->injectPacket( data + 7, player ); } @@ -586,8 +588,9 @@ Sapphire::World::Manager::DebugCommandMgr::injectPacket( char* data, Entity::Pla void Sapphire::World::Manager::DebugCommandMgr::injectChatPacket( char* data, Entity::Player& player, std::shared_ptr< DebugCommand > command ) { - auto pServerZone = framework()->get< World::ServerMgr >(); - auto pSession = pServerZone->getSession( player.getId() ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + + auto pSession = serverMgr.getSession( player.getId() ); if( pSession ) pSession->getChatConnection()->injectPacket( data + 8, player ); } @@ -595,7 +598,8 @@ void Sapphire::World::Manager::DebugCommandMgr::injectChatPacket( char* data, En void Sapphire::World::Manager::DebugCommandMgr::replay( char* data, Entity::Player& player, std::shared_ptr< DebugCommand > command ) { - auto pServerZone = framework()->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + std::string subCommand; std::string params = ""; @@ -620,19 +624,19 @@ void Sapphire::World::Manager::DebugCommandMgr::replay( char* data, Entity::Play if( subCommand == "start" ) { - auto pSession = pServerZone->getSession( player.getId() ); + auto pSession = serverMgr.getSession( player.getId() ); if( pSession ) pSession->startReplay( params ); } else if( subCommand == "stop" ) { - auto pSession = pServerZone->getSession( player.getId() ); + auto pSession = serverMgr.getSession( player.getId() ); if( pSession ) pSession->stopReplay(); } else if( subCommand == "info" ) { - auto pSession = pServerZone->getSession( player.getId() ); + auto pSession = serverMgr.getSession( player.getId() ); if( pSession ) pSession->sendReplayInfo(); } @@ -695,10 +699,11 @@ void Sapphire::World::Manager::DebugCommandMgr::serverInfo( char* data, Entity::Player& player, std::shared_ptr< DebugCommand > command ) { - auto pServerZone = framework()->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + player.sendDebug( "SapphireZone {0} \nRev: {1}", Version::VERSION, Version::GIT_HASH ); player.sendDebug( "Compiled: " __DATE__ " " __TIME__ ); - player.sendDebug( "Sessions: {0}", pServerZone->getSessionCount() ); + player.sendDebug( "Sessions: {0}", serverMgr.getSessionCount() ); } void Sapphire::World::Manager::DebugCommandMgr::script( char* data, Entity::Player& player, diff --git a/src/world/Manager/HousingMgr.cpp b/src/world/Manager/HousingMgr.cpp index 43d697ee..ff3e0b39 100644 --- a/src/world/Manager/HousingMgr.cpp +++ b/src/world/Manager/HousingMgr.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "Actor/Player.h" #include "Actor/EventObject.h" @@ -310,6 +311,8 @@ Sapphire::LandPtr Sapphire::World::Manager::HousingMgr::getLandByOwnerId( uint32 void Sapphire::World::Manager::HousingMgr::sendLandSignOwned( Entity::Player& player, const Common::LandIdent ident ) { + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + player.setActiveLand( static_cast< uint8_t >( ident.landId ), static_cast< uint8_t >( ident.wardNum ) ); auto landSetId = toLandSetId( static_cast< uint16_t >( ident.territoryTypeId ), static_cast< uint8_t >( ident.wardNum ) ); @@ -336,7 +339,8 @@ void Sapphire::World::Manager::HousingMgr::sendLandSignOwned( Entity::Player& pl } uint32_t playerId = static_cast< uint32_t >( land->getOwnerId() ); - std::string playerName = framework()->get< World::ServerMgr >()->getPlayerNameFromDb( playerId ); + + std::string playerName = serverMgr.getPlayerNameFromDb( playerId ); memcpy( &landInfoSignPacket->data().ownerName, playerName.c_str(), playerName.size() ); @@ -463,6 +467,8 @@ bool Sapphire::World::Manager::HousingMgr::relinquishLand( Entity::Player& playe void Sapphire::World::Manager::HousingMgr::sendWardLandInfo( Entity::Player& player, uint8_t wardId, uint16_t territoryTypeId ) { + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto landSetId = toLandSetId( territoryTypeId, wardId ); auto hZone = getHousingZoneByLandSetId( landSetId ); @@ -509,7 +515,7 @@ void Sapphire::World::Manager::HousingMgr::sendWardLandInfo( Entity::Player& pla entry.infoFlags |= Common::WardlandFlags::IsEstateOwned; auto owner = land->getOwnerId(); - auto playerName = framework()->get< World::ServerMgr >()->getPlayerNameFromDb( static_cast< uint32_t >( owner ) ); + auto playerName = serverMgr.getPlayerNameFromDb( static_cast< uint32_t >( owner ) ); memcpy( &entry.estateOwnerName, playerName.c_str(), playerName.size() ); break; diff --git a/src/world/Manager/TerritoryMgr.cpp b/src/world/Manager/TerritoryMgr.cpp index 916f94a6..25d7458b 100644 --- a/src/world/Manager/TerritoryMgr.cpp +++ b/src/world/Manager/TerritoryMgr.cpp @@ -5,6 +5,7 @@ #include "ServerMgr.h" #include +#include #include "Actor/Player.h" @@ -72,7 +73,8 @@ bool Sapphire::World::Manager::TerritoryMgr::init() return false; } - auto& cfg = framework()->get< World::ServerMgr >()->getConfig(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto& cfg = serverMgr.getConfig(); m_inRangeDistance = cfg.network.inRangeDistance; diff --git a/src/world/Navi/NaviProvider.cpp b/src/world/Navi/NaviProvider.cpp index fc05f9bf..15569ba0 100644 --- a/src/world/Navi/NaviProvider.cpp +++ b/src/world/Navi/NaviProvider.cpp @@ -17,6 +17,7 @@ #include #include #include +#include Sapphire::World::Navi::NaviProvider::NaviProvider( const std::string& internalName, FrameworkPtr pFw ) : m_naviMesh( nullptr ), @@ -32,7 +33,8 @@ Sapphire::World::Navi::NaviProvider::NaviProvider( const std::string& internalNa bool Sapphire::World::Navi::NaviProvider::init() { - auto& cfg = m_pFw->get< Sapphire::World::ServerMgr >()->getConfig(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto& cfg = serverMgr.getConfig(); auto meshesFolder = std::filesystem::path( cfg.navigation.meshPath ); auto meshFolder = meshesFolder / std::filesystem::path( m_internalName ); diff --git a/src/world/Network/GameConnection.cpp b/src/world/Network/GameConnection.cpp index fd8fa095..68ca997a 100644 --- a/src/world/Network/GameConnection.cpp +++ b/src/world/Network/GameConnection.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Territory/Territory.h" @@ -380,7 +381,7 @@ void Sapphire::Network::GameConnection::injectPacket( const std::string& packetp void Sapphire::Network::GameConnection::handlePackets( const Sapphire::Network::Packets::FFXIVARR_PACKET_HEADER& ipcHeader, const std::vector< Sapphire::Network::Packets::FFXIVARR_PACKET_RAW >& packetData ) { - auto pServerZone = m_pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); // if a session is set, update the last time it recieved a game packet if( m_pSession ) @@ -397,18 +398,18 @@ void Sapphire::Network::GameConnection::handlePackets( const Sapphire::Network:: auto pCon = std::static_pointer_cast< GameConnection, Connection >( shared_from_this() ); // try to retrieve the session for this id - auto session = pServerZone->getSession( playerId ); + auto session = serverMgr.getSession( playerId ); if( !session ) { Logger::info( "[{0}] Session not registered, creating", id ); // return; - if( !pServerZone->createSession( playerId ) ) + if( !serverMgr.createSession( playerId ) ) { disconnect(); return; } - session = pServerZone->getSession( playerId ); + session = serverMgr.getSession( playerId ); } //TODO: Catch more things in lobby and send real errors else if( !session->isValid() || ( session->getPlayer() && session->getPlayer()->getLastPing() != 0 ) ) diff --git a/src/world/Network/Handlers/ClientTriggerHandler.cpp b/src/world/Network/Handlers/ClientTriggerHandler.cpp index b6decf1b..374a5bf3 100644 --- a/src/world/Network/Handlers/ClientTriggerHandler.cpp +++ b/src/world/Network/Handlers/ClientTriggerHandler.cpp @@ -34,6 +34,7 @@ #include "Forwards.h" #include "Framework.h" #include +#include using namespace Sapphire::Common; using namespace Sapphire::Network::Packets; @@ -45,7 +46,8 @@ void examineHandler( Sapphire::FrameworkPtr pFw, Sapphire::Entity::Player& playe { using namespace Sapphire; - auto pSession = pFw->get< World::ServerMgr >()->getSession( targetId ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto pSession = serverMgr.getSession( targetId ); if( pSession ) { auto pTarget = pSession->getPlayer(); diff --git a/src/world/Network/Handlers/GMCommandHandlers.cpp b/src/world/Network/Handlers/GMCommandHandlers.cpp index 77bd4380..d01e3a0e 100644 --- a/src/world/Network/Handlers/GMCommandHandlers.cpp +++ b/src/world/Network/Handlers/GMCommandHandlers.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "Network/GameConnection.h" @@ -594,7 +595,7 @@ void Sapphire::Network::GameConnection::gm2Handler( FrameworkPtr pFw, if( player.getGmRank() <= 0 ) return; - auto pServerZone = pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); const auto packet = ZoneChannelPacket< Client::FFXIVIpcGmCommand2 >( inPacket ); @@ -608,7 +609,7 @@ void Sapphire::Network::GameConnection::gm2Handler( FrameworkPtr pFw, Logger::debug( "{0} used GM2 commandId: {1}, params: {2}, {3}, {4}, {5}, target: {6}", player.getName(), commandId, param1, param2, param3, param4, target ); - auto targetSession = pServerZone->getSession( target ); + auto targetSession = serverMgr.getSession( target ); Sapphire::Entity::CharaPtr targetActor; if( targetSession != nullptr ) diff --git a/src/world/Network/Handlers/PacketHandlers.cpp b/src/world/Network/Handlers/PacketHandlers.cpp index 5e23045d..c2373960 100644 --- a/src/world/Network/Handlers/PacketHandlers.cpp +++ b/src/world/Network/Handlers/PacketHandlers.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "Network/GameConnection.h" @@ -120,7 +121,8 @@ void Sapphire::Network::GameConnection::reqExamineSearchCommentHandler( Framewor { auto targetId = *reinterpret_cast< const uint32_t* >( &inPacket.data[ 0x10 ] ); - auto pSession = pFw->get< World::ServerMgr >()->getSession( targetId ); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto pSession = serverMgr.getSession( targetId ); Logger::debug( "reqExamineSearchCommentHandler: {0}", targetId ); @@ -148,7 +150,9 @@ void Sapphire::Network::GameConnection::reqExamineFcInfo( FrameworkPtr pFw, { auto targetId = *reinterpret_cast< const uint32_t* >( &inPacket.data[ 0x18 ] ); - auto pSession = pFw->get< World::ServerMgr >()->getSession( targetId ); + + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto pSession = serverMgr.getSession( targetId ); Logger::debug( "reqExamineFcInfo: {0}", targetId ); @@ -566,9 +570,9 @@ void Sapphire::Network::GameConnection::tellHandler( FrameworkPtr pFw, { const auto packet = ZoneChannelPacket< Client::FFXIVIpcTellHandler >( inPacket ); - auto pZoneServer = pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); - auto pSession = pZoneServer->getSession( packet.data().targetPCName ); + auto pSession = serverMgr.getSession( packet.data().targetPCName ); if( !pSession ) { diff --git a/src/world/Script/NativeScriptMgr.cpp b/src/world/Script/NativeScriptMgr.cpp index 2cf8fa37..b8392351 100644 --- a/src/world/Script/NativeScriptMgr.cpp +++ b/src/world/Script/NativeScriptMgr.cpp @@ -1,6 +1,7 @@ #include "NativeScriptMgr.h" #include +#include #include "ServerMgr.h" #include "Framework.h" @@ -124,8 +125,9 @@ namespace Sapphire::Scripting NativeScriptMgr::NativeScriptMgr( FrameworkPtr pFw ) : World::Manager::BaseManager( pFw ) { - auto pServerMgr = framework()->get< Sapphire::World::ServerMgr >(); - m_loader.setCachePath( pServerMgr->getConfig().scripts.cachePath ); + auto& serverMgr = Common::Service< Sapphire::World::ServerMgr >::ref(); + + m_loader.setCachePath( serverMgr.getConfig().scripts.cachePath ); } diff --git a/src/world/Script/ScriptMgr.cpp b/src/world/Script/ScriptMgr.cpp index fc0a7736..0593cda1 100644 --- a/src/world/Script/ScriptMgr.cpp +++ b/src/world/Script/ScriptMgr.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "Territory/Territory.h" #include "Territory/InstanceContent.h" @@ -49,9 +50,9 @@ void Sapphire::Scripting::ScriptMgr::update() bool Sapphire::Scripting::ScriptMgr::init() { std::set< std::string > files; - auto pServerMgr = framework()->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); - auto status = loadDir( pServerMgr->getConfig().scripts.path, files, m_nativeScriptMgr->getModuleExtension() ); + auto status = loadDir( serverMgr.getConfig().scripts.path, files, m_nativeScriptMgr->getModuleExtension() ); if( !status ) { @@ -81,12 +82,12 @@ bool Sapphire::Scripting::ScriptMgr::init() void Sapphire::Scripting::ScriptMgr::watchDirectories() { - auto pServerMgr = framework()->get< World::ServerMgr >(); - auto shouldWatch = pServerMgr->getConfig().scripts.hotSwap; + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + auto shouldWatch = serverMgr.getConfig().scripts.hotSwap; if( !shouldWatch ) return; - Watchdog::watchMany( pServerMgr->getConfig().scripts.path + "*" + + Watchdog::watchMany( serverMgr.getConfig().scripts.path + "*" + m_nativeScriptMgr->getModuleExtension(), [ this ]( const std::vector< ci::fs::path >& paths ) { diff --git a/src/world/ServerMgr.cpp b/src/world/ServerMgr.cpp index 460b8c98..5b7159ee 100644 --- a/src/world/ServerMgr.cpp +++ b/src/world/ServerMgr.cpp @@ -28,6 +28,7 @@ #include #include +#include #include "Manager/LinkshellMgr.h" #include "Manager/TerritoryMgr.h" #include "Manager/HousingMgr.h" @@ -66,20 +67,20 @@ size_t Sapphire::World::ServerMgr::getSessionCount() const bool Sapphire::World::ServerMgr::loadSettings( int32_t argc, char* argv[] ) { - auto pConfig = framework()->get< Common::ConfigMgr >(); + auto& configMgr = Common::Service< Common::ConfigMgr >::ref(); Logger::info( "Loading config {0}", m_configName ); bool failedLoad = false; // load global cfg first - if( !pConfig->loadGlobalConfig( m_config.global ) ) + if( !configMgr.loadGlobalConfig( m_config.global ) ) { Logger::fatal( "Error loading config global.ini, copying default..." ); failedLoad = true; } - if( !pConfig->loadConfig( m_configName ) ) + if( !configMgr.loadConfig( m_configName ) ) { Logger::fatal( "Error loading config {0}", m_configName ); failedLoad = true; @@ -93,20 +94,20 @@ bool Sapphire::World::ServerMgr::loadSettings( int32_t argc, char* argv[] ) } // load world specific config - m_config.scripts.hotSwap = pConfig->getValue( "Scripts", "HotSwap", true ); - m_config.scripts.path = pConfig->getValue< std::string >( "Scripts", "Path", "./compiledscripts/" ); - m_config.scripts.cachePath = pConfig->getValue< std::string >( "Scripts", "CachePath", "./cache/" ); + m_config.scripts.hotSwap = configMgr.getValue( "Scripts", "HotSwap", true ); + m_config.scripts.path = configMgr.getValue< std::string >( "Scripts", "Path", "./compiledscripts/" ); + m_config.scripts.cachePath = configMgr.getValue< std::string >( "Scripts", "CachePath", "./cache/" ); - m_config.navigation.meshPath = pConfig->getValue< std::string >( "Navigation", "MeshPath", "navi" ); + m_config.navigation.meshPath = configMgr.getValue< std::string >( "Navigation", "MeshPath", "navi" ); - m_config.network.disconnectTimeout = pConfig->getValue< uint16_t >( "Network", "DisconnectTimeout", 20 ); - m_config.network.listenIp = pConfig->getValue< std::string >( "Network", "ListenIp", "0.0.0.0" ); - m_config.network.listenPort = pConfig->getValue< uint16_t >( "Network", "ListenPort", 54992 ); - m_config.network.inRangeDistance = pConfig->getValue< float >( "Network", "InRangeDistance", 80.f ); + m_config.network.disconnectTimeout = configMgr.getValue< uint16_t >( "Network", "DisconnectTimeout", 20 ); + m_config.network.listenIp = configMgr.getValue< std::string >( "Network", "ListenIp", "0.0.0.0" ); + m_config.network.listenPort = configMgr.getValue< uint16_t >( "Network", "ListenPort", 54992 ); + m_config.network.inRangeDistance = configMgr.getValue< float >( "Network", "InRangeDistance", 80.f ); - m_config.motd = pConfig->getValue< std::string >( "General", "MotD", "" ); + m_config.motd = configMgr.getValue< std::string >( "General", "MotD", "" ); - m_config.housing.defaultEstateName = pConfig->getValue< std::string >( "Housing", "DefaultEstateName", "Estate #{}" ); + m_config.housing.defaultEstateName = configMgr.getValue< std::string >( "Housing", "DefaultEstateName", "Estate #{}" ); m_port = m_config.network.listenPort; m_ip = m_config.network.listenIp; @@ -124,7 +125,7 @@ void Sapphire::World::ServerMgr::run( int32_t argc, char* argv[] ) printBanner(); auto pConfig = std::make_shared< Common::ConfigMgr >(); - framework()->set< Common::ConfigMgr >( pConfig ); + Common::Service< Common::ConfigMgr >::set( pConfig ); if( !loadSettings( argc, argv ) ) { Logger::fatal( "Unable to load settings!" ); diff --git a/src/world/Territory/Territory.cpp b/src/world/Territory/Territory.cpp index 910a7785..07a6269f 100644 --- a/src/world/Territory/Territory.cpp +++ b/src/world/Territory/Territory.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "Territory.h" #include "InstanceContent.h" @@ -251,7 +252,6 @@ void Sapphire::Territory::pushActor( Entity::ActorPtr pActor ) agentId = m_pNaviProvider->addAgent( *pPlayer ); pPlayer->setAgentId( agentId ); - auto pServerZone = m_pFw->get< World::ServerMgr >(); m_playerMap[ pPlayer->getId() ] = pPlayer; updateCellActivity( cx, cy, 2 ); } @@ -319,7 +319,8 @@ void Sapphire::Territory::queuePacketForRange( Entity::Player& sourcePlayer, uin if( pTeriMgr->isPrivateTerritory( getTerritoryTypeId() ) ) return; - auto pServerZone = m_pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + for( auto entry : m_playerMap ) { auto player = entry.second; @@ -329,7 +330,7 @@ void Sapphire::Territory::queuePacketForRange( Entity::Player& sourcePlayer, uin if( ( distance < range ) && sourcePlayer.getId() != player->getId() ) { - auto pSession = pServerZone->getSession( player->getId() ); + auto pSession = serverMgr.getSession( player->getId() ); //pPacketEntry->setValAt< uint32_t >( 0x08, player->getId() ); if( pSession ) pSession->getZoneConnection()->queueOutPacket( pPacketEntry ); @@ -345,14 +346,15 @@ void Sapphire::Territory::queuePacketForZone( Entity::Player& sourcePlayer, if( pTeriMgr->isPrivateTerritory( getTerritoryTypeId() ) ) return; - auto pServerZone = m_pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); + for( auto entry : m_playerMap ) { auto player = entry.second; if( ( sourcePlayer.getId() != player->getId() ) || ( ( sourcePlayer.getId() == player->getId() ) && forSelf ) ) { - auto pSession = pServerZone->getSession( player->getId() ); + auto pSession = serverMgr.getSession( player->getId() ); if( pSession ) pSession->getZoneConnection()->queueOutPacket( pPacketEntry ); } @@ -874,9 +876,9 @@ void Sapphire::Territory::updateSpawnPoints() { if( !point->getLinkedBNpc() && ( Util::getTimeSeconds() - point->getTimeOfDeath() ) > 60 ) { - auto serverZone = m_pFw->get< World::ServerMgr >(); + auto& serverMgr = Common::Service< World::ServerMgr >::ref(); - auto bNpcTemplate = serverZone->getBNpcTemplate( group.getTemplateId() ); + auto bNpcTemplate = serverMgr.getBNpcTemplate( group.getTemplateId() ); if( !bNpcTemplate ) { diff --git a/src/world/mainGameServer.cpp b/src/world/mainGameServer.cpp index 2c77ea66..93f3d139 100644 --- a/src/world/mainGameServer.cpp +++ b/src/world/mainGameServer.cpp @@ -4,17 +4,21 @@ #include #include +#include using namespace Sapphire; using namespace Sapphire::World; +[[maybe_unused]] Common::Util::CrashHandler crashHandler; int main( int32_t argc, char* argv[] ) { auto pFramework = make_Framework(); auto pServer = std::make_shared< ServerMgr >( "world.ini", pFramework ); - pFramework->set< ServerMgr >( pServer ); + + Common::Service< ServerMgr >::set( pServer ); + pServer->run( argc, argv ); return 0; }