Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_SavedGame.cpp
Show All 22 Lines | |||||
#include "network/NetServer.h" | #include "network/NetServer.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/SavedGame.h" | #include "ps/SavedGame.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/system/TurnManager.h" | #include "simulation2/system/TurnManager.h" | ||||
JS::Value JSI_SavedGame::GetSavedGames(ScriptInterface::CxPrivate* pCxPrivate) | JS::Value JSI_SavedGame::GetSavedGames(ScriptInterface::CmptPrivate* pCmptPrivate) | ||||
{ | { | ||||
return SavedGames::GetSavedGames(*(pCxPrivate->pScriptInterface)); | return SavedGames::GetSavedGames(*(pCmptPrivate->pScriptInterface)); | ||||
} | } | ||||
bool JSI_SavedGame::DeleteSavedGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& name) | bool JSI_SavedGame::DeleteSavedGame(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::wstring& name) | ||||
{ | { | ||||
return SavedGames::DeleteSavedGame(name); | return SavedGames::DeleteSavedGame(name); | ||||
} | } | ||||
void JSI_SavedGame::SaveGame(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename, const std::wstring& description, JS::HandleValue GUIMetadata) | void JSI_SavedGame::SaveGame(ScriptInterface::CmptPrivate* pCmptPrivate, const std::wstring& filename, const std::wstring& description, JS::HandleValue GUIMetadata) | ||||
{ | { | ||||
shared_ptr<ScriptInterface::StructuredClone> GUIMetadataClone = pCxPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | shared_ptr<ScriptInterface::StructuredClone> GUIMetadataClone = pCmptPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | ||||
if (SavedGames::Save(filename, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | if (SavedGames::Save(filename, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | ||||
LOGERROR("Failed to save game"); | LOGERROR("Failed to save game"); | ||||
} | } | ||||
void JSI_SavedGame::SaveGamePrefix(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& prefix, const std::wstring& description, JS::HandleValue GUIMetadata) | void JSI_SavedGame::SaveGamePrefix(ScriptInterface::CmptPrivate* pCmptPrivate, const std::wstring& prefix, const std::wstring& description, JS::HandleValue GUIMetadata) | ||||
{ | { | ||||
shared_ptr<ScriptInterface::StructuredClone> GUIMetadataClone = pCxPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | shared_ptr<ScriptInterface::StructuredClone> GUIMetadataClone = pCmptPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | ||||
if (SavedGames::SavePrefix(prefix, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | if (SavedGames::SavePrefix(prefix, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | ||||
LOGERROR("Failed to save game"); | LOGERROR("Failed to save game"); | ||||
} | } | ||||
void JSI_SavedGame::QuickSave(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), JS::HandleValue GUIMetadata) | void JSI_SavedGame::QuickSave(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), JS::HandleValue GUIMetadata) | ||||
{ | { | ||||
if (g_NetServer || g_NetClient) | if (g_NetServer || g_NetClient) | ||||
LOGERROR("Can't store quicksave during multiplayer!"); | LOGERROR("Can't store quicksave during multiplayer!"); | ||||
else if (g_Game) | else if (g_Game) | ||||
g_Game->GetTurnManager()->QuickSave(GUIMetadata); | g_Game->GetTurnManager()->QuickSave(GUIMetadata); | ||||
else | else | ||||
LOGERROR("Can't store quicksave if game is not running!"); | LOGERROR("Can't store quicksave if game is not running!"); | ||||
} | } | ||||
void JSI_SavedGame::QuickLoad(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_SavedGame::QuickLoad(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate)) | ||||
{ | { | ||||
if (g_NetServer || g_NetClient) | if (g_NetServer || g_NetClient) | ||||
LOGERROR("Can't load quicksave during multiplayer!"); | LOGERROR("Can't load quicksave during multiplayer!"); | ||||
else if (g_Game) | else if (g_Game) | ||||
g_Game->GetTurnManager()->QuickLoad(); | g_Game->GetTurnManager()->QuickLoad(); | ||||
else | else | ||||
LOGERROR("Can't load quicksave if game is not running!"); | LOGERROR("Can't load quicksave if game is not running!"); | ||||
} | } | ||||
JS::Value JSI_SavedGame::StartSavedGame(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name) | JS::Value JSI_SavedGame::StartSavedGame(ScriptInterface::CmptPrivate* pCmptPrivate, 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. | ||||
ScriptInterface::Request rqGui(pCxPrivate); | ScriptInterface::Request rqGui(pCmptPrivate); | ||||
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(rqGui.cx); | 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, *(pCmptPrivate->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(); | ||||
ScriptInterface::Request rqGame(sim->GetScriptInterface()); | ScriptInterface::Request rqGame(sim->GetScriptInterface()); | ||||
JS::RootedValue gameContextMetadata(rqGame.cx, | JS::RootedValue gameContextMetadata(rqGame.cx, | ||||
sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), guiContextMetadata)); | sim->GetScriptInterface().CloneValueFromOtherContext(*(pCmptPrivate->pScriptInterface), guiContextMetadata)); | ||||
JS::RootedValue gameInitAttributes(rqGame.cx); | 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 15 Lines |
Wildfire Games · Phabricator