LCOV - code coverage report
Current view: top level - source/ps - TemplateLoader.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 4 4 100.0 %
Date: 2023-01-19 00:18:29 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2023 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_TEMPLATELOADER
      19             : #define INCLUDED_TEMPLATELOADER
      20             : 
      21             : #include "simulation2/system/ParamNode.h"
      22             : 
      23             : #include <string_view>
      24             : #include <unordered_map>
      25             : 
      26             : enum ETemplatesType
      27             : {
      28             :     ALL_TEMPLATES,
      29             :     ACTOR_TEMPLATES,
      30             :     SIMULATION_TEMPLATES
      31             : };
      32             : 
      33             : /**
      34             :  * Template loader: Handles the loading of entity template files for:
      35             :  * - the initialisation and deserialization of entity components in the
      36             :  *   simulation (CmpTemplateManager).
      37             :  * - access to actor templates, obstruction data, etc. in RMS/RMGEN
      38             :  * - access to various templates in the GUI, to display faction specificities
      39             :  *
      40             :  * Template names are intentionally restricted to ASCII strings for storage/serialization
      41             :  * efficiency (we have a lot of strings so this is significant);
      42             :  * they correspond to filenames so they shouldn't contain non-ASCII anyway.
      43             :  *
      44             :  *
      45             :  * TODO: Find a way to validate templates outside of the simulation.
      46             :  */
      47          20 : class CTemplateLoader
      48             : {
      49             : public:
      50          20 :     CTemplateLoader()
      51          20 :     {
      52          20 :     }
      53             : 
      54             :     /**
      55             :      * Provides the file data for requested template.
      56             :      */
      57             :     const CParamNode& GetTemplateFileData(const std::string& templateName);
      58             : 
      59             :     /**
      60             :      * Check if the template XML file exits, without trying to load it.
      61             :      */
      62             :     bool TemplateExists(const std::string& templateName) const;
      63             : 
      64             :     /**
      65             :      * Returns a list of strings that could be validly passed as @c templateName to LoadTemplateFile.
      66             :      * (This includes "actor|foo" etc names).
      67             :      */
      68             :     std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType) const;
      69             : 
      70             :     /**
      71             :      * Returns a list of strings that could validly be passed as @c templateName to LoadTemplateFile.
      72             :      * Only returns templates with the specified root name.
      73             :      */
      74             :     std::vector<std::string> FindTemplatesWithRootName(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType, const std::string& rootName);
      75             : 
      76             : private:
      77             :     /**
      78             :      * (Re)loads the given template, regardless of whether it exists already,
      79             :      * and saves into m_TemplateFileData. Also loads any parents that are not yet
      80             :      * loaded. Returns false on error.
      81             :      * @param templateName - XML filename to load (may be a |-separated string)
      82             :      * @param compositing - whether this template is an intermediary layer in a |-separated string.
      83             :      * @param depth - the current recursion depth.
      84             :      */
      85             :     bool LoadTemplateFile(CParamNode& node, std::string_view templateName, bool compositing, int depth);
      86             : 
      87             :     /**
      88             :      * Constructs a standard static-decorative-object template for the given actor
      89             :      */
      90             :     void ConstructTemplateActor(std::string_view actorName, CParamNode& out);
      91             : 
      92             :     /**
      93             :      * Map from template name (XML filename or special |-separated string) to the most recently
      94             :      * loaded non-broken template data. This includes files that will fail schema validation.
      95             :      * (Failed loads won't remove existing entries under the same name, so we behave more nicely
      96             :      * when hotloading broken files)
      97             :      */
      98             :     std::unordered_map<std::string, CParamNode> m_TemplateFileData;
      99             : };
     100             : 
     101             : #endif // INCLUDED_TEMPLATELOADER

Generated by: LCOV version 1.13