Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_SavedGame.cpp
Show All 35 Lines | |||||
bool JSI_SavedGame::DeleteSavedGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& name) | bool JSI_SavedGame::DeleteSavedGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), 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::CxPrivate* pCxPrivate, 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 = pCxPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | ||||
if (SavedGames::Save(filename, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | if (SavedGames::Save(filename, description, *CGame::GetGame()->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::CxPrivate* pCxPrivate, 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 = pCxPrivate->pScriptInterface->WriteStructuredClone(GUIMetadata); | ||||
if (SavedGames::SavePrefix(prefix, description, *g_Game->GetSimulation2(), GUIMetadataClone) < 0) | if (SavedGames::SavePrefix(prefix, description, *CGame::GetGame()->GetSimulation2(), GUIMetadataClone) < 0) | ||||
LOGERROR("Failed to save game"); | LOGERROR("Failed to save game"); | ||||
} | } | ||||
void JSI_SavedGame::QuickSave(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_SavedGame::QuickSave(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
{ | { | ||||
g_Game->GetTurnManager()->QuickSave(); | CGame::GetGame()->GetTurnManager()->QuickSave(); | ||||
} | } | ||||
void JSI_SavedGame::QuickLoad(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_SavedGame::QuickLoad(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
{ | { | ||||
g_Game->GetTurnManager()->QuickLoad(); | CGame::GetGame()->GetTurnManager()->QuickLoad(); | ||||
} | } | ||||
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(); | JSContext* cxGui = pCxPrivate->pScriptInterface->GetContext(); | ||||
JSAutoRequest rq(cxGui); | JSAutoRequest rq(cxGui); | ||||
ENSURE(!g_NetServer); | ENSURE(!g_NetServer); | ||||
ENSURE(!g_NetClient); | ENSURE(!g_NetClient); | ||||
// ENSURE(!CGame::IsGameInitialised()); | |||||
Silier: probably should uncomment this back | |||||
ENSURE(!g_Game); | |||||
// Load the saved game data from disk | // Load the saved game data from disk | ||||
JS::RootedValue guiContextMetadata(cxGui); | JS::RootedValue guiContextMetadata(cxGui); | ||||
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(); | CGame::InitGame(); | ||||
{ | { | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = CGame::GetGame()->GetSimulation2(); | ||||
JSContext* cxGame = sim->GetScriptInterface().GetContext(); | JSContext* cxGame = sim->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rq(cxGame); | JSAutoRequest rq(cxGame); | ||||
JS::RootedValue gameContextMetadata(cxGame, | JS::RootedValue gameContextMetadata(cxGame, | ||||
sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), guiContextMetadata)); | sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), guiContextMetadata)); | ||||
JS::RootedValue gameInitAttributes(cxGame); | JS::RootedValue gameInitAttributes(cxGame); | ||||
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); | CGame::GetGame()->SetPlayerID(playerID); | ||||
g_Game->StartGame(&gameInitAttributes, savedState); | CGame::GetGame()->StartGame(&gameInitAttributes, savedState); | ||||
} | } | ||||
return guiContextMetadata; | return guiContextMetadata; | ||||
} | } | ||||
void JSI_SavedGame::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_SavedGame::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, &GetSavedGames>("GetSavedGames"); | scriptInterface.RegisterFunction<JS::Value, &GetSavedGames>("GetSavedGames"); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, &DeleteSavedGame>("DeleteSavedGame"); | scriptInterface.RegisterFunction<bool, std::wstring, &DeleteSavedGame>("DeleteSavedGame"); | ||||
scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGame>("SaveGame"); | scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGame>("SaveGame"); | ||||
scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGamePrefix>("SaveGamePrefix"); | scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGamePrefix>("SaveGamePrefix"); | ||||
scriptInterface.RegisterFunction<void, &QuickSave>("QuickSave"); | scriptInterface.RegisterFunction<void, &QuickSave>("QuickSave"); | ||||
scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad"); | scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &StartSavedGame>("StartSavedGame"); | scriptInterface.RegisterFunction<JS::Value, std::wstring, &StartSavedGame>("StartSavedGame"); | ||||
} | } |
Wildfire Games · Phabricator
probably should uncomment this back