From d9537d87cd04ff5d56d817ed3a069a4b9804bc75 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Sat, 25 Mar 2023 00:10:40 +0100 Subject: [PATCH] attempt at adding mirror instances and shito --- src/frontend/qt_sdl/IPC.cpp | 35 ++++++++++++++ src/frontend/qt_sdl/IPC.h | 5 ++ src/frontend/qt_sdl/Netplay.cpp | 77 ++++++++++++++++++++++++++++-- src/frontend/qt_sdl/ROMManager.cpp | 3 ++ src/frontend/qt_sdl/ROMManager.h | 2 + 5 files changed, 117 insertions(+), 5 deletions(-) diff --git a/src/frontend/qt_sdl/IPC.cpp b/src/frontend/qt_sdl/IPC.cpp index 609d42d7..108dd76b 100644 --- a/src/frontend/qt_sdl/IPC.cpp +++ b/src/frontend/qt_sdl/IPC.cpp @@ -36,6 +36,9 @@ #include "IPC.h" #include "Config.h" #include "main.h" +#include "ROMManager.h" +#include "Netplay.h" +#include "NDS.h" extern EmuThread* emuThread; @@ -511,6 +514,29 @@ void ProcessCommands() case Cmd_Pause: emuThread->IPCPause(cmddata[0] != 0); break; + + case Cmd_LoadROM: + { + u16 len = *(u16*)&cmddata[0]; + cmddata[2+len] = '\0'; + + char* cstr = (char*)&cmddata[2]; + std::string str = cstr; + ROMManager::LoadROM(QString::fromStdString(str).split('|'), true); + } + break; + + case Cmd_SetupNetplayMirror: + { + Netplay::Player* player = (Netplay::Player*)&cmddata[0]; + Netplay::StartMirror(player); + } + break; + + case Cmd_Start: + NDS::Start(); + emuThread->emuRun(); + break; } } @@ -567,6 +593,15 @@ bool SendCommandU8(u16 recipients, u16 command, u8 arg) return SendCommand(recipients, command, 1, &data); } +bool SendCommandStr(u16 recipients, u16 command, std::string str) +{ + u8 data[kMaxCommandSize] = {0}; + + strncpy((char*)&data[2], str.c_str(), kMaxCommandSize-3); + *(u16*)&data[0] = strlen((const char*)&data[2]) + 1; + return SendCommand(recipients, command, 2+(*(u16*)&data[0]), data); +} + int SendMPPacketGeneric(u32 type, u8* packet, int len, u64 timestamp) { diff --git a/src/frontend/qt_sdl/IPC.h b/src/frontend/qt_sdl/IPC.h index cf1a2390..7a1ceab8 100644 --- a/src/frontend/qt_sdl/IPC.h +++ b/src/frontend/qt_sdl/IPC.h @@ -19,6 +19,8 @@ #ifndef IPC_H #define IPC_H +#include + #include "types.h" namespace IPC @@ -28,7 +30,9 @@ enum { Cmd_Pause = 1, + Cmd_LoadROM, Cmd_SetupNetplayMirror, + Cmd_Start, Cmd_MAX }; @@ -51,6 +55,7 @@ u16 GetInstanceBitmask(); void ProcessCommands(); bool SendCommand(u16 recipients, u16 command, u16 len, void* data); bool SendCommandU8(u16 recipients, u16 command, u8 arg); +bool SendCommandStr(u16 recipients, u16 command, std::string str); int SendMPPacket(u8* data, int len, u64 timestamp); int RecvMPPacket(u8* data, u64* timestamp); diff --git a/src/frontend/qt_sdl/Netplay.cpp b/src/frontend/qt_sdl/Netplay.cpp index 8328a121..d078da69 100644 --- a/src/frontend/qt_sdl/Netplay.cpp +++ b/src/frontend/qt_sdl/Netplay.cpp @@ -31,6 +31,7 @@ #include "IPC.h" #include "Netplay.h" #include "Input.h" +#include "ROMManager.h" #include "ui_NetplayStartHostDialog.h" #include "ui_NetplayStartClientDialog.h" @@ -272,7 +273,7 @@ void StartHost(const char* playername, int port) void StartClient(const char* playername, const char* host, int port) { - Host = enet_host_create(nullptr, 16, 1, 0, 0); + Host = enet_host_create(nullptr, 1, 1, 0, 0); if (!Host) { printf("client shat itself :(\n"); @@ -322,6 +323,54 @@ void StartClient(const char* playername, const char* host, int port) IsMirror = false; } +void StartMirror(const Player* player) +{ + MirrorHost = enet_host_create(nullptr, 1, 1, 0, 0); + if (!MirrorHost) + { + printf("mirror shat itself :(\n"); + return; + } + + printf("mirror created, connecting\n"); + + ENetAddress addr; + addr.host = player->Address; + addr.port = 8064+1 + player->ID; // FIXME!!!!!!!!!! + ENetPeer* peer = enet_host_connect(MirrorHost, &addr, 1, 0); + if (!peer) + { + printf("connect shat itself :(\n"); + return; + } + + ENetEvent event; + bool conn = false; + if (enet_host_service(MirrorHost, &event, 5000) > 0) + { + if (event.type == ENET_EVENT_TYPE_CONNECT) + { + printf("connected!\n"); + conn = true; + } + } + + if (!conn) + { + printf("connection failed\n"); + enet_peer_reset(peer); + return; + } + + memcpy(&MyPlayer, player, sizeof(Player)); + + HostAddress = addr.host; + + Active = true; + IsHost = false; + IsMirror = true; +} + u32 PlayerAddress(int id) { @@ -333,7 +382,7 @@ u32 PlayerAddress(int id) } -bool SpawnMirrorInstance(Player* player) +bool SpawnMirrorInstance(Player player) { u16 curmask = IPC::GetInstanceBitmask(); @@ -375,9 +424,13 @@ bool SpawnMirrorInstance(Player* player) if (newid == -1) return false; // setup that instance - printf("netplay: spawned mirror instance for player %d with ID %d, configuring\n", player->ID, newid); + printf("netplay: spawned mirror instance for player %d with ID %d, configuring\n", player.ID, newid); - IPC::SendCommand(1<emuRun(); @@ -542,6 +604,11 @@ void ProcessClient() netplayDlg->updatePlayerList(Players, NumPlayers); } break; + + case 0x04: // start game + NDS::Start(); + emuThread->emuRun(); + break; } } break; diff --git a/src/frontend/qt_sdl/ROMManager.cpp b/src/frontend/qt_sdl/ROMManager.cpp index a2a5fca6..8de2157e 100644 --- a/src/frontend/qt_sdl/ROMManager.cpp +++ b/src/frontend/qt_sdl/ROMManager.cpp @@ -41,6 +41,8 @@ using namespace Platform; namespace ROMManager { +QStringList FullROMPath; + int CartType = -1; std::string BaseROMDir = ""; std::string BaseROMName = ""; @@ -729,6 +731,7 @@ bool LoadROM(QStringList filepath, bool reset) if (NDSSave) delete NDSSave; NDSSave = nullptr; + FullROMPath = filepath; BaseROMDir = basepath; BaseROMName = romname; BaseAssetName = romname.substr(0, romname.rfind('.')); diff --git a/src/frontend/qt_sdl/ROMManager.h b/src/frontend/qt_sdl/ROMManager.h index 8e199dba..99b57ba6 100644 --- a/src/frontend/qt_sdl/ROMManager.h +++ b/src/frontend/qt_sdl/ROMManager.h @@ -29,6 +29,8 @@ namespace ROMManager { +extern QStringList FullROMPath; + extern SaveManager* NDSSave; extern SaveManager* GBASave;