Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_VFS.cpp
Show All 19 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/ScriptVal.h" | #include "scriptinterface/ScriptVal.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include <sstream> | #include <sstream> | ||||
// 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/"} | ||||
#define PathRestriction_Maps {L"simulation/", L"maps/"} | #define PathRestriction_Maps {L"simulation/", L"maps/"} | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
// 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) | ||||
{ | { | ||||
return PathRestrictionMet(pCxPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK; | return PathRestrictionMet(pCxPrivate, 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(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(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(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | bool Script_FileExists_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ | ||||
return JSI_VFS::FileExists(pCxPrivate, PathRestriction_##context, filePath);\ | return JSI_VFS::FileExists(pCxPrivate, 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 | ||||
#define Register(func, name) ScriptWrapper::WrapAndRegister<decltype(&func), &func>(scriptInterface, name) | |||||
void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface) | void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, std::wstring, bool, &Script_ListDirectoryFiles_GUI>("ListDirectoryFiles"); | Register(Script_ListDirectoryFiles_GUI, "ListDirectoryFiles"); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, Script_FileExists_GUI>("FileExists"); | Register(Script_FileExists_GUI, "FileExists"); | ||||
scriptInterface.RegisterFunction<double, std::wstring, &JSI_VFS::GetFileMTime>("GetFileMTime"); | Register(JSI_VFS::GetFileMTime, "GetFileMTime"); | ||||
scriptInterface.RegisterFunction<unsigned int, std::wstring, &JSI_VFS::GetFileSize>("GetFileSize"); | Register(JSI_VFS::GetFileSize, "GetFileSize"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &JSI_VFS::ReadFile>("ReadFile"); | Register(JSI_VFS::ReadFile, "ReadFile"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &JSI_VFS::ReadFileLines>("ReadFileLines"); | Register(JSI_VFS::ReadFileLines, "ReadFileLines"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &Script_ReadJSONFile_GUI>("ReadJSONFile"); | Register(Script_ReadJSONFile_GUI, "ReadJSONFile"); | ||||
scriptInterface.RegisterFunction<void, std::wstring, JS::HandleValue, &WriteJSONFile>("WriteJSONFile"); | Register(WriteJSONFile, "WriteJSONFile"); | ||||
} | } | ||||
void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface) | void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, std::wstring, bool, &Script_ListDirectoryFiles_Simulation>("ListDirectoryFiles"); | Register(Script_ListDirectoryFiles_Simulation, "ListDirectoryFiles"); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, Script_FileExists_Simulation>("FileExists"); | Register(Script_FileExists_Simulation, "FileExists"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &Script_ReadJSONFile_Simulation>("ReadJSONFile"); | Register(Script_ReadJSONFile_Simulation, "ReadJSONFile"); | ||||
} | } | ||||
void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface) | void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, std::wstring, bool, &Script_ListDirectoryFiles_Maps>("ListDirectoryFiles"); | Register(Script_ListDirectoryFiles_Maps, "ListDirectoryFiles"); | ||||
scriptInterface.RegisterFunction<bool, std::wstring, Script_FileExists_Maps>("FileExists"); | Register(Script_FileExists_Maps, "FileExists"); | ||||
scriptInterface.RegisterFunction<JS::Value, std::wstring, &Script_ReadJSONFile_Maps>("ReadJSONFile"); | Register(Script_ReadJSONFile_Maps, "ReadJSONFile"); | ||||
} | } | ||||
#undef Register |
Wildfire Games · Phabricator