diff --git a/Core/GameEngine/Include/GameNetwork/FileTransfer.h b/Core/GameEngine/Include/GameNetwork/FileTransfer.h index 474599405f0..ea9285bf168 100644 --- a/Core/GameEngine/Include/GameNetwork/FileTransfer.h +++ b/Core/GameEngine/Include/GameNetwork/FileTransfer.h @@ -46,3 +46,7 @@ AsciiString GetReadmeFromMap( AsciiString path ); // The meat of file (map) transfers Bool DoAnyMapTransfers(GameInfo *game); + +// Resume-from-replay: the host pushes the resume source +// (a replay file) to every human guest over the same transfer path at game start. +Bool DoResumeReplayTransfer(GameInfo *game, AsciiString path); diff --git a/Core/GameEngine/Include/GameNetwork/GameInfo.h b/Core/GameEngine/Include/GameNetwork/GameInfo.h index 5483bbeba58..e2b10e24053 100644 --- a/Core/GameEngine/Include/GameNetwork/GameInfo.h +++ b/Core/GameEngine/Include/GameNetwork/GameInfo.h @@ -220,6 +220,13 @@ class GameInfo void setCRCInterval( Int val ) { m_crcInterval = (val > 0 && val < 100) ? val : 100; } Int getCRCInterval() const { return m_crcInterval; } + // Resume-from-replay arming. When non-zero, every client + // replays its copy of the resume source (Resume.rep, pushed from the host) in lockstep + // up to this frame on game start before live control resumes (see ResumeFromReplay). + void setResumeHandoffFrame( UnsignedInt f ) { m_resumeHandoffFrame = f; } + UnsignedInt getResumeHandoffFrame() const { return m_resumeHandoffFrame; } + Bool isResumeArmed() const { return m_resumeHandoffFrame != 0; } + Bool haveWeSurrendered() { return m_surrendered; } void markAsSurrendered() { m_surrendered = TRUE; } @@ -254,6 +261,9 @@ class GameInfo Money m_startingCash; UnsignedShort m_superweaponRestriction; Bool m_oldFactionsOnly; // Only USA, China, GLA -- not USA Air Force General, GLA Toxic General, et al + + // resume-from-replay arming (appended last so existing member offsets are untouched) + UnsignedInt m_resumeHandoffFrame; }; extern GameInfo *TheGameInfo; diff --git a/Core/GameEngine/Include/GameNetwork/NetworkDefs.h b/Core/GameEngine/Include/GameNetwork/NetworkDefs.h index 6295493ea7d..eba656831bb 100644 --- a/Core/GameEngine/Include/GameNetwork/NetworkDefs.h +++ b/Core/GameEngine/Include/GameNetwork/NetworkDefs.h @@ -43,6 +43,12 @@ static Int MIN_RUNAHEAD = 6; static constexpr const Int MIN_RUNAHEAD = 4; #endif +// Resume-from-replay catchup pins the run-ahead and the +// logic frame rate that ConnectionManager::updateRunAhead negotiates, so every +// peer derives the same short packet send interval while fast-forwarding. +static constexpr const Int CATCHUP_RUNAHEAD = 32; +static constexpr const Int CATCHUP_FRAME_RATE = 1000; + // FRAME_DATA_LENGTH needs to be MAX_FRAMES_AHEAD+1 because a player on a different // computer can send commands for a frame that is one beyond twice the max runahead. static constexpr const Int FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD + 1) * 2; diff --git a/Core/GameEngine/Source/Common/FramePacer.cpp b/Core/GameEngine/Source/Common/FramePacer.cpp index d764c9c1414..809d6458826 100644 --- a/Core/GameEngine/Source/Common/FramePacer.cpp +++ b/Core/GameEngine/Source/Common/FramePacer.cpp @@ -24,6 +24,7 @@ #include "GameLogic/GameLogic.h" #include "GameLogic/ScriptEngine.h" +#include "Common/Recorder.h" #include "GameNetwork/NetworkDefs.h" #include "GameNetwork/NetworkInterface.h" @@ -109,6 +110,14 @@ Bool FramePacer::isActualFramesPerSecondLimitEnabled() const #endif } + // Resume-from-replay catchup runs one logic frame per + // render frame and fast-forwards many times faster than realtime, so the render + // cap has to get out of the way until the lead-in. + if (TheRecorder != nullptr && TheRecorder->isResumeCatchupMode() && !TheRecorder->isResumeCatchupLeadIn()) + { + allowFpsLimit = false; + } + allowFpsLimit &= TheGlobalData->m_useFpsLimit; allowFpsLimit &= isFramesPerSecondLimitEnabled(); diff --git a/Core/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp b/Core/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp index 5e2c41a3d48..142296b3f08 100644 --- a/Core/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp +++ b/Core/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp @@ -35,6 +35,7 @@ #include "Common/Money.h" #include "Common/Player.h" #include "Common/PlayerList.h" +#include "Common/Recorder.h" #include "Common/Science.h" #include "Common/SpecialPower.h" #include "Common/ThingTemplate.h" @@ -120,6 +121,11 @@ CBCommandStatus ControlBar::processCommandTransitionUI( GameWindow *control, Gad CBCommandStatus ControlBar::processCommandUI( GameWindow *control, GadgetGameMessage gadgetMessage ) { + // Resume-from-replay catchup: control bar buttons and + // their hotkeys (which re-dispatch into this path) are inert until control is handed back + if( TheRecorder && TheRecorder->isResumeInputBlocked() ) + return CBC_COMMAND_NOT_USED; + // get the command pointer from the control user data we put in the button const CommandButton *commandButton = (const CommandButton *)GadgetButtonGetData(control); if( !commandButton ) diff --git a/Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp b/Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp index 231a6f355b4..54a3b5803a9 100644 --- a/Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp +++ b/Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp @@ -2550,8 +2550,12 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage GameMessage::Type t = msg->getType(); GameMessageDisposition disp = KEEP_MESSAGE; // We want to always be able to get to the options menu even during no input times and a clear game data message should always go through + // Resume-from-replay catchup blocks every command the + // same way disabled input does (camera scrolling lives in LookAtTranslator and + // stays available); the options menu stays reachable so a stuck catchup can be left. + const Bool resumeBlocked = TheRecorder && TheRecorder->isResumeInputBlocked(); if (t != GameMessage::MSG_META_OPTIONS && t != GameMessage::MSG_CLEAR_GAME_DATA && - !TheInGameUI->getInputEnabled() && !isSystemMessage(msg)) + (!TheInGameUI->getInputEnabled() || resumeBlocked) && !isSystemMessage(msg)) { return DESTROY_MESSAGE; } diff --git a/Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp b/Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp index 9bea5b26d4d..41230d952bf 100644 --- a/Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp +++ b/Core/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp @@ -54,6 +54,7 @@ #include "GameClient/Keyboard.h" #include "GameClient/SelectionInfo.h" #include "GameClient/SelectionXlat.h" +#include "Common/Recorder.h" #include "GameClient/TerrainVisual.h" @@ -350,7 +351,8 @@ GameMessageDisposition SelectionTranslator::translateGameMessage(const GameMessa { GameMessageDisposition disp = KEEP_MESSAGE; - if( !TheInGameUI->getInputEnabled() ) + // Resume-from-replay catchup: no selecting until control is handed back + if( !TheInGameUI->getInputEnabled() || (TheRecorder && TheRecorder->isResumeInputBlocked()) ) { //Keep the message so the other translators (WindowXlat) can handle. if( m_dragSelecting ) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index f347db5a83f..0485393ba23 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -67,7 +67,8 @@ static Bool hasValidTransferFileExtension(const AsciiString& filePath) "str", "wak", "tga", - "txt" + "txt", + "rep" // The resume-from-replay source (see ResumeFromReplay) }; const char* fileExt = strrchr(filePath.str(), '.'); @@ -99,6 +100,7 @@ enum TransferFileType TransferFileType_Txt, TransferFileType_Tga, TransferFileType_Wak, + TransferFileType_Rep, TransferFileType_Count }; @@ -116,6 +118,7 @@ static const TransferFileRule transferFileRules[TransferFileType_Count] = { ".txt", 1 * 1024 * 1024 }, { ".tga", 2 * 1024 * 1024 }, { ".wak", 128 * 1024 }, + { ".rep", 16 * 1024 * 1024 }, }; static TransferFileType getTransferFileType(const char* extension) @@ -173,6 +176,18 @@ static Bool hasValidTransferFileContent(const AsciiString& filePath, const Unsig break; } + case TransferFileType_Rep: + { + // a replay starts with the GENREP tag (see RecorderClass::startRecording) + static const char replayTag[] = "GENREP"; + if (dataSize < sizeof(replayTag) - 1 || memcmp(data, replayTag, sizeof(replayTag) - 1) != 0) + { + DEBUG_LOG(("Replay file '%s' does not start with GENREP.", filePath.str())); + return false; + } + break; + } + case TransferFileType_Tga: { if (dataSize < sizeof(TGAHeader) + sizeof(TGA2Footer)) @@ -1510,6 +1525,24 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS newRunAhead = clamp(minRunAheadForClamp, newRunAhead, MAX_FRAMES_AHEAD / 2); + // During resume-from-replay catchup Network::update() + // drops the realtime frame gate, so the only thing bounding how fast we + // replay is lockstep: a frame advances once every peer's commands for it have + // arrived. Throughput is (frames in flight) / (packet send interval), and + // processRunAheadCommand derives that interval from runAhead and frameRate. + // The values negotiated above are tuned for realtime play and are hostile + // here (a low-latency LAN bottoms out at MIN_RUNAHEAD, barely 2x realtime), + // so pin a deep pipeline and a short send interval instead. The lead-in + // (last seconds before handoff) falls through to the normal negotiation so + // run-ahead and frame rate re-converge before control is handed back. + const Bool pinCatchupRates = TheRecorder + && TheRecorder->isResumeCatchupMode() + && !TheRecorder->isResumeCatchupLeadIn(); + if (pinCatchupRates) { + newRunAhead = CATCHUP_RUNAHEAD; + minFps = CATCHUP_FRAME_RATE; + } + NetRunAheadCommandMsg* msg = newInstance(NetRunAheadCommandMsg); msg->setPlayerID(m_localSlot); if (DoesCommandRequireACommandID(msg->getNetCommandType())) { @@ -1582,14 +1615,17 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS // Let the player with the slowest FPS run a little faster than the other computers... // just in case they are able to. Then we might be able to run the game faster which would be good. - Int newMinFps = (minFps * 11) / 10; - if (newMinFps == minFps) { - newMinFps = minFps + 1; - } - - - if (newMinFps > TheNetwork->getFrameRate()) { - newMinFps = TheNetwork->getFrameRate(); // Cap FPS to network frame rate. + // During catchup every peer must derive the SAME packet send interval, so the + // slowest-player nudge and the network frame-rate cap are skipped. + Int newMinFps = minFps; + if (!pinCatchupRates) { + newMinFps = (minFps * 11) / 10; + if (newMinFps == minFps) { + newMinFps = minFps + 1; + } + if (newMinFps > TheNetwork->getFrameRate()) { + newMinFps = TheNetwork->getFrameRate(); // Cap FPS to network frame rate. + } } msg2->setRunAhead(newRunAhead); msg2->setFrameRate(newMinFps); diff --git a/Core/GameEngine/Source/GameNetwork/DisconnectManager.cpp b/Core/GameEngine/Source/GameNetwork/DisconnectManager.cpp index 9d2f1b58574..526084d3467 100644 --- a/Core/GameEngine/Source/GameNetwork/DisconnectManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/DisconnectManager.cpp @@ -98,6 +98,12 @@ void DisconnectManager::update(ConnectionManager *conMgr) { m_lastFrameTime = timeGetTime(); } + // The resume-from-replay freeze deliberately holds the + // logic frame for a countdown; that is not a stalled peer. + if (TheRecorder && TheRecorder->isResumeFreezeActive()) { + m_lastFrameTime = timeGetTime(); + } + // The game logic stalls on the frame we are currently waiting for commands on, // so we have to check for the current logic frame being one higher than // the last one we had the commands ready for. diff --git a/Core/GameEngine/Source/GameNetwork/FileTransfer.cpp b/Core/GameEngine/Source/GameNetwork/FileTransfer.cpp index 5d9cc7dfe64..46b381a20d8 100644 --- a/Core/GameEngine/Source/GameNetwork/FileTransfer.cpp +++ b/Core/GameEngine/Source/GameNetwork/FileTransfer.cpp @@ -82,7 +82,9 @@ static Bool doFileTransfer( AsciiString filename, MapTransferLoadScreen *ls, Int fileTransferPercent = 100; for (i=1; igetConstSlot(i)->isHuman() && !TheGameInfo->getConstSlot(i)->hasMap()) + // Wait on the slots in the mask (the callers build it + // from the slots that lack the file), so the same loop serves the resume-from-replay transfer. + if (TheGameInfo->getConstSlot(i)->isHuman() && (mask & (1<getFileTransferProgress(i, filename); fileTransferPercent = min(fileTransferPercent, slotTransferPercent); @@ -281,3 +283,38 @@ Bool DoAnyMapTransfers(GameInfo *game) TheShell->showShell(); return ok; } + +//------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------------- + +// Push the resume-from-replay source from the host to every +// human guest. Every guest needs it (there is no "has file" state for replays), so the mask is +// every human slot but the host's; the file lands in the guest's replay directory under the same +// name (see GameState::portableMapPathToRealMapPath). +Bool DoResumeReplayTransfer(GameInfo *game, AsciiString path) +{ + TheGameInfo = game; + path.toLower(); // the transfer bookkeeping compares against the normalized (lower-case) real path + Int mask = 0; + Int i=0; + for (i=1; igetConstSlot(i)->isHuman()) + { + DEBUG_LOG(("Adding player %d to resume replay transfer mask", i)); + mask |= (1<hideShell(); + MapTransferLoadScreen *ls = NEW MapTransferLoadScreen; + ls->init(TheGameInfo); + Bool ok = doFileTransfer(path, ls, mask); + delete ls; + ls = nullptr; + if (!ok) + TheShell->showShell(); + return ok; +} diff --git a/Core/GameEngine/Source/GameNetwork/FrameMetrics.cpp b/Core/GameEngine/Source/GameNetwork/FrameMetrics.cpp index 0de0dd2096c..1d6cc03a34b 100644 --- a/Core/GameEngine/Source/GameNetwork/FrameMetrics.cpp +++ b/Core/GameEngine/Source/GameNetwork/FrameMetrics.cpp @@ -30,6 +30,7 @@ #include "GameNetwork/FrameMetrics.h" #include "GameClient/Display.h" +#include "Common/Recorder.h" #include "GameNetwork/networkutil.h" #include "../NGMP_include.h" #include "../NetworkMesh.h" @@ -145,16 +146,21 @@ void FrameMetrics::doPerFrameMetrics(UnsignedInt frame) { // Do the measurement of the fps. time_t curTime = timeGetTime(); if ((curTime - m_lastFpsTimeThing) >= 1000) { -// if ((m_fpsListIndex % 16) == 0) { -// DEBUG_LOG(("FrameMetrics::doPerFrameMetrics - adding %f to fps history. average before: %f ", m_fpsList[m_fpsListIndex], m_averageFps)); -// } - m_averageFps -= ((m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // subtract out the old value from the average. - m_fpsList[m_fpsListIndex] = TheDisplay->getAverageFPS(); -// m_fpsList[m_fpsListIndex] = TheGameClient->getFrame() - m_fpsStartingFrame; - m_averageFps += ((Real)(m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // add the new value to the average. -// DEBUG_LOG(("average after: %f", m_averageFps)); - ++m_fpsListIndex; - m_fpsListIndex %= TheGlobalData->m_networkFPSHistoryLength; + // This history is the input to + // ConnectionManager::updateRunAhead, i.e. how this machine tells the rest of + // the game how fast it can run. During resume-from-replay catchup the renderer + // is deliberately throttled while logic fast-forwards, so display FPS measures + // the throttle, not the machine; sampling it would collapse the run-ahead + // negotiation. Hold the history steady through catchup and resume sampling + // for real on handoff. + const Bool inCatchup = (TheRecorder != nullptr) && TheRecorder->isResumeCatchupMode(); + if (!inCatchup) { + m_averageFps -= ((m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // subtract out the old value from the average. + m_fpsList[m_fpsListIndex] = TheDisplay->getAverageFPS(); + m_averageFps += ((Real)(m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // add the new value to the average. + ++m_fpsListIndex; + m_fpsListIndex %= TheGlobalData->m_networkFPSHistoryLength; + } m_lastFpsTimeThing = curTime; } diff --git a/Core/GameEngine/Source/GameNetwork/GameInfo.cpp b/Core/GameEngine/Source/GameNetwork/GameInfo.cpp index d7bd46bd336..0f78a017c91 100644 --- a/Core/GameEngine/Source/GameNetwork/GameInfo.cpp +++ b/Core/GameEngine/Source/GameNetwork/GameInfo.cpp @@ -323,6 +323,7 @@ void GameInfo::reset() m_mapSize = 0; m_superweaponRestriction = 0; m_startingCash = TheGlobalData->m_defaultStartingCash; + m_resumeHandoffFrame = 0; for (Int i=0; igetStartingCash().countMoney(), game->oldFactionsOnly() ? 'Y' : 'N' ); #endif + // Resume-from-replay handoff frame. Only emitted when + // armed so unarmed games still round-trip bit-identically through the serializer, and + // emitted before the slot list so the slot-name truncation below accounts for it. + if (game->isResumeArmed()) + { + AsciiString resume; + resume.format("RH=%u;", game->getResumeHandoffFrame()); + optionsString.concat(resume); + } + //add player info for each slot optionsString.concat(slotListID); optionsString.concat('='); @@ -1028,6 +1039,7 @@ Bool ParseAsciiStringToGameInfo(GameInfo *game, AsciiString options) Int useStats = TRUE; Money startingCash = TheGlobalData->m_defaultStartingCash; UnsignedShort restriction = 0; // Always the default + UnsignedInt resumeHandoffFrame = 0; Bool sawMap = FALSE; Bool sawMapCRC = FALSE; @@ -1147,6 +1159,11 @@ Bool ParseAsciiStringToGameInfo(GameInfo *game, AsciiString options) oldFactionsOnly = ( val.compareNoCase( "Y" ) == 0 ); sawOldFactions = TRUE; } + else if (key.compare("RH") == 0) + { + // resume-from-replay handoff frame (see GameInfoToAsciiString) + resumeHandoffFrame = (UnsignedInt)strtoul(val.str(), nullptr, 10); + } else if (key.getLength() == 1 && *key.str() == slotListID) { sawSlotlist = true; @@ -1515,6 +1532,7 @@ Bool ParseAsciiStringToGameInfo(GameInfo *game, AsciiString options) game->setSuperweaponRestriction(restriction); game->setStartingCash(startingCash); game->setOldFactionsOnly(oldFactionsOnly); + game->setResumeHandoffFrame(resumeHandoffFrame); return true; } diff --git a/Core/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp b/Core/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp index 701ef3e987e..a775529a9c8 100644 --- a/Core/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp +++ b/Core/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp @@ -46,6 +46,7 @@ #include "GameNetwork/FileTransfer.h" #include "GameNetwork/LANAPICallbacks.h" #include "GameNetwork/networkutil.h" +#include "GameNetwork/ResumeFromReplay.h" LANAPI *TheLAN = nullptr; extern Bool LANbuttonPushed; @@ -243,6 +244,38 @@ void LANAPI::OnGameStart() return; } + // An armed resume-from-replay first pushes the host's + // resume source to every guest over the map transfer path, then every client checks its + // copy against the lobby and arms the recorder. Either failing backs out the same way as + // a missing map rather than start a fresh match that would desync everyone. + if (m_currentGame->isResumeArmed()) + { + UnicodeString resumeWhy; + Bool resumeOk = DoResumeReplayTransfer(m_currentGame, ResumeFromReplay::getSourceFilePath()); + if (!resumeOk) + resumeWhy = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeTransferFailed", L"Resume aborted: the resume replay could not be transferred"); + else + resumeOk = ResumeFromReplay::prepareGameStart(m_currentGame, resumeWhy); + if (!resumeOk) + { + DEBUG_LOG(("Resume-from-replay transfer or validation failed. Bailing...")); + OnPlayerLeave(m_name); + removeGame(m_currentGame); + m_currentGame = nullptr; + m_inLobby = TRUE; + + delete TheNetwork; + TheNetwork = nullptr; + + OnChat(UnicodeString::TheEmptyString, 0, resumeWhy, LANCHAT_SYSTEM); + return; + } + } + else if (TheRecorder != nullptr) + { + TheRecorder->disarmResume(); + } + m_currentGame->startGame(0); // shutdown the top, but do not pop it off the stack diff --git a/Core/GameEngine/Source/GameNetwork/Network.cpp b/Core/GameEngine/Source/GameNetwork/Network.cpp index a149bca7179..3836f4b0609 100644 --- a/Core/GameEngine/Source/GameNetwork/Network.cpp +++ b/Core/GameEngine/Source/GameNetwork/Network.cpp @@ -485,6 +485,13 @@ void Network::attachTransport(Transport* transport) { } Bool Network::isMessageTypeWithinNetworkRange(GameMessage::Type type) { + // During resume-from-replay catchup every peer injects + // the same commands from its own copy of the replay, so re-broadcasting them + // would double them on the receiver. Treat everything as non-transfer so it flows + // through processCommand instead, which also drives the per-frame tick forward. + if (TheRecorder && TheRecorder->isResumeCatchupMode()) { + return FALSE; + } return type > GameMessage::MSG_BEGIN_NETWORK_MESSAGES && type < GameMessage::MSG_END_NETWORK_MESSAGES; } @@ -492,11 +499,20 @@ Bool Network::isMessageTypeWithinNetworkRange(GameMessage::Type type) { * Take commands from TheCommandList and give them to the connection manager for transport. */ void Network::GetCommandsFromCommandList() { + // During resume-from-replay catchup the local CRCs + // are not exchanged (every peer is replaying the source's recorded CRC messages instead), + // and leaving one in TheCommandList would have it execute a frame later on its own: on + // the handoff frame that is a lone CRC with validation back on, a false "Not enough CRCs". + const Bool inCatchup = TheRecorder && TheRecorder->isResumeCatchupMode(); GameMessage* msg = TheCommandList->getFirstMessage(); GameMessage* next = NULL; while (msg != NULL) { next = msg->next(); - if (isMessageTypeWithinNetworkRange(msg->getType())) { // Is this something we should be sending to the other players? + if (inCatchup && msg->getType() == GameMessage::MSG_LOGIC_CRC) { + TheCommandList->removeMessage(msg); + deleteInstance(msg); + } + else if (isMessageTypeWithinNetworkRange(msg->getType())) { // Is this something we should be sending to the other players? if (m_localStatus == NETLOCALSTATUS_INGAME) { m_conMgr->sendLocalGameMessage(msg, getExecutionFrame()); } @@ -631,7 +647,8 @@ void Network::RelayCommandsToCommandList(UnsignedInt frame) { if (isMessageTypeWithinNetworkRange(gmsg->getGameMessageType())) { //DEBUG_LOG(("Network::RelayCommandsToCommandList - appending command %d of type %s to command list on frame %d", msg->getCommand()->getID(), gmsg->getCommandAsString(), TheGameLogic->getFrame())); TheCommandList->appendMessage(gmsg->constructGameMessage()); - } else { + } else if (!(TheRecorder && TheRecorder->isResumeCatchupMode())) { + // (during resume-from-replay catchup every type is non-transfer, so this would log every message) DEBUG_LOG(("Network::RelayCommandsToCommandList - rejecting game message from player %d of type %s, which is not a network type.", gmsg->getPlayerID(), GameMessage::getCommandTypeAsString(gmsg->getGameMessageType()))); } @@ -778,10 +795,19 @@ void Network::update() endOfGameCheck(); } - if (AllCommandsReady(TheGameLogic->getFrame())) { // If all the commands are ready for the next frame... + // Resume-from-replay: the freeze after the handoff holds + // the simulation on this frame for a wall-clock countdown while the network keeps + // pumping, and catchup before it skips the realtime timing gate (timeForNewFrame) so + // frames advance as fast as lockstep allows; the lead-in (last seconds before the + // handoff) keeps the gate so it runs at the logic rate whatever the render rate does. + // AllCommandsReady stays enforced either way. + const Bool resumeFrozen = TheRecorder && TheRecorder->updateResumeFreeze(); + const Bool inCatchup = TheRecorder && TheRecorder->isResumeCatchupMode() && !TheRecorder->isResumeCatchupLeadIn(); + + if (!resumeFrozen && AllCommandsReady(TheGameLogic->getFrame())) { // If all the commands are ready for the next frame... m_conMgr->handleAllCommandsReady(); // DEBUG_LOG(("Network::update - frame %d is ready", TheGameLogic->getFrame())); - if (timeForNewFrame()) { // This needs to come after any other pre-frame execution checks as this changes the timing variables. + if (inCatchup || timeForNewFrame()) { // This needs to come after any other pre-frame execution checks as this changes the timing variables. RelayCommandsToCommandList(TheGameLogic->getFrame()); // Put the commands for the next frame on TheCommandList. m_frameDataReady = TRUE; // Tell the GameEngine to run the commands for the new frame. } diff --git a/GeneralsMD/Code/GameEngine/CMakeLists.txt b/GeneralsMD/Code/GameEngine/CMakeLists.txt index a84c0935e98..de158132fa8 100644 --- a/GeneralsMD/Code/GameEngine/CMakeLists.txt +++ b/GeneralsMD/Code/GameEngine/CMakeLists.txt @@ -529,6 +529,7 @@ set(GAMEENGINE_SRC # Include/GameNetwork/GameSpyOverlay.h # Include/GameNetwork/GameSpyThread.h Include/GameNetwork/GUIUtil.h + Include/GameNetwork/ResumeFromReplay.h # Include/GameNetwork/IPEnumeration.h # Include/GameNetwork/LANAPI.h # Include/GameNetwork/LANAPICallbacks.h @@ -1115,6 +1116,7 @@ set(GAMEENGINE_SRC # Source/GameNetwork/GameSpy/Thread/ThreadUtils.cpp # Source/GameNetwork/GameSpyOverlay.cpp Source/GameNetwork/GUIUtil.cpp + Source/GameNetwork/ResumeFromReplay.cpp # Source/GameNetwork/IPEnumeration.cpp # Source/GameNetwork/LANAPI.cpp # Source/GameNetwork/LANAPICallbacks.cpp diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h b/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h index fba73ad5ec3..54ecb30f1cf 100644 --- a/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h +++ b/GeneralsMD/Code/GameEngine/Include/Common/Recorder.h @@ -50,7 +50,8 @@ enum RecorderModeType CPP_11(: Int) { RECORDERMODETYPE_RECORD, RECORDERMODETYPE_PLAYBACK, RECORDERMODETYPE_SIMULATION_PLAYBACK, // Play back replay without any graphics - RECORDERMODETYPE_NONE // this is a valid state to be in on the shell map, or in saved games + RECORDERMODETYPE_NONE, // this is a valid state to be in on the shell map, or in saved games + RECORDERMODETYPE_RESUME_CATCHUP // Resume-from-replay: feed replay commands into a live network game until the handoff frame. }; class RecorderClass : public SubsystemInterface @@ -138,6 +139,32 @@ class RecorderClass : public SubsystemInterface RecorderModeType getMode(); ///< Returns the current operating mode. Bool isPlaybackMode() const { return m_mode == RECORDERMODETYPE_PLAYBACK || m_mode == RECORDERMODETYPE_SIMULATION_PLAYBACK; } + + // Resume-from-replay. + // + // The lobby's start path validates this client's copy of the resume source (Resume.rep, + // see ResumeFromReplay) and arms the recorder. On the next MSG_NEW_GAME a brand new + // recording of the resumed match is started as for any other game, and the source's + // commands are injected into TheCommandList frame by frame while the live network game + // runs in lockstep at an uncapped rate (catchup); every injected command is written to the + // new recording as it goes, so the new replay holds the whole match. The last + // RESUME_LEADIN_SECONDS before the handoff frame run at normal speed (lead-in), then the + // simulation is held for RESUME_FREEZE_SECONDS of wall-clock time with a countdown (freeze), + // and finally control is handed back to the players. Camera scrolling works throughout; + // every other interaction is blocked until the freeze ends (see isResumeInputBlocked). + static const Int RESUME_LEADIN_SECONDS = 10; + static const Int RESUME_FREEZE_SECONDS = 10; + static AsciiString getResumeReplayFileName(); ///< name (without extension) of the resume source every client replays + void armResumeForNextGame(UnsignedInt handoffFrame, UnsignedInt sourceLastFrame); ///< validated by ResumeFromReplay::prepareGameStart + void disarmResume(); + Bool isResumeArmed() const { return m_resumeArmedHandoffFrame != 0; } + Bool isResumeCatchupMode() const { return m_mode == RECORDERMODETYPE_RESUME_CATCHUP; } + Bool isResumeCatchupLeadIn() const; ///< catchup, within the last RESUME_LEADIN_SECONDS before handoff + Bool isResumeFreezeActive() const { return m_resumeFreezeStartMs != 0; } ///< handoff reached, holding the sim with a countdown + Bool isResumeInputBlocked() const { return isResumeCatchupMode() || isResumeFreezeActive(); } + Bool isResumeCRCValidationSuppressed() const; ///< catchup, and the run-ahead window after the handoff + Bool updateResumeFreeze(); ///< pumped every engine frame by Network::update; TRUE while frames must not advance + UnsignedInt scanReplayLastFrame(AsciiString filename); ///< last frame with a complete record; walks the records, for recordings whose header was never finalized (crash) or whose tail is torn void initControls(); ///< Show or Hide the Replay controls static AsciiString getReplayDir(); ///< Returns the directory that holds the replay files. @@ -169,9 +196,9 @@ class RecorderClass : public SubsystemInterface AsciiString readAsciiString(); ///< Read the next string from m_file using ascii characters. UnicodeString readUnicodeString(); ///< Read the next string from m_file using unicode characters. void readNextFrame(); ///< Read the next frame number to execute a command on. - void appendNextCommand(); ///< Read the next GameMessage and append it to TheCommandList. + Bool appendNextCommand(); ///< Read the next GameMessage and append it to TheCommandList. FALSE on a torn record. void writeArgument(GameMessageArgumentDataType type, const GameMessageArgumentType arg); - void readArgument(GameMessageArgumentDataType type, GameMessage *msg); + Bool readArgument(GameMessageArgumentDataType type, GameMessage *msg); ///< FALSE on a short read. struct CullBadCommandsResult { @@ -198,6 +225,23 @@ class RecorderClass : public SubsystemInterface Int m_originalGameMode; // valid in replays UnsignedInt m_nextFrame; ///< The Frame that the next message is to be executed on. This can be -1. + + // resume-from-replay state (see the accessors above) + File* m_resumeSourceFile; ///< the resume source (Resume.rep) being read during catchup; m_file is the new recording + UnsignedInt m_resumeArmedHandoffFrame; ///< handoff frame for the next MSG_NEW_GAME, 0 when not armed + UnsignedInt m_resumeArmedSourceLastFrame; ///< last complete frame of the resume source, from scanReplayLastFrame + UnsignedInt m_resumeHandoffFrame; ///< frame at which injection stops and the freeze begins; stays set after the handoff for the CRC window + UnsignedInt m_resumeSourceLastFrame; ///< injection never reads past this frame, so a torn tail cannot be replayed + Bool m_resumeLeadInAnnounced; ///< the lead-in message has been shown + UnsignedInt m_resumeFreezeStartMs; ///< wall-clock start of the freeze, 0 when not frozen + Int m_resumeFreezeLastAnnounced; ///< last countdown second shown + + Bool startResumeCatchup(); ///< open the resume source and switch to catchup; the recording is already started + void updateResumeCatchup(); ///< inject the source's commands for this frame, hand off at the handoff frame + void injectResumeCommands(UnsignedInt curFrame); ///< read the source's records for curFrame into TheCommandList + void endResumeCatchup(); ///< close the source and leave catchup without handing off + void abortResume(const UnicodeString &why); ///< leave the game: the source cannot be replayed to the handoff + void clearResumeState(); }; extern RecorderClass *TheRecorder; diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.h index 5e89ed8d313..8ce5bb92bfd 100644 --- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.h +++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.h @@ -9,7 +9,9 @@ #include "GameClient/InGameUI.h" #include "GameLogic/VictoryConditions.h" #include +#include #include +#include extern NGMPGame* TheNGMPGame; @@ -24,6 +26,7 @@ struct LobbyMemberEntry : public NetworkMemberBase int team = -1; int startpos = -1; bool has_map = false; + bool has_resume_replay = false; // Holds a usable copy of the resume-from-replay source uint16_t m_SlotIndex = 999999; uint16_t m_SlotState = SlotState::SLOT_OPEN; @@ -72,6 +75,11 @@ struct LobbyEntry int rng_seed = -1; + // Resume-from-replay arming (0 when not armed); see ResumeFromReplay. + // The generation counts the service's arms, so a guest can tell a fresh upload from the one it already holds. + uint32_t resume_handoff_frame = 0; + uint32_t resume_replay_generation = 0; + bool passworded = false; std::string password; @@ -81,6 +89,20 @@ struct LobbyEntry int latency = 0; }; +// One slot of a HOST_ACTION_ALIGN_SLOTS request: what the +// slot must hold once the service has applied the whole arrangement. A human is named by user id, an +// AI by its difficulty state; open and closed slots carry nothing else. +struct LobbySlotSpec +{ + int slotIndex = -1; + int slotState = SlotState::SLOT_OPEN; + int64_t userID = -1; + int side = -1; + int color = -1; + int startPos = -1; + int team = -1; +}; + enum class EJoinLobbyResult { JoinLobbyResult_Success, // The room was joined. @@ -209,9 +231,11 @@ class NGMP_OnlineServices_LobbyInterface // updates - void UpdateCurrentLobby_Map(UnicodeString strMap, AsciiString strMapPath, bool bIsOfficial, int newMaxPlayers); - void UpdateCurrentLobby_LimitSuperweapons(bool bLimitSuperweapons); - void UpdateCurrentLobby_StartingCash(UnsignedInt startingCashValue); + // The three settings a resume has to align carry an optional + // completion callback, so the arm can run them one after another and only then send the arm. + void UpdateCurrentLobby_Map(UnicodeString strMap, AsciiString strMapPath, bool bIsOfficial, int newMaxPlayers, std::function onDone = nullptr); + void UpdateCurrentLobby_LimitSuperweapons(bool bLimitSuperweapons, std::function onDone = nullptr); + void UpdateCurrentLobby_StartingCash(UnsignedInt startingCashValue, std::function onDone = nullptr); void UpdateCurrentLobby_HasMap(); @@ -226,6 +250,18 @@ class NGMP_OnlineServices_LobbyInterface void UpdateCurrentLobby_AITeam(int slot, int team); void UpdateCurrentLobby_AIStartPos(int slot, int startpos); + // Resume-from-replay: place every slot the way the recording + // had it, in one host action the service applies whole or not at all. On refusal `error` says why. + void UpdateCurrentLobby_AlignSlots(const std::vector& slots, std::function onDone); + + // Resume-from-replay (see ResumeFromReplay). The host uploads + // the resume source, then arms with the handoff frame and the replay's seed (handoff 0 disarms); + // guests download the source when the arm reaches them and flag themselves as holding it. + void UpdateCurrentLobby_ArmResume(uint32_t handoffFrame, int rngSeed, std::function onComplete); + void UpdateCurrentLobby_HasResumeReplay(bool bHasResumeReplay); + void UploadResumeReplay(const AsciiString& path, std::function onComplete); + void DownloadResumeReplay(const AsciiString& path, std::function onComplete); + void UpdateCurrentLobbyMaxCameraHeight(uint16_t maxCameraHeight); void SetJoinability(ELobbyJoinability joinabilityFlag); @@ -531,6 +567,9 @@ class NGMP_OnlineServices_LobbyInterface } private: + void SendResumeReplayChunk(int64_t lobbyID, std::shared_ptr> data, size_t offset, int conflictRetries, std::function onComplete); + void FetchResumeReplayChunk(int64_t lobbyID, AsciiString path, std::shared_ptr> data, std::function onComplete); + std::function m_cb_CreateLobbyPendingCallback = nullptr; std::function m_callbackJoinedLobby = nullptr; diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ResumeFromReplay.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ResumeFromReplay.h new file mode 100644 index 00000000000..46aa269a590 --- /dev/null +++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ResumeFromReplay.h @@ -0,0 +1,86 @@ +/* +** Command & Conquer Generals Zero Hour(tm) +** Copyright 2025 Electronic Arts Inc. +** +** This program is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program. If not, see . +*/ + +// Resume-from-replay: the arming and validation +// shared by the LAN lobby, the online lobby and the game start path. +// +// After a match dies, the same players resume it from the host's recording. The host arms it +// in the lobby (/resume): its own last recording (00000000.rep) is copied to Resume.rep, the +// lobby is aligned with the recording (slots, map, seed) and the handoff frame is broadcast in +// the game options. That one file is then pushed to every guest (the map transfer path on LAN, +// an upload to the service online), each guest validates its copy against the lobby, and on +// game start RecorderClass replays Resume.rep in lockstep up to the handoff frame while +// recording a brand new 00000000.rep of the resumed match. + +#pragma once + +#include "Common/Recorder.h" + +class GameInfo; +class MapMetaData; + +/// Reads a replay's header and game info without opening it for playback. Defined in ReplayMenu.cpp. +Bool readReplayMapInfo(const AsciiString& filename, RecorderClass::ReplayHeader &header, ReplayGameInfo &info, const MapMetaData *&mapData); + +namespace ResumeFromReplay +{ + /// The handoff sits this far before the end of the recording, so the last seconds before + /// the match died (which every player has seen play out) are not replayed as if new. + constexpr Int HANDOFF_SLACK_SECONDS = 10; + + /// What a client knows about its copy of the resume source (Resume.rep). + struct SourceReplay + { + RecorderClass::ReplayHeader header; + ReplayGameInfo game; + const MapMetaData *mapData = nullptr; + UnsignedInt lastFrame = 0; ///< last frame with a complete record; the header's count is 0 for a recording cut short by a crash + }; + + AsciiString getSourceFileName(); ///< "Resume.rep" + AsciiString getSourceFilePath(); ///< full path of the resume source in the replay directory + UnicodeString formatGameTime(UnsignedInt frames); ///< m:ss + + /// Host: copies the last recording to Resume.rep, reads it back, checks the version and computes the + /// handoff frame. The reason for a refusal is returned in why (without a "Resume:" prefix). + Bool prepareHostSource(SourceReplay &out, UnsignedInt &handoffFrame, UnicodeString &why); + + /// Reads this client's Resume.rep: header, version check and the last complete frame. + Bool readSource(SourceReplay &out, UnicodeString &why); + + /// Compares the source recording against the lobby: map, map CRC, seed, starting cash, superweapon + /// restriction, every slot's occupant, and (when handoffFrame != 0) that the recording reaches it. + Bool sourceMatchesGame(const SourceReplay &replay, const GameInfo *game, UnsignedInt handoffFrame, UnicodeString &why); + + /// Host arming: every human in the recording is in the lobby under the same name, the host is + /// the recording's slot-0 player and nobody extra is present (slot order is checked separately). + Bool validateRoster(const ReplayGameInfo &replay, const GameInfo *lobby, UnicodeString &why); + + /// Every slot as recorded: occupant (human by name, AI by difficulty, or empty), faction, color, start position and team. + Bool slotsMatch(const ReplayGameInfo &replay, const GameInfo *lobby, UnicodeString &why); + + /// Guest: after its copy of Resume.rep arrived, whether it matches the lobby, with one chat line + /// for everyone either way. + Bool guestSourceReport(const GameInfo *lobby, UnicodeString &report); + + /// Game start (LAN OnGameStart / NGMPGame::startGame), before MSG_NEW_GAME: when the lobby is armed, + /// validates this client's Resume.rep against it, arms the recorder and clears the arm on the game + /// info (the recorder owns it from here). Returns FALSE with the reason when the start must be + /// abandoned; the recorder is never left armed in that case. + Bool prepareGameStart(GameInfo *game, UnicodeString &why); +} diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp index 0c497745de0..a6376dc6db6 100644 --- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp +++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp @@ -937,7 +937,12 @@ Bool GameEngine::canUpdateRegularGameLogic(UnsignedInt logicTimeQueryFlags) const Bool useFastMode = TheGlobalData->m_TiVOFastMode && TheGameLogic->isInReplayGame(); #endif - if (useFastMode || logicTimeScaleFps >= maxRenderFps) + // Resume-from-replay catchup fast-forwards by running + // one logic frame per render frame; the logic time scale must not throttle it. + const Bool inCatchup = TheRecorder && TheRecorder->isResumeCatchupMode() + && !TheRecorder->isResumeCatchupLeadIn(); + + if (useFastMode || inCatchup || logicTimeScaleFps >= maxRenderFps) { // Logic time scale is uncapped or larger equal Render FPS. Update straight away. return true; @@ -963,6 +968,10 @@ Bool GameEngine::canUpdateRegularGameLogic(UnsignedInt logicTimeQueryFlags) extern NGMPGame* TheNGMPGame; #endif +// Wall-clock cadence of the render passes during +// resume-from-replay catchup (about 30 fps), see GameEngine::update. +static constexpr const UnsignedInt CATCHUP_RENDER_INTERVAL_MS = 33; + /// ----------------------------------------------------------------------------------------------- DECLARE_PERF_TIMER(GameEngine_update) @@ -973,6 +982,27 @@ void GameEngine::update() { USE_PERF_TIMER(GameEngine_update) { + // During resume-from-replay catchup, render on a + // wall-clock cadence rather than once per logic frame: logic is fast-forwarding + // at many times realtime, so drawing every logic frame would burn the CPU on + // frames no one can perceive, while drawing at ~30 fps keeps the catchup + // watchable. Render rate is not what bounds catchup (lockstep is, see + // ConnectionManager::updateRunAhead) and FrameMetrics stops sampling display + // FPS during catchup, which is what makes it safe to throttle here. The lead-in + // renders every frame for a realtime preview before control is handed back. + Bool catchupSkipRender = TheRecorder && TheRecorder->isResumeCatchupMode() + && !TheRecorder->isResumeCatchupLeadIn(); + if (catchupSkipRender) + { + static UnsignedInt s_lastCatchupRenderMs = 0; + const UnsignedInt nowMs = timeGetTime(); + if (nowMs - s_lastCatchupRenderMs >= CATCHUP_RENDER_INTERVAL_MS) + { + s_lastCatchupRenderMs = nowMs; + catchupSkipRender = FALSE; + } + } + { // VERIFY CRC needs to be in this code block. Please to not pull TheGameLogic->update() inside this block. VERIFY_CRC @@ -995,7 +1025,18 @@ void GameEngine::update() /// @todo Move audio init, update, etc, into GameClient update TheAudio->UPDATE(); - TheGameClient->UPDATE(); + if (catchupSkipRender) + { + TheParticleSystemManager->update(); // let particles finish instead of piling up while frames are skipped + + // The frame tick is the client update's first act (GameClient::update) and the + // network relies on one per logic frame: Network::processCommand drives the + // per-frame command counts off it and switches PREGAME to INGAME on frame 1. + GameMessage *frameMsg = TheMessageStream->appendMessage( GameMessage::MSG_FRAME_TICK ); + frameMsg->appendTimestampArgument( TheGameClient->getFrame() ); + } + else + TheGameClient->UPDATE(); TheMessageStream->propagateMessages(); if (TheNetwork != nullptr) diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp index c53ffced7dc..79b3eed68ac 100644 --- a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp +++ b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp @@ -31,6 +31,9 @@ #include "Common/Player.h" #include "Common/GlobalData.h" #include "Common/GameEngine.h" +#include "GameNetwork/NetworkDefs.h" +#include "GameNetwork/NetworkInterface.h" +#include "GameNetwork/ResumeFromReplay.h" #include "GameClient/ClientInstance.h" #include "GameClient/GameWindow.h" #include "GameClient/GameWindowManager.h" @@ -52,6 +55,13 @@ extern NGMPGame* TheNGMPGame; +// Resume-from-replay milestones go to the release log online. +#if defined(GENERALS_ONLINE) +#define RESUME_LOG(...) NetworkLog(ELogVerbosity::LOG_RELEASE, __VA_ARGS__) +#else +#define RESUME_LOG(...) DEBUG_LOG((__VA_ARGS__)) +#endif + constexpr const char s_genrep[] = "GENREP"; constexpr const UnsignedInt replayBufferBytes = 8192; @@ -59,6 +69,7 @@ Int REPLAY_CRC_INTERVAL = 100; const char* replayExtention = ".rep"; const char* lastReplayFileName = "00000000"; // a name the user is unlikely to ever type, but won't cause panic & confusion +const char* resumeReplayFileName = "Resume"; // the resume-from-replay source every client replays (see ResumeFromReplay) // TheSuperHackers @tweak helmutbuhler 25/04/2025 // The replay header contains two time fields; startTime and endTime of type time_t. @@ -344,6 +355,7 @@ RecorderClass::RecorderClass() m_originalGameMode = GAME_NONE; m_mode = RECORDERMODETYPE_RECORD; m_file = nullptr; + m_resumeSourceFile = nullptr; m_fileName.clear(); m_currentFilePosition = 0; m_doingAnalysis = FALSE; @@ -383,6 +395,8 @@ void RecorderClass::init() { m_doingAnalysis = FALSE; m_playbackFrameCount = 0; + clearResumeState(); + OptionPreferences optionPref; m_archiveReplays = optionPref.getArchiveReplaysEnabled(); } @@ -405,7 +419,15 @@ void RecorderClass::reset() { * Do the update for this frame. */ void RecorderClass::update() { - if (m_mode == RECORDERMODETYPE_RECORD || m_mode == RECORDERMODETYPE_NONE) { + if (m_mode == RECORDERMODETYPE_RESUME_CATCHUP) { + // Inject the resume source's commands for this + // frame, then record them like any other frame: the new recording is a normal replay of + // the resumed match from frame 0. The handoff (or an abort) switches the mode inside. + updateResumeCatchup(); + if (m_mode != RECORDERMODETYPE_NONE) + updateRecord(); + } + else if (m_mode == RECORDERMODETYPE_RECORD || m_mode == RECORDERMODETYPE_NONE) { updateRecord(); } else if (isPlaybackMode()) { @@ -489,7 +511,23 @@ void RecorderClass::updateRecord() if (msg->getArgumentCount() >= 4) maxFPS = msg->getArgument(3)->integer; + // The new recording starts as for any game. + // When the lobby's start path armed a resume-from-replay (ResumeFromReplay::prepareGameStart + // validated this client's copy of the source), the source is then replayed into it. + // startRecording resets the recorder, so the arming is carried across by hand. A source + // that cannot be opened leaves the game: a client that silently recorded a fresh match + // would desync everyone else. + const UnsignedInt armedHandoffFrame = m_resumeArmedHandoffFrame; + const UnsignedInt armedSourceLastFrame = m_resumeArmedSourceLastFrame; startRecording(diff, m_originalGameMode, rankPoints, maxFPS); + if (armedHandoffFrame != 0) + { + armResumeForNextGame(armedHandoffFrame, armedSourceLastFrame); + if (startResumeCatchup()) + DEBUG_LOG(("RecorderClass::updateRecord() - resume catchup to frame %u", armedHandoffFrame)); + else + abortResume(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSourceUnreadable", L"Resume aborted: the resume replay could not be opened")); + } } else if (msg->getType() == GameMessage::MSG_CLEAR_GAME_DATA) { if (m_file != nullptr) { @@ -499,6 +537,7 @@ void RecorderClass::updateRecord() needFlush = FALSE; } m_fileName.clear(); + clearResumeState(); } else { if (m_file != nullptr) { @@ -1307,22 +1346,31 @@ void RecorderClass::readNextFrame() { if (bytesRead != sizeof(m_nextFrame)) { DEBUG_LOG(("RecorderClass::readNextFrame - read failed on frame %d", TheGameLogic->getFrame())); m_nextFrame = -1; + // Resume-from-replay catchup: running out of + // records is handled by updateResumeCatchup (which never ends the live game here). + if (m_mode == RECORDERMODETYPE_RESUME_CATCHUP) + return; stopPlayback(); } } /** * This reads the next command from the replay file and appends it to TheCommandList. + * Returns FALSE when the record is incomplete (a + * recording cut short by a crash ends in a torn record); the torn record is discarded and + * m_nextFrame is set to -1 so the callers treat it as the end of the usable data. */ -void RecorderClass::appendNextCommand() { +Bool RecorderClass::appendNextCommand() { GameMessage::Type type; Int bytesRead = m_file->read(&type, sizeof(type)); if (bytesRead != sizeof(type)) { DEBUG_LOG(("RecorderClass::appendNextCommand - read failed on frame %d", m_nextFrame/*TheGameLogic->getFrame()*/)); - return; + m_nextFrame = -1; + return FALSE; } GameMessage* msg = newInstance(GameMessage)(type); + Bool complete = TRUE; #ifdef DEBUG_LOGGING AsciiString commandName = msg->getCommandAsString(); @@ -1337,7 +1385,8 @@ void RecorderClass::appendNextCommand() { #endif // DEBUG_LOGGING Int playerIndex = -1; - m_file->read(&playerIndex, sizeof(playerIndex)); + if (m_file->read(&playerIndex, sizeof(playerIndex)) != sizeof(playerIndex)) + complete = FALSE; msg->friend_setPlayerIndex(playerIndex); // don't debug log this if we're debugging sync errors, as it will cause diff problems between a game and it's replay... @@ -1356,14 +1405,17 @@ void RecorderClass::appendNextCommand() { UnsignedByte numTypes = 0; Int totalArgs = 0; - m_file->read(&numTypes, sizeof(numTypes)); + if (complete && m_file->read(&numTypes, sizeof(numTypes)) != sizeof(numTypes)) + complete = FALSE; GameMessageParser* parser = newInstance(GameMessageParser)(); - for (UnsignedByte i = 0; i < numTypes; ++i) { + for (UnsignedByte i = 0; complete && i < numTypes; ++i) { UnsignedByte type = (UnsignedByte)ARGUMENTDATATYPE_UNKNOWN; - m_file->read(&type, sizeof(type)); UnsignedByte numArgs = 0; - m_file->read(&numArgs, sizeof(numArgs)); + if (m_file->read(&type, sizeof(type)) != sizeof(type) || m_file->read(&numArgs, sizeof(numArgs)) != sizeof(numArgs)) { + complete = FALSE; + break; + } parser->addArgType((GameMessageArgumentDataType)type, numArgs); totalArgs += numArgs; } @@ -1375,14 +1427,20 @@ void RecorderClass::appendNextCommand() { lasttype = parserArgType->getType(); argsLeftForType = parserArgType->getArgCount(); } - for (Int j = 0; j < totalArgs; ++j) { - readArgument(lasttype, msg); + for (Int j = 0; complete && j < totalArgs; ++j) { + if (!readArgument(lasttype, msg)) { + complete = FALSE; + break; + } --argsLeftForType; if (argsLeftForType == 0) { DEBUG_ASSERTCRASH(parserArgType != nullptr, ("parserArgType was null when it shouldn't have been.")); if (parserArgType == nullptr) { - return; + deleteInstance(msg); + deleteInstance(parser); + m_nextFrame = -1; + return FALSE; } parserArgType = parserArgType->getNext(); @@ -1394,6 +1452,15 @@ void RecorderClass::appendNextCommand() { } } + if (!complete) + { + DEBUG_LOG(("RecorderClass::appendNextCommand - torn record on frame %d, treating it as the end of the replay", m_nextFrame)); + deleteInstance(msg); + deleteInstance(parser); + m_nextFrame = -1; + return FALSE; + } + if (type != GameMessage::MSG_BEGIN_NETWORK_MESSAGES && type != GameMessage::MSG_CLEAR_GAME_DATA && !m_doingAnalysis) { TheCommandList->appendMessage(msg); @@ -1406,13 +1473,16 @@ void RecorderClass::appendNextCommand() { deleteInstance(parser); parser = nullptr; + return TRUE; } -void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *msg) { +/// Reads one argument of the given type; FALSE on a short read. +Bool RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *msg) { switch (type) { case ARGUMENTDATATYPE_INTEGER: { Int theint; - m_file->read(&theint, sizeof(theint)); + if (m_file->read(&theint, sizeof(theint)) != sizeof(theint)) + return FALSE; msg->appendIntegerArgument(theint); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1424,7 +1494,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_REAL: { Real thereal; - m_file->read(&thereal, sizeof(thereal)); + if (m_file->read(&thereal, sizeof(thereal)) != sizeof(thereal)) + return FALSE; msg->appendRealArgument(thereal); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1436,7 +1507,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_BOOLEAN: { Bool thebool; - m_file->read(&thebool, sizeof(thebool)); + if (m_file->read(&thebool, sizeof(thebool)) != sizeof(thebool)) + return FALSE; msg->appendBooleanArgument(thebool); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1448,7 +1520,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_OBJECTID: { ObjectID theid; - m_file->read(&theid, sizeof(theid)); + if (m_file->read(&theid, sizeof(theid)) != sizeof(theid)) + return FALSE; msg->appendObjectIDArgument(theid); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1460,7 +1533,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_DRAWABLEID: { DrawableID theid; - m_file->read(&theid, sizeof(theid)); + if (m_file->read(&theid, sizeof(theid)) != sizeof(theid)) + return FALSE; msg->appendDrawableIDArgument(theid); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1472,7 +1546,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_TEAMID: { UnsignedInt theid; - m_file->read(&theid, sizeof(theid)); + if (m_file->read(&theid, sizeof(theid)) != sizeof(theid)) + return FALSE; msg->appendTeamIDArgument(theid); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1484,7 +1559,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_LOCATION: { Coord3D loc; - m_file->read(&loc, sizeof(loc)); + if (m_file->read(&loc, sizeof(loc)) != sizeof(loc)) + return FALSE; msg->appendLocationArgument(loc); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1497,7 +1573,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_PIXEL: { ICoord2D pixel; - m_file->read(&pixel, sizeof(pixel)); + if (m_file->read(&pixel, sizeof(pixel)) != sizeof(pixel)) + return FALSE; msg->appendPixelArgument(pixel); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1509,7 +1586,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_PIXELREGION: { IRegion2D reg; - m_file->read(®, sizeof(reg)); + if (m_file->read(®, sizeof(reg)) != sizeof(reg)) + return FALSE; msg->appendPixelRegionArgument(reg); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1521,7 +1599,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_TIMESTAMP: { // Not to be confused with Terrance Stamp... Kneel before Zod!!! UnsignedInt stamp; - m_file->read(&stamp, sizeof(stamp)); + if (m_file->read(&stamp, sizeof(stamp)) != sizeof(stamp)) + return FALSE; msg->appendTimestampArgument(stamp); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1533,7 +1612,8 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * } case ARGUMENTDATATYPE_WIDECHAR: { WideChar theid; - m_file->read(&theid, sizeof(theid)); + if (m_file->read(&theid, sizeof(theid)) != sizeof(theid)) + return FALSE; msg->appendWideCharArgument(theid); #ifdef DEBUG_LOGGING if (m_doingAnalysis) @@ -1546,6 +1626,7 @@ void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage * default: break; } + return TRUE; } /** @@ -1736,6 +1817,338 @@ Bool RecorderClass::isMultiplayer() return false; } +//------------------------------------------------------------------------------------------------- +// Resume-from-replay. See Recorder.h for the phase overview. +//------------------------------------------------------------------------------------------------- + +AsciiString RecorderClass::getResumeReplayFileName() +{ + return AsciiString(resumeReplayFileName); +} + +void RecorderClass::clearResumeState() +{ + if (m_resumeSourceFile != nullptr) + { + m_resumeSourceFile->close(); + m_resumeSourceFile = nullptr; + } + m_resumeArmedHandoffFrame = 0; + m_resumeArmedSourceLastFrame = 0; + m_resumeHandoffFrame = 0; + m_resumeSourceLastFrame = 0; + m_resumeLeadInAnnounced = FALSE; + m_resumeFreezeStartMs = 0; + m_resumeFreezeLastAnnounced = -1; + if (m_mode == RECORDERMODETYPE_RESUME_CATCHUP) + m_mode = RECORDERMODETYPE_NONE; +} + +void RecorderClass::armResumeForNextGame(UnsignedInt handoffFrame, UnsignedInt sourceLastFrame) +{ + m_resumeArmedHandoffFrame = handoffFrame; + m_resumeArmedSourceLastFrame = sourceLastFrame; +} + +void RecorderClass::disarmResume() +{ + m_resumeArmedHandoffFrame = 0; + m_resumeArmedSourceLastFrame = 0; +} + +Bool RecorderClass::isResumeCatchupLeadIn() const +{ + if (!isResumeCatchupMode()) + return FALSE; + const UnsignedInt leadInFrames = RESUME_LEADIN_SECONDS * LOGICFRAMES_PER_SECOND; + const UnsignedInt curFrame = TheGameLogic ? TheGameLogic->getFrame() : 0; + return m_resumeHandoffFrame >= leadInFrames && curFrame >= m_resumeHandoffFrame - leadInFrames; +} + +/** + * CRC validation is off during catchup (the CRC messages of that window come out of the resume + * source, not from the live peers) and stays off for one full run-ahead after the handoff: a CRC + * generated on frame F is compared on F plus the run-ahead, and every set that could still be + * compared before that was generated while the peers were not exchanging any. + */ +Bool RecorderClass::isResumeCRCValidationSuppressed() const +{ + if (isResumeCatchupMode()) + return TRUE; + if (m_resumeHandoffFrame == 0 || TheGameLogic == nullptr) + return FALSE; + return TheGameLogic->getFrame() <= m_resumeHandoffFrame + MAX_FRAMES_AHEAD; +} + +/** + * Open the resume source (Resume.rep), skip its header, prime the first frame of commands and + * switch the recorder into RECORDERMODETYPE_RESUME_CATCHUP. startRecording has already opened the + * new recording in m_file and written its header for the live game; the source is read through a + * second handle. While in catchup, update() injects the source's commands into TheCommandList each + * frame and records them, while the live network keeps running in lockstep. + */ +Bool RecorderClass::startResumeCatchup() +{ + if (m_file == nullptr || m_resumeArmedHandoffFrame == 0) + return FALSE; + + // The read helpers (readReplayHeader, readNextFrame, appendNextCommand) work on m_file: + // point it at the source for the duration and put the recording back afterwards. + File *recordFile = m_file; + m_file = nullptr; + ReplayHeader header; + header.forPlayback = TRUE; + header.filename = ResumeFromReplay::getSourceFileName(); + const Bool headerOk = readReplayHeader(header); + m_resumeSourceFile = m_file; + m_file = recordFile; + if (!headerOk || m_resumeSourceFile == nullptr) + { + DEBUG_LOG(("RecorderClass::startResumeCatchup - readReplayHeader failed for %s", header.filename.str())); + if (m_resumeSourceFile != nullptr) + { + m_resumeSourceFile->close(); + m_resumeSourceFile = nullptr; + } + return FALSE; + } + + // playbackFile consumes these four values between the header and the command stream + // (difficulty, original game mode, rank points, max FPS); mirror it or readNextFrame reads + // the difficulty as a frame number. + Int skipped[4]; + if (m_resumeSourceFile->read(skipped, sizeof(skipped)) != sizeof(skipped)) + { + m_resumeSourceFile->close(); + m_resumeSourceFile = nullptr; + return FALSE; + } + + m_mode = RECORDERMODETYPE_RESUME_CATCHUP; + m_resumeHandoffFrame = m_resumeArmedHandoffFrame; + m_resumeSourceLastFrame = m_resumeArmedSourceLastFrame; + m_resumeArmedHandoffFrame = 0; + m_resumeArmedSourceLastFrame = 0; + m_resumeLeadInAnnounced = FALSE; + m_resumeFreezeStartMs = 0; + m_resumeFreezeLastAnnounced = -1; + + // Prime the first record's frame number (in catchup mode, readNextFrame never ends the game). + m_file = m_resumeSourceFile; + readNextFrame(); + m_resumeSourceFile = m_file; + m_file = recordFile; + + // Nothing may be selected when catchup starts: selection is blocked from here on and + // a pre-existing selection would be a way to issue commands with the right mouse button. + if (TheInGameUI) + { + TheInGameUI->deselectAllDrawables(); + TheInGameUI->message(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeCatchupStart", + L"Resuming from replay: fast-forwarding to %ls", ResumeFromReplay::formatGameTime(m_resumeHandoffFrame).str())); + } + + // The realtime pacing is bypassed for the duration by the catchup checks in FramePacer, + // GameEngine and Network (isResumeCatchupMode); lockstep is unchanged, every frame still + // waits for every peer's commands. + RESUME_LOG("Resume: catching up %s to frame %u (source ends at frame %u, lead-in %d s, freeze %d s)", + header.filename.str(), m_resumeHandoffFrame, m_resumeSourceLastFrame, RESUME_LEADIN_SECONDS, RESUME_FREEZE_SECONDS); + return TRUE; +} + +/** + * Last frame with a complete record in a replay, found by walking its records. The header's frame + * count is only patched in when a recording ends normally, so a recording cut short by a crash + * (the very file a resume is for) reads 0 there, and its last record is usually torn. Uses the + * recorder's analysis mode, in which appendNextCommand parses and discards instead of feeding + * TheCommandList. Lobby use only: refuses to run while the recorder has a file open. + */ +UnsignedInt RecorderClass::scanReplayLastFrame(AsciiString filename) +{ + if (m_file != nullptr || isPlaybackMode() || isResumeCatchupMode()) + { + // Lobby-only helper; the read helpers it borrows (readReplayHeader, appendNextCommand) work on + // m_file, so it cannot run while a recording or playback holds that. 0 is "no usable frames". + DEBUG_CRASH(("RecorderClass::scanReplayLastFrame(%s) called while the recorder has a file open (mode %d)", filename.str(), m_mode)); + RESUME_LOG("Resume: cannot scan %s while the recorder has a file open (mode %d)", filename.str(), m_mode); + return 0; + } + + ReplayHeader header; + header.forPlayback = TRUE; + header.filename = filename; + if (!readReplayHeader(header)) + return 0; + + // difficulty, original game mode, rank points, max FPS (see playbackFile) + Int skipped[4]; + if (m_file->read(skipped, sizeof(skipped)) != sizeof(skipped)) + { + m_file->close(); + m_file = nullptr; + return 0; + } + + const Bool savedAnalysis = m_doingAnalysis; + m_doingAnalysis = TRUE; + + UnsignedInt lastFrame = 0; + UnsignedInt frame = 0; + while (m_file->read(&frame, sizeof(frame)) == sizeof(frame)) + { + if (frame < lastFrame) + break; // torn tail: frame numbers only ever grow + m_nextFrame = frame; + if (!appendNextCommand()) + break; // torn record: everything before it is usable + lastFrame = frame; + } + + m_file->close(); + m_file = nullptr; + m_doingAnalysis = savedAnalysis; + m_nextFrame = (UnsignedInt)-1; + return lastFrame; +} + +/** + * Read the source's records for curFrame into TheCommandList. Never reads past the last complete + * frame found by scanReplayLastFrame, so a torn tail cannot be replayed, and never past the handoff. + */ +void RecorderClass::injectResumeCommands(UnsignedInt curFrame) +{ + File *recordFile = m_file; + m_file = m_resumeSourceFile; + while (m_nextFrame == curFrame && m_nextFrame <= m_resumeSourceLastFrame && curFrame <= m_resumeHandoffFrame) + { + if (!appendNextCommand()) + break; // torn record: appendNextCommand marked the end of the usable data + readNextFrame(); + if (m_nextFrame != (UnsignedInt)-1 && m_nextFrame < curFrame) + m_nextFrame = (UnsignedInt)-1; // regression: the file is not the one that was scanned + } + m_resumeSourceFile = m_file; + m_file = recordFile; +} + +/** + * Close the source and leave catchup. The recording of the resumed match goes on in m_file. + */ +void RecorderClass::endResumeCatchup() +{ + if (m_resumeSourceFile != nullptr) + { + m_resumeSourceFile->close(); + m_resumeSourceFile = nullptr; + } + m_nextFrame = (UnsignedInt)-1; + m_resumeSourceLastFrame = 0; + if (m_mode == RECORDERMODETYPE_RESUME_CATCHUP) + m_mode = RECORDERMODETYPE_RECORD; +} + +/** + * The source cannot be replayed to the handoff on this client. Every client validated its copy + * against the handoff before starting, so this is not a shorter-replay case a local handoff could + * paper over (that would desync the peers): tell the player and leave the game, never fall back + * to playing on. + */ +void RecorderClass::abortResume(const UnicodeString &why) +{ + RESUME_LOG("Resume: aborted at frame %u: %ls", TheGameLogic ? TheGameLogic->getFrame() : 0, why.str()); + endResumeCatchup(); + m_resumeHandoffFrame = 0; + m_resumeFreezeStartMs = 0; + if (TheInGameUI) + TheInGameUI->message(why); + if (TheGameLogic) + TheGameLogic->exitGame(); +} + +/** + * Per-frame update for RECORDERMODETYPE_RESUME_CATCHUP: inject the source's commands for the + * current logic frame, announce the lead-in, and start the freeze when the handoff frame is reached. + * Unlike stopPlayback this does NOT call exitGame at the handoff; the network session continues. + */ +void RecorderClass::updateResumeCatchup() +{ + const UnsignedInt curFrame = TheGameLogic ? TheGameLogic->getFrame() : 0; + + // Lead-in: the pacing bypasses switch off (isResumeCatchupLeadIn) so players get a + // normal-speed look at the last seconds before the handoff. If the handoff is closer than + // that to the start, it stays fast. + if (!m_resumeLeadInAnnounced && isResumeCatchupLeadIn()) + { + m_resumeLeadInAnnounced = TRUE; + RESUME_LOG("Resume: lead-in reached at frame %u, back to realtime", curFrame); + if (TheInGameUI) + TheInGameUI->message(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeCatchupLeadIn", + L"Catching up: the last %d seconds play at normal speed", RESUME_LEADIN_SECONDS)); + } + + // NOTE: cullBadCommands() is intentionally skipped here. It strips every command in + // the network range from TheCommandList, which is exactly what appendNextCommand adds, + // and the live network layer writes into the same list each frame. Local input is + // blocked at its sources instead (isResumeInputBlocked). + + // Inject every command recorded for this frame, including the handoff frame itself. + injectResumeCommands(curFrame); + + if (curFrame >= m_resumeHandoffFrame) + { + endResumeCatchup(); + + // Hold the simulation with a countdown before control comes back. Network::update + // polls updateResumeFreeze() and refuses to advance frames while it returns TRUE. + m_resumeFreezeStartMs = timeGetTime(); + if (m_resumeFreezeStartMs == 0) + m_resumeFreezeStartMs = 1; + m_resumeFreezeLastAnnounced = -1; + RESUME_LOG("Resume: handoff at frame %u, freezing for %d s", curFrame, RESUME_FREEZE_SECONDS); + return; + } + + // The source's records ended before the last complete frame the scan found: the file is + // not the one that was validated. A local handoff here would desync the peers. + if (m_nextFrame == (UnsignedInt)-1 && curFrame < m_resumeSourceLastFrame) + { + abortResume(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSourceEnded", + L"Resume aborted: the replay ended at %ls, before the handoff at %ls", + ResumeFromReplay::formatGameTime(curFrame).str(), ResumeFromReplay::formatGameTime(m_resumeHandoffFrame).str())); + } +} + +/** + * Pumped from Network::update() every engine frame (GameLogic, and with it the recorder's own + * update, does not run while frames are held). Shows the countdown and ends the freeze. + */ +Bool RecorderClass::updateResumeFreeze() +{ + if (m_resumeFreezeStartMs == 0) + return FALSE; + + const UnsignedInt elapsedMs = timeGetTime() - m_resumeFreezeStartMs; + const Int remaining = RESUME_FREEZE_SECONDS - (Int)(elapsedMs / 1000); + if (remaining > 0) + { + if (remaining != m_resumeFreezeLastAnnounced && TheInGameUI) + { + m_resumeFreezeLastAnnounced = remaining; + RESUME_LOG("Resume: countdown %d", remaining); + TheInGameUI->message(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeCountdown", + L"Resuming in %d", remaining)); + } + return TRUE; + } + + m_resumeFreezeStartMs = 0; + m_resumeFreezeLastAnnounced = -1; + if (TheInGameUI) + TheInGameUI->message(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHandoffComplete", L"Control handed back. Go!")); + RESUME_LOG("Resume: freeze over at frame %u, live play resumes", TheGameLogic ? TheGameLogic->getFrame() : 0); + return FALSE; +} + /** * Create a new recorder object. */ diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp index 6649a0c9f65..32201d87a16 100644 --- a/GeneralsMD/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp +++ b/GeneralsMD/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp @@ -33,6 +33,7 @@ #include "Common/FileSystem.h" #include "Common/GameEngine.h" #include "Common/GameState.h" +#include "Common/Recorder.h" #include "Common/GameStateMap.h" #include "Common/LatchRestore.h" #include "Common/MapObject.h" @@ -863,6 +864,7 @@ static AsciiString getMapLeafAndDirName(const AsciiString& in) const char* PORTABLE_SAVE = "Save\\"; const char* PORTABLE_MAPS = "Maps\\"; const char* PORTABLE_USER_MAPS = "UserData\\Maps\\"; +const char* PORTABLE_REPLAYS = "Replays\\"; // The resume-from-replay source travels the map transfer path // ------------------------------------------------------------------------------------------------ AsciiString GameState::realMapPathToPortableMapPath(const AsciiString& in) const @@ -883,6 +885,12 @@ AsciiString GameState::realMapPathToPortableMapPath(const AsciiString& in) const prefix = PORTABLE_USER_MAPS; prefix.concat(getMapLeafAndDirName(in)); } + else if (in.startsWithNoCase(RecorderClass::getReplayDir())) + { + // the replay dir ends with "\\" + prefix = PORTABLE_REPLAYS; + prefix.concat(getMapLeafName(in)); + } else { DEBUG_CRASH(("Map file was not found in any of the expected directories; this is impossible")); @@ -923,6 +931,21 @@ AsciiString GameState::portableMapPathToRealMapPath(const AsciiString& in) const containingBasePath = prefix; prefix.concat(getMapLeafAndDirName(in)); } + else if (in.startsWithNoCase(PORTABLE_REPLAYS)) + { + // the replay dir ends with "\\". Only the resume source may land here, so a peer + // cannot name another replay on this machine. + AsciiString resumeLeaf = RecorderClass::getResumeReplayFileName(); + resumeLeaf.concat(RecorderClass::getReplayExtention()); + if (getMapLeafName(in).compareNoCase(resumeLeaf) != 0) + { + DEBUG_LOG(("Transferred replay '%s' is not the resume source.", in.str())); + return AsciiString::TheEmptyString; + } + prefix = RecorderClass::getReplayDir(); + containingBasePath = prefix; + prefix.concat(resumeLeaf); + } else { DEBUG_CRASH(("Map file was not found in any of the expected directories; this is impossible")); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp index 53cda5a2a2e..f4e2b2b5c00 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/LanGameOptionsMenu.cpp @@ -59,6 +59,8 @@ #include "Common/MultiplayerSettings.h" #include "GameClient/GameText.h" #include "GameNetwork/GUIUtil.h" +#include "GameClient/MapUtil.h" +#include "GameNetwork/ResumeFromReplay.h" extern char *LANnextScreen; @@ -112,6 +114,8 @@ static NameKeyType buttonBackID = NAMEKEY_INVALID; static NameKeyType buttonStartID = NAMEKEY_INVALID; static NameKeyType buttonEmoteID = NAMEKEY_INVALID; static NameKeyType buttonSelectMapID = NAMEKEY_INVALID; +// Resume-from-replay button, back on the layout: the host arms or cancels, guests see the state. +static NameKeyType buttonResumeFromReplayID = NAMEKEY_INVALID; static NameKeyType checkboxLimitSuperweaponsID = NAMEKEY_INVALID; static NameKeyType comboBoxStartingCashID = NAMEKEY_INVALID; static NameKeyType windowMapID = NAMEKEY_INVALID; @@ -120,6 +124,26 @@ static GameWindow *parentLanGameOptions = nullptr; static GameWindow *buttonBack = nullptr; static GameWindow *buttonStart = nullptr; static GameWindow *buttonSelectMap = nullptr; +static GameWindow *buttonResumeFromReplay = nullptr; + +// What the Resume button says: RESUME or CANCEL RESUME for the host, RESUME ARMED +// (disabled) for a guest while one is pending, so the state is visible without the chat. +static void lanUpdateResumeButton() +{ + if (buttonResumeFromReplay == nullptr || TheLAN == nullptr) + return; + const Bool host = TheLAN->AmIHost(); + const Bool armed = TheLAN->GetMyGame() != nullptr && TheLAN->GetMyGame()->isResumeArmed(); + buttonResumeFromReplay->winEnable(host); + if (host) + buttonResumeFromReplay->winSetText(armed + ? TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeCancel", L"CANCEL RESUME") + : TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFromReplay", L"RESUME")); + else + buttonResumeFromReplay->winSetText(armed + ? TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeArmedButton", L"RESUME ARMED") + : TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFromReplay", L"RESUME")); +} static GameWindow *buttonEmote = nullptr; static GameWindow *textEntryChat = nullptr; static GameWindow *textEntryMapDisplay = nullptr; @@ -218,6 +242,9 @@ static void playerTooltip(GameWindow *window, setLANPlayerTooltip(player); } +static Bool lanArmedResumeStillMatches(LANGameInfo *game); +static void lanDisarmResumeForSettingsChange(); +static void lanCheckResumeArmChange(LANGameInfo *game); void StartPressed() { LANGameInfo *myGame = TheLAN->GetMyGame(); @@ -259,6 +286,12 @@ void StartPressed() return; } + // An armed resume-from-replay only works if the lobby still matches the recording. + if (TheLAN->AmIHost() && myGame->isResumeArmed() && !lanArmedResumeStillMatches(myGame)) + { + return; + } + // Check for observer + AI players if (TheGlobalData->m_netMinPlayers && !numHumans) { @@ -617,6 +650,7 @@ static void handleStartingCashSelection() { if (!s_isIniting) { + lanDisarmResumeForSettingsChange(); // send around the new data TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); lanUpdateSlotList(); // Update the accepted button UI @@ -647,6 +681,7 @@ static void handleLimitSuperweaponsClick() { if (!s_isIniting) { + lanDisarmResumeForSettingsChange(); // send around a new slotlist TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); lanUpdateSlotList(); // Update the accepted button UI @@ -668,6 +703,201 @@ void lanUpdateSlotList() //------------------------------------------------------------------------------------------------- /** Initialize the Gadgets Options Menu */ //------------------------------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// Resume-from-replay arming (host only). +// +// /resume copies the host's last recording to Resume.rep (ResumeFromReplay::prepareHostSource), +// aligns the lobby with it (slot order, factions, colors, start spots, teams, map, seed, +// starting cash and superweapon limit) and broadcasts the handoff frame in the game options +// (RH=). The file itself is pushed to every guest over the map transfer path when the game +// starts (LANAPI::OnGameStart), where every client validates its copy against the lobby +// before the recorder replays it in lockstep (see RecorderClass). +// ----------------------------------------------------------------------------- + +static UnsignedInt s_lanLastSeenResumeHandoff = 0; ///< the arm the last game options carried, to notice changes + +// Everyone in the lobby sees resume messages: the player who has to fix +// something (wrong name, wrong slot) is usually not the host. +static void lanSystemChat(const UnicodeString &text) +{ + if (TheLAN) + TheLAN->RequestChat(text, LANAPIInterface::LANCHAT_SYSTEM); +} + +static void lanLocalSystemChat(const UnicodeString &text) +{ + if (TheLAN) + TheLAN->OnChat(L"SYSTEM", TheLAN->GetLocalIP(), text, LANAPI::LANCHAT_SYSTEM); +} + +// Called from StartPressed while a resume is armed. Re-reads the resume source and refuses +// the start when the lobby has drifted from it since arming (someone left, rejoined under +// another name, joined into a recorded slot, or a setting changed). +static Bool lanArmedResumeStillMatches(LANGameInfo *game) +{ + ResumeFromReplay::SourceReplay replay; + UnicodeString why; + if (ResumeFromReplay::readSource(replay, why) + && ResumeFromReplay::sourceMatchesGame(replay, game, game->getResumeHandoffFrame(), why)) + return TRUE; + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeStartRefused", + L"Resume is armed and the lobby no longer matches the recording (%ls). Fix it, or type /resume off to start a fresh game.", why.str())); + return FALSE; +} + +// Put every human back into the slot they held in the replay and restore the +// recorded faction, color, start position and team on every occupied slot whose kind +// (human, or AI at the recorded difficulty) already matches. Recorded commands are +// indexed by slot position, so the lobby must line up with the recording exactly. +// Slot 0 is never swapped: the LAN protocol treats it as the host's slot and validation +// has already checked the host matches. Slot kinds are never changed here: setState() +// has destructive side effects on LAN slot identity, so a missing or wrong AI is +// reported by the full check afterwards for the host to fix. +static void reorderLobbyForResume(const ReplayGameInfo &replayInfo, LANGameInfo *lobby) +{ + Int i; + for (i = 1; i < MAX_SLOTS; ++i) + { + const GameSlot *rs = replayInfo.getConstSlot(i); + if (!rs || !rs->isHuman()) + continue; + Int currentIdx = -1; + for (Int j = 1; j < MAX_SLOTS; ++j) + { + const GameSlot *ls = lobby->getConstSlot(j); + if (ls && ls->isHuman() && ls->getName().compare(rs->getName()) == 0) + { + currentIdx = j; + break; + } + } + if (currentIdx < 0 || currentIdx == i) + continue; + LANGameSlot saved = *lobby->getLANSlot(i); + lobby->setSlot(i, *lobby->getLANSlot(currentIdx)); + lobby->setSlot(currentIdx, saved); + } + for (i = 0; i < MAX_SLOTS; ++i) + { + const GameSlot *rs = replayInfo.getConstSlot(i); + LANGameSlot *ls = lobby->getLANSlot(i); + if (!rs || !rs->isOccupied() || !ls || !ls->isOccupied() || rs->getState() != ls->getState()) + continue; + ls->setColor(rs->getColor()); + ls->setPlayerTemplate(rs->getPlayerTemplate()); + ls->setStartPos(rs->getStartPos()); + ls->setTeamNumber(rs->getTeamNumber()); + } +} + +static void lanClearResumeArm() +{ + if (TheLAN && TheLAN->GetMyGame()) + TheLAN->GetMyGame()->setResumeHandoffFrame(0); + s_lanLastSeenResumeHandoff = 0; +} + +// "/resume off": disarm and tell the lobby the next start is a fresh game. +static void disarmResumeFromReplay() +{ + if (!TheLAN || !TheLAN->GetMyGame() || !TheLAN->AmIHost()) + return; + const Bool wasArmed = TheLAN->GetMyGame()->isResumeArmed(); + lanClearResumeArm(); + if (wasArmed) + { + TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmed", L"Resume disarmed: the next start is a fresh game")); + } + lanUpdateResumeButton(); +} + +// A host change to a setting the recording fixes (map, starting cash, superweapon limit) +// disarms; the caller sends the game options right after. +static void lanDisarmResumeForSettingsChange() +{ + if (!TheLAN || !TheLAN->GetMyGame() || !TheLAN->AmIHost() || !TheLAN->GetMyGame()->isResumeArmed()) + return; + lanClearResumeArm(); + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmedSettings", L"Resume disarmed: the game settings changed")); + lanUpdateResumeButton(); +} + +// Guests: the arm arrives in the host's game options (RH=). The file itself is sent when the +// game starts, so all a guest can do here is say so, and note a disarm. +static void lanCheckResumeArmChange(LANGameInfo *game) +{ + if (!TheLAN || !game || TheLAN->AmIHost()) + return; + const UnsignedInt handoff = game->getResumeHandoffFrame(); + if (handoff == s_lanLastSeenResumeHandoff) + return; + s_lanLastSeenResumeHandoff = handoff; + lanUpdateResumeButton(); + if (handoff != 0) + lanLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeArmedGuest", + L"Resume armed by the host: the replay is sent when the game starts and replays to %ls", ResumeFromReplay::formatGameTime(handoff).str())); + else + lanLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmed", L"Resume disarmed: the next start is a fresh game")); +} + +// Copies the last recorded replay to the resume source, validates it against the current +// lobby, aligns the lobby with it and arms. Reports success or the reason for refusal in +// the lobby chat. +static void tryArmResumeFromReplay() +{ + if (!TheLAN || !TheLAN->GetMyGame() || !TheLAN->AmIHost()) + return; + LANGameInfo *game = TheLAN->GetMyGame(); + + ResumeFromReplay::SourceReplay replay; + UnsignedInt handoff = 0; + UnicodeString why; + if (!ResumeFromReplay::prepareHostSource(replay, handoff, why) + || !ResumeFromReplay::validateRoster(replay.game, game, why)) + { + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeRefused", L"Resume: %ls", why.str())); + return; + } + + reorderLobbyForResume(replay.game, game); + game->setMap(replay.game.getMap()); + game->setMapCRC(replay.game.getMapCRC()); + game->setMapSize(replay.game.getMapSize()); + game->setSeed(replay.game.getSeed()); + game->setStartingCash(replay.game.getStartingCash()); + game->setSuperweaponRestriction(replay.game.getSuperweaponRestriction()); + game->setResumeHandoffFrame(handoff); + + // The alignment above cannot add or change AI slots; the full check reports what is left for the host to fix. + if (!ResumeFromReplay::sourceMatchesGame(replay, game, handoff, why)) + { + lanClearResumeArm(); + TheLAN->RequestGameOptions(GenerateGameOptionsString(), true); + lanUpdateSlotList(); + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeRefused", L"Resume: %ls", why.str())); + return; + } + + // The options travel in one LAN packet; refuse to arm rather than let the field be cut off. + AsciiString options = GenerateGameOptionsString(); + if (options.getLength() >= m_lanMaxOptionsLength) + { + lanClearResumeArm(); + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeOptionsTooLong", L"Resume: the game options are too long to carry the resume, shorten the player names")); + return; + } + TheLAN->RequestGameOptions(options, true); + lanUpdateSlotList(); + updateGameOptions(); + s_lanLastSeenResumeHandoff = handoff; + + lanSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeArmed", + L"Resume armed: the next start replays the last match to %ls, then hands control back", ResumeFromReplay::formatGameTime(handoff).str())); + lanUpdateResumeButton(); +} + void InitLanGameGadgets() { //Initialize the gadget IDs @@ -679,6 +909,7 @@ void InitLanGameGadgets() listboxChatWindowLanGameID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:ListboxChatWindowLanGame" ); buttonEmoteID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:ButtonEmote" ); buttonSelectMapID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:ButtonSelectMap" ); + buttonResumeFromReplayID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:ButtonResumeFromReplay" ); checkboxLimitSuperweaponsID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:CheckboxLimitSuperweapons" ); comboBoxStartingCashID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:ComboBoxStartingCash" ); windowMapID = TheNameKeyGenerator->nameToKey( "LanGameOptionsMenu.wnd:MapWindow" ); @@ -692,6 +923,8 @@ void InitLanGameGadgets() DEBUG_ASSERTCRASH(buttonSelectMap, ("Could not find the buttonSelectMap")); buttonStart = TheWindowManager->winGetWindowFromId( parentLanGameOptions,buttonStartID ); DEBUG_ASSERTCRASH(buttonStart, ("Could not find the buttonStart")); + buttonResumeFromReplay = TheWindowManager->winGetWindowFromId( parentLanGameOptions, buttonResumeFromReplayID ); // absent from older .wnd layouts + lanUpdateResumeButton(); buttonBack = TheWindowManager->winGetWindowFromId( parentLanGameOptions, buttonBackID); DEBUG_ASSERTCRASH(buttonBack, ("Could not find the buttonBack")); listboxChatWindowLanGame = TheWindowManager->winGetWindowFromId( parentLanGameOptions, listboxChatWindowLanGameID ); @@ -790,11 +1023,14 @@ void InitLanGameGadgets() void DeinitLanGameGadgets() { + // Disarm any pending resume so it cannot survive into a future lobby session. + lanClearResumeArm(); parentLanGameOptions = nullptr; buttonEmote = nullptr; buttonSelectMap = nullptr; buttonStart = nullptr; buttonBack = nullptr; + buttonResumeFromReplay = nullptr; listboxChatWindowLanGame = nullptr; textEntryChat = nullptr; textEntryMapDisplay = nullptr; @@ -885,6 +1121,7 @@ void LanGameOptionsMenuInit( WindowLayout *layout, void *userData ) //DEBUG_LOG(("LanGameOptionsMenuInit(): map is %s", TheLAN->GetMyGame()->getMap().str())); buttonStart->winSetText(TheGameText->fetch("GUI:Accept")); buttonSelectMap->winEnable( FALSE ); + lanUpdateResumeButton(); checkboxLimitSuperweapons->winEnable( FALSE ); // Can look but only host can touch comboBoxStartingCash->winEnable( FALSE ); // Ditto TheLAN->GetMyGame()->setMapCRC( TheLAN->GetMyGame()->getMapCRC() ); // force a recheck @@ -940,6 +1177,7 @@ void updateGameOptions() { LANGameInfo *theGame = TheLAN->GetMyGame(); UnicodeString mapDisplayName; + lanCheckResumeArmChange(theGame); if (theGame && AreSlotListUpdatesEnabled()) { const GameSlot *localSlot = theGame->getConstSlot(theGame->getLocalSlotNum()); @@ -1247,6 +1485,17 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m if (!txtInput.isEmpty()) TheLAN->RequestChat(txtInput, LANAPIInterface::LANCHAT_EMOTE); } + else if ( controlID == buttonResumeFromReplayID ) + { + // Same as /resume and /resume off. + if (TheLAN->AmIHost()) + { + if (TheLAN->GetMyGame() && TheLAN->GetMyGame()->isResumeArmed()) + disarmResumeFromReplay(); + else + tryArmResumeFromReplay(); + } + } else if ( controlID == buttonSelectMapID ) { //buttonBack->winEnable( false ); @@ -1381,6 +1630,17 @@ WindowMsgHandledType LanGameOptionsMenuSystem( GameWindow *window, UnsignedInt m txtInput.trim(); // Echo the user's input to the chat window if (!txtInput.isEmpty()) + // "/resume" arms resume-from-replay, "/resume off" disarms + if (txtInput.compareNoCase(L"/resume") == 0 || txtInput.compareNoCase(L"/resume off") == 0) + { + if (!TheLAN->AmIHost()) + TheLAN->OnChat(L"SYSTEM", TheLAN->GetLocalIP(), TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHostOnly", L"Only the host can resume a game"), LANAPI::LANCHAT_SYSTEM); + else if (txtInput.compareNoCase(L"/resume off") == 0) + disarmResumeFromReplay(); + else + tryArmResumeFromReplay(); + } + else TheLAN->RequestChat(txtInput, LANAPIInterface::LANCHAT_NORMAL); } @@ -1408,6 +1668,7 @@ void PostToLanGameOptions( PostToLanGameType post ) { LANGameInfo *game = TheLAN->GetMyGame(); game->resetAccepted(); + lanDisarmResumeForSettingsChange(); // a new map is not the recording's updateGameOptions(); lanUpdateSlotList(); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ReplayMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ReplayMenu.cpp index 4188d26ba91..ce171bbfcb1 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ReplayMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ReplayMenu.cpp @@ -45,6 +45,7 @@ #include "GameClient/GameWindowManager.h" #include "GameClient/MessageBox.h" #include "GameClient/MapUtil.h" +#include "GameNetwork/ResumeFromReplay.h" #include "GameClient/Mouse.h" #include "GameClient/GameText.h" #include "GameClient/GameWindowTransitions.h" @@ -105,7 +106,8 @@ UnicodeString GetReplayFilenameFromListbox(GameWindow *listbox, Int index) //------------------------------------------------------------------------------------------------- -static Bool readReplayMapInfo(const AsciiString& filename, RecorderClass::ReplayHeader &header, ReplayGameInfo &info, const MapMetaData *&mapData) +// No longer static: resume-from-replay reads replay headers too (declared in ResumeFromReplay.h). +Bool readReplayMapInfo(const AsciiString& filename, RecorderClass::ReplayHeader &header, ReplayGameInfo &info, const MapMetaData *&mapData) { header.forPlayback = FALSE; header.filename = filename; diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp index d8c0b2c2eba..705ef032159 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLGameSetupMenu.cpp @@ -53,6 +53,7 @@ #include "GameClient/GadgetStaticText.h" #include "GameClient/GadgetCheckBox.h" #include "GameClient/MapUtil.h" +#include "GameNetwork/ResumeFromReplay.h" #include "GameClient/EstablishConnectionsMenu.h" #include "GameClient/GameWindowTransitions.h" #include "GameNetwork/GameSpy/LobbyUtils.h" @@ -203,6 +204,8 @@ static NameKeyType buttonBackID = NAMEKEY_INVALID; static NameKeyType buttonStartID = NAMEKEY_INVALID; static NameKeyType buttonEmoteID = NAMEKEY_INVALID; static NameKeyType buttonSelectMapID = NAMEKEY_INVALID; +// Resume-from-replay button (host arms or cancels; guests see the state). +static NameKeyType buttonResumeID = NAMEKEY_INVALID; static NameKeyType windowMapID = NAMEKEY_INVALID; #if defined(GENERALS_ONLINE_ENABLE_MATCH_START_COUNTDOWN) @@ -220,6 +223,7 @@ static GameWindow *parentWOLGameSetup = NULL; static GameWindow *buttonBack = NULL; static GameWindow *buttonStart = NULL; static GameWindow *buttonSelectMap = NULL; +static GameWindow *buttonResume = NULL; static GameWindow *buttonEmote = NULL; static GameWindow *textEntryChat = NULL; static GameWindow *textEntryMapDisplay = NULL; @@ -977,8 +981,18 @@ static void WOLLockSettings() } } +static Bool wolArmedResumeStillMatches(NGMPGame *game); +static void wolCheckResumeArmChange(); static void StartPressed() { + // An armed resume-from-replay only works if the lobby still + // matches the recording and every human holds the resume source. + { + NGMP_OnlineServices_LobbyInterface* pResumeLobby = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *resumeGame = pResumeLobby ? pResumeLobby->GetCurrentGame() : nullptr; + if (resumeGame != nullptr && resumeGame->isResumeArmed() && !wolArmedResumeStillMatches(resumeGame)) + return; + } Bool isReady = TRUE; Bool allHaveMap = TRUE; Int playerCount = 0; @@ -1596,9 +1610,465 @@ void WOLDisplaySlotList(void) //------------------------------------------------------------------------------------------------- /** Initialize the Gadgets Options Menu */ //------------------------------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// Resume-from-replay arming for online lobbies (host only). +// +// Same contract as the LAN lobby (LanGameOptionsMenu.cpp): /resume copies the host's last +// recording to Resume.rep and aligns the lobby with it. Online the file is uploaded to the +// service first and the arm (handoff frame plus the replay's seed) is sent only once the +// upload finished; guests download the file when the arm reaches them, report in chat +// whether it matches and flag themselves as holding it, and the host cannot start until +// every human holds it. Slot order is owned by the service, so the lobby must already +// match the replay's slot order; the host is told what differs instead of the lobby +// being reordered. +// ----------------------------------------------------------------------------- + +static UnsignedInt s_wolLastSeenResumeHandoff = 0; ///< the arm the last lobby update carried, to notice changes +static Bool s_wolResumeArmInProgress = FALSE; ///< an upload plus arm is under way (host) +static uint32_t s_wolResumeDownloadedGeneration = 0; ///< the service's upload generation this client last downloaded (guest) +static Bool s_wolResumeDownloadInProgress = FALSE; ///< a download is under way (guest) + +static void wolLocalSystemChat(const UnicodeString &text) +{ + if (listboxGameSetupChat) + GadgetListBoxAddEntryText(listboxGameSetupChat, text, GameSpyColor[GSCOLOR_DEFAULT], -1, -1); +} + +// Roster problems go to the whole lobby: the player who has to fix something is +// usually not the host. +static void wolLobbySystemChat(const UnicodeString &text) +{ + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + UnicodeString msg = text; + if (pLobbyInterface != nullptr) + pLobbyInterface->SendAnnouncementMessageToCurrentLobby(msg, true); + else + wolLocalSystemChat(text); +} + +// Called on the host's start while a resume is armed: refuse when the lobby has drifted +// from the recording since arming, or when a human does not hold the resume source yet. +static Bool wolArmedResumeStillMatches(NGMPGame *game) +{ + ResumeFromReplay::SourceReplay replay; + UnicodeString why; + if (!ResumeFromReplay::readSource(replay, why) + || !ResumeFromReplay::sourceMatchesGame(replay, game, game->getResumeHandoffFrame(), why)) + { + wolLobbySystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeStartRefused", + L"Resume is armed but the lobby no longer matches the recording (%ls). Host: press Resume again to realign it, or Cancel Resume for a fresh game.", why.str())); + return FALSE; + } + + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + if (pLobbyInterface != nullptr) + { + for (const LobbyMemberEntry &member : pLobbyInterface->GetCurrentLobby().members) + { + if (member.IsHuman() && !member.has_resume_replay) + { + UnicodeString name(from_utf8(member.display_name).c_str()); + wolLobbySystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeWaitingForDownload", + L"Resume: waiting for %ls to download the replay. If it never arrives, they should leave and rejoin, then the host presses Resume again", name.str())); + return FALSE; + } + } + } + return TRUE; +} + +// ----------------------------------------------------------------------------- +// The arm is a chain of service +// requests, each started from the previous one's completion: the map, the +// starting cash and the superweapon limit (only those that differ from the +// recording), then the whole slot table in one piece, then the upload and the +// arm itself. The service disarms on every one of those changes, which is why +// the arm comes last and nothing is sent in parallel. The local lobby is not +// touched here: every step shows up through the lobby push, exactly as it does +// when the host changes a setting by hand. +// ----------------------------------------------------------------------------- +struct WolResumeArmJob +{ + UnsignedInt handoff = 0; + Int seed = 0; + AsciiString mapKey; + UnicodeString mapDisplayU; + Bool mapOfficial = FALSE; + Int mapNumPlayers = 0; + UnsignedInt startingCash = 0; + Bool limitSuperweapons = FALSE; + std::vector slots; +}; +static WolResumeArmJob s_wolResumeJob; + +// Everyone sees why it stopped and what to do about it; the host is usually not +// the one who has to act. +static void wolResumeArmFailed(const UnicodeString &what, const UnicodeString &fix) +{ + s_wolResumeArmInProgress = FALSE; + // Some reasons already end in a period; do not double it. + UnicodeString reason = what; + if (!reason.isEmpty() && reason.getCharAt(reason.getLength() - 1) == L'.') + { + reason.removeLastChar(); + } + UnicodeString msg; + msg.format(L"Resume: %ls. %ls", reason.str(), fix.str()); + wolLobbySystemChat(msg); +} + +static Bool wolResumeSlotsAlreadyAligned(NGMP_OnlineServices_LobbyInterface *pLobbyInterface) +{ + const LobbyEntry &lobby = pLobbyInterface->GetCurrentLobby(); + for (const LobbySlotSpec &spec : s_wolResumeJob.slots) + { + const LobbyMemberEntry *m = nullptr; + for (const LobbyMemberEntry &member : lobby.members) + { + if (member.m_SlotIndex == spec.slotIndex) + { + m = &member; + break; + } + } + if (m == nullptr || m->m_SlotState != spec.slotState) + return FALSE; + if (spec.slotState == SLOT_PLAYER && m->user_id != spec.userID) + return FALSE; + if (spec.slotState == SLOT_OPEN || spec.slotState == SLOT_CLOSED) + continue; + if (m->side != spec.side || m->color != spec.color || m->startpos != spec.startPos || m->team != spec.team) + return FALSE; + } + return TRUE; +} + +static void wolUpdateResumeButton(); + +static void wolResumeArmStep(Int step) +{ + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *game = pLobbyInterface == nullptr ? nullptr : pLobbyInterface->GetCurrentGame(); + if (pLobbyInterface == nullptr || game == nullptr || !pLobbyInterface->IsHost()) + { + s_wolResumeArmInProgress = FALSE; + return; + } + const WolResumeArmJob &job = s_wolResumeJob; + const LobbyEntry &lobby = pLobbyInterface->GetCurrentLobby(); + NetworkLog(ELogVerbosity::LOG_RELEASE, "Resume: arm step %d", step); + + if (step <= 0 && game->getMap().compareNoCase(job.mapKey) != 0) + { + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSettingMap", + L"Resume: switching the lobby to the recording's map, %ls", job.mapDisplayU.str())); + pLobbyInterface->UpdateCurrentLobby_Map(job.mapDisplayU, job.mapKey, job.mapOfficial, job.mapNumPlayers, [](bool bOK) + { + if (bOK) + wolResumeArmStep(1); + else + wolResumeArmFailed(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMapRefused", L"the service refused the map change"), + TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixMap", L"Host: select the recording's map with Select Map, then press Resume again.")); + }); + return; + } + if (step <= 1 && lobby.starting_cash != job.startingCash) + { + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSettingCash", + L"Resume: setting the starting cash to %u, as recorded", job.startingCash)); + pLobbyInterface->UpdateCurrentLobby_StartingCash(job.startingCash, [](bool bOK) + { + if (bOK) + wolResumeArmStep(2); + else + wolResumeArmFailed(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeCashRefused", L"the service refused the starting cash change"), + TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixCash", L"Host: set the starting cash the recording used, then press Resume again.")); + }); + return; + } + if (step <= 2 && lobby.limit_superweapons != (job.limitSuperweapons != FALSE)) + { + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSettingSuperweapons", + L"Resume: setting the superweapon limit as recorded")); + pLobbyInterface->UpdateCurrentLobby_LimitSuperweapons(job.limitSuperweapons != FALSE, [](bool bOK) + { + if (bOK) + wolResumeArmStep(3); + else + wolResumeArmFailed(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSuperweaponsRefused", L"the service refused the superweapon limit change"), + TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixSuperweapons", L"Host: set the superweapon limit the recording used, then press Resume again.")); + }); + return; + } + if (step <= 3 && !wolResumeSlotsAlreadyAligned(pLobbyInterface)) + { + wolLobbySystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeAligning", + L"Resume: moving everyone to the slot, faction, color and start position they had in the recording")); + pLobbyInterface->UpdateCurrentLobby_AlignSlots(job.slots, [](bool bOK, const std::string &error) + { + if (bOK) + { + wolResumeArmStep(4); + return; + } + UnicodeString what; + what.format(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeAlignRefused", L"the lobby could not be arranged like the recording (%hs)"), error.c_str()); + wolResumeArmFailed(what, TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixAlign", + L"Everyone: take the slot you had in that match, then the host presses Resume again.")); + }); + return; + } + + // Upload the source, then arm; the arm is announced only once the service accepted it. + const UnsignedInt handoffFrame = job.handoff; + const Int seed = job.seed; + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeUploading", L"Resume: uploading the replay to the service...")); + pLobbyInterface->UploadResumeReplay(ResumeFromReplay::getSourceFilePath(), [handoffFrame, seed](bool bUploaded) + { + NGMP_OnlineServices_LobbyInterface* pLobby = NGMP_OnlineServicesManager::GetInterface(); + if (!bUploaded || pLobby == nullptr) + { + wolResumeArmFailed(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeUploadFailed", L"the replay upload failed, nothing is armed"), + TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixUpload", L"Host: press Resume again; if it keeps failing the replay may be too large for the service.")); + return; + } + pLobby->UpdateCurrentLobby_ArmResume(handoffFrame, seed, [handoffFrame](bool bArmed) + { + s_wolResumeArmInProgress = FALSE; + NGMP_OnlineServices_LobbyInterface* pLobby2 = NGMP_OnlineServicesManager::GetInterface(); + if (!bArmed || pLobby2 == nullptr) + { + wolResumeArmFailed(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeArmRefused", L"the service refused to arm"), + TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFixArm", L"Host: press Resume again. A setting changed while it was arming, or the upload was not accepted.")); + return; + } + s_wolLastSeenResumeHandoff = handoffFrame; + pLobby2->UpdateCurrentLobby_HasResumeReplay(true); // the host holds the source by definition + wolLobbySystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeArmed", + L"Resume armed: the next start replays the last match to %ls, then hands control back", ResumeFromReplay::formatGameTime(handoffFrame).str())); + wolUpdateResumeButton(); + }); + }); +} + +// Press Resume (or type /resume): checks the recording against the lobby, then +// drives the service through whatever has to change (see wolResumeArmStep). +// /resume off, or the button while armed, disarms. +static void wolTryArmResumeFromReplay(Bool disarm) +{ + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + if (pLobbyInterface == nullptr || pLobbyInterface->GetCurrentGame() == nullptr) + return; + if (!pLobbyInterface->IsHost()) + { + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHostOnly", L"Only the host can resume a game")); + return; + } + NGMPGame *game = pLobbyInterface->GetCurrentGame(); + + if (disarm) + { + pLobbyInterface->UpdateCurrentLobby_ArmResume(0, game->getSeed(), [](bool bSuccess) + { + if (bSuccess) + wolLobbySystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmed", L"Resume disarmed: the next start is a fresh game")); + else + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmRefused", L"Resume: the service refused to disarm; try again")); + wolUpdateResumeButton(); + }); + return; + } + if (s_wolResumeArmInProgress) + { + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeUploadInProgress", L"Resume: still arming, wait for it to finish")); + return; + } + + ResumeFromReplay::SourceReplay replay; + UnsignedInt handoff = 0; + UnicodeString why; + if (!ResumeFromReplay::prepareHostSource(replay, handoff, why)) + { + // The recording itself is unusable (or there is none): nothing in the lobby can fix that. + UnicodeString reason = why; + if (!reason.isEmpty() && reason.getCharAt(reason.getLength() - 1) == L'.') + { + reason.removeLastChar(); // the sentence continues + } + UnicodeString msg; + msg.format(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSourceRefused", L"Resume: %ls. The host's last recording is what gets resumed, and it must come from this build."), reason.str()); + wolLobbySystemChat(msg); + return; + } + const ReplayGameInfo &rg = replay.game; + + if (replay.mapData->m_CRC != rg.getMapCRC()) + { + UnicodeString msg; + msg.format(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHostMapDiffers", + L"Resume: the host's copy of %ls is not the file that match was played on. Host: reinstall that map, then press Resume again."), replay.mapData->m_displayName.str()); + wolLobbySystemChat(msg); + return; + } + + // Every human must be present under the recorded name, and only the recorded host can do this. + if (!ResumeFromReplay::validateRoster(rg, game, why)) + { + UnicodeString msg; + msg.format(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeRosterRefused", L"Resume: %ls. Get exactly the players of that match into this lobby, then press Resume again."), why.str()); + wolLobbySystemChat(msg); + return; + } + + // Everything the service has to be told, taken from the recording up front: the job + // outlives this call, and the ReplayGameInfo does not. + WolResumeArmJob &job = s_wolResumeJob; + job = WolResumeArmJob(); + job.handoff = handoff; + job.seed = rg.getSeed(); + job.mapKey = rg.getMap(); + job.mapDisplayU = replay.mapData->m_displayName; + job.mapOfficial = replay.mapData->m_isOfficial; + job.mapNumPlayers = replay.mapData->m_numPlayers; + job.startingCash = rg.getStartingCash().countMoney(); + job.limitSuperweapons = rg.getSuperweaponRestriction() != 0; + const LobbyEntry &lobby = pLobbyInterface->GetCurrentLobby(); + for (Int i = 0; i < MAX_SLOTS; ++i) + { + LobbySlotSpec spec; + spec.slotIndex = i; + const GameSlot *rs = rg.getConstSlot(i); + if (rs != nullptr && rs->isHuman()) + { + spec.slotState = SLOT_PLAYER; + for (const LobbyMemberEntry &member : lobby.members) + { + if (member.IsHuman() && rs->getName().compare(UnicodeString(from_utf8(member.display_name).c_str())) == 0) + { + spec.userID = member.user_id; + break; + } + } + if (spec.userID == -1) + { + // validateRoster matched the names against the local slots, which mirror the members list; + // a miss here means the two disagree, which a lobby push will cure. + UnicodeString msg; + msg.format(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMemberUnknown", + L"Resume: %ls is not in the lobby yet. Wait a moment, then press Resume again."), rs->getName().str()); + wolLobbySystemChat(msg); + return; + } + } + else if (rs != nullptr && rs->isOccupied()) + { + spec.slotState = rs->getState(); + } + else + { + spec.slotState = (i < job.mapNumPlayers) ? SLOT_OPEN : SLOT_CLOSED; + } + if (rs != nullptr && rs->isOccupied()) + { + spec.side = rs->getPlayerTemplate(); + spec.color = rs->getColor(); + spec.startPos = rs->getStartPos(); + spec.team = rs->getTeamNumber(); + } + job.slots.push_back(spec); + } + + NetworkLog(ELogVerbosity::LOG_RELEASE, "Resume: arming, handoff=%u seed=%d map=%s cash=%u sw=%d", handoff, job.seed, job.mapKey.str(), job.startingCash, (int)job.limitSuperweapons); + s_wolResumeArmInProgress = TRUE; + wolResumeArmStep(0); +} + +// The Resume button says what the arm is doing: for the host it arms or cancels; +// for everyone else it is read-only and shows RESUME ARMED while one is pending, +// so a guest who missed the chat line still knows the next start is a resume. +static void wolUpdateResumeButton() +{ + if (buttonResume == NULL) + return; + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *game = pLobbyInterface == nullptr ? nullptr : pLobbyInterface->GetCurrentGame(); + const Bool armed = game != nullptr && game->isResumeArmed(); + const Bool host = pLobbyInterface != nullptr && pLobbyInterface->IsHost(); + if (host) + { + buttonResume->winEnable(TRUE); + buttonResume->winSetText(armed + ? TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeCancel", L"CANCEL RESUME") + : TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFromReplay", L"RESUME")); + } + else + { + buttonResume->winEnable(FALSE); + buttonResume->winSetText(armed + ? TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeArmedButton", L"RESUME ARMED") + : TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeFromReplay", L"RESUME")); + } +} + + +// Every lobby update: a guest downloads the source whenever the lobby is armed with an upload +// it has not fetched yet (the service bumps ResumeReplayGeneration on every arm, so a re-arm +// with the same handoff frame is fetched again), tells the lobby whether its copy matches and +// flags itself; everyone notes an arm or a disarm. +static void wolCheckResumeArmChange() +{ + NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *game = pLobbyInterface == nullptr ? nullptr : pLobbyInterface->GetCurrentGame(); + if (game == nullptr) + return; + const UnsignedInt handoff = game->getResumeHandoffFrame(); + if (handoff != s_wolLastSeenResumeHandoff) + { + s_wolLastSeenResumeHandoff = handoff; + if (handoff == 0) + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDisarmed", L"Resume disarmed: the next start is a fresh game")); + else if (!pLobbyInterface->IsHost()) + // Said locally, from the lobby state itself, so it does not depend on the host's + // chat line getting through. + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeArmedGuest", + L"Resume armed by the host: the next start replays the last match to %ls, then hands control back", ResumeFromReplay::formatGameTime(handoff).str())); + } + wolUpdateResumeButton(); + if (handoff == 0 || pLobbyInterface->IsHost()) + return; // nothing armed, or the host: it announced the arm and holds the source + + const uint32_t generation = pLobbyInterface->GetCurrentLobby().resume_replay_generation; + if (generation == s_wolResumeDownloadedGeneration || s_wolResumeDownloadInProgress) + return; + s_wolResumeDownloadedGeneration = generation; + s_wolResumeDownloadInProgress = TRUE; + + wolLocalSystemChat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDownloading", L"Resume: downloading the replay from the service...")); + pLobbyInterface->DownloadResumeReplay(ResumeFromReplay::getSourceFilePath(), [](bool bDownloaded) + { + s_wolResumeDownloadInProgress = FALSE; + NGMP_OnlineServices_LobbyInterface* pLobby = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *lobbyGame = pLobby == nullptr ? nullptr : pLobby->GetCurrentGame(); + if (pLobby == nullptr || lobbyGame == nullptr) + return; + UnicodeString report; + Bool usable = FALSE; + if (!bDownloaded) + report = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeDownloadFailed", L"Resume: my download of the replay failed. Host: press Cancel Resume, then Resume again"); + else if (!(usable = ResumeFromReplay::guestSourceReport(lobbyGame, report))) + report.concat(TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeGuestMismatchFix", L". Host: press Resume again to realign the lobby")); + pLobby->SendChatMessageToCurrentLobby(report, false); + pLobby->UpdateCurrentLobby_HasResumeReplay(usable); + }); +} + void InitWOLGameGadgets() { ClearGSMessageBoxes(); + s_wolLastSeenResumeHandoff = 0; + s_wolResumeArmInProgress = FALSE; + s_wolResumeDownloadedGeneration = 0; + s_wolResumeDownloadInProgress = FALSE; NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface(); NGMPGame* theGameInfo = pLobbyInterface == nullptr ? nullptr : pLobbyInterface->GetCurrentGame(); @@ -1620,6 +2090,7 @@ void InitWOLGameGadgets() listboxGameSetupChatID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:ListboxChatWindowGameSpyGameSetup" ); buttonEmoteID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:ButtonEmote" ); buttonSelectMapID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:ButtonSelectMap" ); + buttonResumeID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:ButtonResume" ); checkBoxUseStatsID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:CheckBoxUseStats" ); windowMapID = TheNameKeyGenerator->nameToKey( "GameSpyGameOptionsMenu.wnd:MapWindow" ); checkBoxLimitSuperweaponsID = TheNameKeyGenerator->nameToKey("GameSpyGameOptionsMenu.wnd:CheckboxLimitSuperweapons"); @@ -1633,6 +2104,8 @@ void InitWOLGameGadgets() parentWOLGameSetup = TheWindowManager->winGetWindowFromId( NULL, parentWOLGameSetupID ); buttonEmote = TheWindowManager->winGetWindowFromId( parentWOLGameSetup,buttonEmoteID ); buttonSelectMap = TheWindowManager->winGetWindowFromId( parentWOLGameSetup,buttonSelectMapID ); + buttonResume = TheWindowManager->winGetWindowFromId( parentWOLGameSetup, buttonResumeID ); // absent from older .wnd layouts + wolUpdateResumeButton(); checkBoxUseStats = TheWindowManager->winGetWindowFromId( parentWOLGameSetup, checkBoxUseStatsID ); buttonStart = TheWindowManager->winGetWindowFromId( parentWOLGameSetup,buttonStartID ); buttonBack = TheWindowManager->winGetWindowFromId( parentWOLGameSetup, buttonBackID); @@ -1679,6 +2152,7 @@ void InitWOLGameGadgets() { checkBoxLimitSuperweapons->winEnable( false ); comboBoxStartingCash->winEnable( false ); + wolUpdateResumeButton(); NameKeyType labelID = TheNameKeyGenerator->nameToKey("GameSpyGameOptionsMenu.wnd:StartingCashLabel"); TheWindowManager->winGetWindowFromId(parentWOLGameSetup, labelID)->winEnable( FALSE ); } @@ -1687,6 +2161,7 @@ void InitWOLGameGadgets() { checkBoxLimitSuperweapons->winEnable(true); comboBoxStartingCash->winEnable(true); + wolUpdateResumeButton(); } #endif @@ -1840,6 +2315,7 @@ void DeinitWOLGameGadgets() parentWOLGameSetup = NULL; buttonEmote = NULL; buttonSelectMap = NULL; + buttonResume = NULL; buttonStart = NULL; buttonBack = NULL; listboxGameSetupChat = NULL; @@ -2005,6 +2481,7 @@ void WOLGameSetupMenuInit( WindowLayout *layout, void *userData ) { WOLDisplaySlotList(); WOLDisplayGameOptions(); + wolCheckResumeArmChange(); // Resume-from-replay arm arrived or left }); pLobbyInterface->RegisterForGameStartPacket([]() @@ -2240,6 +2717,7 @@ void WOLGameSetupMenuInit( WindowLayout *layout, void *userData ) buttonStart->winSetText(TheGameText->fetch("GUI:Accept")); buttonStart->winEnable( FALSE ); buttonSelectMap->winEnable( FALSE ); + wolUpdateResumeButton(); initialAcceptEnable = FALSE; WOLDisplaySlotList(); @@ -2531,6 +3009,7 @@ void WOLGameSetupMenuUpdate( WindowLayout * layout, void *userData) buttonStart->winSetText(TheGameText->fetch("GUI:Start")); buttonStart->winEnable(TRUE); buttonSelectMap->winEnable(TRUE); + wolUpdateResumeButton(); initialAcceptEnable = TRUE; comboBoxStartingCash->winEnable(TRUE); @@ -3635,10 +4114,20 @@ Bool handleGameSetupSlashCommands(UnicodeString uText) } #if defined(GENERALS_ONLINE) + else if (token == "resume") + { + // Resume-from-replay arming lives behind a chat command so no layout change is needed + AsciiString arg; + remainder.nextToken(&arg); + arg.toLower(); + wolTryArmResumeFromReplay(arg == "off"); + return TRUE; // was a slash command + } else if (token == "help" || token == "commands") { const Color helpColor = GameMakeColor(127, 127, 127, 255); GadgetListBoxAddEntryText(listboxGameSetupChat, UnicodeString(L"/me - Send an emote."), helpColor, -1, -1); + GadgetListBoxAddEntryText(listboxGameSetupChat, UnicodeString(L"/resume - Same as the Resume button: continue the host's last recorded match; /resume off cancels (host only)."), helpColor, -1, -1); GadgetListBoxAddEntryText(listboxGameSetupChat, UnicodeString(L"/friendsonly - Let only friends join (host only)."), helpColor, -1, -1); GadgetListBoxAddEntryText(listboxGameSetupChat, UnicodeString(L"/public - Let anyone join (host only)."), helpColor, -1, -1); GadgetListBoxAddEntryText(listboxGameSetupChat, UnicodeString(L"/setpassword - Set a lobby password (host only)."), helpColor, -1, -1); @@ -4137,6 +4626,13 @@ WindowMsgHandledType WOLGameSetupMenuSystem( GameWindow *window, UnsignedInt msg WOLMapSelectLayout->hide( FALSE ); WOLMapSelectLayout->bringForward(); } + else if ( controlID == buttonResumeID ) + { + // Same as /resume and /resume off. + NGMP_OnlineServices_LobbyInterface* pResumeLobby = NGMP_OnlineServicesManager::GetInterface(); + NGMPGame *resumeGame = pResumeLobby ? pResumeLobby->GetCurrentGame() : nullptr; + wolTryArmResumeFromReplay(resumeGame != nullptr && resumeGame->isResumeArmed()); + } else if ( controlID == buttonStartID ) { savePlayerInfo(); diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp index d898c52b9ff..f055b319a1f 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/WOLMapSelectMenu.cpp @@ -101,6 +101,7 @@ static const char *gadgetsToHide[] = "ButtonStart", "StaticTextMapPreview", + "ButtonResume", nullptr }; static const char *perPlayerGadgetsToHide[] = diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp index b833f4c59d1..cc61879077e 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp @@ -3292,7 +3292,8 @@ Coord2D InGameUI::getScrollAmount() //------------------------------------------------------------------------------------------------- void InGameUI::setGUICommand(const CommandButton* command) { - if (TheRecorder->getMode() == RECORDERMODETYPE_PLAYBACK) + // Resume-from-replay catchup: no GUI commands until control is handed back + if (TheRecorder->getMode() == RECORDERMODETYPE_PLAYBACK || TheRecorder->isResumeInputBlocked()) return; // sanity diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp index 1fa00d1b2e6..02e7636ba58 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp @@ -2780,7 +2780,13 @@ void GameLogic::processCommandList(CommandList* list) logicMessageDispatcher(msg, NULL); } - if (m_shouldValidateCRCs && !TheNetwork->sawCRCMismatch()) + // Skip CRC validation while in resume-from-replay + // catchup (the CRC messages of that window come out of the resume source, not from the + // live peers) and for one run-ahead after the handoff, until the first set the peers + // exchanged live can have arrived. + const Bool resumeSuppressed = (TheRecorder && TheRecorder->isResumeCRCValidationSuppressed()); + + if (m_shouldValidateCRCs && !TheNetwork->sawCRCMismatch() && !resumeSuppressed) { Bool sawCRCMismatch = FALSE; Int numPlayers = 0; diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/HTTP/HTTPRequest.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/HTTP/HTTPRequest.cpp index 02786d7f3d9..08d513ef342 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/HTTP/HTTPRequest.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/HTTP/HTTPRequest.cpp @@ -273,6 +273,17 @@ void HTTPRequest::PlatformStartRequest() } } + // Say what the body is. Without a Content-Type + // libcurl sends application/x-www-form-urlencoded, and a service that form-parses on that + // header rejects any body large enough to exceed its form key limit, because the whole + // payload reads as one enormous key. Every request built here carries JSON unless the + // caller set a type of its own (the S3 uploads set theirs). + if ((m_httpVerb == EHTTPVerb::HTTP_VERB_POST || m_httpVerb == EHTTPVerb::HTTP_VERB_PUT || m_httpVerb == EHTTPVerb::HTTP_VERB_DELETE) + && m_mapHeaders.find("Content-Type") == m_mapHeaders.end()) + { + m_mapHeaders["Content-Type"] = "application/json"; + } + for (auto& kvPair : m_mapHeaders) { std::string strHeader = kvPair.first + ": " + kvPair.second; diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMPGame.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMPGame.cpp index 62f7b703cba..179a1b92618 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMPGame.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMPGame.cpp @@ -9,6 +9,7 @@ #include "Common/RandomValue.h" #include "GameNetwork/GeneralsOnline/NGMP_interfaces.h" #include "GameNetwork/NetworkInterface.h" +#include "GameNetwork/ResumeFromReplay.h" #include "Common/GlobalData.h" #include "GameClient/View.h" #include "../NextGenMP_defines.h" @@ -115,6 +116,9 @@ void NGMPGame::SyncWithLobby(LobbyEntry& lobby) // rng seed setSeed(lobby.rng_seed); + // Resume-from-replay arming (see ResumeFromReplay) + setResumeHandoffFrame(lobby.resume_handoff_frame); + // observers setAllowObservers(lobby.allow_observers); @@ -510,6 +514,27 @@ void NGMPGame::launchGame(void) return; } + // An armed resume-from-replay needs this client's copy + // of the resume source to match the lobby; when it does not, back out the same way as for a + // missing map rather than start a fresh match that would desync everyone. + UnicodeString resumeWhy; + if (!ResumeFromReplay::prepareGameStart(this, resumeWhy)) + { + DEBUG_LOG(("Resume-from-replay validation failed. Bailing...")); + if (TheNetwork != NULL) { + delete TheNetwork; + TheNetwork = NULL; + } + NGMP_OnlineServices_LobbyInterface* pResumeLobby = NGMP_OnlineServicesManager::GetInterface(); + if (pResumeLobby != nullptr) + pResumeLobby->SendAnnouncementMessageToCurrentLobby(resumeWhy, true); + GSMessageBoxOk(TheGameText->fetch("GUI:Error"), resumeWhy); + + void PopBackToLobby(void); + PopBackToLobby(); + return; + } + // Force camera to update from config TheTacticalView->setDefaultView(DEG_TO_RADF(TheGlobalData->m_cameraPitch), DEG_TO_RADF(TheGlobalData->m_cameraYaw), diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMP_Helpers.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMP_Helpers.cpp index 77223c06347..e91bac3daa6 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMP_Helpers.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/NGMP_Helpers.cpp @@ -204,21 +204,23 @@ std::string Base64Encode(const std::vector& data) "0123456789+/"; std::string encoded; - size_t i = 0; - uint32_t octet_a, octet_b, octet_c, triple; + encoded.reserve((data.size() + 2) / 3 * 4); - while (i < data.size()) + // A one-byte tail was emitted as "XXX=" instead of "XX==": + // the old padding test compared the consumed index against size + 1, which can never be + // true, so every input whose length is 1 mod 3 decoded to one byte too many. + for (size_t i = 0; i < data.size(); i += 3) { - octet_a = i < data.size() ? data[i++] : 0; - octet_b = i < data.size() ? data[i++] : 0; - octet_c = i < data.size() ? data[i++] : 0; - - triple = (octet_a << 16) | (octet_b << 8) | octet_c; + const size_t remaining = data.size() - i; + const uint32_t octet_a = data[i]; + const uint32_t octet_b = remaining > 1 ? data[i + 1] : 0; + const uint32_t octet_c = remaining > 2 ? data[i + 2] : 0; + const uint32_t triple = (octet_a << 16) | (octet_b << 8) | octet_c; encoded += base64_chars[(triple >> 18) & 0x3F]; encoded += base64_chars[(triple >> 12) & 0x3F]; - encoded += (i >= data.size() + 1) ? '=' : base64_chars[(triple >> 6) & 0x3F]; - encoded += (i >= data.size()) ? '=' : base64_chars[triple & 0x3F]; + encoded += remaining > 1 ? base64_chars[(triple >> 6) & 0x3F] : '='; + encoded += remaining > 2 ? base64_chars[triple & 0x3F] : '='; } return encoded; diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.cpp index 4b5b9da9cbc..e2e5611e11b 100644 --- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.cpp +++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GeneralsOnline/OnlineServices_LobbyInterface.cpp @@ -74,10 +74,55 @@ enum class ELobbyUpdateField AI_TEAM = 15, AI_START_POS = 16, MAX_CAMERA_HEIGHT = 17, - JOINABILITY = 18 + JOINABILITY = 18, + HOST_ACTION_BULK_SLOT_UPDATE = 19, + HOST_ACTION_ARM_RESUME = 20, // Resume-from-replay arming (see ResumeFromReplay) + LOCAL_PLAYER_HAS_RESUME_REPLAY = 21, // This member holds a usable copy of the resume source + HOST_ACTION_ALIGN_SLOTS = 22 // Resume-from-replay: the whole slot table at once }; -void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_Map(UnicodeString strMap, AsciiString strMapPath, bool bIsOfficial, int newMaxPlayers) +std::string Base64Encode(const std::vector& data); +std::vector Base64Decode(const std::string& encodedData); + +namespace +{ + // Resume-from-replay source transfer: replays are command logs, a long match is a few MB. + const size_t RESUME_REPLAY_CHUNK_BYTES = 256 * 1024; + const size_t RESUME_REPLAY_MAX_BYTES = 16 * 1024 * 1024; + const int RESUME_REPLAY_MAX_CONFLICT_RETRIES = 3; // 409s in a row before an upload gives up + + std::vector ReadWholeFile(const char* path) + { + std::vector bytes; + FILE* fp = fopen(path, "rb"); + if (fp == nullptr) + return bytes; + if (fseek(fp, 0, SEEK_END) == 0) + { + const long len = ftell(fp); + if (len > 0 && fseek(fp, 0, SEEK_SET) == 0) + { + bytes.resize((size_t)len); + const size_t n = fread(bytes.data(), 1, bytes.size(), fp); + bytes.resize(n); + } + } + fclose(fp); + return bytes; + } + + bool WriteWholeFile(const char* path, const std::vector& bytes) + { + FILE* fp = fopen(path, "wb"); + if (fp == nullptr) + return false; + const size_t n = fwrite(bytes.data(), 1, bytes.size(), fp); + fclose(fp); + return n == bytes.size(); + } +} + +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_Map(UnicodeString strMap, AsciiString strMapPath, bool bIsOfficial, int newMaxPlayers, std::function onDone) { // reset autostart if host changes anything (because ready flag will reset too) #if !defined(GENERALS_ONLINE_DISABLE_AUTO_ACCEPT) @@ -108,13 +153,17 @@ void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_Map(UnicodeString st std::string strPostData = j.dump(); // convert - NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [onDone](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) { - + const bool bOK = bSuccess && statusCode >= 200 && statusCode < 300; + if (!bOK) + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_Map failed: success=%d, status=%d", bSuccess, statusCode); + if (onDone) + onDone(bOK); }); } -void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_LimitSuperweapons(bool bLimitSuperweapons) +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_LimitSuperweapons(bool bLimitSuperweapons, std::function onDone) { // reset autostart if host changes anything (because ready flag will reset too) #if !defined(GENERALS_ONLINE_DISABLE_AUTO_ACCEPT) @@ -132,13 +181,17 @@ void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_LimitSuperweapons(bo std::string strPostData = j.dump(); // convert - NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [onDone](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) { - + const bool bOK = bSuccess && statusCode >= 200 && statusCode < 300; + if (!bOK) + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_LimitSuperweapons failed: success=%d, status=%d", bSuccess, statusCode); + if (onDone) + onDone(bOK); }); } -void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_StartingCash(UnsignedInt startingCashValue) +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_StartingCash(UnsignedInt startingCashValue, std::function onDone) { // reset autostart if host changes anything (because ready flag will reset too) #if !defined(GENERALS_ONLINE_DISABLE_AUTO_ACCEPT) @@ -156,9 +209,13 @@ void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_StartingCash(Unsigne std::string strPostData = j.dump(); // convert - NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [onDone](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) { - + const bool bOK = bSuccess && statusCode >= 200 && statusCode < 300; + if (!bOK) + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_StartingCash failed: success=%d, status=%d", bSuccess, statusCode); + if (onDone) + onDone(bOK); }); } @@ -493,6 +550,266 @@ void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_ForceReady() }); } +// Resume-from-replay: the whole slot table in one host action. +// The service rebuilds its member table from this (humans moved, AI and empty slots re-created) or refuses +// the lot with a reason; nothing is half-applied. The next lobby push shows the result everywhere. +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_AlignSlots(const std::vector& slots, std::function onDone) +{ +#if !defined(GENERALS_ONLINE_DISABLE_AUTO_ACCEPT) + ClearAutoReadyCountdown(); +#endif + if (TheNGMPGame && TheNGMPGame->IsCountdownStarted()) + TheNGMPGame->StopCountdown(); + + std::string strURI = std::format("{}/{}", NGMP_OnlineServicesManager::GetAPIEndpoint("Lobby"), m_CurrentLobby.lobbyID); + std::map mapHeaders; + + nlohmann::json j; + j["field"] = ELobbyUpdateField::HOST_ACTION_ALIGN_SLOTS; + nlohmann::json slotsArray = nlohmann::json::array(); + for (const LobbySlotSpec& spec : slots) + { + nlohmann::json e; + e["slot_index"] = spec.slotIndex; + e["slot_state"] = spec.slotState; + if (spec.slotState == SlotState::SLOT_PLAYER) + e["user_id"] = spec.userID; + e["side"] = spec.side; + e["color"] = spec.color; + e["start_pos"] = spec.startPos; + e["team"] = spec.team; + slotsArray.push_back(e); + } + j["slots"] = slotsArray; + std::string strPostData = j.dump(); + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_AlignSlots: %s", strPostData.c_str()); + + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [onDone](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + { + bool bOK = bSuccess && statusCode >= 200 && statusCode < 300; + std::string error; + try + { + nlohmann::json jResp = nlohmann::json::parse(strBody); + if (bOK) + bOK = jResp.value("success", false); + error = jResp.value("error", std::string()); + } + catch (...) + { + bOK = false; + } + if (!bOK) + { + if (error.empty()) + error = std::format("service error {}", statusCode); + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_AlignSlots failed: success=%d, status=%d, error=%s", bSuccess, statusCode, error.c_str()); + } + if (onDone) + onDone(bOK, error); + }); +} + +// Resume-from-replay: arm (handoff 0 disarms) the resume source +// the host uploaded. The service stores the handoff frame on the lobby and takes the replay's seed +// as the lobby seed, so the next start replays that file in lockstep before live control resumes. +// The caller announces the arm only from the success callback: the service refuses an arm without +// an uploaded source or with a bad frame, and a lobby that announced an arm it does not hold +// would start a fresh match on every guest. +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_ArmResume(uint32_t handoffFrame, int rngSeed, std::function onComplete) +{ + // reset autostart if host changes anything (because ready flag will reset too) +#if !defined(GENERALS_ONLINE_DISABLE_AUTO_ACCEPT) + ClearAutoReadyCountdown(); +#endif + if (TheNGMPGame && TheNGMPGame->IsCountdownStarted()) + TheNGMPGame->StopCountdown(); + + std::string strURI = std::format("{}/{}", NGMP_OnlineServicesManager::GetAPIEndpoint("Lobby"), m_CurrentLobby.lobbyID); + std::map mapHeaders; + + nlohmann::json j; + j["field"] = ELobbyUpdateField::HOST_ACTION_ARM_RESUME; + j["handoff_frame"] = handoffFrame; + j["rng_seed"] = rngSeed; + std::string strPostData = j.dump(); + + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [onComplete](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + { + bool bArmed = bSuccess && statusCode >= 200 && statusCode < 300; + if (bArmed) + { + try + { + nlohmann::json jResp = nlohmann::json::parse(strBody); + bArmed = jResp.value("success", false); + } + catch (...) + { + bArmed = false; + } + } + if (!bArmed) + { + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_ArmResume failed: success=%d, status=%d", bSuccess, statusCode); + } + if (onComplete != nullptr) + { + onComplete(bArmed); + } + }); +} + +// Whether this member holds a usable copy of the resume source; +// the host's start is refused until every human does (the way has_map gates the map). +void NGMP_OnlineServices_LobbyInterface::UpdateCurrentLobby_HasResumeReplay(bool bHasResumeReplay) +{ + std::string strURI = std::format("{}/{}", NGMP_OnlineServicesManager::GetAPIEndpoint("Lobby"), m_CurrentLobby.lobbyID); + std::map mapHeaders; + + nlohmann::json j; + j["field"] = ELobbyUpdateField::LOCAL_PLAYER_HAS_RESUME_REPLAY; + j["has_resume_replay"] = bHasResumeReplay; + std::string strPostData = j.dump(); + + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + { + if (!bSuccess || statusCode < 200 || statusCode >= 300) + { + NetworkLog(ELogVerbosity::LOG_RELEASE, "UpdateCurrentLobby_HasResumeReplay failed: success=%d, status=%d", bSuccess, statusCode); + } + }); +} + +// Host: push the resume source to the service in chunks (the +// service keeps it in memory with the lobby). The first chunk (offset 0) replaces whatever the lobby held. +void NGMP_OnlineServices_LobbyInterface::UploadResumeReplay(const AsciiString& path, std::function onComplete) +{ + std::vector bytes = ReadWholeFile(path.str()); + if (bytes.empty() || bytes.size() > RESUME_REPLAY_MAX_BYTES) + { + NetworkLog(ELogVerbosity::LOG_RELEASE, "UploadResumeReplay: %s is empty or too large (%u bytes)", path.str(), (unsigned)bytes.size()); + if (onComplete != nullptr) + onComplete(false); + return; + } + NetworkLog(ELogVerbosity::LOG_RELEASE, "UploadResumeReplay: uploading %s (%u bytes)", path.str(), (unsigned)bytes.size()); + SendResumeReplayChunk(m_CurrentLobby.lobbyID, std::make_shared>(std::move(bytes)), 0, 0, onComplete); +} + +void NGMP_OnlineServices_LobbyInterface::SendResumeReplayChunk(int64_t lobbyID, std::shared_ptr> data, size_t offset, int conflictRetries, std::function onComplete) +{ + if (offset >= data->size()) + { + if (onComplete != nullptr) + onComplete(true); + return; + } + const size_t remaining = data->size() - offset; + const size_t len = remaining < RESUME_REPLAY_CHUNK_BYTES ? remaining : RESUME_REPLAY_CHUNK_BYTES; // (std::min collides with the Windows min macro here) + std::vector chunk(data->begin() + offset, data->begin() + offset + len); + + std::string strURI = std::format("{}/{}/resume_replay", NGMP_OnlineServicesManager::GetAPIEndpoint("Lobby"), lobbyID); + std::map mapHeaders; + + nlohmann::json j; + j["offset"] = offset; + j["total"] = data->size(); + j["data"] = Base64Encode(chunk); + std::string strPostData = j.dump(); + + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [this, lobbyID, data, offset, len, conflictRetries, onComplete](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + { + if (bSuccess && statusCode == 200 && m_CurrentLobby.lobbyID == lobbyID) + { + SendResumeReplayChunk(lobbyID, data, offset + len, 0, onComplete); + return; + } + + // 409 carries the service's real length (a lost reply, a duplicate send, or the service + // dropped the file): resume from there, or from 0 when it does not fit our file. + if (statusCode == 409 && m_CurrentLobby.lobbyID == lobbyID && conflictRetries < RESUME_REPLAY_MAX_CONFLICT_RETRIES) + { + size_t resumeAt = 0; + try + { + nlohmann::json jResp = nlohmann::json::parse(strBody); + const int64_t total = jResp.value("total", (int64_t)0); + if (total > 0 && (uint64_t)total < (uint64_t)data->size()) + resumeAt = (size_t)total; + } + catch (...) + { + } + NetworkLog(ELogVerbosity::LOG_RELEASE, "UploadResumeReplay: chunk at %u conflicted, resuming from %u (retry %d)", (unsigned)offset, (unsigned)resumeAt, conflictRetries + 1); + SendResumeReplayChunk(lobbyID, data, resumeAt, conflictRetries + 1, onComplete); + return; + } + + NetworkLog(ELogVerbosity::LOG_RELEASE, "UploadResumeReplay: chunk at %u failed (success=%d, status=%d)", (unsigned)offset, bSuccess, statusCode); + if (onComplete != nullptr) + onComplete(false); + }); +} + +// Guest: pull the resume source back from the service into the +// replay directory, in the chunks the service serves. +void NGMP_OnlineServices_LobbyInterface::DownloadResumeReplay(const AsciiString& path, std::function onComplete) +{ + NetworkLog(ELogVerbosity::LOG_RELEASE, "DownloadResumeReplay: downloading to %s", path.str()); + FetchResumeReplayChunk(m_CurrentLobby.lobbyID, path, std::make_shared>(), onComplete); +} + +void NGMP_OnlineServices_LobbyInterface::FetchResumeReplayChunk(int64_t lobbyID, AsciiString path, std::shared_ptr> data, std::function onComplete) +{ + std::string strURI = std::format("{}/{}/resume_replay?from={}", NGMP_OnlineServicesManager::GetAPIEndpoint("Lobby"), lobbyID, data->size()); + std::map mapHeaders; + + NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendGETRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, [this, lobbyID, path, data, onComplete](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq) + { + bool bDone = false; + bool bOK = false; + if (bSuccess && statusCode == 200 && m_CurrentLobby.lobbyID == lobbyID) + { + try + { + nlohmann::json jResp = nlohmann::json::parse(strBody); + const int64_t total = jResp.value("total", (int64_t)0); + const std::string strData = jResp.value("data", std::string()); + if (jResp.value("success", false) && total > 0 && (int64_t)total <= (int64_t)RESUME_REPLAY_MAX_BYTES) + { + if (!strData.empty()) + { + std::vector bytes = Base64Decode(strData); + data->insert(data->end(), bytes.begin(), bytes.end()); + } + if ((int64_t)data->size() < total && !strData.empty()) + { + FetchResumeReplayChunk(lobbyID, path, data, onComplete); + return; // more to come + } + bDone = true; + bOK = (int64_t)data->size() == total && WriteWholeFile(path.str(), *data); + } + } + catch (...) + { + bOK = false; + } + } + if (!bOK) + { + NetworkLog(ELogVerbosity::LOG_RELEASE, "DownloadResumeReplay: failed at %u bytes (success=%d, status=%d, done=%d)", (unsigned)data->size(), bSuccess, statusCode, bDone); + } + else + { + NetworkLog(ELogVerbosity::LOG_RELEASE, "DownloadResumeReplay: wrote %u bytes to %s", (unsigned)data->size(), path.str()); + } + if (onComplete != nullptr) + onComplete(bOK); + }); +} + void NGMP_OnlineServices_LobbyInterface::SendChatMessageToCurrentLobby(UnicodeString& strChatMsgUnicode, bool bIsAction) { std::shared_ptr pWS = NGMP_OnlineServicesManager::GetWebSocket();; @@ -921,6 +1238,11 @@ void NGMP_OnlineServices_LobbyInterface::UpdateRoomDataCache(std::function. +*/ + +// Resume-from-replay arming and validation. See the header. + +#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine + +#include "GameNetwork/ResumeFromReplay.h" + +#include "Common/GlobalData.h" +#include "Common/Version.h" +#include "GameClient/GameText.h" +#include "GameClient/MapUtil.h" +#include "GameNetwork/GameInfo.h" + +namespace ResumeFromReplay +{ + +AsciiString getSourceFileName() +{ + AsciiString name = RecorderClass::getResumeReplayFileName(); + name.concat(RecorderClass::getReplayExtention()); + return name; +} + +AsciiString getSourceFilePath() +{ + AsciiString path = RecorderClass::getReplayDir(); + path.concat(getSourceFileName()); + return path; +} + +UnicodeString formatGameTime(UnsignedInt frames) +{ + const UnsignedInt seconds = frames / LOGICFRAMES_PER_SECOND; + UnicodeString text; + text.format(L"%u:%02u", seconds / 60, seconds % 60); + return text; +} + +Bool readSource(SourceReplay &out, UnicodeString &why) +{ + out.mapData = nullptr; + out.lastFrame = 0; + if (TheRecorder == nullptr || !readReplayMapInfo(getSourceFileName(), out.header, out.game, out.mapData)) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHeaderUnreadable", L"the resume replay could not be read"); + return FALSE; + } + if (out.header.versionNumber != TheVersion->getVersionNumber() + || out.header.exeCRC != TheGlobalData->m_exeCRC + || out.header.iniCRC != TheGlobalData->m_iniCRC) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeVersionMismatch", L"the resume replay was recorded with a different game version"); + return FALSE; + } + // The header's frame count is only patched in when a recording ends normally, so a recording + // cut short by a crash (the very file a resume is for) reads 0 there, and a torn tail may hold + // fewer complete records than the count says. Walk the records either way. + out.lastFrame = TheRecorder->scanReplayLastFrame(getSourceFileName()); + if (out.lastFrame == 0) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeNoFrames", L"the resume replay holds no complete frames (or the recorder is busy)"); + return FALSE; + } + return TRUE; +} + +static Bool computeHandoffFrame(UnsignedInt lastFrame, UnsignedInt &handoffOut, UnicodeString &why) +{ + const UnsignedInt slackFrames = HANDOFF_SLACK_SECONDS * LOGICFRAMES_PER_SECOND; + if (lastFrame <= slackFrames) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeTooShort", L"the last replay is too short to resume"); + return FALSE; + } + handoffOut = lastFrame - slackFrames; + return TRUE; +} + +Bool prepareHostSource(SourceReplay &out, UnsignedInt &handoffFrame, UnicodeString &why) +{ + if (TheRecorder == nullptr) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHeaderUnreadable", L"the resume replay could not be read"); + return FALSE; + } + + AsciiString lastReplay = RecorderClass::getReplayDir(); + lastReplay.concat(RecorderClass::getLastReplayFileName()); + lastReplay.concat(RecorderClass::getReplayExtention()); + const AsciiString source = getSourceFilePath(); + if (!CopyFile(lastReplay.str(), source.str(), FALSE)) + { + DEBUG_LOG(("ResumeFromReplay::prepareHostSource - could not copy %s to %s", lastReplay.str(), source.str())); + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeCopyFailed", L"the last replay could not be copied to the resume replay"); + return FALSE; + } + + if (!readSource(out, why)) + return FALSE; + if (out.mapData == nullptr) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMapMissing", L"the replay's map is not installed"); + return FALSE; + } + return computeHandoffFrame(out.lastFrame, handoffFrame, why); +} + +static UnicodeString slotOccupantName(const GameSlot *slot) +{ + if (slot != nullptr && slot->isHuman()) + return slot->getName(); + return TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeAIPlayer", L"the AI player"); +} + +static UnicodeString slotStateName(const GameSlot *slot) +{ + if (slot == nullptr) + return UnicodeString::TheEmptyString; + switch (slot->getState()) + { + case SLOT_EASY_AI: return TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeEasyAI", L"an easy AI"); + case SLOT_MED_AI: return TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMediumAI", L"a medium AI"); + case SLOT_BRUTAL_AI: return TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeHardAI", L"a hard AI"); + default: return slot->getName(); + } +} + +// The lobby must line up with the recording slot for slot. Recorded commands are indexed by +// slot position and the simulation is seeded by every slot's occupant, faction, start spot +// and team, so all of it has to match: every recorded human in the slot they held under the +// same name, every recorded AI in its slot at its difficulty, nobody in a slot that was empty, +// and the same faction, color, start position and team everywhere. Open and closed slots +// are both empty. +Bool slotsMatch(const ReplayGameInfo &replay, const GameInfo *lobby, UnicodeString &why) +{ + for (Int i = 0; i < MAX_SLOTS; ++i) + { + const GameSlot *rs = replay.getConstSlot(i); + const GameSlot *ls = lobby->getConstSlot(i); + const Bool replayOccupied = rs && rs->isOccupied(); + const Bool lobbyOccupied = ls && ls->isOccupied(); + if (!replayOccupied && !lobbyOccupied) + continue; + if (replayOccupied && !lobbyOccupied) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSlotMismatch", + L"slot %d must be %ls (it was theirs in the recording)", i + 1, slotStateName(rs).str()); + return FALSE; + } + if (!replayOccupied && lobbyOccupied) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSlotExtra", + L"slot %d was not a player in the recording, close it or move %ls", i + 1, slotOccupantName(ls).str()); + return FALSE; + } + // human vs AI, AI difficulty + if (rs->getState() != ls->getState() || (rs->isHuman() && rs->getName().compare(ls->getName()) != 0)) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSlotMismatch", + L"slot %d must be %ls (it was theirs in the recording)", i + 1, slotStateName(rs).str()); + return FALSE; + } + if (rs->getPlayerTemplate() != ls->getPlayerTemplate() + || rs->getColor() != ls->getColor() + || rs->getStartPos() != ls->getStartPos() + || rs->getTeamNumber() != ls->getTeamNumber()) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSlotSettingsMismatch", + L"slot %d (%ls) has a different faction, color, start position or team than in the recording", i + 1, slotOccupantName(ls).str()); + return FALSE; + } + } + return TRUE; +} + +Bool sourceMatchesGame(const SourceReplay &replay, const GameInfo *game, UnsignedInt handoffFrame, UnicodeString &why) +{ + if (game == nullptr) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeNoLobby", L"there is no lobby to compare the replay with"); + return FALSE; + } + const ReplayGameInfo &rg = replay.game; + if (replay.mapData == nullptr) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMapMissing", L"the replay's map is not installed"); + return FALSE; + } + if (game->getMap().compareNoCase(rg.getMap()) != 0) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeMapMismatch", L"the lobby map is not the replay's map (%hs)", rg.getMap().str()); + return FALSE; + } + if (game->getMapCRC() != rg.getMapCRC()) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeMapCRCMismatch", L"the map file differs from the one the replay was recorded on"); + return FALSE; + } + if (game->getSeed() != rg.getSeed()) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSeedMismatch", L"the lobby's random seed is not the replay's"); + return FALSE; + } + if (game->getStartingCash().countMoney() != rg.getStartingCash().countMoney()) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeCashMismatch", L"the starting cash differs from the recording (%u)", rg.getStartingCash().countMoney()); + return FALSE; + } + if (game->getSuperweaponRestriction() != rg.getSuperweaponRestriction()) + { + why = TheGameText->FETCH_OR_SUBSTITUTE("GUI:ResumeSuperweaponMismatch", L"the superweapon limit differs from the recording"); + return FALSE; + } + if (!slotsMatch(rg, game, why)) + return FALSE; + if (handoffFrame != 0 && replay.lastFrame < handoffFrame) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeSourceTooShort", + L"the replay ends at %ls, before the handoff at %ls", formatGameTime(replay.lastFrame).str(), formatGameTime(handoffFrame).str()); + return FALSE; + } + return TRUE; +} + +Bool validateRoster(const ReplayGameInfo &replay, const GameInfo *lobby, UnicodeString &why) +{ + Int i; + Int replayCount = 0; + Int lobbyCount = 0; + for (i = 0; i < MAX_SLOTS; ++i) + { + const GameSlot *rs = replay.getConstSlot(i); + if (rs && rs->isHuman()) + ++replayCount; + const GameSlot *ls = lobby->getConstSlot(i); + if (ls && ls->isHuman()) + ++lobbyCount; + } + if (replayCount != lobbyCount) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeCountMismatch", + L"the lobby has %d players but the replay has %d", lobbyCount, replayCount); + return FALSE; + } + + const GameSlot *replayHost = replay.getConstSlot(0); + const GameSlot *lobbyHost = lobby->getConstSlot(0); + if (!replayHost || !lobbyHost || !replayHost->isHuman() || replayHost->getName().compare(lobbyHost->getName()) != 0) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeHostMismatch", + L"only %ls, the host of the recorded game, can resume it", replayHost ? replayHost->getName().str() : L"?"); + return FALSE; + } + + for (i = 0; i < MAX_SLOTS; ++i) + { + const GameSlot *rs = replay.getConstSlot(i); + if (!rs || !rs->isHuman()) + continue; + Bool found = FALSE; + for (Int j = 0; j < MAX_SLOTS && !found; ++j) + { + const GameSlot *ls = lobby->getConstSlot(j); + found = ls && ls->isHuman() && ls->getName().compare(rs->getName()) == 0; + } + if (!found) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeMissingPlayer", L"%ls is not in the lobby", rs->getName().str()); + return FALSE; + } + } + return TRUE; +} + +Bool guestSourceReport(const GameInfo *lobby, UnicodeString &report) +{ + SourceReplay replay; + UnicodeString why; + if (!readSource(replay, why)) + { + report = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeGuestUnreadable", L"Resume: my copy of the replay could not be used (%ls)", why.str()); + return FALSE; + } + if (!sourceMatchesGame(replay, lobby, lobby ? lobby->getResumeHandoffFrame() : 0, why)) + { + report = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeGuestMismatch", L"Resume: the replay does not match this lobby (%ls)", why.str()); + return FALSE; + } + report = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeGuestDownloaded", L"Resume: replay downloaded (ends at %ls)", formatGameTime(replay.lastFrame).str()); + return TRUE; +} + +Bool prepareGameStart(GameInfo *game, UnicodeString &why) +{ + if (TheRecorder == nullptr) + return TRUE; + TheRecorder->disarmResume(); + if (game == nullptr || game->getResumeHandoffFrame() == 0) + return TRUE; + + SourceReplay replay; + UnicodeString reason; + if (!readSource(replay, reason) || !sourceMatchesGame(replay, game, game->getResumeHandoffFrame(), reason)) + { + why = TheGameText->FETCH_OR_SUBSTITUTE_FORMAT("GUI:ResumeStartAborted", + L"Resume aborted: %ls (my replay ends at %ls)", reason.str(), formatGameTime(replay.lastFrame).str()); + return FALSE; + } + TheRecorder->armResumeForNextGame(game->getResumeHandoffFrame(), replay.lastFrame); + // The recorder owns the arm from here. Clearing it on the game info keeps RH= out of the + // new recording's options string (external replay parsers do not know the token) and + // leaves the lobby disarmed when the players come back after the match. + game->setResumeHandoffFrame(0); + return TRUE; +} + +} // namespace ResumeFromReplay