Skip to content

Commit

Permalink
Added netplay ROM comparison logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
thor2016 committed Mar 24, 2024
1 parent cc61b7b commit bed9b10
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class consoleWin_t : public QMainWindow
public:
signals:
void romLoaded(void);
void romUnload(void);
void stateLoaded(void);
void nesResetOccurred(void);

Expand Down
59 changes: 57 additions & 2 deletions src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QMessageBox>

#include "../../fceu.h"
#include "../../cart.h"
#include "../../state.h"
#include "../../movie.h"
#include "../../debug.h"
Expand Down Expand Up @@ -181,6 +182,11 @@ NetPlayServer::NetPlayServer(QObject *parent)

FCEU_WRAPPER_LOCK();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);

if (currCartInfo != nullptr)
{
romCrc32 = currCartInfo->CRC32;
}
FCEU_WRAPPER_UNLOCK();
}

Expand Down Expand Up @@ -443,6 +449,11 @@ void NetPlayServer::onRomLoad()
//printf("New ROM Loaded!\n");
FCEU_WRAPPER_LOCK();

if (currCartInfo != nullptr)
{
romCrc32 = currCartInfo->CRC32;
}

opsCrc32 = 0;
netPlayFrameData.reset();

Expand Down Expand Up @@ -618,6 +629,8 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
client->setPaused( (msg->flags & netPlayClientState::PAUSE_FLAG ) ? true : false );
client->setDesync( (msg->flags & netPlayClientState::DESYNC_FLAG) ? true : false );

client->romMatch = (romCrc32 == msg->romCrc32);

NetPlayFrameData data;
if ( (msg->opsFrame == 0) || netPlayFrameData.find( msg->opsFrame, data ) )
{
Expand All @@ -638,12 +651,11 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
if (client->desyncCount > forceResyncCount)
{
FCEU_WRAPPER_LOCK();
sendStateSyncReq( client );
resyncClient( client );
FCEU_WRAPPER_UNLOCK();

client->desyncCount = 0;
}

}
else
{
Expand Down Expand Up @@ -962,6 +974,19 @@ NetPlayClient::NetPlayClient(QObject *parent, bool outGoing)
{
printf("Error: NetPlayClient failed to allocate recvMsgBuf\n");
}

if (outGoing)
{
connect(consoleWindow, SIGNAL(romLoaded(void)), this, SLOT(onRomLoad(void)));
connect(consoleWindow, SIGNAL(romUnload(void)), this, SLOT(onRomUnload(void)));

FCEU_WRAPPER_LOCK();
if (currCartInfo != nullptr)
{
romCrc32 = currCartInfo->CRC32;
}
FCEU_WRAPPER_UNLOCK();
}
}


Expand Down Expand Up @@ -1025,6 +1050,25 @@ int NetPlayClient::Destroy()
return 0;
}
//-----------------------------------------------------------------------------
void NetPlayClient::onRomLoad()
{
FCEU_WRAPPER_LOCK();
if (currCartInfo != nullptr)
{
romCrc32 = currCartInfo->CRC32;
}
else
{
romCrc32 = 0;
}
FCEU_WRAPPER_UNLOCK();
}
//-----------------------------------------------------------------------------
void NetPlayClient::onRomUnload()
{
romCrc32 = 0;
}
//-----------------------------------------------------------------------------
void NetPlayClient::forceDisconnect()
{
disconnectPending = true;
Expand Down Expand Up @@ -1272,6 +1316,7 @@ void NetPlayClient::update(void)
statusMsg.opsFrame = lastFrameData.frameNum;
statusMsg.opsChkSum = lastFrameData.opsCrc32;
statusMsg.ramChkSum = lastFrameData.ramCrc32;
statusMsg.romCrc32 = romCrc32;
statusMsg.ctrlState[0] = (ctlrData ) & 0x000000ff;
statusMsg.ctrlState[1] = (ctlrData >> 8) & 0x000000ff;
statusMsg.ctrlState[2] = (ctlrData >> 16) & 0x000000ff;
Expand Down Expand Up @@ -1343,6 +1388,12 @@ int NetPlayClient::readMessages( void (*msgCallback)( void *userData, void *msgB
recvMsgSize = netPlayByteSwap(hdr->msgSize) - sizeof(netPlayMsgHdr);
recvMsgBytesLeft = recvMsgSize;

if ( (netPlayByteSwap(hdr->magic[0]) != NETPLAY_MAGIC_NUMBER) ||
(netPlayByteSwap(hdr->magic[1]) != NETPLAY_MAGIC_NUMBER) )
{
printf("Error: Message Header Validity Check Failed: %08X\n", recvMsgId);
}

if (netPlayByteSwap(hdr->msgSize) > recvMsgBufSize)
{
printf("Error: Message size too large: %08X\n", recvMsgId);
Expand Down Expand Up @@ -2122,6 +2173,10 @@ void NetPlayClientTreeItem::updateData()
{
state += QObject::tr(",Desync");
}
if (!client->romMatch)
{
state += QObject::tr(",ROM Mismatch");
}

setText( 0, client->userName );
setText( 1, QObject::tr(roleString) );
Expand Down
5 changes: 5 additions & 0 deletions src/drivers/Qt/NetPlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class NetPlayServer : public QTcpServer
uint32_t maxLeadFrames = 10u;
uint32_t clientWaitCounter = 0;
uint32_t inputFrameCount = 0;
uint32_t romCrc32 = 0;
bool enforceAppVersionCheck = true;
bool allowClientRomLoadReq = false;
bool allowClientStateLoadReq = false;
Expand Down Expand Up @@ -276,6 +277,7 @@ class NetPlayClient : public QObject
int state = 0;
int desyncCount = 0;
bool syncOk = false;
bool romMatch = false;
unsigned int currentFrame = 0;
unsigned int readyFrame = 0;
unsigned int catchUpThreshold = 10;
Expand All @@ -301,6 +303,7 @@ class NetPlayClient : public QObject
uint64_t pingDelaySum = 0;
uint64_t pingDelayLast = 0;
uint64_t pingNumSamples = 0;
uint32_t romCrc32 = 0;

std::list <NetPlayFrameInput> input;
FCEU::mutex inputMtx;
Expand All @@ -315,6 +318,8 @@ class NetPlayClient : public QObject
void onConnect(void);
void onDisconnect(void);
void onSocketError(QAbstractSocket::SocketError);
void onRomLoad(void);
void onRomUnload(void);
};


Expand Down
5 changes: 4 additions & 1 deletion src/drivers/Qt/NetPlayMsgDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,15 @@ struct netPlayClientState
uint32_t opsFrame; // Last frame for ops data
uint32_t opsChkSum;
uint32_t ramChkSum;
uint32_t romCrc32;
uint8_t ctrlState[4];

static constexpr uint32_t PAUSE_FLAG = 0x0001;
static constexpr uint32_t DESYNC_FLAG = 0x0002;

netPlayClientState(void)
: hdr(NETPLAY_CLIENT_STATE, sizeof(netPlayClientState)), flags(0),
frameRdy(0), frameRun(0), opsChkSum(0), ramChkSum(0)
frameRdy(0), frameRun(0), opsChkSum(0), ramChkSum(0), romCrc32(0)
{
memset( ctrlState, 0, sizeof(ctrlState) );
}
Expand All @@ -316,6 +317,7 @@ struct netPlayClientState
opsFrame = netPlayByteSwap(opsFrame);
opsChkSum = netPlayByteSwap(opsChkSum);
ramChkSum = netPlayByteSwap(ramChkSum);
romCrc32 = netPlayByteSwap(romCrc32);
}

void toNetworkByteOrder()
Expand All @@ -327,6 +329,7 @@ struct netPlayClientState
opsFrame = netPlayByteSwap(opsFrame);
opsChkSum = netPlayByteSwap(opsChkSum);
ramChkSum = netPlayByteSwap(ramChkSum);
romCrc32 = netPlayByteSwap(romCrc32);
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/drivers/Qt/fceuWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ CloseGame(void)
return(0);
}

// Signal to listeners that current ROM is being unloaded
if ( consoleWindow )
{
emit consoleWindow->romUnload();
}

// If the emulation thread is stuck hanging at a breakpoint,
// disable breakpoint debugging and wait for the thread to
// complete its frame. So that it is idle with a minimal call
Expand Down

0 comments on commit bed9b10

Please sign in to comment.