Ticket #1462: recursive tech loading.2.patch
File recursive tech loading.2.patch, 3.5 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); … … 237 243 static std::vector<std::string> Script_FindJSONFiles(void* cbdata, std::wstring subPath); 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
957 957 958 958 return componentManager->GetScriptInterface().ReadJSONFile(path).get(); 959 959 } 960 961 Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const FileInfo& UNUSED(fileInfo), const uintptr_t cbData) 962 { 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()); 960 968 969 data->templates.push_back(std::string(name.begin(), name.end())); 970 971 return INFO::OK; 972 } 973 961 974 std::vector<std::string> CComponentManager::Script_FindJSONFiles(void* UNUSED(cbdata), std::wstring subPath) 962 975 { 963 VfsPath path(L"simulation/data/" + subPath + L"/");964 VfsPaths pathnames;976 FindJSONFilesCallbackData cbData; 977 cbData.path = VfsPath(L"simulation/data/" + subPath + L"/"); 965 978 966 std::vector<std::string> templates; 967 968 // Find all simulation/data/{subPath}/*.json 969 Status ret = vfs::GetPathnames(g_VFS, path, L"*.json", pathnames); 970 if (ret == INFO::OK) 979 // Find all simulation/data/{subPath}/*.json recursively 980 Status ret = vfs::ForEachFile(g_VFS, cbData.path, FindJSONFilesCallback, (uintptr_t)&cbData, L"*.json", vfs::DIR_RECURSIVE); 981 if (ret != INFO::OK) 971 982 { 972 for (VfsPaths::iterator it = pathnames.begin(); it != pathnames.end(); ++it)973 {974 // Strip the .json extension975 VfsPath pathstem = it->ChangeExtension(L"");976 // Strip the root from the path977 std::wstring name = pathstem.string().substr(path.string().length());978 979 templates.push_back(std::string(name.begin(), name.end()));980 }981 }982 else983 {984 983 // Some error reading directory 985 984 wchar_t error[200]; 986 LOGERROR(L"Error reading directory '%ls': %ls", path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error)));985 LOGERROR(L"Error reading directory '%ls': %ls", cbData.path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error))); 987 986 } 988 989 return templates;987 988 return cbData.templates; 990 989 }