From 85cca6911310e6efec9e672377df423e244a4f29 Mon Sep 17 00:00:00 2001 From: hiker Date: Sun, 25 Oct 2015 00:19:30 +1100 Subject: [PATCH] Moved GameSetup handling from NetworkManager to STKHost. --- src/network/network_manager.cpp | 16 ---------------- src/network/network_manager.hpp | 5 ----- .../protocols/client_lobby_room_protocol.cpp | 2 +- src/network/protocols/game_events_protocol.cpp | 4 ++-- .../protocols/server_lobby_room_protocol.cpp | 2 +- src/network/protocols/start_game_protocol.cpp | 2 +- src/network/server_console.cpp | 4 ++-- src/network/stk_host.cpp | 18 ++++++++++++++++++ src/network/stk_host.hpp | 8 ++++++++ src/states_screens/network_kart_selection.cpp | 2 +- 10 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index d34f608e5..62bd3ae44 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -30,7 +30,6 @@ NetworkManager::NetworkManager() { - m_game_setup = NULL; } // NetworkManager //----------------------------------------------------------------------------- @@ -156,27 +155,12 @@ void NetworkManager::sendPacketExcept(STKPeer* peer, const NetworkString& data, } } // sendPacketExcept -//----------------------------------------------------------------------------- -/** A previous GameSetup is deletea and a new one is created. - * \return Newly create GameSetup object. - */ -GameSetup* NetworkManager::setupNewGame() -{ - if (m_game_setup) - delete m_game_setup; - m_game_setup = new GameSetup(); - return m_game_setup; -} // setupNewGame //----------------------------------------------------------------------------- /** Called when you leave a server. */ void NetworkManager::disconnected() { - // delete the game setup - if (m_game_setup) - delete m_game_setup; - m_game_setup = NULL; // remove all peers for (unsigned int i = 0; i < m_peers.size(); i++) diff --git a/src/network/network_manager.hpp b/src/network/network_manager.hpp index 324c52d95..bcda29e2e 100644 --- a/src/network/network_manager.hpp +++ b/src/network/network_manager.hpp @@ -55,7 +55,6 @@ protected: std::vector m_peers; private: - GameSetup* m_game_setup; PlayerLogin m_player_login; @@ -77,7 +76,6 @@ public: bool reliable = true); // Game related functions - virtual GameSetup* setupNewGame(); virtual void disconnected(); virtual bool isServer() = 0; @@ -105,9 +103,6 @@ public: // -------------------------------------------------------------------- unsigned int getPeerCount() { return (int)m_peers.size(); } - // -------------------------------------------------------------------- - /** Returns the current game setup. */ - GameSetup* getGameSetup() { return m_game_setup; } }; // class NetworkManager diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index a0a5edc86..9363e3900 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -46,7 +46,7 @@ ClientLobbyRoomProtocol::~ClientLobbyRoomProtocol() void ClientLobbyRoomProtocol::setup() { - m_setup = NetworkManager::getInstance()->setupNewGame(); // create a new setup + m_setup = STKHost::get()->setupNewGame(); // create a new setup m_setup->getRaceConfig()->setPlayerCount(16); //FIXME : this has to be changed when logging into the server m_state = NONE; } // setup diff --git a/src/network/protocols/game_events_protocol.cpp b/src/network/protocols/game_events_protocol.cpp index cc65b9c38..4c2ac108a 100644 --- a/src/network/protocols/game_events_protocol.cpp +++ b/src/network/protocols/game_events_protocol.cpp @@ -51,7 +51,7 @@ bool GameEventsProtocol::notifyEvent(Event* event) uint8_t kart_race_id = data.gui8(5); // now set the kart powerup AbstractKart* kart = World::getWorld()->getKart( - NetworkManager::getInstance()->getGameSetup()->getProfile(kart_race_id)->world_kart_id); + STKHost::get()->getGameSetup()->getProfile(kart_race_id)->world_kart_id); ItemManager::get()->collectedItem( ItemManager::get()->getItem(item_id), kart, @@ -75,7 +75,7 @@ void GameEventsProtocol::update() void GameEventsProtocol::collectedItem(Item* item, AbstractKart* kart) { - GameSetup* setup = NetworkManager::getInstance()->getGameSetup(); + GameSetup* setup = STKHost::get()->getGameSetup(); assert(setup); const NetworkPlayerProfile* player_profile = setup->getProfile(kart->getIdent()); // use kart name diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index 8dce2812a..4aadd4848 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -51,7 +51,7 @@ ServerLobbyRoomProtocol::~ServerLobbyRoomProtocol() void ServerLobbyRoomProtocol::setup() { - m_setup = NetworkManager::getInstance()->setupNewGame(); // create a new setup + m_setup = STKHost::get()->setupNewGame(); // create a new setup m_setup->getRaceConfig()->setPlayerCount(16); //FIXME : this has to be moved to when logging into the server m_next_id = 0; m_state = NONE; diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index c98d1f124..155167e19 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -197,7 +197,7 @@ void StartGameProtocol::update() else if (m_state == READY) { // set karts into the network game setup - NetworkManager::getInstance()->getGameSetup()->bindKartsToProfiles(); + STKHost::get()->getGameSetup()->bindKartsToProfiles(); m_state = EXITING; requestTerminate(); } diff --git a/src/network/server_console.cpp b/src/network/server_console.cpp index d560169d6..43a5d6e5b 100644 --- a/src/network/server_console.cpp +++ b/src/network/server_console.cpp @@ -104,12 +104,12 @@ void* ServerConsole::mainLoop(void* data) } else if (str == "compute_race") { - GameSetup* setup = NetworkManager::getInstance()->getGameSetup(); + GameSetup* setup = STKHost::get()->getGameSetup(); setup->getRaceConfig()->computeRaceMode(); } else if (str == "compute_track") { - GameSetup* setup = NetworkManager::getInstance()->getGameSetup(); + GameSetup* setup = STKHost::get()->getGameSetup(); setup->getRaceConfig()->computeNextTrack(); } } diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 5a06a66bb..b821d0ea1 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -51,6 +51,7 @@ STKHost::STKHost() { m_network = NULL; m_listening_thread = NULL; + m_game_setup = NULL; m_public_address.lock(); m_public_address.getData().clear(); @@ -77,6 +78,11 @@ STKHost::STKHost() */ STKHost::~STKHost() { + // delete the game setup + if (m_game_setup) + delete m_game_setup; + m_game_setup = NULL; + Network::closeLog(); stopListening(); delete m_network; @@ -90,6 +96,18 @@ void STKHost::setPublicAddress(const TransportAddress& addr) m_public_address.unlock(); } // setPublicAddress +//----------------------------------------------------------------------------- +/** A previous GameSetup is deletea and a new one is created. + * \return Newly create GameSetup object. + */ +GameSetup* STKHost::setupNewGame() +{ + if (m_game_setup) + delete m_game_setup; + m_game_setup = new GameSetup(); + return m_game_setup; +} // setupNewGame + // ---------------------------------------------------------------------------- /** \brief Starts the listening of events from ENet. * Starts a thread for receiveData that updates it as often as possible. diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 6249c7f8e..72e65c98f 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -35,6 +35,8 @@ #include +class GameSetup; + /*! \class STKHost * \brief Represents the local host. * This host is either a server host or a client host. A client host is in @@ -75,6 +77,8 @@ private: * be updated from a separate thread. */ Synchronised m_public_address; + GameSetup* m_game_setup; + /** Id of thread listening to enet events. */ pthread_t* m_listening_thread; @@ -114,6 +118,7 @@ public: static void* mainLoop(void* self); + virtual GameSetup* setupNewGame(); void setPublicAddress(const TransportAddress& addr); void setupServer(uint32_t address, uint16_t port, @@ -138,6 +143,9 @@ public: return m_network->connectTo(address); } // connectTo + // -------------------------------------------------------------------- + /** Returns the current game setup. */ + GameSetup* getGameSetup() { return m_game_setup; } // -------------------------------------------------------------------- uint8_t* receiveRawPacket(TransportAddress* sender) { diff --git a/src/states_screens/network_kart_selection.cpp b/src/states_screens/network_kart_selection.cpp index 51b63b24f..b351e7d4e 100644 --- a/src/states_screens/network_kart_selection.cpp +++ b/src/states_screens/network_kart_selection.cpp @@ -64,7 +64,7 @@ void NetworkKartSelectionScreen::init() m_multiplayer = false; // add a widget for each player except self (already exists): - GameSetup* setup = NetworkManager::getInstance()->getGameSetup(); + GameSetup* setup = STKHost::get()->getGameSetup(); if (!setup) { Log::error("NetworkKartSelectionScreen", "No network game setup registered.");