Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_SavedGame.cpp
Show All 19 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/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.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::CxPrivate* pCxPrivate) | ||||
{ | { | ||||
return SavedGames::GetSavedGames(*(pCxPrivate->pScriptInterface)); | return SavedGames::GetSavedGames(*(pCxPrivate->pScriptInterface)); | ||||
} | } | ||||
bool JSI_SavedGame::DeleteSavedGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& name) | bool JSI_SavedGame::DeleteSavedGame(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, *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::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, *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(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() | ||||
{ | { | ||||
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!"); | ||||
} | } | ||||
Show All 38 Lines | g_Game = new CGame(true); | ||||
g_Game->StartGame(&gameInitAttributes, savedState); | g_Game->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"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name) | ||||
scriptInterface.RegisterFunction<bool, std::wstring, &DeleteSavedGame>("DeleteSavedGame"); | Register(GetSavedGames); | ||||
scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGame>("SaveGame"); | Register(DeleteSavedGame); | ||||
scriptInterface.RegisterFunction<void, std::wstring, std::wstring, JS::HandleValue, &SaveGamePrefix>("SaveGamePrefix"); | Register(SaveGame); | ||||
scriptInterface.RegisterFunction<void, JS::HandleValue, &QuickSave>("QuickSave"); | Register(SaveGamePrefix); | ||||
scriptInterface.RegisterFunction<void, &QuickLoad>("QuickLoad"); | Register(QuickSave); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &StartSavedGame>("StartSavedGame"); | Register(QuickLoad); | ||||
Register(StartSavedGame); | |||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator