Changeset View
Changeset View
Standalone View
Standalone View
source/ps/TemplateLoader.h
Show All 12 Lines | |||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#ifndef INCLUDED_TEMPLATELOADER | #ifndef INCLUDED_TEMPLATELOADER | ||||
#define INCLUDED_TEMPLATELOADER | #define INCLUDED_TEMPLATELOADER | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "scriptinterface/ScriptInterface.h" | |||||
#include "simulation2/system/ParamNode.h" | #include "simulation2/system/ParamNode.h" | ||||
#include <string_view> | #include <string_view> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
class ScriptRequest; | |||||
class CTemplateLoader; | |||||
template <class T> | |||||
CTemplateLoader* TemplateLoaderGetter(const ScriptRequest& rq, JS::CallArgs& ca) | |||||
{ | |||||
T* instance = rq.GetScriptInterface().ObjectFromCBData<T>(rq, ca); | |||||
return &(instance->GetTemplateLoader()); | |||||
} | |||||
enum ETemplatesType | enum ETemplatesType | ||||
{ | { | ||||
ALL_TEMPLATES, | ALL_TEMPLATES, | ||||
ACTOR_TEMPLATES, | ACTOR_TEMPLATES, | ||||
SIMULATION_TEMPLATES | SIMULATION_TEMPLATES | ||||
}; | }; | ||||
/** | /** | ||||
* Template loader: Handles the loading of entity template files for: | * Template loader: Handles the loading of entity template files for: | ||||
* - the initialisation and deserialization of entity components in the | * - the initialisation and deserialization of entity components in the | ||||
* simulation (CmpTemplateManager). | * simulation (CmpTemplateManager). | ||||
* - access to actor templates, obstruction data, etc. in RMS/RMGEN | * - access to actor templates, obstruction data, etc. in RMS/RMGEN | ||||
* - access to various templates in the GUI, to display faction specificities | * - access to various templates in the GUI, to display faction specificities | ||||
* | * | ||||
* Template names are intentionally restricted to ASCII strings for storage/serialization | * Template names are intentionally restricted to ASCII strings for storage/serialization | ||||
* efficiency (we have a lot of strings so this is significant); | * efficiency (we have a lot of strings so this is significant); | ||||
* they correspond to filenames so they shouldn't contain non-ASCII anyway. | * they correspond to filenames so they shouldn't contain non-ASCII anyway. | ||||
* | * | ||||
* | * | ||||
* TODO: Find a way to validate templates outside of the simulation. | * TODO: Find a way to validate templates outside of the simulation. | ||||
*/ | */ | ||||
class CTemplateLoader | class CTemplateLoader | ||||
{ | { | ||||
public: | public: | ||||
CTemplateLoader() | CTemplateLoader() = default; | ||||
/** | |||||
* Helpers for providing JS interfaces. | |||||
*/ | |||||
const CParamNode& Script_GetTemplate(const std::string& templateName); | |||||
bool Script_TemplateExists(const std::string& templateName) const; | |||||
std::vector<std::string> Script_FindTemplates(const std::string& path, bool includeSubdirectories) const; | |||||
std::vector<std::string> Script_FindActorTemplates(const std::string& path, bool includeSubdirectories) const; | |||||
template <class T> | |||||
void RegisterScriptFunctions(const ScriptRequest& rq) const | |||||
{ | { | ||||
ScriptFunction::Register<&CTemplateLoader::Script_GetTemplate, &TemplateLoaderGetter<T>>(rq, "GetTemplate"); | |||||
ScriptFunction::Register<&CTemplateLoader::Script_TemplateExists, &TemplateLoaderGetter<T>>(rq, "TemplateExists"); | |||||
ScriptFunction::Register<&CTemplateLoader::Script_FindTemplates, &TemplateLoaderGetter<T>>(rq, "FindTemplates"); | |||||
ScriptFunction::Register<&CTemplateLoader::Script_FindActorTemplates, &TemplateLoaderGetter<T>>(rq, "FindActorTemplates"); | |||||
} | } | ||||
/** | /** | ||||
* Provides the file data for requested template. | * Provides the file data for requested template. | ||||
*/ | */ | ||||
const CParamNode& GetTemplateFileData(const std::string& templateName); | const CParamNode& GetTemplateFileData(const std::string& templateName); | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator