Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData) | ||||
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::CmptPrivate* pCmptPrivate, const std::vector<CStrW>& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse) | ||||
{ | { | ||||
if (!PathRestrictionMet(pCxPrivate, validPaths, path)) | if (!PathRestrictionMet(pCmptPrivate, 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; | ||||
// build array in the callback function | // build array in the callback function | ||||
BuildDirEntListState state(pCxPrivate->pScriptInterface); | BuildDirEntListState state(pCmptPrivate->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::CmptPrivate* pCmptPrivate, const std::vector<CStrW>& validPaths, const CStrW& filename) | ||||
{ | { | ||||
return PathRestrictionMet(pCxPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK; | return PathRestrictionMet(pCmptPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK; | ||||
} | } | ||||
// Return time [seconds since 1970] of the last modification to the specified file. | // Return time [seconds since 1970] of the last modification to the specified file. | ||||
double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename) | double JSI_VFS::GetFileMTime(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::wstring& filename) | ||||
{ | { | ||||
CFileInfo fileInfo; | CFileInfo fileInfo; | ||||
Status err = g_VFS->GetFileInfo(filename, &fileInfo); | Status err = g_VFS->GetFileInfo(filename, &fileInfo); | ||||
JS_CHECK_FILE_ERR(err); | JS_CHECK_FILE_ERR(err); | ||||
return (double)fileInfo.MTime(); | return (double)fileInfo.MTime(); | ||||
} | } | ||||
// Return current size of file. | // Return current size of file. | ||||
unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename) | unsigned int JSI_VFS::GetFileSize(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::wstring& filename) | ||||
{ | { | ||||
CFileInfo fileInfo; | CFileInfo fileInfo; | ||||
Status err = g_VFS->GetFileInfo(filename, &fileInfo); | Status err = g_VFS->GetFileInfo(filename, &fileInfo); | ||||
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::CmptPrivate* pCmptPrivate, const std::wstring& filename) | ||||
{ | { | ||||
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 | ||||
ScriptInterface::Request rq(pCxPrivate); | ScriptInterface::Request rq(pCmptPrivate); | ||||
JS::RootedValue ret(rq.cx); | JS::RootedValue ret(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &ret, contents.FromUTF8()); | 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::CmptPrivate* pCmptPrivate, const std::wstring& filename) | ||||
{ | { | ||||
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); | ||||
const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | const ScriptInterface& scriptInterface = *pCmptPrivate->pScriptInterface; | ||||
ScriptInterface::Request rq(scriptInterface); | ScriptInterface::Request rq(scriptInterface); | ||||
JS::RootedValue line_array(rq.cx); | JS::RootedValue line_array(rq.cx); | ||||
ScriptInterface::CreateArray(rq, &line_array); | 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(rq.cx); | JS::RootedValue val(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &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::CmptPrivate* pCmptPrivate, const std::vector<CStrW>& validPaths, const CStrW& filePath) | ||||
{ | { | ||||
if (!PathRestrictionMet(pCxPrivate, validPaths, filePath)) | if (!PathRestrictionMet(pCmptPrivate, validPaths, filePath)) | ||||
return JS::NullValue(); | return JS::NullValue(); | ||||
const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | const ScriptInterface& scriptInterface = *pCmptPrivate->pScriptInterface; | ||||
ScriptInterface::Request rq(scriptInterface); | ScriptInterface::Request rq(scriptInterface); | ||||
JS::RootedValue out(rq.cx); | JS::RootedValue out(rq.cx); | ||||
scriptInterface.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::CmptPrivate* pCmptPrivate, const std::wstring& filePath, JS::HandleValue val1) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | const ScriptInterface& scriptInterface = *pCmptPrivate->pScriptInterface; | ||||
ScriptInterface::Request rq(scriptInterface); | 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(rq.cx, val1); | JS::RootedValue val(rq.cx, val1); | ||||
std::string str(scriptInterface.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::CmptPrivate* pCmptPrivate, 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"\""; | ||||
} | } | ||||
ScriptInterface::Request rq(pCxPrivate); | ScriptInterface::Request rq(pCmptPrivate); | ||||
JS_ReportError(rq.cx, "This part of the engine may only read from %s!", utf8_from_wstring(allowedPaths).c_str()); | JS_ReportError(rq.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::CmptPrivate* pCmptPrivate, const std::wstring& filePath)\ | ||||
{\ | {\ | ||||
return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\ | return JSI_VFS::ReadJSONFile(pCmptPrivate, PathRestriction_##context, filePath);\ | ||||
}\ | }\ | ||||
JS::Value Script_ListDirectoryFiles_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)\ | JS::Value Script_ListDirectoryFiles_##context(ScriptInterface::CmptPrivate* pCmptPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)\ | ||||
{\ | {\ | ||||
return JSI_VFS::BuildDirEntList(pCxPrivate, PathRestriction_##context, path, filterStr, recurse);\ | return JSI_VFS::BuildDirEntList(pCmptPrivate, PathRestriction_##context, path, filterStr, recurse);\ | ||||
}\ | }\ | ||||
bool Script_FileExists_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ | bool Script_FileExists_##context(ScriptInterface::CmptPrivate* pCmptPrivate, const std::wstring& filePath)\ | ||||
{\ | {\ | ||||
return JSI_VFS::FileExists(pCxPrivate, PathRestriction_##context, filePath);\ | return JSI_VFS::FileExists(pCmptPrivate, PathRestriction_##context, filePath);\ | ||||
}\ | }\ | ||||
VFS_ScriptFunctions(GUI); | VFS_ScriptFunctions(GUI); | ||||
VFS_ScriptFunctions(Simulation); | VFS_ScriptFunctions(Simulation); | ||||
VFS_ScriptFunctions(Maps); | VFS_ScriptFunctions(Maps); | ||||
#undef VFS_ScriptFunctions | #undef VFS_ScriptFunctions | ||||
void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface) | void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface) | ||||
Show All 24 Lines |
Wildfire Games · Phabricator