From eb4e66aab3f28c987866e09158f9847c6b61430f Mon Sep 17 00:00:00 2001 From: elexis Date: Fri, 8 Sep 2017 04:01:26 +0000 Subject: [PATCH] Move VFS GUI script function registration to its interface file. This was SVN commit r20133. --- source/gui/scripting/ScriptFunctions.cpp | 8 +---- source/ps/scripting/JSInterface_VFS.cpp | 40 +++++++----------------- source/ps/scripting/JSInterface_VFS.h | 19 ++--------- 3 files changed, 16 insertions(+), 51 deletions(-) diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index f60b7a12e0..8832e0a2a4 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -1044,15 +1044,9 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) JSI_Sound::RegisterScriptFunctions(scriptInterface); JSI_L10n::RegisterScriptFunctions(scriptInterface); JSI_Lobby::RegisterScriptFunctions(scriptInterface); + JSI_VFS::RegisterScriptFunctions(scriptInterface); JSI_VisualReplay::RegisterScriptFunctions(scriptInterface); - // VFS (external) - scriptInterface.RegisterFunction("BuildDirEntList"); - scriptInterface.RegisterFunction("FileExists"); - scriptInterface.RegisterFunction("GetFileMTime"); - scriptInterface.RegisterFunction("GetFileSize"); - scriptInterface.RegisterFunction("ReadFile"); - scriptInterface.RegisterFunction("ReadFileLines"); // GUI manager functions: scriptInterface.RegisterFunction("PushGuiPage"); scriptInterface.RegisterFunction("SwitchGuiPage"); diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index 21047cdfb4..05d7ee3751 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -40,8 +40,6 @@ /* else: success */ - - // state held across multiple BuildDirEntListCB calls; init by BuildDirEntList. struct BuildDirEntListState { @@ -102,19 +100,12 @@ JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const } // Return true iff the file exits -// -// if (fileExists(filename)) { ... } -// filename: VFS filename (may include path) bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const CStrW& filename) { return (g_VFS->GetFileInfo(filename, 0) == INFO::OK); } - // Return time [seconds since 1970] of the last modification to the specified file. -// -// mtime = getFileMTime(filename); -// filename: VFS filename (may include path) double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename) { CFileInfo fileInfo; @@ -124,11 +115,7 @@ double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), con return (double)fileInfo.MTime(); } - // Return current size of file. -// -// size = getFileSize(filename); -// filename: VFS filename (may include path) unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename) { CFileInfo fileInfo; @@ -138,11 +125,7 @@ unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate) return (unsigned int)fileInfo.Size(); } - // Return file contents in a string. Assume file is UTF-8 encoded text. -// -// contents = readFile(filename); -// filename: VFS filename (may include path) JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) { JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); @@ -163,18 +146,12 @@ JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::w return ret; } - // Return file contents as an array of lines. Assume file is UTF-8 encoded text. -// -// lines = readFileLines(filename); -// filename: VFS filename (may include path) JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename) { JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); JSAutoRequest rq(cx); - // - // read file - // + CVFSFile file; if (file.Load(g_VFS, filename) != PSRETURN_OK) return JSVAL_NULL; @@ -184,10 +161,7 @@ JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const s // 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) - // - std::stringstream ss(contents); JS::RootedObject line_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())); std::string line; @@ -203,3 +177,13 @@ JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const s return JS::ObjectValue(*line_array); } + +void JSI_VFS::RegisterScriptFunctions(const ScriptInterface& scriptInterface) +{ + scriptInterface.RegisterFunction("BuildDirEntList"); + scriptInterface.RegisterFunction("FileExists"); + scriptInterface.RegisterFunction("GetFileMTime"); + scriptInterface.RegisterFunction("GetFileSize"); + scriptInterface.RegisterFunction("ReadFile"); + scriptInterface.RegisterFunction("ReadFileLines"); +} diff --git a/source/ps/scripting/JSInterface_VFS.h b/source/ps/scripting/JSInterface_VFS.h index 569c851f75..977f502517 100644 --- a/source/ps/scripting/JSInterface_VFS.h +++ b/source/ps/scripting/JSInterface_VFS.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -41,34 +41,21 @@ namespace JSI_VFS JS::Value BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse); // Return true iff the file exists - // - // if (fileExists(filename) { ... } - // filename: VFS filename (may include path) bool FileExists(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& filename); // Return time [seconds since 1970] of the last modification to the specified file. - // - // mtime = getFileMTime(filename); - // filename: VFS filename (may include path) double GetFileMTime(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename); // Return current size of file. - // - // size = getFileSize(filename); - // filename: VFS filename (may include path) unsigned int GetFileSize(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename); // Return file contents in a string. - // - // contents = readFile(filename); - // filename: VFS filename (may include path) JS::Value ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename); // Return file contents as an array of lines. - // - // lines = readFileLines(filename); - // filename: VFS filename (may include path) JS::Value ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename); + + void RegisterScriptFunctions(const ScriptInterface& scriptInterface); } #endif