From 2504fe0b71ca0db61dee8d5d3eaf8694f82f3dc8 Mon Sep 17 00:00:00 2001 From: Sour Date: Mon, 2 Jul 2018 18:11:12 -0400 Subject: [PATCH] Netplay: Fixed regressions/crashes caused by refactoring --- Core/GameClientConnection.cpp | 7 ++++--- Core/GameServer.cpp | 25 ++++++++++++++++++++++--- Core/GameServer.h | 9 +++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Core/GameClientConnection.cpp b/Core/GameClientConnection.cpp index 4bb2a747..cb23e81e 100644 --- a/Core/GameClientConnection.cpp +++ b/Core/GameClientConnection.cpp @@ -27,7 +27,6 @@ GameClientConnection::GameClientConnection(shared_ptr console, shared_p _minimumQueueSize = 3; MessageManager::DisplayMessage("NetPlay", "ConnectedToServer"); - _console->GetControlManager()->RegisterInputProvider(this); } GameClientConnection::~GameClientConnection() @@ -208,10 +207,10 @@ bool GameClientConnection::SetInput(BaseControlDevice *device) void GameClientConnection::InitControlDevice() { if(_controllerPort == BaseControlDevice::ExpDevicePort) { - _newControlDevice = ControlManager::CreateExpansionDevice(EmulationSettings::GetExpansionDevice(), nullptr); + _newControlDevice = ControlManager::CreateExpansionDevice(EmulationSettings::GetExpansionDevice(), _console); } else { //Pretend we are using port 0 (to use player 1's keybindings during netplay) - _newControlDevice = ControlManager::CreateControllerDevice(EmulationSettings::GetControllerType(_controllerPort), 0, nullptr); + _newControlDevice = ControlManager::CreateControllerDevice(EmulationSettings::GetControllerType(_controllerPort), 0, _console); } } @@ -219,6 +218,8 @@ void GameClientConnection::ProcessNotification(ConsoleNotificationType type, voi { if(type == ConsoleNotificationType::ConfigChanged) { InitControlDevice(); + } else if(type == ConsoleNotificationType::GameLoaded) { + _console->GetControlManager()->RegisterInputProvider(this); } } diff --git a/Core/GameServer.cpp b/Core/GameServer.cpp index 08709ae3..f4a03c38 100644 --- a/Core/GameServer.cpp +++ b/Core/GameServer.cpp @@ -10,7 +10,7 @@ using std::thread; #include "PlayerListMessage.h" #include "NotificationManager.h" -unique_ptr GameServer::Instance; +shared_ptr GameServer::Instance; GameServer::GameServer(shared_ptr console, uint16_t listenPort, string password, string hostPlayerName) { @@ -20,8 +20,9 @@ GameServer::GameServer(shared_ptr console, uint16_t listenPort, string _password = password; _hostPlayerName = hostPlayerName; _hostControllerPort = 0; - _console->GetControlManager()->RegisterInputRecorder(this); - _console->GetControlManager()->RegisterInputProvider(this); + + //If a game is already running, register ourselves as an input recorder/provider right away + RegisterServerInput(); } GameServer::~GameServer() @@ -35,6 +36,15 @@ GameServer::~GameServer() _console->GetControlManager()->UnregisterInputProvider(this); } +void GameServer::RegisterServerInput() +{ + ControlManager* controlManager = _console->GetControlManager(); + if(controlManager) { + controlManager->RegisterInputRecorder(this); + controlManager->RegisterInputProvider(this); + } +} + void GameServer::AcceptConnections() { while(true) { @@ -102,6 +112,14 @@ void GameServer::RecordInput(vector> devices) } } +void GameServer::ProcessNotification(ConsoleNotificationType type, void * parameter) +{ + if(type == ConsoleNotificationType::GameLoaded) { + //Register the server as an input provider/recorder + RegisterServerInput(); + } +} + void GameServer::Exec() { _listener.reset(new Socket()); @@ -129,6 +147,7 @@ void GameServer::Stop() void GameServer::StartServer(shared_ptr console, uint16_t port, string password, string hostPlayerName) { Instance.reset(new GameServer(console, port, password, hostPlayerName)); + console->GetNotificationManager()->RegisterNotificationListener(Instance); Instance->_serverThread.reset(new thread(&GameServer::Exec, Instance.get())); } diff --git a/Core/GameServer.h b/Core/GameServer.h index c22ab18c..475519da 100644 --- a/Core/GameServer.h +++ b/Core/GameServer.h @@ -9,10 +9,10 @@ using std::thread; class Console; -class GameServer : public IInputRecorder, public IInputProvider +class GameServer : public IInputRecorder, public IInputProvider, public INotificationListener { private: - static unique_ptr Instance; + static shared_ptr Instance; shared_ptr _console; unique_ptr _serverThread; atomic _stop; @@ -35,6 +35,8 @@ public: GameServer(shared_ptr console, uint16_t port, string password, string hostPlayerName); virtual ~GameServer(); + void RegisterServerInput(); + static void StartServer(shared_ptr console, uint16_t port, string password, string hostPlayerName); static void StopServer(); static bool Started(); @@ -50,4 +52,7 @@ public: bool SetInput(BaseControlDevice *device) override; void RecordInput(vector> devices) override; + + // Inherited via INotificationListener + virtual void ProcessNotification(ConsoleNotificationType type, void * parameter) override; }; \ No newline at end of file