Index: binaries/data/mods/public/globalscripts/Resources.js =================================================================== --- binaries/data/mods/public/globalscripts/Resources.js +++ 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.BuildDirEntList("simulation/data/resources/", "*.json", false)) { let data = Engine.ReadJSONFile(filename); if (!data) Index: binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- binaries/data/mods/public/globalscripts/Templates.js +++ 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. Index: binaries/data/mods/public/simulation/components/DataTemplateManager.js =================================================================== --- binaries/data/mods/public/simulation/components/DataTemplateManager.js +++ 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.BuildDirEntList(this.technologiesPath, "*.json", true).map(file => file.slice(this.technologiesPath.length, -".json".length)); }; DataTemplateManager.prototype.ListAllAuras = function() { - return Engine.FindJSONFiles("auras", true); + return Engine.BuildDirEntList(this.aurasPath, "*.json", true).map(file => file.slice(this.aurasPath.length, -".json".length)); }; DataTemplateManager.prototype.GetAllTechs = function() Index: source/ps/scripting/JSInterface_VFS.h =================================================================== --- source/ps/scripting/JSInterface_VFS.h +++ source/ps/scripting/JSInterface_VFS.h @@ -38,7 +38,7 @@ // // 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: source/ps/scripting/JSInterface_VFS.cpp =================================================================== --- source/ps/scripting/JSInterface_VFS.cpp +++ source/ps/scripting/JSInterface_VFS.cpp @@ -84,8 +84,11 @@ // // 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 +241,20 @@ JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\ {\ return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\ -} +}\ +JS::Value Script_BuildDirEntList_##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("BuildDirEntList"); scriptInterface.RegisterFunction("FileExists"); scriptInterface.RegisterFunction("GetFileMTime"); scriptInterface.RegisterFunction("GetFileSize"); @@ -260,12 +266,13 @@ void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface) { + scriptInterface.RegisterFunction("BuildDirEntList"); scriptInterface.RegisterFunction("ReadJSONFile"); } void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface) { - scriptInterface.RegisterFunction("BuildDirEntList"); + scriptInterface.RegisterFunction("BuildDirEntList"); scriptInterface.RegisterFunction("FileExists"); scriptInterface.RegisterFunction("ReadJSONFile"); } Index: source/simulation2/system/ComponentManager.cpp =================================================================== --- source/simulation2/system/ComponentManager.cpp +++ 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; -}