Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_SavedGame.cpp
/* Copyright (C) 2020 Wildfire Games. | /* Copyright (C) 2021 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 All 10 Lines | |||||
#include "JSInterface_SavedGame.h" | #include "JSInterface_SavedGame.h" | ||||
#include "network/NetClient.h" | #include "network/NetClient.h" | ||||
#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/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/StructuredClone.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/system/TurnManager.h" | #include "simulation2/system/TurnManager.h" | ||||
namespace JSI_SavedGame | namespace JSI_SavedGame | ||||
{ | { | ||||
JS::Value GetSavedGames(const ScriptInterface& scriptInterface) | JS::Value GetSavedGames(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
return SavedGames::GetSavedGames(scriptInterface); | return SavedGames::GetSavedGames(scriptInterface); | ||||
} | } | ||||
bool DeleteSavedGame(const std::wstring& name) | bool DeleteSavedGame(const std::wstring& name) | ||||
{ | { | ||||
return SavedGames::DeleteSavedGame(name); | return SavedGames::DeleteSavedGame(name); | ||||
} | } | ||||
void SaveGame(const ScriptInterface& scriptInterface, const std::wstring& filename, const std::wstring& description, JS::HandleValue GUIMetadata) | void SaveGame(const ScriptRequest& rq, const std::wstring& filename, const std::wstring& description, JS::HandleValue GUIMetadata) | ||||
{ | { | ||||
ScriptInterface::StructuredClone GUIMetadataClone = scriptInterface.WriteStructuredClone(GUIMetadata); | Script::StructuredClone GUIMetadataClone = Script::WriteStructuredClone(rq, 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 SaveGamePrefix(const ScriptInterface& scriptInterface, const std::wstring& prefix, const std::wstring& description, JS::HandleValue GUIMetadata) | void SaveGamePrefix(const ScriptRequest& rq, const std::wstring& prefix, const std::wstring& description, JS::HandleValue GUIMetadata) | ||||
{ | { | ||||
ScriptInterface::StructuredClone GUIMetadataClone = scriptInterface.WriteStructuredClone(GUIMetadata); | Script::StructuredClone GUIMetadataClone = Script::WriteStructuredClone(rq, 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 QuickSave(JS::HandleValue GUIMetadata) | void QuickSave(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!"); | ||||
Show All 34 Lines | 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(); | ||||
ScriptRequest rqGame(sim->GetScriptInterface()); | ScriptRequest rqGame(sim->GetScriptInterface()); | ||||
JS::RootedValue gameContextMetadata(rqGame.cx, | JS::RootedValue gameContextMetadata(rqGame.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), scriptInterface, guiContextMetadata)); | ||||
sim->GetScriptInterface().CloneValueFromOtherCompartment(scriptInterface, 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 24 Lines |
Wildfire Games · Phabricator