Ticket #1462: recursive tech loading.2.patch

File recursive tech loading.2.patch, 3.5 KB (added by Jonathan Waller, 12 years ago)

Added error checking

  • source/simulation2/system/ComponentManager.h

     
    2222#include "Components.h"
    2323#include "scriptinterface/ScriptInterface.h"
    2424#include "simulation2/helpers/Player.h"
     25#include "ps/Filesystem.h"
    2526
    2627#include <boost/random/linear_congruential.hpp>
    2728#include <boost/unordered_map.hpp>
     
    7071        std::string schema; // RelaxNG fragment
    7172        CScriptValRooted ctor; // only valid if type == CT_Script
    7273    };
     74   
     75    struct FindJSONFilesCallbackData {
     76        VfsPath path;
     77        std::vector<std::string> templates;
     78    };
    7379
    7480public:
    7581    CComponentManager(CSimContext&, bool skipScriptFunctions = false);
     
    237243    static std::vector<std::string> Script_FindJSONFiles(void* cbdata, std::wstring subPath);
    238244
    239245    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);
    240249
    241250    CMessage* ConstructMessage(int mtid, CScriptVal data);
    242251    void SendGlobalMessage(entity_id_t ent, const CMessage& msg) const;
  • source/simulation2/system/ComponentManager.cpp

     
    957957
    958958    return componentManager->GetScriptInterface().ReadJSONFile(path).get();
    959959}
     960   
     961Status 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());
    960968
     969    data->templates.push_back(std::string(name.begin(), name.end()));
     970
     971    return INFO::OK;
     972}
     973
    961974std::vector<std::string> CComponentManager::Script_FindJSONFiles(void* UNUSED(cbdata), std::wstring subPath)
    962975{
    963     VfsPath path(L"simulation/data/" + subPath + L"/");
    964     VfsPaths pathnames;
     976    FindJSONFilesCallbackData cbData;
     977    cbData.path = VfsPath(L"simulation/data/" + subPath + L"/");
    965978
    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)
    971982    {
    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());
    978 
    979             templates.push_back(std::string(name.begin(), name.end()));
    980         }
    981     }
    982     else
    983     {
    984983        // Some error reading directory
    985984        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)));
    987986    }
    988 
    989     return templates;
     987   
     988    return cbData.templates;
    990989}