2012-05-03 20:46:05 -07:00
|
|
|
/* Copyright (C) 2012 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "ps/CLogger.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "ps/CStr.h"
|
2007-12-20 12:21:45 -08:00
|
|
|
#include "ps/Filesystem.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "scriptinterface/ScriptVal.h"
|
2013-10-20 10:13:53 -07:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2006-06-01 20:56:24 -07:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2011-05-25 03:39:13 -07:00
|
|
|
#include "lib/file/vfs/vfs_util.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// shared error handling code
|
|
|
|
|
#define JS_CHECK_FILE_ERR(err)\
|
|
|
|
|
/* this is liable to happen often, so don't complain */\
|
2010-11-16 15:00:52 -08:00
|
|
|
if (err == ERR::VFS_FILE_NOT_FOUND)\
|
2006-04-23 16:14:18 -07:00
|
|
|
{\
|
2014-01-04 02:14:53 -08:00
|
|
|
return 0; \
|
2006-04-23 16:14:18 -07:00
|
|
|
}\
|
2014-01-04 02:14:53 -08:00
|
|
|
/* unknown failure. We output an error message. */\
|
2010-11-16 15:00:52 -08:00
|
|
|
else if (err < 0)\
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Unknown failure in VFS %i", err );
|
2006-04-23 16:14:18 -07:00
|
|
|
/* else: success */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-08 11:41:30 -07:00
|
|
|
// state held across multiple BuildDirEntListCB calls; init by BuildDirEntList.
|
|
|
|
|
struct BuildDirEntListState
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
JSContext* cx;
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::PersistentRootedObject filename_array;
|
2006-04-23 16:14:18 -07:00
|
|
|
int cur_idx;
|
|
|
|
|
|
2006-09-08 11:41:30 -07:00
|
|
|
BuildDirEntListState(JSContext* cx_)
|
2015-01-24 06:46:52 -08:00
|
|
|
: cx(cx_),
|
|
|
|
|
filename_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())),
|
|
|
|
|
cur_idx(0)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// called for each matching directory entry; add its full pathname to array.
|
2013-09-10 07:17:04 -07:00
|
|
|
static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-09-25 02:39:20 -07:00
|
|
|
BuildDirEntListState* s = (BuildDirEntListState*)cbData;
|
2015-01-24 06:46:52 -08:00
|
|
|
JSAutoRequest rq(s->cx);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedObject filenameArrayObj(s->cx, s->filename_array);
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue val(s->cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ScriptInterface::ToJSVal( s->cx, &val, CStrW(pathname.string()) );
|
2015-01-24 06:46:52 -08:00
|
|
|
JS_SetElement(s->cx, filenameArrayObj, s->cur_idx++, val);
|
2011-07-18 02:21:56 -07:00
|
|
|
return INFO::OK;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return an array of pathname strings, one for each matching entry in the
|
|
|
|
|
// specified directory.
|
|
|
|
|
//
|
2006-09-08 11:41:30 -07:00
|
|
|
// pathnames = buildDirEntList(start_path [, filter_string [, recursive ] ]);
|
2006-04-23 16:14:18 -07:00
|
|
|
// directory: VFS path
|
|
|
|
|
// filter_string: default "" matches everything; otherwise, see vfs_next_dirent.
|
|
|
|
|
// recurse: should subdirectories be included in the search? default false.
|
|
|
|
|
//
|
|
|
|
|
// note: full pathnames of each file/subdirectory are returned,
|
|
|
|
|
// ready for use as a "filename" for the other functions.
|
2016-01-23 07:17:56 -08:00
|
|
|
JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
// convert to const wchar_t*; if there's no filter, pass 0 for speed
|
2006-04-23 16:14:18 -07:00
|
|
|
// (interpreted as: "accept all files without comparing").
|
2009-11-03 13:46:35 -08:00
|
|
|
const wchar_t* filter = 0;
|
2014-01-04 02:14:53 -08:00
|
|
|
if (!filterStr.empty())
|
|
|
|
|
filter = filterStr.c_str();
|
2016-11-23 05:02:58 -08:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
int flags = recurse ? vfs::DIR_RECURSIVE : 0;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2016-09-18 02:34:45 -07:00
|
|
|
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
|
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
// build array in the callback function
|
2016-09-18 02:34:45 -07:00
|
|
|
BuildDirEntListState state(cx);
|
2011-05-25 03:39:13 -07:00
|
|
|
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
return OBJECT_TO_JSVAL(state.filename_array);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2013-11-07 12:07:24 -08:00
|
|
|
// Return true iff the file exits
|
|
|
|
|
//
|
|
|
|
|
// if (fileExists(filename)) { ... }
|
|
|
|
|
// filename: VFS filename (may include path)
|
2016-01-23 07:17:56 -08:00
|
|
|
bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const CStrW& filename)
|
2013-11-07 12:07:24 -08:00
|
|
|
{
|
2014-01-04 02:14:53 -08:00
|
|
|
return (g_VFS->GetFileInfo(filename, 0) == INFO::OK);
|
2013-11-07 12:07:24 -08:00
|
|
|
}
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Return time [seconds since 1970] of the last modification to the specified file.
|
|
|
|
|
//
|
|
|
|
|
// mtime = getFileMTime(filename);
|
|
|
|
|
// filename: VFS filename (may include path)
|
2016-01-23 07:17:56 -08:00
|
|
|
double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2013-09-10 07:17:04 -07:00
|
|
|
CFileInfo fileInfo;
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
|
2010-11-16 15:00:52 -08:00
|
|
|
JS_CHECK_FILE_ERR(err);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
return (double)fileInfo.MTime();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return current size of file.
|
|
|
|
|
//
|
|
|
|
|
// size = getFileSize(filename);
|
|
|
|
|
// filename: VFS filename (may include path)
|
2016-01-23 07:17:56 -08:00
|
|
|
unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2013-09-10 07:17:04 -07:00
|
|
|
CFileInfo fileInfo;
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
|
2006-04-23 16:14:18 -07:00
|
|
|
JS_CHECK_FILE_ERR(err);
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
return (unsigned int)fileInfo.Size();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-03 20:46:05 -07:00
|
|
|
// Return file contents in a string. Assume file is UTF-8 encoded text.
|
2006-04-23 16:14:18 -07:00
|
|
|
//
|
|
|
|
|
// contents = readFile(filename);
|
|
|
|
|
// filename: VFS filename (may include path)
|
2016-01-23 07:17:56 -08:00
|
|
|
JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
|
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2012-05-03 20:46:05 -07:00
|
|
|
CVFSFile file;
|
|
|
|
|
if (file.Load(g_VFS, filename) != PSRETURN_OK)
|
2014-07-14 12:52:35 -07:00
|
|
|
return JS::NullValue();
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2012-05-03 20:46:05 -07:00
|
|
|
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Fix CRLF line endings. (This function will only ever be used on text files.)
|
|
|
|
|
contents.Replace("\r\n", "\n");
|
|
|
|
|
|
2009-07-16 08:51:35 -07:00
|
|
|
// Decode as UTF-8
|
2014-07-14 12:52:35 -07:00
|
|
|
JS::RootedValue ret(cx);
|
|
|
|
|
ScriptInterface::ToJSVal(cx, &ret, contents.FromUTF8());
|
2015-01-24 06:46:52 -08:00
|
|
|
return ret;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-03 20:46:05 -07:00
|
|
|
// Return file contents as an array of lines. Assume file is UTF-8 encoded text.
|
2006-04-23 16:14:18 -07:00
|
|
|
//
|
|
|
|
|
// lines = readFileLines(filename);
|
|
|
|
|
// filename: VFS filename (may include path)
|
2016-01-23 07:17:56 -08:00
|
|
|
JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
|
|
|
|
|
JSAutoRequest rq(cx);
|
2006-04-23 16:14:18 -07:00
|
|
|
//
|
|
|
|
|
// read file
|
|
|
|
|
//
|
2012-05-03 20:46:05 -07:00
|
|
|
CVFSFile file;
|
|
|
|
|
if (file.Load(g_VFS, filename) != PSRETURN_OK)
|
2014-01-04 02:14:53 -08:00
|
|
|
return JSVAL_NULL;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2012-05-03 20:46:05 -07:00
|
|
|
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Fix CRLF line endings. (This function will only ever be used on text files.)
|
|
|
|
|
contents.Replace("\r\n", "\n");
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// split into array of strings (one per line)
|
|
|
|
|
//
|
|
|
|
|
|
2010-11-16 15:00:52 -08:00
|
|
|
std::stringstream ss(contents);
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedObject line_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty()));
|
2006-04-23 16:14:18 -07:00
|
|
|
std::string line;
|
|
|
|
|
int cur_line = 0;
|
2015-01-24 06:46:52 -08:00
|
|
|
|
2010-11-16 15:00:52 -08:00
|
|
|
while (std::getline(ss, line))
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2009-07-16 08:51:35 -07:00
|
|
|
// Decode each line as UTF-8
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue val(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8());
|
2015-01-24 06:46:52 -08:00
|
|
|
JS_SetElement(cx, line_array, cur_line++, val);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
return JS::ObjectValue(*line_array);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|