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 "JSInterface_SavedGame.h" 21 : 22 : #include "gui/GUIManager.h" 23 : #include "lib/debug.h" 24 : #include "network/NetClient.h" 25 : #include "network/NetServer.h" 26 : #include "ps/CLogger.h" 27 : #include "ps/CStr.h" 28 : #include "ps/Game.h" 29 : #include "ps/SavedGame.h" 30 : #include "scriptinterface/FunctionWrapper.h" 31 : #include "scriptinterface/Object.h" 32 : #include "scriptinterface/Request.h" 33 : #include "scriptinterface/StructuredClone.h" 34 : #include "simulation2/Simulation2.h" 35 : #include "simulation2/system/TurnManager.h" 36 : 37 : #include <js/RootingAPI.h> 38 : #include <js/TypeDecls.h> 39 : #include <js/Value.h> 40 : #include <optional> 41 : #include <string> 42 : 43 : namespace Script { class Interface; } 44 : 45 : namespace JSI_SavedGame 46 : { 47 0 : JS::Value GetSavedGames(const Script::Interface& scriptInterface) 48 : { 49 0 : return SavedGames::GetSavedGames(scriptInterface); 50 : } 51 : 52 0 : bool DeleteSavedGame(const std::wstring& name) 53 : { 54 0 : return SavedGames::DeleteSavedGame(name); 55 : } 56 : 57 0 : void SaveGame(const Script::Request& rq, const std::wstring& filename, const std::wstring& description, JS::HandleValue GUIMetadata) 58 : { 59 0 : Script::StructuredClone GUIMetadataClone = Script::WriteStructuredClone(rq, GUIMetadata); 60 0 : if (SavedGames::Save(filename, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) 61 0 : LOGERROR("Failed to save game"); 62 0 : } 63 : 64 0 : void SaveGamePrefix(const Script::Request& rq, const std::wstring& prefix, const std::wstring& description, JS::HandleValue GUIMetadata) 65 : { 66 0 : Script::StructuredClone GUIMetadataClone = Script::WriteStructuredClone(rq, GUIMetadata); 67 0 : if (SavedGames::SavePrefix(prefix, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) 68 0 : LOGERROR("Failed to save game"); 69 0 : } 70 : 71 0 : void QuickSave(JS::HandleValue GUIMetadata) 72 : { 73 0 : if (g_NetServer || g_NetClient) 74 0 : LOGERROR("Can't store quicksave during multiplayer!"); 75 0 : else if (g_Game) 76 0 : g_Game->GetTurnManager()->QuickSave(GUIMetadata); 77 : else 78 0 : LOGERROR("Can't store quicksave if game is not running!"); 79 0 : } 80 : 81 0 : void QuickLoad() 82 : { 83 0 : if (g_NetServer || g_NetClient) 84 : { 85 0 : LOGERROR("Can't load quicksave during multiplayer!"); 86 0 : return; 87 : } 88 0 : if (!g_Game) 89 : { 90 0 : LOGERROR("Can't load quicksave if game is not running!"); 91 0 : return; 92 : } 93 : 94 0 : const std::optional<JS::Value> maybeMetadata{g_Game->GetTurnManager()->TryQuickLoad()}; 95 : 96 0 : if (!g_GUI || !maybeMetadata.has_value()) 97 : return; 98 : 99 0 : const Script::Request rq{g_Game->GetSimulation2()->GetScriptInterface()}; 100 0 : JS::RootedValue metadata{rq.cx, maybeMetadata.value()}; 101 : 102 : // Provide a copy, so that GUI components don't have to clone to get mutable objects 103 0 : JS::RootedValue quickSaveMetadataClone(rq.cx, Script::DeepCopy(rq, metadata)); 104 : 105 0 : JS::RootedValueArray<1> paramData(rq.cx); 106 0 : paramData[0].set(quickSaveMetadataClone); 107 0 : g_GUI->SendEventToAll(CTurnManager::EventNameSavegameLoaded, paramData); 108 : 109 0 : LOGMESSAGERENDER("Quickloaded game"); 110 0 : } 111 : 112 0 : JS::Value LoadSavedGameMetadata(const Script::Interface& scriptInterface, const std::wstring& name) 113 : { 114 0 : std::optional<SavedGames::LoadResult> data{SavedGames::Load(scriptInterface, name)}; 115 : 116 0 : return data ? data->metadata : JS::UndefinedValue(); 117 0 : } 118 : 119 0 : JS::Value StartSavedGame(const Script::Interface& scriptInterface, const std::wstring& name) 120 : { 121 : // We need to be careful with different compartments and contexts. 122 : // The GUI calls this function from the GUI context and expects the return value in the same context. 123 : // The game we start from here creates another context and expects data in this context. 124 : 125 0 : Script::Request rqGui(scriptInterface); 126 : 127 0 : ENSURE(!g_NetServer); 128 0 : ENSURE(!g_NetClient); 129 : 130 0 : ENSURE(!g_Game); 131 : 132 0 : std::optional<SavedGames::LoadResult> data{SavedGames::Load(scriptInterface, name)}; 133 0 : if (!data) 134 0 : return JS::UndefinedValue(); 135 : 136 0 : JS::RootedValue guiContextMetadata{rqGui.cx, data->metadata}; 137 : 138 0 : g_Game = new CGame(true); 139 : 140 0 : { 141 0 : CSimulation2* sim = g_Game->GetSimulation2(); 142 0 : Script::Request rqGame(sim->GetScriptInterface()); 143 : 144 0 : JS::RootedValue gameContextMetadata(rqGame.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), scriptInterface, guiContextMetadata)); 145 0 : JS::RootedValue gameInitAttributes(rqGame.cx); 146 0 : Script::GetProperty(rqGame, gameContextMetadata, "initAttributes", &gameInitAttributes); 147 : 148 0 : int playerID; 149 0 : Script::GetProperty(rqGame, gameContextMetadata, "playerID", playerID); 150 : 151 0 : g_Game->SetPlayerID(playerID); 152 0 : g_Game->StartGame(&gameInitAttributes, data->savedState); 153 0 : } 154 : 155 0 : return guiContextMetadata; 156 0 : } 157 : 158 0 : void ActivateRejoinTest() 159 : { 160 0 : if (!g_Game || !g_Game->GetSimulation2() || !g_Game->GetTurnManager()) 161 : return; 162 0 : g_Game->GetSimulation2()->ActivateRejoinTest(g_Game->GetTurnManager()->GetCurrentTurn() + 1); 163 : } 164 : 165 26 : void RegisterScriptFunctions(const Script::Request& rq) 166 : { 167 26 : Script::Function::Register<&GetSavedGames>(rq, "GetSavedGames"); 168 26 : Script::Function::Register<&DeleteSavedGame>(rq, "DeleteSavedGame"); 169 26 : Script::Function::Register<&SaveGame>(rq, "SaveGame"); 170 26 : Script::Function::Register<&SaveGamePrefix>(rq, "SaveGamePrefix"); 171 26 : Script::Function::Register<&QuickSave>(rq, "QuickSave"); 172 26 : Script::Function::Register<&QuickLoad>(rq, "QuickLoad"); 173 26 : Script::Function::Register<&ActivateRejoinTest>(rq, "ActivateRejoinTest"); 174 26 : Script::Function::Register<&LoadSavedGameMetadata>(rq, "LoadSavedGameMetadata"); 175 26 : Script::Function::Register<&StartSavedGame>(rq, "StartSavedGame"); 176 26 : } 177 : }