From 0a517ebdbdd99d3c6495f5b55283df76fc2cd982 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Tue, 13 Sep 2022 04:44:59 +0200 Subject: [PATCH] Core: Pass Netplay SRAM through boot process. This removes the Netplay classes touching emulated hardware structures before emulation even starts. --- Source/Core/Core/BootManager.cpp | 4 ---- Source/Core/Core/Core.cpp | 2 +- Source/Core/Core/HW/EXI/EXI.cpp | 19 ++++++++++++++++--- Source/Core/Core/HW/EXI/EXI.h | 3 ++- Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp | 11 ++--------- Source/Core/Core/HW/HW.cpp | 4 ++-- Source/Core/Core/HW/HW.h | 3 ++- Source/Core/Core/HW/Sram.h | 1 - Source/Core/Core/NetPlayClient.cpp | 21 +++------------------ Source/Core/Core/NetPlayClient.h | 1 - Source/Core/Core/NetPlayProto.h | 4 +++- Source/Core/Core/NetPlayServer.cpp | 20 ++++++-------------- 12 files changed, 37 insertions(+), 56 deletions(-) diff --git a/Source/Core/Core/BootManager.cpp b/Source/Core/Core/BootManager.cpp index d2a1f86841..7a17307af9 100644 --- a/Source/Core/Core/BootManager.cpp +++ b/Source/Core/Core/BootManager.cpp @@ -93,10 +93,6 @@ bool BootCore(std::unique_ptr boot, const WindowSystemInfo& wsi) Config::AddLayer(ConfigLoaders::GenerateNetPlayConfigLoader(*netplay_settings)); StartUp.bCopyWiiSaveNetplay = netplay_settings->copy_wii_save; } - else - { - g_SRAM_netplay_initialized = false; - } // Override out-of-region languages/countries to prevent games from crashing or behaving oddly if (!Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS)) diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index a3d2379db7..913c9956e9 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -511,7 +511,7 @@ static void EmuThread(std::unique_ptr boot, WindowSystemInfo wsi AudioCommon::InitSoundStream(); Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream}; - HW::Init(); + HW::Init(NetPlay::IsNetPlayRunning() ? &(boot_session_data.GetNetplaySettings()->sram) : nullptr); Common::ScopeGuard hw_guard{[] { // We must set up this flag before executing HW::Shutdown() diff --git a/Source/Core/Core/HW/EXI/EXI.cpp b/Source/Core/Core/HW/EXI/EXI.cpp index 08b5fe8eec..9590b627bb 100644 --- a/Source/Core/Core/HW/EXI/EXI.cpp +++ b/Source/Core/Core/HW/EXI/EXI.cpp @@ -8,6 +8,7 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" +#include "Common/IOFile.h" #include "Core/Config/MainSettings.h" #include "Core/ConfigManager.h" @@ -24,7 +25,7 @@ #include "DiscIO/Enums.h" Sram g_SRAM; -bool g_SRAM_netplay_initialized = false; +bool s_using_overridden_sram = false; namespace ExpansionInterface { @@ -102,11 +103,17 @@ u8 SlotToEXIDevice(Slot slot) } } -void Init() +void Init(const Sram* override_sram) { - if (!g_SRAM_netplay_initialized) + if (override_sram) + { + g_SRAM = *override_sram; + s_using_overridden_sram = true; + } + else { InitSRAM(&g_SRAM, SConfig::GetInstance().m_strSRAM); + s_using_overridden_sram = false; } CEXIMemoryCard::Init(); @@ -151,6 +158,12 @@ void Shutdown() channel.reset(); CEXIMemoryCard::Shutdown(); + + if (!s_using_overridden_sram) + { + File::IOFile file(SConfig::GetInstance().m_strSRAM, "wb"); + file.WriteArray(&g_SRAM, 1); + } } void DoState(PointerWrap& p) diff --git a/Source/Core/Core/HW/EXI/EXI.h b/Source/Core/Core/HW/EXI/EXI.h index 01661a0920..c18070f514 100644 --- a/Source/Core/Core/HW/EXI/EXI.h +++ b/Source/Core/Core/HW/EXI/EXI.h @@ -10,6 +10,7 @@ #include "Core/CoreTiming.h" class PointerWrap; +struct Sram; namespace CoreTiming { @@ -51,7 +52,7 @@ constexpr bool IsMemcardSlot(Slot slot) u8 SlotToEXIChannel(Slot slot); u8 SlotToEXIDevice(Slot slot); -void Init(); +void Init(const Sram* override_sram); void Shutdown(); void DoState(PointerWrap& p); void PauseAndLock(bool doLock, bool unpauseOnUnlock); diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp index b1ff59cbab..58d0aabf0d 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp @@ -139,15 +139,8 @@ CEXIIPL::CEXIIPL() FixSRAMChecksums(&g_SRAM); } -CEXIIPL::~CEXIIPL() -{ - // SRAM - if (!g_SRAM_netplay_initialized) - { - File::IOFile file(SConfig::GetInstance().m_strSRAM, "wb"); - file.WriteArray(&g_SRAM, 1); - } -} +CEXIIPL::~CEXIIPL() = default; + void CEXIIPL::DoState(PointerWrap& p) { p.Do(g_SRAM); diff --git a/Source/Core/Core/HW/HW.cpp b/Source/Core/Core/HW/HW.cpp index 9b9251c201..0cecd485c5 100644 --- a/Source/Core/Core/HW/HW.cpp +++ b/Source/Core/Core/HW/HW.cpp @@ -29,7 +29,7 @@ namespace HW { -void Init() +void Init(const Sram* override_sram) { CoreTiming::Init(); SystemTimers::PreInit(); @@ -41,7 +41,7 @@ void Init() VideoInterface::Init(); SerialInterface::Init(); ProcessorInterface::Init(); - ExpansionInterface::Init(); // Needs to be initialized before Memory + ExpansionInterface::Init(override_sram); // Needs to be initialized before Memory HSP::Init(); Memory::Init(); // Needs to be initialized before AddressSpace AddressSpace::Init(); diff --git a/Source/Core/Core/HW/HW.h b/Source/Core/Core/HW/HW.h index a527f65f85..1122ca5e2d 100644 --- a/Source/Core/Core/HW/HW.h +++ b/Source/Core/Core/HW/HW.h @@ -4,10 +4,11 @@ #pragma once class PointerWrap; +struct Sram; namespace HW { -void Init(); +void Init(const Sram* override_sram); void Shutdown(); void DoState(PointerWrap& p); } // namespace HW diff --git a/Source/Core/Core/HW/Sram.h b/Source/Core/Core/HW/Sram.h index 3063b9383d..12e4148ea3 100644 --- a/Source/Core/Core/HW/Sram.h +++ b/Source/Core/Core/HW/Sram.h @@ -139,4 +139,3 @@ void SetCardFlashID(Sram* sram, const u8* buffer, ExpansionInterface::Slot card_ void FixSRAMChecksums(Sram* sram); extern Sram g_SRAM; -extern bool g_SRAM_netplay_initialized; diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 090ff2353f..9c744420e5 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -432,10 +432,6 @@ void NetPlayClient::OnData(sf::Packet& packet) OnDesyncDetected(packet); break; - case MessageID::SyncGCSRAM: - OnSyncGCSRAM(packet); - break; - case MessageID::SyncSaveData: OnSyncSaveData(packet); break; @@ -893,6 +889,9 @@ void NetPlayClient::OnStartGame(sf::Packet& packet) packet >> m_net_settings.use_fma; packet >> m_net_settings.hide_remote_gbas; + for (size_t i = 0; i < sizeof(m_net_settings.sram); ++i) + packet >> m_net_settings.sram[i]; + m_net_settings.is_hosting = m_local_player->IsHost(); } @@ -960,20 +959,6 @@ void NetPlayClient::OnDesyncDetected(sf::Packet& packet) m_dialog->OnDesync(frame, player); } -void NetPlayClient::OnSyncGCSRAM(sf::Packet& packet) -{ - const size_t sram_settings_len = sizeof(g_SRAM) - offsetof(Sram, settings); - u8 sram[sram_settings_len]; - for (u8& cell : sram) - packet >> cell; - - { - std::lock_guard lkg(m_crit.game); - memcpy(&g_SRAM.settings, sram, sram_settings_len); - g_SRAM_netplay_initialized = true; - } -} - void NetPlayClient::OnSyncSaveData(sf::Packet& packet) { SyncSaveDataID sub_id; diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index 36fa2cc31a..5e7fe3a3c5 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -281,7 +281,6 @@ private: void OnPing(sf::Packet& packet); void OnPlayerPingData(sf::Packet& packet); void OnDesyncDetected(sf::Packet& packet); - void OnSyncGCSRAM(sf::Packet& packet); void OnSyncSaveData(sf::Packet& packet); void OnSyncSaveDataNotify(sf::Packet& packet); void OnSyncSaveDataRaw(sf::Packet& packet); diff --git a/Source/Core/Core/NetPlayProto.h b/Source/Core/Core/NetPlayProto.h index cc8eb77e23..cd78e6700b 100644 --- a/Source/Core/Core/NetPlayProto.h +++ b/Source/Core/Core/NetPlayProto.h @@ -12,6 +12,7 @@ #include "Core/Config/SYSCONFSettings.h" #include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" +#include "Core/HW/Sram.h" namespace DiscIO { @@ -105,6 +106,8 @@ struct NetSettings bool use_fma = false; bool hide_remote_gbas = false; + Sram sram; + // These aren't sent over the network directly bool is_hosting = false; std::array gba_rom_paths{}; @@ -180,7 +183,6 @@ enum class MessageID : u8 Pong = 0xE1, PlayerPingData = 0xE2, - SyncGCSRAM = 0xF0, SyncSaveData = 0xF1, SyncCodes = 0xF2, }; diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 1327ad2e03..f521e20a24 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -1436,20 +1436,9 @@ bool NetPlayServer::StartGame() const std::string region = Config::GetDirectoryForRegion( Config::ToGameCubeRegion(m_dialog->FindGameFile(m_selected_game_identifier)->GetRegion())); - // sync GC SRAM with clients - if (!g_SRAM_netplay_initialized) - { - SConfig::GetInstance().m_strSRAM = File::GetUserPath(F_GCSRAM_IDX); - InitSRAM(&g_SRAM, SConfig::GetInstance().m_strSRAM); - g_SRAM_netplay_initialized = true; - } - sf::Packet srampac; - srampac << MessageID::SyncGCSRAM; - for (size_t i = 0; i < sizeof(g_SRAM) - offsetof(Sram, settings); ++i) - { - srampac << g_SRAM[offsetof(Sram, settings) + i]; - } - SendAsyncToClients(std::move(srampac), 1); + // load host's GC SRAM + SConfig::GetInstance().m_strSRAM = File::GetUserPath(F_GCSRAM_IDX); + InitSRAM(&m_settings.sram, SConfig::GetInstance().m_strSRAM); // tell clients to start game sf::Packet spac; @@ -1544,6 +1533,9 @@ bool NetPlayServer::StartGame() spac << m_settings.use_fma; spac << m_settings.hide_remote_gbas; + for (size_t i = 0; i < sizeof(m_settings.sram); ++i) + spac << m_settings.sram[i]; + SendAsyncToClients(std::move(spac)); m_start_pending = false;