From 63bad92765cfd237660d84198eced46722cc6522 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 28 Feb 2020 09:43:00 +0800 Subject: [PATCH] Turn race event manager into a singleton get for 2 processes --- .../controller/local_player_controller.cpp | 2 +- src/karts/kart.cpp | 2 +- src/main.cpp | 2 ++ src/main_loop.cpp | 5 ++- src/network/protocols/client_lobby.cpp | 14 ++++---- src/network/protocols/lobby_protocol.cpp | 7 ++-- src/network/protocols/server_lobby.cpp | 30 ++++++++--------- src/network/race_event_manager.cpp | 32 +++++++++++++++++++ src/network/race_event_manager.hpp | 19 ++++++----- src/race/race_manager.cpp | 13 ++++---- .../online/networking_lobby.cpp | 4 +-- 11 files changed, 83 insertions(+), 47 deletions(-) diff --git a/src/karts/controller/local_player_controller.cpp b/src/karts/controller/local_player_controller.cpp index 430288051..7c360ca6e 100644 --- a/src/karts/controller/local_player_controller.cpp +++ b/src/karts/controller/local_player_controller.cpp @@ -181,7 +181,7 @@ bool LocalPlayerController::action(PlayerAction action, int value, } else if (NetworkConfig::get()->isClient()) { - auto ge = RaceEventManager::getInstance()->getProtocol(); + auto ge = RaceEventManager::get()->getProtocol(); assert(ge); ge->sendStartupBoost((uint8_t)m_kart->getWorldKartId()); } diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index 87a7d5b93..157858209 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -940,7 +940,7 @@ void Kart::finishedRace(float time, bool from_server) { if (NetworkConfig::get()->isServer()) { - RaceEventManager::getInstance()->kartFinishedRace(this, time); + RaceEventManager::get()->kartFinishedRace(this, time); } // isServer // Ignore local detection of a kart finishing a race in a diff --git a/src/main.cpp b/src/main.cpp index 5a7c2c075..c0d244271 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -225,6 +225,7 @@ #include "network/protocols/connect_to_server.hpp" #include "network/protocols/client_lobby.hpp" #include "network/protocols/server_lobby.hpp" +#include "network/race_event_manager.hpp" #include "network/rewind_manager.hpp" #include "network/rewind_queue.hpp" #include "network/server.hpp" @@ -1739,6 +1740,7 @@ void clearGlobalVariables() STKHost::clear(); RaceManager::clear(); ProjectileManager::clear(); + RaceEventManager::clear(); music_manager = NULL; irr_driver = NULL; #ifdef ENABLE_WIIUSE diff --git a/src/main_loop.cpp b/src/main_loop.cpp index 7a9eb4fc8..53d781108 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -291,9 +291,8 @@ void MainLoop::updateRace(int ticks, bool fast_forward) if (!World::getWorld()) return; // No race on atm - i.e. we are in menu // The race event manager will update world in case of an online race - if ( RaceEventManager::getInstance() && - RaceEventManager::getInstance()->isRunning() ) - RaceEventManager::getInstance()->update(ticks, fast_forward); + if (RaceEventManager::get() && RaceEventManager::get()->isRunning()) + RaceEventManager::get()->update(ticks, fast_forward); else World::getWorld()->updateWorld(ticks); } // updateRace diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index fd172dc5e..5488d5380 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -434,7 +434,7 @@ void ClientLobby::update(int ticks) } break; case RACE_FINISHED: - if (!RaceEventManager::getInstance()->protocolStopped() || + if (!RaceEventManager::get()->protocolStopped() || !GameProtocol::emptyInstance()) return; if (!m_received_server_result) @@ -573,9 +573,9 @@ void ClientLobby::disconnectedPlayer(Event* event) // If in-game world exists the kart rewinder will know which player // disconnects bool in_game_world = World::getWorld() && - RaceEventManager::getInstance() && - RaceEventManager::getInstance()->isRunning() && - !RaceEventManager::getInstance()->isRaceOver(); + RaceEventManager::get() && + RaceEventManager::get()->isRunning() && + !RaceEventManager::get()->isRaceOver(); if (!in_game_world) SFXManager::get()->quickSound("appear"); @@ -1128,7 +1128,7 @@ void ClientLobby::raceFinished(Event* event) } // stop race protocols - RaceEventManager::getInstance()->stop(); + RaceEventManager::get()->stop(); ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS); ProtocolManager::lock()->findAndTerminate(PROTOCOL_CONTROLLER_EVENTS); m_state.store(RACE_FINISHED); @@ -1149,9 +1149,9 @@ void ClientLobby::backToLobby(Event *event) m_auto_started = false; m_state.store(CONNECTED); - if (RaceEventManager::getInstance()) + if (RaceEventManager::get()) { - RaceEventManager::getInstance()->stop(); + RaceEventManager::get()->stop(); ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS); } auto gp = GameProtocol::lock(); diff --git a/src/network/protocols/lobby_protocol.cpp b/src/network/protocols/lobby_protocol.cpp index 0886b02e2..29c277dbb 100644 --- a/src/network/protocols/lobby_protocol.cpp +++ b/src/network/protocols/lobby_protocol.cpp @@ -57,8 +57,7 @@ LobbyProtocol::LobbyProtocol() // ---------------------------------------------------------------------------- LobbyProtocol::~LobbyProtocol() { - if (RaceEventManager::getInstance()) - RaceEventManager::getInstance()->stop(); + RaceEventManager::destroy(); delete m_game_setup; joinStartGameThread(); } // ~LobbyProtocol @@ -78,7 +77,9 @@ void LobbyProtocol::loadWorld() // --------------------- // This creates the network world. auto gep = std::make_shared(); - RaceEventManager::getInstance()->start(gep); + if (!RaceEventManager::get()) + RaceEventManager::create(); + RaceEventManager::get()->start(gep); // Make sure that if there is only a single local player this player can // use all input devices. diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 8d628845c..24e103800 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -1684,8 +1684,8 @@ bool ServerLobby::canLiveJoinNow() const */ bool ServerLobby::worldIsActive() const { - return World::getWorld() && RaceEventManager::getInstance()->isRunning() && - !RaceEventManager::getInstance()->isRaceOver() && + return World::getWorld() && RaceEventManager::get()->isRunning() && + !RaceEventManager::get()->isRaceOver() && World::getWorld()->getPhase() == WorldStatus::RACE_PHASE; } // worldIsActive @@ -2048,8 +2048,8 @@ void ServerLobby::update(int ticks) // Reset server to initial state if no more connected players if (m_rs_state.load() == RS_WAITING) { - if ((RaceEventManager::getInstance() && - !RaceEventManager::getInstance()->protocolStopped()) || + if ((RaceEventManager::get() && + !RaceEventManager::get()->protocolStopped()) || !GameProtocol::emptyInstance()) return; @@ -2064,8 +2064,8 @@ void ServerLobby::update(int ticks) (STKHost::get()->getPlayersInGame() == 0 || all_players_in_world_disconnected)) { - if (RaceEventManager::getInstance() && - RaceEventManager::getInstance()->isRunning()) + if (RaceEventManager::get() && + RaceEventManager::get()->isRunning()) { // Send a notification to all players who may have start live join // or spectate to go back to lobby @@ -2075,8 +2075,8 @@ void ServerLobby::update(int ticks) sendMessageToPeersInServer(back_to_lobby, /*reliable*/true); delete back_to_lobby; - RaceEventManager::getInstance()->stop(); - RaceEventManager::getInstance()->getProtocol()->requestTerminate(); + RaceEventManager::get()->stop(); + RaceEventManager::get()->getProtocol()->requestTerminate(); GameProtocol::lock()->requestTerminate(); } else if (auto ai = m_ai_peer.lock()) @@ -2138,14 +2138,14 @@ void ServerLobby::update(int ticks) m_state = WAIT_FOR_WORLD_LOADED; break; case RACING: - if (World::getWorld() && - RaceEventManager::getInstance()->isRunning()) + if (World::getWorld() && RaceEventManager::get() && + RaceEventManager::get()->isRunning()) { checkRaceFinished(); } break; case WAIT_FOR_RACE_STOPPED: - if (!RaceEventManager::getInstance()->protocolStopped() || + if (!RaceEventManager::get()->protocolStopped() || !GameProtocol::emptyInstance()) return; @@ -2698,16 +2698,16 @@ void ServerLobby::checkIncomingConnectionRequests() */ void ServerLobby::checkRaceFinished() { - assert(RaceEventManager::getInstance()->isRunning()); + assert(RaceEventManager::get()->isRunning()); assert(World::getWorld()); - if (!RaceEventManager::getInstance()->isRaceOver()) return; + if (!RaceEventManager::get()->isRaceOver()) return; Log::info("ServerLobby", "The game is considered finished."); // notify the network world that it is stopped - RaceEventManager::getInstance()->stop(); + RaceEventManager::get()->stop(); // stop race protocols before going back to lobby (end race) - RaceEventManager::getInstance()->getProtocol()->requestTerminate(); + RaceEventManager::get()->getProtocol()->requestTerminate(); GameProtocol::lock()->requestTerminate(); // Save race result before delete the world diff --git a/src/network/race_event_manager.cpp b/src/network/race_event_manager.cpp index 93e064c2c..eba72b37f 100644 --- a/src/network/race_event_manager.cpp +++ b/src/network/race_event_manager.cpp @@ -7,7 +7,39 @@ #include "network/protocols/game_events_protocol.hpp" #include "network/rewind_manager.hpp" #include "utils/profiler.hpp" +#include "utils/stk_process.hpp" +//============================================================================= +RaceEventManager* g_race_event_manager[PT_COUNT]; +// ---------------------------------------------------------------------------- +RaceEventManager* RaceEventManager::get() +{ + ProcessType type = STKProcess::getType(); + return g_race_event_manager[type]; +} // get + +// ---------------------------------------------------------------------------- +void RaceEventManager::create() +{ + ProcessType type = STKProcess::getType(); + g_race_event_manager[type] = new RaceEventManager(); +} // create + +// ---------------------------------------------------------------------------- +void RaceEventManager::destroy() +{ + ProcessType type = STKProcess::getType(); + delete g_race_event_manager[type]; + g_race_event_manager[type] = NULL; +} // destroy + +// ---------------------------------------------------------------------------- +void RaceEventManager::clear() +{ + memset(g_race_event_manager, 0, sizeof(g_race_event_manager)); +} // clear + +// ---------------------------------------------------------------------------- RaceEventManager::RaceEventManager() { m_running = false; diff --git a/src/network/race_event_manager.hpp b/src/network/race_event_manager.hpp index 5488eb8d2..1bd765015 100644 --- a/src/network/race_event_manager.hpp +++ b/src/network/race_event_manager.hpp @@ -19,8 +19,6 @@ #ifndef NETWORK_WORLD_HPP #define NETWORK_WORLD_HPP -#include "input/input.hpp" -#include "utils/singleton.hpp" #include class Controller; @@ -34,20 +32,25 @@ class Item; * server to all clients. This object then triggers the right message * from the various running protocols. */ -class RaceEventManager : public AbstractSingleton +class RaceEventManager { private: bool m_running; - float m_race_time; std::weak_ptr m_game_events_protocol; - friend class AbstractSingleton; - - RaceEventManager(); - virtual ~RaceEventManager(); + RaceEventManager(); + ~RaceEventManager(); public: + // ---------------------------------------------------------------------------------------- + static RaceEventManager* get(); + // ---------------------------------------------------------------------------------------- + static void create(); + // ---------------------------------------------------------------------------------------- + static void destroy(); + // ---------------------------------------------------------------------------------------- + static void clear(); // ------------------------------------------------------------------------ void update(int ticks, bool fast_forward); // ------------------------------------------------------------------------ diff --git a/src/race/race_manager.cpp b/src/race/race_manager.cpp index bb984f0fa..f58f3b6b7 100644 --- a/src/race/race_manager.cpp +++ b/src/race/race_manager.cpp @@ -49,7 +49,6 @@ #include "network/protocol_manager.hpp" #include "network/network_config.hpp" #include "network/network_string.hpp" -#include "network/race_event_manager.hpp" #include "replay/replay_play.hpp" #include "scriptengine/property_animator.hpp" #include "states_screens/grand_prix_cutscene.hpp" @@ -375,7 +374,7 @@ void RaceManager::startNew(bool from_overworld) m_num_laps = m_grand_prix.getLaps(); m_reverse_track = m_grand_prix.getReverse(); - if (!RaceEventManager::getInstance()->isRunning()) + if (!NetworkConfig::get()->isNetworking()) { // We look if Player 1 has a saved version of this GP. m_saved_gp = SavedGrandPrix::getSavedGP( @@ -678,8 +677,8 @@ void RaceManager::next() m_track_number++; if(m_track_number<(int)m_tracks.size()) { - if( m_major_mode==MAJOR_MODE_GRAND_PRIX && - !RaceEventManager::getInstance()->isRunning() ) + if (m_major_mode == MAJOR_MODE_GRAND_PRIX && + !NetworkConfig::get()->isNetworking()) { // Saving GP state saveGP(); @@ -841,8 +840,8 @@ void RaceManager::exitRace(bool delete_world) m_track_number==(int)m_tracks.size() ) { PlayerManager::getCurrentPlayer()->grandPrixFinished(); - if( m_major_mode==MAJOR_MODE_GRAND_PRIX && - !RaceEventManager::getInstance()->isRunning() ) + if (m_major_mode == MAJOR_MODE_GRAND_PRIX && + !NetworkConfig::get()->isNetworking()) { if(m_saved_gp != NULL) m_saved_gp->remove(); @@ -1052,7 +1051,7 @@ void RaceManager::startSingleRace(const std::string &track_ident, setCoinTarget( 0 ); // Might still be set from a previous challenge // if not in a network world, setup player karts - if (!RaceEventManager::getInstance()->isRunning()) + if (!NetworkConfig::get()->isNetworking()) setupPlayerKartInfo(); // do this setup player kart startNew(from_overworld); diff --git a/src/states_screens/online/networking_lobby.cpp b/src/states_screens/online/networking_lobby.cpp index 1b6d81c3c..f44be640c 100644 --- a/src/states_screens/online/networking_lobby.cpp +++ b/src/states_screens/online/networking_lobby.cpp @@ -432,8 +432,8 @@ void NetworkingLobby::onUpdate(float delta) // You can live join or spectator if u have the current play track // and network timer is synchronized, and no game protocols exist - bool no_gep = !RaceEventManager::getInstance() || - RaceEventManager::getInstance()->protocolStopped(); + bool no_gep = !RaceEventManager::get() || + RaceEventManager::get()->protocolStopped(); bool no_gp = GameProtocol::emptyInstance(); if (t && STKHost::get()->getNetworkTimerSynchronizer()->isSynchronised() &&