Ticket #1462: recursive tech loading.patch
File recursive tech loading.patch, 5.3 KB (added by , 12 years ago) |
---|
-
source/simulation2/system/ComponentManager.h
22 22 #include "Components.h" 23 23 #include "scriptinterface/ScriptInterface.h" 24 24 #include "simulation2/helpers/Player.h" 25 #include "ps/Filesystem.h" 25 26 26 27 #include <boost/random/linear_congruential.hpp> 27 28 #include <boost/unordered_map.hpp> … … 70 71 std::string schema; // RelaxNG fragment 71 72 CScriptValRooted ctor; // only valid if type == CT_Script 72 73 }; 74 75 struct FindJSONFilesCallbackData { 76 VfsPath path; 77 std::vector<std::string> templates; 78 }; 73 79 74 80 public: 75 81 CComponentManager(CSimContext&, bool skipScriptFunctions = false); … … 234 240 static void Script_DestroyEntity(void* cbdata, int ent); 235 241 static CScriptVal Script_ReadJSONFile(void* cbdata, std::wstring fileName); 236 242 static CScriptVal Script_ReadCivJSONFile(void* cbdata, std::wstring fileName); 237 static std::vector<std::string> Script_FindJSONFiles(void* cbdata, std::wstring subPath );243 static std::vector<std::string> Script_FindJSONFiles(void* cbdata, std::wstring subPath, bool recursive); 238 244 239 245 static CScriptVal ReadJSONFile(void *cbdata, std::wstring filePath, std::wstring fileName); 246 247 // callback function to handle recursively finding files in a directory 248 static Status FindJSONFilesCallback(const VfsPath&, const FileInfo&, const uintptr_t); 240 249 241 250 CMessage* ConstructMessage(int mtid, CScriptVal data); 242 251 void SendGlobalMessage(entity_id_t ent, const CMessage& msg) const; -
source/simulation2/system/ComponentManager.cpp
83 83 m_ScriptInterface.RegisterFunction<void, int, CComponentManager::Script_DestroyEntity> ("DestroyEntity"); 84 84 m_ScriptInterface.RegisterFunction<CScriptVal, std::wstring, CComponentManager::Script_ReadJSONFile> ("ReadJSONFile"); 85 85 m_ScriptInterface.RegisterFunction<CScriptVal, std::wstring, CComponentManager::Script_ReadCivJSONFile> ("ReadCivJSONFile"); 86 m_ScriptInterface.RegisterFunction<std::vector<std::string>, std::wstring, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles");86 m_ScriptInterface.RegisterFunction<std::vector<std::string>, std::wstring, bool, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles"); 87 87 } 88 88 89 89 // Define MT_*, IID_* as script globals, and store their names … … 957 957 958 958 return componentManager->GetScriptInterface().ReadJSONFile(path).get(); 959 959 } 960 961 std::vector<std::string> CComponentManager::Script_FindJSONFiles(void* UNUSED(cbdata), std::wstring subPath)960 961 Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const FileInfo& UNUSED(fileInfo), const uintptr_t cbData) 962 962 { 963 VfsPath path(L"simulation/data/" + subPath + L"/"); 964 VfsPaths pathnames; 963 FindJSONFilesCallbackData* data = (FindJSONFilesCallbackData*)cbData; 964 965 VfsPath pathstem = pathname.ChangeExtension(L""); 966 // Strip the root from the path 967 std::wstring name = pathstem.string().substr(data->path.string().length()); 965 968 966 std::vector<std::string> templates;969 data->templates.push_back(std::string(name.begin(), name.end())); 967 970 968 // Find all simulation/data/{subPath}/*.json 969 Status ret = vfs::GetPathnames(g_VFS, path, L"*.json", pathnames); 970 if (ret == INFO::OK) 971 { 972 for (VfsPaths::iterator it = pathnames.begin(); it != pathnames.end(); ++it) 973 { 974 // Strip the .json extension 975 VfsPath pathstem = it->ChangeExtension(L""); 976 // Strip the root from the path 977 std::wstring name = pathstem.string().substr(path.string().length()); 971 return INFO::OK; 972 } 978 973 979 templates.push_back(std::string(name.begin(), name.end())); 980 } 974 std::vector<std::string> CComponentManager::Script_FindJSONFiles(void* UNUSED(cbdata), std::wstring subPath, bool recursive) 975 { 976 FindJSONFilesCallbackData cbData; 977 cbData.path = VfsPath(L"simulation/data/" + subPath + L"/"); 978 979 int dir_flags = 0; 980 if (recursive) { 981 dir_flags = vfs::DIR_RECURSIVE; 981 982 } 982 else 983 984 // Find all simulation/data/{subPath}/*.json recursively 985 Status ret = vfs::ForEachFile(g_VFS, cbData.path, FindJSONFilesCallback, (uintptr_t)&cbData, L"*.json", dir_flags); 986 if (ret != INFO::OK) 983 987 { 984 988 // Some error reading directory 985 989 wchar_t error[200]; 986 LOGERROR(L"Error reading directory '%ls': %ls", path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error)));990 LOGERROR(L"Error reading directory '%ls': %ls", cbData.path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error))); 987 991 } 988 989 return templates;992 993 return cbData.templates; 990 994 } -
binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js
28 28 29 29 TechnologyTemplateManager.prototype.ListAllTechs = function() 30 30 { 31 return Engine.FindJSONFiles("technologies");31 return Engine.FindJSONFiles("technologies", true); 32 32 } 33 33 34 34 TechnologyTemplateManager.prototype.GetAllTechs = function()