From 7abc89b39c24189104fd910f3f733c65f270ae11 Mon Sep 17 00:00:00 2001 From: Nikhil Narayana Date: Mon, 30 May 2022 00:49:23 -0700 Subject: [PATCH] pull in project-slippi/Ishiiruka/commit/abf44c428f91fa1f947bb44ce0b31b1b96c4f64f --- Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp | 3 +- Source/Core/Core/Slippi/SlippiSpectate.cpp | 56 +++++++++++--------- Source/Core/Core/Slippi/SlippiSpectate.h | 3 +- Source/Core/DolphinQt/Main.cpp | 8 +-- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp index cb0bd7fdb9..6c1b7772e1 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp @@ -248,8 +248,7 @@ CEXISlippi::~CEXISlippi() m_fileWriteThread.join(); } - SlippiSpectateServer::getInstance().write(&empty[0], 0); - SlippiSpectateServer::getInstance().endGame(); + SlippiSpectateServer::getInstance().endGame(true); localSelections.Reset(); diff --git a/Source/Core/Core/Slippi/SlippiSpectate.cpp b/Source/Core/Core/Slippi/SlippiSpectate.cpp index 45831549e6..ce6154020a 100644 --- a/Source/Core/Core/Slippi/SlippiSpectate.cpp +++ b/Source/Core/Core/Slippi/SlippiSpectate.cpp @@ -40,15 +40,22 @@ void SlippiSpectateServer::startGame() { m_event_queue.Push("START_GAME"); } + json start_game_message; + start_game_message["type"] = "start_game"; + m_event_queue.Push(start_game_message.dump()); } // CALLED FROM DOLPHIN MAIN THREAD -void SlippiSpectateServer::endGame() +void SlippiSpectateServer::endGame(bool dolphin_closed) { if (isSpectatorEnabled()) { m_event_queue.Push("END_GAME"); } + json end_game_message; + end_game_message["type"] = "end_game"; + end_game_message["dolphin_closed"] = dolphin_closed; + m_event_queue.Push(end_game_message.dump()); } // CALLED FROM SERVER THREAD @@ -95,39 +102,36 @@ void SlippiSpectateServer::popEvents() std::string event; m_event_queue.Pop(event); // These two are meta-events, used to signify the start/end of a game - // They are not sent over the wire - if (event == "END_GAME") + json json_message = json::parse(event, nullptr, false); + if (!json_message.is_discarded() && (json_message.find("type") != json_message.end())) { - m_menu_cursor = 0; - if (m_event_buffer.size() > 0) + if (json_message["type"] == "end_game") { + u32 cursor = (u32)(m_event_buffer.size() + m_cursor_offset); + json_message["cursor"] = cursor; + json_message["next_cursor"] = cursor + 1; + m_menu_cursor = 0; + m_event_buffer.push_back(json_message.dump()); m_cursor_offset += m_event_buffer.size(); + m_menu_event.clear(); + m_in_game = false; + continue; + } + else if (json_message["type"] == "start_game") + { + m_event_buffer.clear(); + u32 cursor = (u32)(m_event_buffer.size() + m_cursor_offset); + m_in_game = true; + json_message["cursor"] = cursor; + json_message["next_cursor"] = cursor + 1; + m_event_buffer.push_back(json_message.dump()); + continue; } - m_menu_event.clear(); - m_in_game = false; - continue; - } - if (event == "START_GAME") - { - m_event_buffer.clear(); - m_in_game = true; - continue; } // Make json wrapper for game event json game_event; - // An SLP event with an empty payload is a quasi-event that signifies - // the unclean exit of a game. Send this out as its own event - // (Since you can't meaningfully concat it with other events) - if (event.empty()) - { - game_event["payload"] = ""; - game_event["type"] = "game_event"; - m_event_buffer.push_back(game_event.dump()); - continue; - } - if (!m_in_game) { game_event["payload"] = base64::Base64::Encode(event); @@ -168,6 +172,8 @@ SlippiSpectateServer::SlippiSpectateServer() { m_in_game = false; m_menu_cursor = 0; + m_menu_event.clear(); + m_cursor_offset = 0; // Spawn thread for socket listener m_stop_socket_thread = false; diff --git a/Source/Core/Core/Slippi/SlippiSpectate.h b/Source/Core/Core/Slippi/SlippiSpectate.h index 6e36a47f43..488c8fa0f4 100644 --- a/Source/Core/Core/Slippi/SlippiSpectate.h +++ b/Source/Core/Core/Slippi/SlippiSpectate.h @@ -56,7 +56,8 @@ public: // when a new client connects to the server mid-match, it can recieve all // the game events that have happened so far. This buffer needs to be // cleared when a match ends. - void endGame(); + // If this is called due to dolphin closing then dolphin_closed will be true + void endGame(bool dolphin_closed = false); private: // ACCESSED FROM BOTH DOLPHIN AND SERVER THREADS diff --git a/Source/Core/DolphinQt/Main.cpp b/Source/Core/DolphinQt/Main.cpp index 19c4221df7..c8829d646b 100644 --- a/Source/Core/DolphinQt/Main.cpp +++ b/Source/Core/DolphinQt/Main.cpp @@ -10,7 +10,6 @@ #include #endif - #include #include #include @@ -197,10 +196,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine save_state_path = static_cast(options.get("save_state")); } -#ifndef IS_PLAYBACK - SlippiSpectateServer::getInstance().endGame(); -#endif - std::unique_ptr boot; bool game_specified = false; if (options.is_set("exec")) @@ -258,7 +253,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine #ifndef IS_PLAYBACK if (Settings::Instance().IsBootDefaultISO() && !Settings::Instance().GetDefaultGame().isEmpty()) { - boot = BootParameters::GenerateFromFile(Settings::Instance().GetDefaultGame().toStdString(), save_state_path); + boot = BootParameters::GenerateFromFile(Settings::Instance().GetDefaultGame().toStdString(), + save_state_path); } #endif