diff --git a/dNet/ZoneInstanceManager.cpp b/dNet/ZoneInstanceManager.cpp index b57bb634..d5457bac 100644 --- a/dNet/ZoneInstanceManager.cpp +++ b/dNet/ZoneInstanceManager.cpp @@ -1,27 +1,22 @@ -#define _VARIADIC_MAX 10 #include "ZoneInstanceManager.h" -// Custom Classes #include "MasterPackets.h" #include "PacketUtils.h" #include "dServer.h" -// C++ +#include #include -// Static Variables -ZoneInstanceManager* ZoneInstanceManager::m_Address = nullptr; - //! Requests a zone transfer -void ZoneInstanceManager::RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, std::function callback) { +void ZoneInstanceManager::RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, ZoneTransferCallback callback) { - ZoneTransferRequest* request = new ZoneTransferRequest(); - request->requestID = ++currentRequestID; - request->callback = callback; + ZoneTransferRequest request{}; + request.requestID = ++currentRequestID; + request.callback = callback; this->requests.push_back(request); - MasterPackets::SendZoneTransferRequest(server, request->requestID, mythranShift, zoneID, zoneClone); + MasterPackets::SendZoneTransferRequest(server, request.requestID, mythranShift, zoneID, zoneClone); } //! Handles a zone transfer response @@ -34,18 +29,15 @@ void ZoneInstanceManager::HandleRequestZoneTransferResponse(uint64_t requestID, uint16_t serverPort = PacketUtils::ReadPacketU16(29, packet); std::string serverIP = PacketUtils::ReadString(31, packet, false); - for (uint32_t i = 0; i < this->requests.size(); ++i) { - if (this->requests[i]->requestID == requestID) { + auto transferRequest = std::find(requests.begin(), requests.end(), [requestID](const ZoneTransferRequest& transfer) { + return requestID == transfer.requestID; + }); + if (transferRequest == requests.end()) return; - // Call the request callback - this->requests[i]->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort); - - delete this->requests[i]; - this->requests.erase(this->requests.begin() + i); - return; - } - } + // Now begin the callback since we confirmed we have found the matching request. + transferRequest->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort); + requests.erase(transferRequest); } void ZoneInstanceManager::CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password) { @@ -56,12 +48,12 @@ void ZoneInstanceManager::RequestPrivateZone( dServer* server, bool mythranShift, const std::string& password, - std::function callback) { - ZoneTransferRequest* request = new ZoneTransferRequest(); - request->requestID = ++currentRequestID; - request->callback = callback; + ZoneTransferCallback callback) { + ZoneTransferRequest request; + request.requestID = ++currentRequestID; + request.callback = callback; - this->requests.push_back(request); + requests.push_back(request); - MasterPackets::SendZoneRequestPrivate(server, request->requestID, mythranShift, password); + MasterPackets::SendZoneRequestPrivate(server, request.requestID, mythranShift, password); } diff --git a/dNet/ZoneInstanceManager.h b/dNet/ZoneInstanceManager.h index a8e32c4e..37f1b13a 100644 --- a/dNet/ZoneInstanceManager.h +++ b/dNet/ZoneInstanceManager.h @@ -1,65 +1,42 @@ #pragma once -// C++ -#define _VARIADIC_MAX 10 #include #include #include -// RakNet -#include +#include "RakNetTypes.h" class dServer; +using ZoneTransferCallback = std::function; -/*! - \file ZoneInstanceManager.hpp - \brief A class for handling zone transfers and zone-related functions - */ - - //! The zone request +// The zone request struct ZoneTransferRequest { - uint64_t requestID; - std::function callback; + uint64_t requestID = 0; + ZoneTransferCallback callback; }; //! The zone manager class ZoneInstanceManager { private: - static ZoneInstanceManager* m_Address; //!< The singleton instance - - std::vector requests; //!< The zone transfer requests + std::vector requests; //!< The zone transfer requests uint64_t currentRequestID; //!< The current request ID - public: - - //! The singleton method - static ZoneInstanceManager* Instance() { - if (m_Address == 0) { - m_Address = new ZoneInstanceManager; - m_Address->currentRequestID = 0; - } - - return m_Address; - } - - //! Requests a zone transfer - /*! - \param zoneID The zone ID - \param zoneClone The zone clone - \param mythranShift Whether or not this is a mythran shift - \param callback The callback function + /** + * @param zoneID The zone ID + * @param zoneClone The zone clone + * @param mythranShift Whether or not this is a mythran shift + * @param callback The callback function */ - void RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, std::function callback); + void RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, ZoneTransferCallback callback); - //! Handles a zone transfer response - /*! - \param requestID The request ID - \param packet The packet + /** + * @param requestID The request ID + * @param packet The packet */ void HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet); void CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password); - void RequestPrivateZone(dServer* server, bool mythranShift, const std::string& password, std::function callback); + void RequestPrivateZone(dServer* server, bool mythranShift, const std::string& password, ZoneTransferCallback callback); };