Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
Show All 18 Lines | |||||
#include "JSInterface_VFS.h" | #include "JSInterface_VFS.h" | ||||
#include "lib/file/vfs/vfs_util.h" | #include "lib/file/vfs/vfs_util.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/CStr.h" | #include "ps/CStr.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/ScriptExtraHeaders.h" | #include "scriptinterface/JSON.h" | ||||
#include "scriptinterface/ScriptInterface.h" | |||||
#include <sstream> | #include <sstream> | ||||
namespace JSI_VFS | namespace JSI_VFS | ||||
{ | { | ||||
// Only allow engine compartments to read files they may be concerned about. | // Only allow engine compartments to read files they may be concerned about. | ||||
#define PathRestriction_GUI {L""} | #define PathRestriction_GUI {L""} | ||||
#define PathRestriction_Simulation {L"simulation/"} | #define PathRestriction_Simulation {L"simulation/"} | ||||
▲ Show 20 Lines • Show All 166 Lines • ▼ Show 20 Lines | |||||
// Return file contents parsed as a JS Object | // Return file contents parsed as a JS Object | ||||
JS::Value ReadJSONFile(const ScriptInterface& scriptInterface, const std::vector<CStrW>& validPaths, const CStrW& filePath) | JS::Value ReadJSONFile(const ScriptInterface& scriptInterface, const std::vector<CStrW>& validPaths, const CStrW& filePath) | ||||
{ | { | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
if (!PathRestrictionMet(rq, validPaths, filePath)) | if (!PathRestrictionMet(rq, validPaths, filePath)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
JS::RootedValue out(rq.cx); | JS::RootedValue out(rq.cx); | ||||
scriptInterface.ReadJSONFile(filePath, &out); | Script::ReadJSONFile(rq, filePath, &out); | ||||
return out; | return out; | ||||
} | } | ||||
// Save given JS Object to a JSON file | // Save given JS Object to a JSON file | ||||
void WriteJSONFile(const ScriptInterface& scriptInterface, const std::wstring& filePath, JS::HandleValue val1) | void WriteJSONFile(const ScriptInterface& scriptInterface, const std::wstring& filePath, JS::HandleValue val1) | ||||
{ | { | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | ||||
JS::RootedValue val(rq.cx, val1); | JS::RootedValue val(rq.cx, val1); | ||||
std::string str(scriptInterface.StringifyJSON(&val, false)); | std::string str(Script::StringifyJSON(rq, &val, false)); | ||||
VfsPath path(filePath); | VfsPath path(filePath); | ||||
WriteBuffer buf; | WriteBuffer buf; | ||||
buf.Append(str.c_str(), str.length()); | buf.Append(str.c_str(), str.length()); | ||||
g_VFS->CreateFile(path, buf.Data(), buf.Size()); | g_VFS->CreateFile(path, buf.Data(), buf.Size()); | ||||
} | } | ||||
bool DeleteCampaignSave(const CStrW& filePath) | bool DeleteCampaignSave(const CStrW& filePath) | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator