Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStrW>& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse) | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
// build array in the callback function | // build array in the callback function | ||||
BuildDirEntListState state(cx); | BuildDirEntListState state(cx); | ||||
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags); | vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags); | ||||
return OBJECT_TO_JSVAL(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) | ||||
{ | { | ||||
return PathRestrictionMet(pCxPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK; | return PathRestrictionMet(pCxPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
// 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(); | JSContext* cx = pCxPrivate->pScriptInterface->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 JSVAL_NULL; | 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); | ||||
▲ Show 20 Lines • Show All 110 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator