Ticket #1462: recursive tech loading.patch

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

Made recursiveness optional

  • 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);
     
    234240    static void Script_DestroyEntity(void* cbdata, int ent);
    235241    static CScriptVal Script_ReadJSONFile(void* cbdata, std::wstring fileName);
    236242    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);
    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

     
    8383        m_ScriptInterface.RegisterFunction<void, int, CComponentManager::Script_DestroyEntity> ("DestroyEntity");
    8484        m_ScriptInterface.RegisterFunction<CScriptVal, std::wstring, CComponentManager::Script_ReadJSONFile> ("ReadJSONFile");
    8585        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");
    8787    }
    8888
    8989    // Define MT_*, IID_* as script globals, and store their names
     
    957957
    958958    return componentManager->GetScriptInterface().ReadJSONFile(path).get();
    959959}
    960 
    961 std::vector<std::string> CComponentManager::Script_FindJSONFiles(void* UNUSED(cbdata), std::wstring subPath)
     960   
     961Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const FileInfo& UNUSED(fileInfo), const uintptr_t cbData)
    962962{
    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());
    965968
    966     std::vector<std::string> templates;
     969    data->templates.push_back(std::string(name.begin(), name.end()));
    967970
    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}
    978973
    979             templates.push_back(std::string(name.begin(), name.end()));
    980         }
     974std::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;
    981982    }
    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)
    983987    {
    984988        // Some error reading directory
    985989        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)));
    987991    }
    988 
    989     return templates;
     992   
     993    return cbData.templates;
    990994}
  • binaries/data/mods/public/simulation/components/TechnologyTemplateManager.js

     
    2828
    2929TechnologyTemplateManager.prototype.ListAllTechs = function()
    3030{
    31     return Engine.FindJSONFiles("technologies");
     31    return Engine.FindJSONFiles("technologies", true);
    3232}
    3333
    3434TechnologyTemplateManager.prototype.GetAllTechs = function()