Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ReplayTurnManager.h" 21 : 22 : #include "lib/debug.h" 23 : #include "ps/CLogger.h" 24 : #include "ps/Util.h" 25 : #include "scriptinterface/JSON.h" 26 : #include "scriptinterface/Conversions.h" 27 : #include "scriptinterface/Request.h" 28 : #include "simulation2/Simulation2.h" 29 : #include "simulation2/system/LocalTurnManager.h" 30 : 31 : #include <js/GCVector.h> 32 : #include <js/RootingAPI.h> 33 : #include <js/TypeDecls.h> 34 : #include <js/Value.h> 35 : #include <js/ValueArray.h> 36 : #include <tuple> 37 : #include <utility> 38 : 39 : class IReplayLogger; 40 : namespace Script { class Interface; } 41 : 42 : const CStr CReplayTurnManager::EventNameReplayFinished = "ReplayFinished"; 43 : const CStr CReplayTurnManager::EventNameReplayOutOfSync = "ReplayOutOfSync"; 44 : 45 0 : CReplayTurnManager::CReplayTurnManager(CSimulation2& simulation, IReplayLogger& replay) 46 0 : : CLocalTurnManager(simulation, replay) 47 : { 48 0 : } 49 : 50 0 : void CReplayTurnManager::StoreReplayCommand(turn_id_t turn, int player, const std::string& command) 51 : { 52 : // Using the pair we make sure that commands per turn will be processed in the correct order 53 0 : m_ReplayCommands[turn].emplace_back(player, command); 54 0 : } 55 : 56 0 : void CReplayTurnManager::StoreReplayHash(turn_id_t turn, const std::string& hash, bool quick) 57 : { 58 0 : m_ReplayHash[turn] = std::make_pair(hash, quick); 59 0 : } 60 : 61 0 : void CReplayTurnManager::StoreReplayTurnLength(turn_id_t turn, std::uint32_t turnLength) 62 : { 63 0 : m_ReplayTurnLengths[turn] = turnLength; 64 : 65 : // Initialize turn length 66 0 : if (turn == 0) 67 0 : m_TurnLength = m_ReplayTurnLengths[0]; 68 0 : } 69 : 70 0 : void CReplayTurnManager::StoreFinalReplayTurn(turn_id_t turn) 71 : { 72 0 : m_FinalTurn = turn; 73 0 : } 74 : 75 0 : void CReplayTurnManager::NotifyFinishedUpdate(turn_id_t turn, const UpdateCallback& sendEventToAll) 76 : { 77 0 : if (turn == 1 && m_FinalTurn == 0) 78 0 : sendEventToAll(EventNameReplayFinished, std::nullopt); 79 : 80 0 : if (turn > m_FinalTurn) 81 0 : return; 82 : 83 0 : DoTurn(turn, sendEventToAll); 84 : 85 : // Compare hash if it exists in the replay and if we didn't have an OOS already 86 0 : std::map<turn_id_t, std::pair<std::string, bool>>::iterator turnHashIt = m_ReplayHash.find(turn); 87 0 : if (m_HasSyncError || turnHashIt == m_ReplayHash.end()) 88 : return; 89 : 90 0 : std::string expectedHash = turnHashIt->second.first; 91 0 : bool quickHash = turnHashIt->second.second; 92 : 93 : // Compute hash 94 0 : std::string hash; 95 0 : ENSURE(m_Simulation2.ComputeStateHash(hash, quickHash)); 96 0 : hash = Hexify(hash); 97 : 98 0 : if (hash == expectedHash) 99 0 : return; 100 : 101 0 : m_HasSyncError = true; 102 0 : LOGERROR("Replay out of sync on turn {}", turn); 103 : 104 0 : const Script::Interface& scriptInterface = m_Simulation2.GetScriptInterface(); 105 0 : Script::Request rq(scriptInterface); 106 : 107 0 : JS::RootedValueVector paramData(rq.cx); 108 : 109 0 : std::ignore = paramData.append(JS::NumberValue(turn)); 110 : 111 0 : JS::RootedValue hashVal(rq.cx); 112 0 : Script::ToJSVal(rq, &hashVal, hash); 113 0 : std::ignore = paramData.append(hashVal); 114 : 115 0 : JS::RootedValue expectedHashVal(rq.cx); 116 0 : Script::ToJSVal(rq, &expectedHashVal, expectedHash); 117 0 : std::ignore = paramData.append(expectedHashVal); 118 : 119 0 : sendEventToAll(EventNameReplayOutOfSync, paramData); 120 0 : } 121 : 122 0 : void CReplayTurnManager::DoTurn(turn_id_t turn, const UpdateCallback& sendEventToAll) 123 : { 124 0 : debug_printf("Executing turn %d of %d\n", turn, m_FinalTurn); 125 : 126 0 : m_TurnLength = m_ReplayTurnLengths[turn]; 127 : 128 0 : Script::Request rq(m_Simulation2.GetScriptInterface()); 129 : 130 : // Simulate commands for that turn 131 0 : for (const std::pair<player_id_t, std::string>& p : m_ReplayCommands[turn]) 132 : { 133 0 : JS::RootedValue command(rq.cx); 134 0 : Script::ParseJSON(rq, p.second, &command); 135 0 : AddCommand(m_ClientId, p.first, command, m_CurrentTurn + 1); 136 0 : } 137 : 138 0 : if (turn == m_FinalTurn) 139 0 : sendEventToAll(EventNameReplayFinished, std::nullopt); 140 0 : }