Core: Pass Netplay SRAM through boot process.

This removes the Netplay classes touching emulated hardware structures before emulation even starts.
This commit is contained in:
Admiral H. Curtiss 2022-09-13 04:44:59 +02:00
parent 9963637463
commit 0a517ebdbd
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
12 changed files with 37 additions and 56 deletions

View File

@ -93,10 +93,6 @@ bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)
Config::AddLayer(ConfigLoaders::GenerateNetPlayConfigLoader(*netplay_settings)); Config::AddLayer(ConfigLoaders::GenerateNetPlayConfigLoader(*netplay_settings));
StartUp.bCopyWiiSaveNetplay = netplay_settings->copy_wii_save; 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 // Override out-of-region languages/countries to prevent games from crashing or behaving oddly
if (!Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS)) if (!Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS))

View File

@ -511,7 +511,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
AudioCommon::InitSoundStream(); AudioCommon::InitSoundStream();
Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream}; Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream};
HW::Init(); HW::Init(NetPlay::IsNetPlayRunning() ? &(boot_session_data.GetNetplaySettings()->sram) : nullptr);
Common::ScopeGuard hw_guard{[] { Common::ScopeGuard hw_guard{[] {
// We must set up this flag before executing HW::Shutdown() // We must set up this flag before executing HW::Shutdown()

View File

@ -8,6 +8,7 @@
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/IOFile.h"
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
@ -24,7 +25,7 @@
#include "DiscIO/Enums.h" #include "DiscIO/Enums.h"
Sram g_SRAM; Sram g_SRAM;
bool g_SRAM_netplay_initialized = false; bool s_using_overridden_sram = false;
namespace ExpansionInterface 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); InitSRAM(&g_SRAM, SConfig::GetInstance().m_strSRAM);
s_using_overridden_sram = false;
} }
CEXIMemoryCard::Init(); CEXIMemoryCard::Init();
@ -151,6 +158,12 @@ void Shutdown()
channel.reset(); channel.reset();
CEXIMemoryCard::Shutdown(); CEXIMemoryCard::Shutdown();
if (!s_using_overridden_sram)
{
File::IOFile file(SConfig::GetInstance().m_strSRAM, "wb");
file.WriteArray(&g_SRAM, 1);
}
} }
void DoState(PointerWrap& p) void DoState(PointerWrap& p)

View File

@ -10,6 +10,7 @@
#include "Core/CoreTiming.h" #include "Core/CoreTiming.h"
class PointerWrap; class PointerWrap;
struct Sram;
namespace CoreTiming namespace CoreTiming
{ {
@ -51,7 +52,7 @@ constexpr bool IsMemcardSlot(Slot slot)
u8 SlotToEXIChannel(Slot slot); u8 SlotToEXIChannel(Slot slot);
u8 SlotToEXIDevice(Slot slot); u8 SlotToEXIDevice(Slot slot);
void Init(); void Init(const Sram* override_sram);
void Shutdown(); void Shutdown();
void DoState(PointerWrap& p); void DoState(PointerWrap& p);
void PauseAndLock(bool doLock, bool unpauseOnUnlock); void PauseAndLock(bool doLock, bool unpauseOnUnlock);

View File

@ -139,15 +139,8 @@ CEXIIPL::CEXIIPL()
FixSRAMChecksums(&g_SRAM); FixSRAMChecksums(&g_SRAM);
} }
CEXIIPL::~CEXIIPL() CEXIIPL::~CEXIIPL() = default;
{
// SRAM
if (!g_SRAM_netplay_initialized)
{
File::IOFile file(SConfig::GetInstance().m_strSRAM, "wb");
file.WriteArray(&g_SRAM, 1);
}
}
void CEXIIPL::DoState(PointerWrap& p) void CEXIIPL::DoState(PointerWrap& p)
{ {
p.Do(g_SRAM); p.Do(g_SRAM);

View File

@ -29,7 +29,7 @@
namespace HW namespace HW
{ {
void Init() void Init(const Sram* override_sram)
{ {
CoreTiming::Init(); CoreTiming::Init();
SystemTimers::PreInit(); SystemTimers::PreInit();
@ -41,7 +41,7 @@ void Init()
VideoInterface::Init(); VideoInterface::Init();
SerialInterface::Init(); SerialInterface::Init();
ProcessorInterface::Init(); ProcessorInterface::Init();
ExpansionInterface::Init(); // Needs to be initialized before Memory ExpansionInterface::Init(override_sram); // Needs to be initialized before Memory
HSP::Init(); HSP::Init();
Memory::Init(); // Needs to be initialized before AddressSpace Memory::Init(); // Needs to be initialized before AddressSpace
AddressSpace::Init(); AddressSpace::Init();

View File

@ -4,10 +4,11 @@
#pragma once #pragma once
class PointerWrap; class PointerWrap;
struct Sram;
namespace HW namespace HW
{ {
void Init(); void Init(const Sram* override_sram);
void Shutdown(); void Shutdown();
void DoState(PointerWrap& p); void DoState(PointerWrap& p);
} // namespace HW } // namespace HW

View File

@ -139,4 +139,3 @@ void SetCardFlashID(Sram* sram, const u8* buffer, ExpansionInterface::Slot card_
void FixSRAMChecksums(Sram* sram); void FixSRAMChecksums(Sram* sram);
extern Sram g_SRAM; extern Sram g_SRAM;
extern bool g_SRAM_netplay_initialized;

View File

@ -432,10 +432,6 @@ void NetPlayClient::OnData(sf::Packet& packet)
OnDesyncDetected(packet); OnDesyncDetected(packet);
break; break;
case MessageID::SyncGCSRAM:
OnSyncGCSRAM(packet);
break;
case MessageID::SyncSaveData: case MessageID::SyncSaveData:
OnSyncSaveData(packet); OnSyncSaveData(packet);
break; break;
@ -893,6 +889,9 @@ void NetPlayClient::OnStartGame(sf::Packet& packet)
packet >> m_net_settings.use_fma; packet >> m_net_settings.use_fma;
packet >> m_net_settings.hide_remote_gbas; 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(); m_net_settings.is_hosting = m_local_player->IsHost();
} }
@ -960,20 +959,6 @@ void NetPlayClient::OnDesyncDetected(sf::Packet& packet)
m_dialog->OnDesync(frame, player); 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) void NetPlayClient::OnSyncSaveData(sf::Packet& packet)
{ {
SyncSaveDataID sub_id; SyncSaveDataID sub_id;

View File

@ -281,7 +281,6 @@ private:
void OnPing(sf::Packet& packet); void OnPing(sf::Packet& packet);
void OnPlayerPingData(sf::Packet& packet); void OnPlayerPingData(sf::Packet& packet);
void OnDesyncDetected(sf::Packet& packet); void OnDesyncDetected(sf::Packet& packet);
void OnSyncGCSRAM(sf::Packet& packet);
void OnSyncSaveData(sf::Packet& packet); void OnSyncSaveData(sf::Packet& packet);
void OnSyncSaveDataNotify(sf::Packet& packet); void OnSyncSaveDataNotify(sf::Packet& packet);
void OnSyncSaveDataRaw(sf::Packet& packet); void OnSyncSaveDataRaw(sf::Packet& packet);

View File

@ -12,6 +12,7 @@
#include "Core/Config/SYSCONFSettings.h" #include "Core/Config/SYSCONFSettings.h"
#include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI.h"
#include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/EXI/EXI_Device.h"
#include "Core/HW/Sram.h"
namespace DiscIO namespace DiscIO
{ {
@ -105,6 +106,8 @@ struct NetSettings
bool use_fma = false; bool use_fma = false;
bool hide_remote_gbas = false; bool hide_remote_gbas = false;
Sram sram;
// These aren't sent over the network directly // These aren't sent over the network directly
bool is_hosting = false; bool is_hosting = false;
std::array<std::string, 4> gba_rom_paths{}; std::array<std::string, 4> gba_rom_paths{};
@ -180,7 +183,6 @@ enum class MessageID : u8
Pong = 0xE1, Pong = 0xE1,
PlayerPingData = 0xE2, PlayerPingData = 0xE2,
SyncGCSRAM = 0xF0,
SyncSaveData = 0xF1, SyncSaveData = 0xF1,
SyncCodes = 0xF2, SyncCodes = 0xF2,
}; };

View File

@ -1436,20 +1436,9 @@ bool NetPlayServer::StartGame()
const std::string region = Config::GetDirectoryForRegion( const std::string region = Config::GetDirectoryForRegion(
Config::ToGameCubeRegion(m_dialog->FindGameFile(m_selected_game_identifier)->GetRegion())); Config::ToGameCubeRegion(m_dialog->FindGameFile(m_selected_game_identifier)->GetRegion()));
// sync GC SRAM with clients // load host's GC SRAM
if (!g_SRAM_netplay_initialized) SConfig::GetInstance().m_strSRAM = File::GetUserPath(F_GCSRAM_IDX);
{ InitSRAM(&m_settings.sram, SConfig::GetInstance().m_strSRAM);
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);
// tell clients to start game // tell clients to start game
sf::Packet spac; sf::Packet spac;
@ -1544,6 +1533,9 @@ bool NetPlayServer::StartGame()
spac << m_settings.use_fma; spac << m_settings.use_fma;
spac << m_settings.hide_remote_gbas; 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)); SendAsyncToClients(std::move(spac));
m_start_pending = false; m_start_pending = false;