attempt at adding mirror instances and shito

This commit is contained in:
Arisotura 2023-03-25 00:10:40 +01:00
parent 6d3534bc3d
commit d9537d87cd
5 changed files with 117 additions and 5 deletions

View File

@ -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)
{

View File

@ -19,6 +19,8 @@
#ifndef IPC_H
#define IPC_H
#include <string>
#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);

View File

@ -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<<newid, IPC::Cmd_SetupNetplayMirror, sizeof(Player), player);
std::string rompath = ROMManager::FullROMPath.join('|').toStdString();
IPC::SendCommandStr(1<<newid, IPC::Cmd_LoadROM, rompath);
if (player.Address == 0x0100007F) player.Address = HostAddress;
IPC::SendCommand(1<<newid, IPC::Cmd_SetupNetplayMirror, sizeof(Player), &player);
return true;
}
@ -390,11 +443,20 @@ void StartGame()
return;
}
// spawn mirror instances as needed
for (int i = 1; i < NumPlayers; i++)
{
SpawnMirrorInstance(Players[i]);
}
// tell remote peers to start game
u8 cmd[1] = {0x01};
u8 cmd[1] = {0x04};
ENetPacket* pkt = enet_packet_create(cmd, sizeof(cmd), ENET_PACKET_FLAG_RELIABLE);
enet_host_broadcast(Host, 0, pkt);
// tell other mirror instances to start the game
IPC::SendCommand(0xFFFF, IPC::Cmd_Start, 0, nullptr);
// start game locally
NDS::Start();
emuThread->emuRun();
@ -542,6 +604,11 @@ void ProcessClient()
netplayDlg->updatePlayerList(Players, NumPlayers);
}
break;
case 0x04: // start game
NDS::Start();
emuThread->emuRun();
break;
}
}
break;

View File

@ -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('.'));

View File

@ -29,6 +29,8 @@
namespace ROMManager
{
extern QStringList FullROMPath;
extern SaveManager* NDSSave;
extern SaveManager* GBASave;