Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_SavedGame.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
JS::Value JSI_SavedGame::StartSavedGame(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name) | JS::Value JSI_SavedGame::StartSavedGame(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name) | ||||
{ | { | ||||
// We need to be careful with different compartments and contexts. | // We need to be careful with different compartments and contexts. | ||||
// The GUI calls this function from the GUI context and expects the return value in the same context. | // The GUI calls this function from the GUI context and expects the return value in the same context. | ||||
// The game we start from here creates another context and expects data in this context. | // The game we start from here creates another context and expects data in this context. | ||||
JSContext* cxGui = pCxPrivate->pScriptInterface->GetContext(); | ScriptInterface::Request rqGui(pCxPrivate); | ||||
JSAutoRequest rq(cxGui); | |||||
ENSURE(!g_NetServer); | ENSURE(!g_NetServer); | ||||
ENSURE(!g_NetClient); | ENSURE(!g_NetClient); | ||||
ENSURE(!g_Game); | ENSURE(!g_Game); | ||||
// Load the saved game data from disk | // Load the saved game data from disk | ||||
JS::RootedValue guiContextMetadata(cxGui); | JS::RootedValue guiContextMetadata(rqGui.cx); | ||||
std::string savedState; | std::string savedState; | ||||
Status err = SavedGames::Load(name, *(pCxPrivate->pScriptInterface), &guiContextMetadata, savedState); | Status err = SavedGames::Load(name, *(pCxPrivate->pScriptInterface), &guiContextMetadata, savedState); | ||||
if (err < 0) | if (err < 0) | ||||
return JS::UndefinedValue(); | return JS::UndefinedValue(); | ||||
g_Game = new CGame(true); | g_Game = new CGame(true); | ||||
{ | { | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = g_Game->GetSimulation2(); | ||||
JSContext* cxGame = sim->GetScriptInterface().GetContext(); | ScriptInterface::Request rqGame(sim->GetScriptInterface()); | ||||
JSAutoRequest rq(cxGame); | |||||
JS::RootedValue gameContextMetadata(cxGame, | JS::RootedValue gameContextMetadata(rqGame.cx, | ||||
sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), guiContextMetadata)); | sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), guiContextMetadata)); | ||||
JS::RootedValue gameInitAttributes(cxGame); | JS::RootedValue gameInitAttributes(rqGame.cx); | ||||
sim->GetScriptInterface().GetProperty(gameContextMetadata, "initAttributes", &gameInitAttributes); | sim->GetScriptInterface().GetProperty(gameContextMetadata, "initAttributes", &gameInitAttributes); | ||||
int playerID; | int playerID; | ||||
sim->GetScriptInterface().GetProperty(gameContextMetadata, "playerID", playerID); | sim->GetScriptInterface().GetProperty(gameContextMetadata, "playerID", playerID); | ||||
g_Game->SetPlayerID(playerID); | g_Game->SetPlayerID(playerID); | ||||
g_Game->StartGame(&gameInitAttributes, savedState); | g_Game->StartGame(&gameInitAttributes, savedState); | ||||
} | } | ||||
Show All 14 Lines |
Wildfire Games · Phabricator