Index: ps/trunk/binaries/data/mods/public/globalscripts/Resources.js =================================================================== --- ps/trunk/binaries/data/mods/public/globalscripts/Resources.js +++ ps/trunk/binaries/data/mods/public/globalscripts/Resources.js @@ -4,29 +4,12 @@ */ function Resources() { - let jsonFiles = []; - // Simulation context - if (Engine.FindJSONFiles) - { - jsonFiles = Engine.FindJSONFiles("resources", false); - for (let file in jsonFiles) - jsonFiles[file] = "simulation/data/resources/" + jsonFiles[file] + ".json"; - } - // GUI context - else if (Engine.BuildDirEntList) - jsonFiles = Engine.BuildDirEntList("simulation/data/resources/", "*.json", false); - else - { - error("Resources: JSON functions are not available"); - return; - } - this.resourceData = []; this.resourceDataObj = {}; this.resourceCodes = []; this.resourceNames = {}; - for (let filename of jsonFiles) + for (let filename of Engine.ListDirectoryFiles("simulation/data/resources/", "*.json", false)) { let data = Engine.ReadJSONFile(filename); if (!data) Index: ps/trunk/binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- ps/trunk/binaries/data/mods/public/globalscripts/Templates.js +++ ps/trunk/binaries/data/mods/public/globalscripts/Templates.js @@ -1,6 +1,5 @@ /** * Loads history and gameplay data of all civs. - * Can be used from GUI and rmgen (because the simulation functions differ currently). * * @param selectableOnly {boolean} - Only load civs that can be selected * in the gamesetup. Scenario maps might set non-selectable civs. @@ -13,7 +12,7 @@ let civData = {}; - for (let filename of Engine.BuildDirEntList("simulation/data/civs/", "*.json", false)) + for (let filename of Engine.ListDirectoryFiles("simulation/data/civs/", "*.json", false)) { let data = Engine.ReadJSONFile(filename); Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js @@ -10,7 +10,7 @@ // Get list of XML files in pathname with recursion, excepting those starting with _ function getXMLFileList(pathname) { - var files = Engine.BuildDirEntList(pathname, "*.xml", true); + var files = Engine.ListDirectoryFiles(pathname, "*.xml", true); var result = []; @@ -33,7 +33,7 @@ function getJSONFileList(pathname) { // Remove the path and extension from each name, since we just want the filename - return Engine.BuildDirEntList(pathname, "*.json", false).map( + return Engine.ListDirectoryFiles(pathname, "*.json", false).map( filename => filename.substring(pathname.length, filename.length - 5)); } Index: ps/trunk/binaries/data/mods/public/gui/common/settings.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/settings.js +++ ps/trunk/binaries/data/mods/public/gui/common/settings.js @@ -211,7 +211,7 @@ function loadBiomes() { - return Engine.BuildDirEntList(g_BiomesDirectory, "*.json", false).map(file => { + return Engine.ListDirectoryFiles(g_BiomesDirectory, "*.json", false).map(file => { let description = Engine.ReadJSONFile(file).Description; return { "Id": file.substr(g_BiomesDirectory.length).slice(0, -".json".length), @@ -230,7 +230,7 @@ { let subdir = "victory_conditions/"; - let files = Engine.BuildDirEntList(g_SettingsDirectory + subdir, "*.json", false).map( + let files = Engine.ListDirectoryFiles(g_SettingsDirectory + subdir, "*.json", false).map( file => file.substr(g_SettingsDirectory.length)); let victoryConditions = files.map(file => { Index: ps/trunk/binaries/data/mods/public/gui/loading/loading.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/loading/loading.js +++ ps/trunk/binaries/data/mods/public/gui/loading/loading.js @@ -8,7 +8,7 @@ Engine.SetCursor("cursor-wait"); // Get tip image and corresponding tip text - let tipTextLoadingArray = Engine.BuildDirEntList("gui/text/tips/", "*.txt", false); + let tipTextLoadingArray = Engine.ListDirectoryFiles("gui/text/tips/", "*.txt", false); if (tipTextLoadingArray.length > 0) { Index: ps/trunk/binaries/data/mods/public/gui/reference/common/load.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/reference/common/load.js +++ ps/trunk/binaries/data/mods/public/gui/reference/common/load.js @@ -291,7 +291,7 @@ { let techList = []; - for (let filename of Engine.BuildDirEntList(g_TechnologyPath, "*.json", true)) + for (let filename of Engine.ListDirectoryFiles(g_TechnologyPath, "*.json", true)) { // -5 to strip off the file extension let templateName = filename.slice(g_TechnologyPath.length, -5); Index: ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js +++ ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js @@ -8,6 +8,9 @@ DataTemplateManager.prototype.Init = function() { + this.technologiesPath = "simulation/data/technologies/"; + this.aurasPath = "simulation/data/auras/"; + this.allTechs = {}; this.allAuras = {}; @@ -25,7 +28,7 @@ { if (!this.allTechs[template]) { - this.allTechs[template] = Engine.ReadJSONFile("simulation/data/technologies/" + template + ".json"); + this.allTechs[template] = Engine.ReadJSONFile(this.technologiesPath + template + ".json"); if (!this.allTechs[template]) error("Failed to load technology \"" + template + "\""); } @@ -37,7 +40,7 @@ { if (!this.allAuras[template]) { - this.allAuras[template] = Engine.ReadJSONFile("simulation/data/auras/" + template + ".json"); + this.allAuras[template] = Engine.ReadJSONFile(this.aurasPath + template + ".json"); if (!this.allAuras[template]) error("Failed to load aura \"" + template + "\""); } @@ -47,12 +50,12 @@ DataTemplateManager.prototype.ListAllTechs = function() { - return Engine.FindJSONFiles("technologies", true); + return Engine.ListDirectoryFiles(this.technologiesPath, "*.json", true).map(file => file.slice(this.technologiesPath.length, -".json".length)); }; DataTemplateManager.prototype.ListAllAuras = function() { - return Engine.FindJSONFiles("auras", true); + return Engine.ListDirectoryFiles(this.aurasPath, "*.json", true).map(file => file.slice(this.aurasPath.length, -".json".length)); }; DataTemplateManager.prototype.GetAllTechs = function() Index: ps/trunk/source/ps/scripting/JSInterface_VFS.h =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_VFS.h +++ ps/trunk/source/ps/scripting/JSInterface_VFS.h @@ -30,15 +30,7 @@ { // Return an array of pathname strings, one for each matching entry in the // specified directory. - // - // pathnames = buildDirEntList(start_path [, filter_string [, recursive ] ]); - // directory: VFS path - // filter_string: see match_wildcard; "" matches everything. - // 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. - JS::Value BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse); + JS::Value BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse); // Return true iff the file exists bool FileExists(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& filename); Index: ps/trunk/source/ps/scripting/JSInterface_VFS.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_VFS.cpp +++ ps/trunk/source/ps/scripting/JSInterface_VFS.cpp @@ -76,16 +76,13 @@ // Return an array of pathname strings, one for each matching entry in the // specified directory. -// -// pathnames = buildDirEntList(start_path [, filter_string [, recursive ] ]); -// 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. -JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse) +JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse) { + if (!PathRestrictionMet(pCxPrivate, validPaths, path)) + return JS::NullValue(); + // convert to const wchar_t*; if there's no filter, pass 0 for speed // (interpreted as: "accept all files without comparing"). const wchar_t* filter = 0; @@ -238,17 +235,20 @@ JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ {\ return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\ -} +}\ +JS::Value Script_ListDirectoryFiles_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)\ +{\ + return JSI_VFS::BuildDirEntList(pCxPrivate, PathRestriction_##context, path, filterStr, recurse);\ +}\ VFS_ScriptFunctions(GUI); VFS_ScriptFunctions(Simulation); VFS_ScriptFunctions(Maps); - #undef VFS_ScriptFunctions void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface) { - scriptInterface.RegisterFunction("BuildDirEntList"); + scriptInterface.RegisterFunction("ListDirectoryFiles"); scriptInterface.RegisterFunction("FileExists"); scriptInterface.RegisterFunction("GetFileMTime"); scriptInterface.RegisterFunction("GetFileSize"); @@ -260,12 +260,13 @@ void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface) { + scriptInterface.RegisterFunction("ListDirectoryFiles"); scriptInterface.RegisterFunction("ReadJSONFile"); } void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface) { - scriptInterface.RegisterFunction("BuildDirEntList"); + scriptInterface.RegisterFunction("ListDirectoryFiles"); scriptInterface.RegisterFunction("FileExists"); scriptInterface.RegisterFunction("ReadJSONFile"); } Index: ps/trunk/source/simulation2/system/ComponentManager.h =================================================================== --- ps/trunk/source/simulation2/system/ComponentManager.h +++ ps/trunk/source/simulation2/system/ComponentManager.h @@ -115,12 +115,6 @@ } }; - struct FindJSONFilesCallbackData - { - VfsPath path; - std::vector templates; - }; - public: CComponentManager(CSimContext&, shared_ptr rt, bool skipScriptFunctions = false); ~CComponentManager(); @@ -333,10 +327,6 @@ static void Script_DestroyEntity(ScriptInterface::CxPrivate* pCxPrivate, int ent); static void Script_FlushDestroyedEntities(ScriptInterface::CxPrivate* pCxPrivate); static bool Script_DataFileExists(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& fileName); - static std::vector Script_FindJSONFiles(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& subPath, bool recursive); - - // callback function to handle recursively finding files in a directory - static Status FindJSONFilesCallback(const VfsPath&, const CFileInfo&, const uintptr_t); CMessage* ConstructMessage(int mtid, JS::HandleValue data); void SendGlobalMessage(entity_id_t ent, const CMessage& msg); Index: ps/trunk/source/simulation2/system/ComponentManager.cpp =================================================================== --- ps/trunk/source/simulation2/system/ComponentManager.cpp +++ ps/trunk/source/simulation2/system/ComponentManager.cpp @@ -86,7 +86,6 @@ m_ScriptInterface.RegisterFunction ("DestroyEntity"); m_ScriptInterface.RegisterFunction ("FlushDestroyedEntities"); m_ScriptInterface.RegisterFunction ("DataFileExists"); - m_ScriptInterface.RegisterFunction, std::wstring, bool, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles"); } // Define MT_*, IID_* as script globals, and store their names @@ -1191,38 +1190,3 @@ VfsPath path = VfsPath(L"simulation/data") / fileName; return VfsFileExists(path); } - -Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData) -{ - FindJSONFilesCallbackData* data = (FindJSONFilesCallbackData*)cbData; - - VfsPath pathstem = pathname.ChangeExtension(L""); - // Strip the root from the path - std::wstring name = pathstem.string().substr(data->path.string().length()); - - data->templates.push_back(std::string(name.begin(), name.end())); - - return INFO::OK; -} - -std::vector CComponentManager::Script_FindJSONFiles(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& subPath, bool recursive) -{ - FindJSONFilesCallbackData cbData; - cbData.path = VfsPath(L"simulation/data/" + subPath + L"/"); - - int dir_flags = 0; - if (recursive) { - dir_flags = vfs::DIR_RECURSIVE; - } - - // Find all simulation/data/{subPath}/*.json recursively - Status ret = vfs::ForEachFile(g_VFS, cbData.path, FindJSONFilesCallback, (uintptr_t)&cbData, L"*.json", dir_flags); - if (ret != INFO::OK) - { - // Some error reading directory - wchar_t error[200]; - LOGERROR("Error reading directory '%s': %s", cbData.path.string8(), utf8_from_wstring(StatusDescription(ret, error, ARRAY_SIZE(error)))); - } - - return cbData.templates; -}