18 #include "precompiled.h"
38 const VfsPath basenameFormat(L
"saves/" + prefix + L
"-%04d");
44 size_t nextSaveNumber = 0;
53 const VfsPath basenameFormat(L
"saves/" + name);
63 OsPath tempSaveFileRealPath;
65 tempSaveFileRealPath = tempSaveFileRealPath /
"temp.0adsave";
67 time_t now = time(NULL);
71 std::stringstream simStateStream;
96 WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((
const u8*)metadataString.c_str(), metadataString.length(), now,
"metadata.json"));
97 WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((
const u8*)simStateStream.str().c_str(), simStateStream.str().length(), now,
"simulation.dat"));
98 archiveWriter.reset();
128 ((
CGameLoader*)cbData)->ReadEntry(pathname, fileInfo, archiveFile);
133 if (pathname == L
"metadata.json" &&
m_Metadata)
136 buffer.resize(fileInfo.
Size());
155 const VfsPath basename(L
"saves/" + name);
169 CGameLoader loader(scriptInterface, &metadata, &savedState);
177 TIMER(L
"GetSavedGames");
179 std::vector<CScriptValRooted> games;
187 for (
size_t i = 0; i < pathnames.size(); ++i)
190 err =
g_VFS->GetRealPath(pathnames[i], realPath);
206 CGameLoader loader(scriptInterface, &metadata, NULL);
215 scriptInterface.
Eval(
"({})", game);
216 scriptInterface.
SetProperty(game.
get(),
"id", pathnames[i].Basename());
218 games.push_back(game);
226 const VfsPath basename(L
"saves/" + name);
PIArchiveWriter CreateArchiveWriter_Zip(const OsPath &archivePathname, bool noDeflate)
std::vector< CScriptValRooted > GetSavedGames(ScriptInterface &scriptInterface)
Get list of saved games for GUI script usage.
CScriptValRooted * m_Metadata
#define WARN_IF_ERR(expression)
bool DeleteSavedGame(const std::wstring &name)
Permanently deletes the saved game archive with the given name.
void NextNumberedFilename(const PIVFS &fs, const VfsPath &pathnameFormat, size_t &nextNumber, VfsPath &nextPathname)
Determine the next available pathname with a given format.
Status Save(const std::wstring &name, const std::wstring &description, CSimulation2 &simulation, CGUIManager *gui, int playerID)
Create new saved game archive with given name and simulation data.
void Reserve(size_t size)
const jsval & get() const
Returns the current value.
A trivial wrapper around a jsval.
static void ReadEntryCallback(const VfsPath &pathname, const CFileInfo &fileInfo, PIArchiveFile archiveFile, uintptr_t cbData)
PIArchiveReader CreateArchiveReader_Zip(const OsPath &archivePathname)
static const int SAVED_GAME_VERSION_MAJOR
std::string StringifyJSON(jsval obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
shared_ptr< IArchiveWriter > PIArchiveWriter
CScriptValRooted ParseJSON(const std::string &string_utf8)
Parse a UTF-8-encoded JSON string.
Public API for simulation system.
std::string * m_SavedState
Status SavePrefix(const std::wstring &prefix, const std::wstring &description, CSimulation2 &simulation, CGUIManager *gui, int playerID)
Create new saved game archive with given prefix and simulation data.
CScriptVal GetSavedGameData()
Calls the current page's script function getSavedGameData() and returns the result.
void ReadEntry(const VfsPath &pathname, const CFileInfo &fileInfo, PIArchiveFile archiveFile)
#define WARN_RETURN_STATUS_IF_ERR(expression)
shared_ptr< IArchiveReader > PIArchiveReader
Helper class for retrieving data from saved game archives.
LIB_API int wunlink(const OsPath &pathname)
shared_ptr< u8 > Data() const
const String & string() const
Contains functions for managing saved game archives.
ScriptInterface & GetScriptInterface() const
bool Eval(const char *code)
static Status Load(const OsPath &pathname, void *buf, size_t size, const Parameters &p=Parameters(), const CompletedHook &completedHook=CompletedHook(), const IssueHook &issueHook=IssueHook())
i64 Status
Error handling system.
static const int SAVED_GAME_VERSION_MINOR
shared_ptr< T > DummySharedPtr(T *ptr)
bool SerializeState(std::ostream &stream)
jsval CloneValueFromOtherContext(ScriptInterface &otherContext, jsval val)
Construct a new value (usable in this ScriptInterface's context) by cloning a value from a different ...
ScriptInterface & GetScriptInterface()
const Status FILE_NOT_FOUND
#define TIMER(description)
Measures the time taken to execute code up until end of the current scope; displays it via debug_prin...
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
CGameLoader(ScriptInterface &scriptInterface, CScriptValRooted *metadata, std::string *savedState)
shared_ptr< IArchiveFile > PIArchiveFile
Path ChangeExtension(Path extension) const
bool VfsFileExists(const VfsPath &pathname)
Status Load(const std::wstring &name, ScriptInterface &scriptInterface, CScriptValRooted &metadata, std::string &savedState)
Load saved game archive with the given name.
#define WARN_RETURN(status)
std::vector< VfsPath > VfsPaths
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Abstraction around a SpiderMonkey JSContext.
External interface to the GUI system.
bool SetProperty(jsval obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
CScriptValRooted GetInitAttributes()
Get the data passed to SetInitAttributes.
Status GetPathnames(const PIVFS &fs, const VfsPath &path, const wchar_t *filter, VfsPaths &pathnames)
ScriptInterface & m_ScriptInterface
Status GetFileInfo(const OsPath &pathname, CFileInfo *pPtrInfo)