Ticket #1696: progress_1696_II.patch

File progress_1696_II.patch, 6.4 KB (added by trompetin17, 10 years ago)
  • binaries/data/mods/public/simulation/data/placeablesFilter.json

     
     1 {
     2   "templates": [
     3     {"directory": "campaigns", "file": "*.xml"},
     4     {"directory": "gaia", "file": "*.xml"},
     5     {"directory": "other", "file": "*.xml"},
     6     {"directory": "structures", "file": "*.xml"},
     7     {"directory": "units", "file": "*.xml"},
     8     {"directory": "special", "file": "trigger_point*.xml"},
     9     {"directory": "special", "file": "territory*.xml"}
     10   ]
     11 }
     12 No newline at end of file
  • source/ps/TemplateLoader.cpp

     
    191191    return INFO::OK;
    192192}
    193193
     194std::vector<std::string> CTemplateLoader::FindPlaceablesTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType, ScriptInterface& scriptInterface)
     195{
     196   
     197    std::vector<std::string> templates;
     198    Status ok;
     199    VfsPath templatePath;
     200
     201
     202    if (templatesType == SIMULATION_TEMPLATES || templatesType == ALL_TEMPLATES)
     203    {
     204        CScriptValRooted placeablesFilter = scriptInterface.ReadJSONFile("simulation/data/placeablesFilter.json");
     205       
     206        std::vector<CScriptValRooted> folders;
     207        if (scriptInterface.GetProperty(placeablesFilter.get(), "templates", folders))
     208        {
     209            templatePath = VfsPath(TEMPLATE_ROOT) / path;
     210            //I have every object inside, just run for each
     211            for (std::vector<CScriptValRooted>::iterator iterator = folders.begin(); iterator != folders.end();++iterator)
     212            {
     213                jsval val = (*iterator).get();
     214                std::string directoryPath;
     215                std::wstring fileFilter;
     216                scriptInterface.GetProperty(val, "directory", directoryPath);
     217                scriptInterface.GetProperty(val, "file", fileFilter);
     218               
     219                VfsPaths filenames;
     220                if (vfs::GetPathnames(g_VFS, templatePath / (directoryPath + "/"), fileFilter.c_str(), filenames) == INFO::OK)
     221                {
     222                    for (VfsPaths::iterator it = filenames.begin(); it != filenames.end(); ++it)
     223                    {
     224                        VfsPath filename = *it;
     225                        // Strip the .xml extension
     226                        VfsPath pathstem = filename.ChangeExtension(L"");
     227                        // Strip the root from the path
     228                        std::wstring name = pathstem.string().substr(ARRAY_SIZE(TEMPLATE_ROOT) - 1);
     229
     230                        templates.push_back(std::string(name.begin(), name.end()));
     231                    }
     232                }
     233            }
     234           
     235        }
     236    }
     237
     238    if (templatesType == ACTOR_TEMPLATES || templatesType == ALL_TEMPLATES)
     239    {
     240        templatePath = VfsPath(ACTOR_ROOT) / path;
     241        if (includeSubdirectories)
     242            ok = vfs::ForEachFile(g_VFS, templatePath, AddActorToTemplates, (uintptr_t)&templates, L"*.xml", vfs::DIR_RECURSIVE);
     243        else
     244            ok = vfs::ForEachFile(g_VFS, templatePath, AddActorToTemplates, (uintptr_t)&templates, L"*.xml");
     245        WARN_IF_ERR(ok);
     246    }
     247
     248    if (templatesType != SIMULATION_TEMPLATES && templatesType != ACTOR_TEMPLATES && templatesType != ALL_TEMPLATES)
     249        LOGERROR(L"Undefined template type (valid: all, simulation, actor)");
     250
     251    return templates;
     252}
     253
    194254std::vector<std::string> CTemplateLoader::FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType)
    195255{
    196256    std::vector<std::string> templates;
  • source/ps/TemplateLoader.h

     
    1919#define INCLUDED_TEMPLATELOADER
    2020
    2121#include "simulation2/system/ParamNode.h"
     22#include "scriptinterface/ScriptInterface.h"
    2223
    2324enum ETemplatesType
    2425{
     
    5556     */
    5657    std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType);
    5758
     59    std::vector<std::string> FindPlaceablesTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType, ScriptInterface& scriptInterface);
     60
    5861private:
    5962    /**
    6063     * (Re)loads the given template, regardless of whether it exists already,
  • source/simulation2/components/CCmpTemplateManager.cpp

     
    130130
    131131    virtual std::vector<std::string> FindAllTemplates(bool includeActors);
    132132
     133    virtual std::vector<std::string> FindAllPlaceablesTemplates(bool includeActors);
     134
    133135    virtual std::vector<entity_id_t> GetEntitiesUsingTemplate(std::string templateName);
    134136
    135137private:
     
    232234    return m_templateLoader.FindTemplates("", true, templatesType);
    233235}
    234236
     237std::vector<std::string> CCmpTemplateManager::FindAllPlaceablesTemplates(bool includeActors)
     238{
     239    ScriptInterface& scriptInterface = this->GetSimContext().GetScriptInterface();
     240
     241    ETemplatesType templatesType = includeActors ? ALL_TEMPLATES : SIMULATION_TEMPLATES;
     242    return m_templateLoader.FindPlaceablesTemplates("", true, templatesType, scriptInterface);
     243}
     244
    235245/**
    236246 * Get the list of entities using the specified template
    237247 */
  • source/simulation2/components/ICmpTemplateManager.h

     
    9999     */
    100100    virtual std::vector<std::string> FindAllTemplates(bool includeActors) = 0;
    101101
     102    virtual std::vector<std::string> FindAllPlaceablesTemplates(bool includeActors) = 0;
     103
    102104    /**
    103105     * Permanently disable XML validation (intended solely for test cases).
    104106     */
  • source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp

     
    6868    CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
    6969    if (cmpTemplateManager)
    7070    {
    71         std::vector<std::string> names = cmpTemplateManager->FindAllTemplates(true);
     71        std::vector<std::string> names = cmpTemplateManager->FindAllPlaceablesTemplates(true);
    7272
    7373        for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it)
    7474        {