Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | #define JS_CHECK_FILE_ERR(err)\ | ||||
else if (err < 0)\ | else if (err < 0)\ | ||||
LOGERROR("Unknown failure in VFS %i", err ); | LOGERROR("Unknown failure in VFS %i", err ); | ||||
/* else: success */ | /* else: success */ | ||||
// state held across multiple BuildDirEntListCB calls; init by BuildDirEntList. | // state held across multiple BuildDirEntListCB calls; init by BuildDirEntList. | ||||
struct BuildDirEntListState | struct BuildDirEntListState | ||||
{ | { | ||||
JSContext* cx; | ScriptInterface* pScriptInterface; | ||||
JS::PersistentRootedObject filename_array; | JS::PersistentRootedObject filename_array; | ||||
int cur_idx; | int cur_idx; | ||||
BuildDirEntListState(JSContext* cx_) | BuildDirEntListState(ScriptInterface* scriptInterface) | ||||
: cx(cx_), | : pScriptInterface(scriptInterface), | ||||
filename_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())), | filename_array(scriptInterface->GetJSRuntime()), | ||||
cur_idx(0) | cur_idx(0) | ||||
{ | { | ||||
ScriptInterface::Request rq(pScriptInterface); | |||||
filename_array = JS_NewArrayObject(rq.cx, JS::HandleValueArray::empty()); | |||||
} | } | ||||
}; | }; | ||||
// called for each matching directory entry; add its full pathname to array. | // called for each matching directory entry; add its full pathname to array. | ||||
static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData) | static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData) | ||||
{ | { | ||||
BuildDirEntListState* s = (BuildDirEntListState*)cbData; | BuildDirEntListState* s = (BuildDirEntListState*)cbData; | ||||
JSAutoRequest rq(s->cx); | ScriptInterface::Request rq(s->pScriptInterface); | ||||
JS::RootedObject filenameArrayObj(s->cx, s->filename_array); | JS::RootedObject filenameArrayObj(rq.cx, s->filename_array); | ||||
JS::RootedValue val(s->cx); | JS::RootedValue val(rq.cx); | ||||
ScriptInterface::ToJSVal( s->cx, &val, CStrW(pathname.string()) ); | ScriptInterface::ToJSVal(rq, &val, CStrW(pathname.string()) ); | ||||
JS_SetElement(s->cx, filenameArrayObj, s->cur_idx++, val); | JS_SetElement(rq.cx, filenameArrayObj, s->cur_idx++, val); | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } | ||||
// Return an array of pathname strings, one for each matching entry in the | // Return an array of pathname strings, one for each matching entry in the | ||||
// specified directory. | // specified directory. | ||||
// filter_string: default "" matches everything; otherwise, see vfs_next_dirent. | // filter_string: default "" matches everything; otherwise, see vfs_next_dirent. | ||||
// recurse: should subdirectories be included in the search? default false. | // recurse: should subdirectories be included in the search? default false. | ||||
JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse) | JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse) | ||||
{ | { | ||||
if (!PathRestrictionMet(pCxPrivate, validPaths, path)) | if (!PathRestrictionMet(pCxPrivate, validPaths, path)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
// convert to const wchar_t*; if there's no filter, pass 0 for speed | // convert to const wchar_t*; if there's no filter, pass 0 for speed | ||||
// (interpreted as: "accept all files without comparing"). | // (interpreted as: "accept all files without comparing"). | ||||
const wchar_t* filter = 0; | const wchar_t* filter = 0; | ||||
if (!filterStr.empty()) | if (!filterStr.empty()) | ||||
filter = filterStr.c_str(); | filter = filterStr.c_str(); | ||||
int flags = recurse ? vfs::DIR_RECURSIVE : 0; | int flags = recurse ? vfs::DIR_RECURSIVE : 0; | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
// build array in the callback function | // build array in the callback function | ||||
BuildDirEntListState state(cx); | BuildDirEntListState state(pCxPrivate->pScriptInterface); | ||||
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags); | vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags); | ||||
return JS::ObjectValue(*state.filename_array); | return JS::ObjectValue(*state.filename_array); | ||||
} | } | ||||
// Return true iff the file exits | // Return true iff the file exits | ||||
bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filename) | bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filename) | ||||
{ | { | ||||
Show All 18 Lines | unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename) | ||||
JS_CHECK_FILE_ERR(err); | JS_CHECK_FILE_ERR(err); | ||||
return (unsigned int)fileInfo.Size(); | return (unsigned int)fileInfo.Size(); | ||||
} | } | ||||
// Return file contents in a string. Assume file is UTF-8 encoded text. | // Return file contents in a string. Assume file is UTF-8 encoded text. | ||||
JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) | JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) | ||||
{ | { | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
CVFSFile file; | CVFSFile file; | ||||
if (file.Load(g_VFS, filename) != PSRETURN_OK) | if (file.Load(g_VFS, filename) != PSRETURN_OK) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | ||||
// Fix CRLF line endings. (This function will only ever be used on text files.) | // Fix CRLF line endings. (This function will only ever be used on text files.) | ||||
contents.Replace("\r\n", "\n"); | contents.Replace("\r\n", "\n"); | ||||
// Decode as UTF-8 | // Decode as UTF-8 | ||||
JS::RootedValue ret(cx); | ScriptInterface::Request rq(pCxPrivate); | ||||
ScriptInterface::ToJSVal(cx, &ret, contents.FromUTF8()); | JS::RootedValue ret(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &ret, contents.FromUTF8()); | |||||
return ret; | return ret; | ||||
} | } | ||||
// Return file contents as an array of lines. Assume file is UTF-8 encoded text. | // Return file contents as an array of lines. Assume file is UTF-8 encoded text. | ||||
JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) | JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | |||||
JSContext* cx = scriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
CVFSFile file; | CVFSFile file; | ||||
if (file.Load(g_VFS, filename) != PSRETURN_OK) | if (file.Load(g_VFS, filename) != PSRETURN_OK) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | CStr contents = file.DecodeUTF8(); // assume it's UTF-8 | ||||
// Fix CRLF line endings. (This function will only ever be used on text files.) | // Fix CRLF line endings. (This function will only ever be used on text files.) | ||||
contents.Replace("\r\n", "\n"); | contents.Replace("\r\n", "\n"); | ||||
// split into array of strings (one per line) | // split into array of strings (one per line) | ||||
std::stringstream ss(contents); | std::stringstream ss(contents); | ||||
JS::RootedValue line_array(cx); | const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | ||||
ScriptInterface::CreateArray(cx, &line_array); | ScriptInterface::Request rq(scriptInterface); | ||||
JS::RootedValue line_array(rq.cx); | |||||
ScriptInterface::CreateArray(rq, &line_array); | |||||
std::string line; | std::string line; | ||||
int cur_line = 0; | int cur_line = 0; | ||||
while (std::getline(ss, line)) | while (std::getline(ss, line)) | ||||
{ | { | ||||
// Decode each line as UTF-8 | // Decode each line as UTF-8 | ||||
JS::RootedValue val(cx); | JS::RootedValue val(rq.cx); | ||||
ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8()); | ScriptInterface::ToJSVal(rq, &val, CStr(line).FromUTF8()); | ||||
scriptInterface.SetPropertyInt(line_array, cur_line++, val); | scriptInterface.SetPropertyInt(line_array, cur_line++, val); | ||||
} | } | ||||
return line_array; | return line_array; | ||||
} | } | ||||
JS::Value JSI_VFS::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filePath) | JS::Value JSI_VFS::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filePath) | ||||
{ | { | ||||
if (!PathRestrictionMet(pCxPrivate, validPaths, filePath)) | if (!PathRestrictionMet(pCxPrivate, validPaths, filePath)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | ||||
JSAutoRequest rq(cx); | ScriptInterface::Request rq(scriptInterface); | ||||
JS::RootedValue out(cx); | JS::RootedValue out(rq.cx); | ||||
pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out); | scriptInterface.ReadJSONFile(filePath, &out); | ||||
return out; | return out; | ||||
} | } | ||||
void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1) | void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1) | ||||
{ | { | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | ||||
JSAutoRequest rq(cx); | ScriptInterface::Request 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(cx, val1); | JS::RootedValue val(rq.cx, val1); | ||||
std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false)); | std::string str(scriptInterface.StringifyJSON(&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 JSI_VFS::PathRestrictionMet(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filePath) | bool JSI_VFS::PathRestrictionMet(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const CStrW& filePath) | ||||
{ | { | ||||
for (const CStrW& validPath : validPaths) | for (const CStrW& validPath : validPaths) | ||||
if (filePath.find(validPath) == 0) | if (filePath.find(validPath) == 0) | ||||
return true; | return true; | ||||
CStrW allowedPaths; | CStrW allowedPaths; | ||||
for (std::size_t i = 0; i < validPaths.size(); ++i) | for (std::size_t i = 0; i < validPaths.size(); ++i) | ||||
{ | { | ||||
if (i != 0) | if (i != 0) | ||||
allowedPaths += L", "; | allowedPaths += L", "; | ||||
allowedPaths += L"\"" + validPaths[i] + L"\""; | allowedPaths += L"\"" + validPaths[i] + L"\""; | ||||
} | } | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ScriptInterface::Request rq(pCxPrivate); | ||||
JSAutoRequest rq(cx); | JS_ReportError(rq.cx, "This part of the engine may only read from %s!", utf8_from_wstring(allowedPaths).c_str()); | ||||
JS_ReportError(cx, "This part of the engine may only read from %s!", utf8_from_wstring(allowedPaths).c_str()); | |||||
return false; | return false; | ||||
} | } | ||||
#define VFS_ScriptFunctions(context)\ | #define VFS_ScriptFunctions(context)\ | ||||
JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ | JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ | ||||
{\ | {\ | ||||
return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\ | return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\ | ||||
Show All 40 Lines |
Wildfire Games · Phabricator