Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) | ||||
JS::RootedValue ret(cx); | JS::RootedValue ret(cx); | ||||
ScriptInterface::ToJSVal(cx, &ret, contents.FromUTF8()); | ScriptInterface::ToJSVal(cx, &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) | ||||
{ | { | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface; | ||||
JSContext* cx = scriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | 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::RootedObject line_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())); | |||||
JS::RootedValue line_array(cx); | |||||
scriptInterface.CreateArray(&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(cx); | ||||
ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8()); | ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8()); | ||||
JS_SetElement(cx, line_array, cur_line++, val); | scriptInterface.SetPropertyInt(line_array, cur_line++, val); | ||||
} | } | ||||
return JS::ObjectValue(*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(); | JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ||||
▲ Show 20 Lines • Show All 89 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator