2024-12-30 02:31:07 -08:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 11:20:14 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 11:20:14 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 11:20:14 -08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 11:20:14 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ComponentManager.h"
|
|
|
|
|
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "graphics/HeightMipmap.h"
|
2011-02-17 12:08:20 -08:00
|
|
|
#include "lib/utf8.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "ps/CLogger.h"
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "ps/CStr.h"
|
|
|
|
|
#include "ps/Errors.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "ps/Filesystem.h"
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "ps/Profiler2.h"
|
|
|
|
|
#include "ps/algorithm.h"
|
2017-12-03 15:02:27 -08:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2021-03-01 12:52:24 -08:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 13:12:16 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
2019-11-25 06:30:25 -08:00
|
|
|
#include "simulation2/MessageTypes.h"
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "simulation2/components/ICmpTemplateManager.h"
|
|
|
|
|
#include "simulation2/helpers/Player.h"
|
2019-11-25 06:30:25 -08:00
|
|
|
#include "simulation2/system/DynamicSubscription.h"
|
2025-08-03 10:37:17 -07:00
|
|
|
#include "simulation2/system/Message.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2025-08-03 10:37:17 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <js/GCAPI.h>
|
|
|
|
|
#include <js/TracingAPI.h>
|
|
|
|
|
#include <js/ValueArray.h>
|
2025-06-17 23:16:42 -07:00
|
|
|
#include <stdexcept>
|
2022-12-04 11:56:12 -08:00
|
|
|
#include <string_view>
|
|
|
|
|
|
2025-08-03 10:37:17 -07:00
|
|
|
class ScriptContext;
|
|
|
|
|
|
2010-07-18 08:19:49 -07:00
|
|
|
/**
|
|
|
|
|
* Used for script-only message types.
|
|
|
|
|
*/
|
2022-03-03 14:42:26 -08:00
|
|
|
class CMessageScripted final : public CMessage
|
2010-07-18 08:19:49 -07:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual int GetType() const { return mtid; }
|
|
|
|
|
virtual const char* GetScriptHandlerName() const { return handlerName.c_str(); }
|
|
|
|
|
virtual const char* GetScriptGlobalHandlerName() const { return globalHandlerName.c_str(); }
|
2025-06-03 23:10:15 -07:00
|
|
|
virtual JS::Value ToJSVal(const ScriptRequest&) const { return msg.get(); }
|
2010-07-18 08:19:49 -07:00
|
|
|
|
2023-06-21 00:50:00 -07:00
|
|
|
CMessageScripted(const ScriptRequest& rq, int mtid, const std::string& name, JS::HandleValue msg) :
|
|
|
|
|
mtid(mtid), handlerName("On" + name), globalHandlerName("OnGlobal" + name), msg(rq.cx, msg)
|
2010-07-18 08:19:49 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int mtid;
|
|
|
|
|
std::string handlerName;
|
|
|
|
|
std::string globalHandlerName;
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::PersistentRootedValue msg;
|
2010-07-18 08:19:49 -07:00
|
|
|
};
|
|
|
|
|
|
2023-11-19 12:13:19 -08:00
|
|
|
CComponentManager::CComponentManager(CSimContext& context, ScriptContext& cx, bool skipScriptFunctions) :
|
2011-01-15 15:35:20 -08:00
|
|
|
m_NextScriptComponentTypeId(CID__LastNative),
|
2020-11-18 06:39:04 -08:00
|
|
|
m_ScriptInterface("Engine", "Simulation", cx),
|
2010-08-02 12:23:58 -07:00
|
|
|
m_SimContext(context), m_CurrentlyHotloading(false)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-04-14 10:22:32 -07:00
|
|
|
context.SetComponentManager(this);
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ScriptInterface.SetCallbackData(static_cast<void*> (this));
|
2012-07-02 19:16:45 -07:00
|
|
|
m_ScriptInterface.ReplaceNondeterministicRNG(m_RNG);
|
2010-05-21 17:38:33 -07:00
|
|
|
|
2025-01-19 06:19:25 -08:00
|
|
|
JS_AddExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, (void*)this);
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
// For component script tests, the test system sets up its own scripted implementation of
|
|
|
|
|
// these functions, so we skip registering them here in those cases
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!skipScriptFunctions)
|
|
|
|
|
{
|
2022-05-14 23:34:17 -07:00
|
|
|
JSI_VFS::RegisterScriptFunctions_ReadOnlySimulation(m_ScriptInterface);
|
2021-03-01 12:52:24 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2021-05-15 06:54:58 -07:00
|
|
|
constexpr ScriptFunction::ObjectGetter<CComponentManager> Getter = &ScriptInterface::ObjectFromCBData<CComponentManager>;
|
2021-03-01 12:52:24 -08:00
|
|
|
ScriptFunction::Register<&CComponentManager::Script_RegisterComponentType, Getter>(rq, "RegisterComponentType");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_RegisterSystemComponentType, Getter>(rq, "RegisterSystemComponentType");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_ReRegisterComponentType, Getter>(rq, "ReRegisterComponentType");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_RegisterInterface, Getter>(rq, "RegisterInterface");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_RegisterMessageType, Getter>(rq, "RegisterMessageType");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_RegisterGlobal, Getter>(rq, "RegisterGlobal");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_GetEntitiesWithInterface, Getter>(rq, "GetEntitiesWithInterface");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_GetComponentsWithInterface, Getter>(rq, "GetComponentsWithInterface");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_PostMessage, Getter>(rq, "PostMessage");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_BroadcastMessage, Getter>(rq, "BroadcastMessage");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_AddEntity, Getter>(rq, "AddEntity");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::Script_AddLocalEntity, Getter>(rq, "AddLocalEntity");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::QueryInterface, Getter>(rq, "QueryInterface");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::DestroyComponentsSoon, Getter>(rq, "DestroyEntity");
|
|
|
|
|
ScriptFunction::Register<&CComponentManager::FlushDestroyedComponents, Getter>(rq, "FlushDestroyedEntities");
|
2022-05-09 11:13:34 -07:00
|
|
|
ScriptFunction::Register<&CComponentManager::Script_GetTemplate, Getter>(rq, "GetTemplate");
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2017-12-30 17:02:21 -08:00
|
|
|
// Globalscripts may use VFS script functions
|
|
|
|
|
m_ScriptInterface.LoadGlobalScripts();
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
// Define MT_*, IID_* as script globals, and store their names
|
2010-01-09 11:20:14 -08:00
|
|
|
#define MESSAGE(name) m_ScriptInterface.SetGlobal("MT_" #name, (int)MT_##name);
|
2010-01-22 12:03:14 -08:00
|
|
|
#define INTERFACE(name) \
|
|
|
|
|
m_ScriptInterface.SetGlobal("IID_" #name, (int)IID_##name); \
|
|
|
|
|
m_InterfaceIdsByName[#name] = IID_##name;
|
2010-01-09 11:20:14 -08:00
|
|
|
#define COMPONENT(name)
|
|
|
|
|
#include "simulation2/TypeList.h"
|
|
|
|
|
#undef MESSAGE
|
|
|
|
|
#undef INTERFACE
|
|
|
|
|
#undef COMPONENT
|
|
|
|
|
|
2010-09-03 02:55:14 -07:00
|
|
|
m_ScriptInterface.SetGlobal("INVALID_ENTITY", (int)INVALID_ENTITY);
|
2018-01-21 17:02:29 -08:00
|
|
|
m_ScriptInterface.SetGlobal("INVALID_PLAYER", (int)INVALID_PLAYER);
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ScriptInterface.SetGlobal("SYSTEM_ENTITY", (int)SYSTEM_ENTITY);
|
2010-01-14 12:36:29 -08:00
|
|
|
|
2011-03-02 16:16:14 -08:00
|
|
|
m_ComponentsByInterface.resize(IID__LastNative);
|
|
|
|
|
|
2010-01-14 12:36:29 -08:00
|
|
|
ResetState();
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComponentManager::~CComponentManager()
|
|
|
|
|
{
|
2025-01-19 06:19:25 -08:00
|
|
|
JS_RemoveExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, (void*)this);
|
2010-01-14 12:36:29 -08:00
|
|
|
ResetState();
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
void CComponentManager::LoadComponentTypes()
|
|
|
|
|
{
|
2010-01-09 11:20:14 -08:00
|
|
|
#define MESSAGE(name) \
|
|
|
|
|
RegisterMessageType(MT_##name, #name);
|
|
|
|
|
#define INTERFACE(name) \
|
|
|
|
|
extern void RegisterComponentInterface_##name(ScriptInterface&); \
|
|
|
|
|
RegisterComponentInterface_##name(m_ScriptInterface);
|
|
|
|
|
#define COMPONENT(name) \
|
|
|
|
|
extern void RegisterComponentType_##name(CComponentManager&); \
|
|
|
|
|
m_CurrentComponent = CID_##name; \
|
|
|
|
|
RegisterComponentType_##name(*this);
|
|
|
|
|
|
|
|
|
|
#include "simulation2/TypeList.h"
|
2010-01-22 12:03:14 -08:00
|
|
|
|
|
|
|
|
m_CurrentComponent = CID__Invalid;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
#undef MESSAGE
|
|
|
|
|
#undef INTERFACE
|
|
|
|
|
#undef COMPONENT
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
bool CComponentManager::LoadScript(const VfsPath& filename, bool hotload)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
m_CurrentlyHotloading = hotload;
|
|
|
|
|
CVFSFile file;
|
2010-07-04 03:15:53 -07:00
|
|
|
PSRETURN loadOk = file.Load(g_VFS, filename);
|
2014-06-02 08:48:37 -07:00
|
|
|
if (loadOk != PSRETURN_OK) // VFS will log the failed file and the reason
|
2014-06-02 08:41:41 -07:00
|
|
|
return false;
|
2012-05-04 14:48:46 -07:00
|
|
|
std::string content = file.DecodeUTF8(); // assume it's UTF-8
|
2010-01-09 11:20:14 -08:00
|
|
|
bool ok = m_ScriptInterface.LoadScript(filename, content);
|
2019-07-19 14:58:58 -07:00
|
|
|
|
|
|
|
|
m_CurrentlyHotloading = false;
|
2010-01-09 11:20:14 -08:00
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterComponentType_Common(int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// Find the C++ component that wraps the interface
|
2021-03-01 12:52:24 -08:00
|
|
|
int cidWrapper = GetScriptWrapper(iid);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (cidWrapper == CID__Invalid)
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::invalid_argument{"Invalid interface id"};
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
const ComponentType& ctWrapper = m_ComponentTypesById[cidWrapper];
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
bool mustReloadComponents = false; // for hotloading
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
ComponentTypeId cid = LookupCID(cname);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (cid == CID__Invalid)
|
|
|
|
|
{
|
2014-05-18 01:20:02 -07:00
|
|
|
if (reRegister)
|
|
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{fmt::format(
|
|
|
|
|
"ReRegistering component type that was not registered before '{}'", cname.c_str())};
|
2014-05-18 01:20:02 -07:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
// Allocate a new cid number
|
2021-03-01 12:52:24 -08:00
|
|
|
cid = m_NextScriptComponentTypeId++;
|
|
|
|
|
m_ComponentTypeIdsByName[cname] = cid;
|
2014-05-18 06:44:08 -07:00
|
|
|
if (systemComponent)
|
2021-03-01 12:52:24 -08:00
|
|
|
MarkScriptedComponentForSystemEntity(cid);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
// Component type is already loaded, so do hotloading:
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
if (!m_CurrentlyHotloading && !reRegister)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{fmt::format(
|
|
|
|
|
"Registering component type with already-registered name '{}'", cname.c_str())};
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
const ComponentType& ctPrevious = m_ComponentTypesById[cid];
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// We can only replace scripted component types, not native ones
|
|
|
|
|
if (ctPrevious.type != CT_Script)
|
|
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{fmt::format(
|
|
|
|
|
"Loading script component type with same name '%s' as native component",
|
|
|
|
|
cname.c_str())};
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We don't support changing the IID of a component type (it would require fiddling
|
|
|
|
|
// around with m_ComponentsByInterface and being careful to guarantee uniqueness per entity)
|
2010-01-24 09:24:35 -08:00
|
|
|
if (ctPrevious.iid != iid)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// ...though it only matters if any components exist with this type
|
2021-03-01 12:52:24 -08:00
|
|
|
if (!m_ComponentsByTypeId[cid].empty())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{
|
|
|
|
|
"Hotloading script component type mustn't change interface ID"};
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-16 15:00:52 -08:00
|
|
|
// Remove the old component type's message subscriptions
|
2010-02-05 13:40:08 -08:00
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> >::iterator it;
|
2021-03-01 12:52:24 -08:00
|
|
|
for (it = m_LocalMessageSubscriptions.begin(); it != m_LocalMessageSubscriptions.end(); ++it)
|
2010-02-05 13:40:08 -08:00
|
|
|
{
|
|
|
|
|
std::vector<ComponentTypeId>& types = it->second;
|
|
|
|
|
std::vector<ComponentTypeId>::iterator ctit = find(types.begin(), types.end(), cid);
|
|
|
|
|
if (ctit != types.end())
|
|
|
|
|
types.erase(ctit);
|
|
|
|
|
}
|
2021-03-01 12:52:24 -08:00
|
|
|
for (it = m_GlobalMessageSubscriptions.begin(); it != m_GlobalMessageSubscriptions.end(); ++it)
|
2010-02-05 13:40:08 -08:00
|
|
|
{
|
|
|
|
|
std::vector<ComponentTypeId>& types = it->second;
|
|
|
|
|
std::vector<ComponentTypeId>::iterator ctit = find(types.begin(), types.end(), cid);
|
|
|
|
|
if (ctit != types.end())
|
|
|
|
|
types.erase(ctit);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
mustReloadComponents = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue protoVal(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
if (!Script::GetProperty(rq, ctor, "prototype", &protoVal))
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::runtime_error("Failed to get property 'prototype'");
|
2019-07-14 07:13:15 -07:00
|
|
|
if (!protoVal.isObject())
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::invalid_argument{"Component has no constructor"};
|
|
|
|
|
|
2019-07-14 07:13:15 -07:00
|
|
|
std::string schema = "<empty/>";
|
|
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
if (Script::HasProperty(rq, protoVal, "Schema"))
|
|
|
|
|
Script::GetProperty(rq, protoVal, "Schema", schema);
|
2010-04-09 12:02:39 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
// Construct a new ComponentType, using the wrapper's alloc functions
|
2020-01-12 12:04:22 -08:00
|
|
|
ComponentType ct{
|
2010-11-16 15:00:52 -08:00
|
|
|
CT_Script,
|
|
|
|
|
iid,
|
|
|
|
|
ctWrapper.alloc,
|
|
|
|
|
ctWrapper.dealloc,
|
|
|
|
|
cname,
|
|
|
|
|
schema,
|
2021-02-13 15:53:40 -08:00
|
|
|
std::make_unique<JS::PersistentRootedValue>(rq.cx, ctor)
|
2020-01-12 12:04:22 -08:00
|
|
|
};
|
2021-03-01 12:52:24 -08:00
|
|
|
m_ComponentTypesById[cid] = std::move(ct);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
m_CurrentComponent = cid; // needed by Subscribe
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2024-12-30 02:31:07 -08:00
|
|
|
// Only now that we have constructed the CT_Script for this CT_ScriptWrapper, we can subscribe the CT_Script
|
|
|
|
|
ctWrapper.classInit(*this);
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
// Find all the ctor prototype's On* methods, and subscribe to the appropriate messages:
|
|
|
|
|
std::vector<std::string> methods;
|
2016-08-02 09:12:11 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
if (!Script::EnumeratePropertyNames(rq, protoVal, false, methods))
|
2019-07-14 07:13:15 -07:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::runtime_error{"Failed to enumerate component properties."};
|
2019-07-14 07:13:15 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2022-12-04 11:56:12 -08:00
|
|
|
for (const std::string& method : methods)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2022-12-04 11:56:12 -08:00
|
|
|
if (std::string_view{method}.substr(0, 2) != "On")
|
2020-06-14 02:49:32 -07:00
|
|
|
continue;
|
|
|
|
|
|
2022-12-04 11:56:12 -08:00
|
|
|
std::string_view name{std::string_view{method}.substr(2)}; // strip the "On" prefix
|
2010-01-22 12:03:14 -08:00
|
|
|
|
|
|
|
|
// Handle "OnGlobalFoo" functions specially
|
|
|
|
|
bool isGlobal = false;
|
2022-12-04 11:56:12 -08:00
|
|
|
if (std::string_view{name}.substr(0, 6) == "Global")
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
isGlobal = true;
|
2022-12-04 11:56:12 -08:00
|
|
|
name.remove_prefix(6);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 11:56:12 -08:00
|
|
|
auto mit = m_MessageTypeIdsByName.find(std::string{name});
|
2021-03-01 12:52:24 -08:00
|
|
|
if (mit == m_MessageTypeIdsByName.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::invalid_argument{fmt::format(
|
|
|
|
|
"Registered component has unrecognized '{}' message handler method",
|
|
|
|
|
method.c_str())};
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2024-12-30 03:14:34 -08:00
|
|
|
// If we have already subscribed in classInit, do not subscribe again
|
2010-01-22 12:03:14 -08:00
|
|
|
if (isGlobal)
|
2024-12-30 03:14:34 -08:00
|
|
|
{
|
|
|
|
|
if (!IsGloballySubscribed(mit->second))
|
|
|
|
|
SubscribeGloballyToMessageType(mit->second);
|
|
|
|
|
}
|
2010-01-22 12:03:14 -08:00
|
|
|
else
|
2024-12-30 03:14:34 -08:00
|
|
|
{
|
|
|
|
|
if (!IsLocallySubscribed(mit->second))
|
|
|
|
|
SubscribeToMessageType(mit->second);
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
m_CurrentComponent = CID__Invalid;
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
if (mustReloadComponents)
|
|
|
|
|
{
|
|
|
|
|
// For every script component with this cid, we need to switch its
|
|
|
|
|
// prototype from the old constructor's prototype property to the new one's
|
2021-03-01 12:52:24 -08:00
|
|
|
const std::map<entity_id_t, IComponent*>& comps = m_ComponentsByTypeId[cid];
|
2010-01-09 11:20:14 -08:00
|
|
|
std::map<entity_id_t, IComponent*>::const_iterator eit = comps.begin();
|
|
|
|
|
for (; eit != comps.end(); ++eit)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue instance(rq.cx, eit->second->GetJSInstance());
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!instance.isNull())
|
2021-03-01 12:52:24 -08:00
|
|
|
m_ScriptInterface.SetPrototype(instance, protoVal);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor)
|
2014-05-18 01:20:02 -07:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
Script_RegisterComponentType_Common(iid, cname, ctor, false, false);
|
|
|
|
|
m_ScriptInterface.SetGlobal(cname.c_str(), ctor, m_CurrentlyHotloading);
|
2014-05-18 06:44:08 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterSystemComponentType(int iid, const std::string& cname, JS::HandleValue ctor)
|
2014-05-18 06:44:08 -07:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
Script_RegisterComponentType_Common(iid, cname, ctor, false, true);
|
|
|
|
|
m_ScriptInterface.SetGlobal(cname.c_str(), ctor, m_CurrentlyHotloading);
|
2014-05-18 01:20:02 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_ReRegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor)
|
2014-05-18 01:20:02 -07:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
Script_RegisterComponentType_Common(iid, cname, ctor, true, false);
|
2014-05-18 01:20:02 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterInterface(const std::string& name)
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
std::map<std::string, InterfaceId>::iterator it = m_InterfaceIdsByName.find(name);
|
|
|
|
|
if (it != m_InterfaceIdsByName.end())
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
2010-01-24 09:24:35 -08:00
|
|
|
// Redefinitions are fine (and just get ignored) when hotloading; otherwise
|
|
|
|
|
// they're probably unintentional and should be reported
|
2021-03-01 12:52:24 -08:00
|
|
|
if (!m_CurrentlyHotloading)
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{fmt::format(
|
|
|
|
|
"Registering interface with already-registered name '{}'", name.c_str())};
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
}
|
2010-01-22 12:03:14 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IIDs start at 1, so size+1 is the next unused one
|
2021-03-01 12:52:24 -08:00
|
|
|
size_t id = m_InterfaceIdsByName.size() + 1;
|
|
|
|
|
m_InterfaceIdsByName[name] = (InterfaceId)id;
|
|
|
|
|
m_ComponentsByInterface.resize(id+1); // add one so we can index by InterfaceId
|
|
|
|
|
m_ScriptInterface.SetGlobal(("IID_" + name).c_str(), (int)id);
|
2010-07-18 08:19:49 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterMessageType(const std::string& name)
|
2010-07-18 08:19:49 -07:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
std::map<std::string, MessageTypeId>::iterator it = m_MessageTypeIdsByName.find(name);
|
|
|
|
|
if (it != m_MessageTypeIdsByName.end())
|
2010-07-18 08:19:49 -07:00
|
|
|
{
|
|
|
|
|
// Redefinitions are fine (and just get ignored) when hotloading; otherwise
|
|
|
|
|
// they're probably unintentional and should be reported
|
2021-03-01 12:52:24 -08:00
|
|
|
if (!m_CurrentlyHotloading)
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
{
|
2025-06-17 23:16:42 -07:00
|
|
|
throw std::logic_error{fmt::format(
|
|
|
|
|
"Registering message type with already-registered name '{}'", name.c_str())};
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
}
|
2010-07-18 08:19:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MTIDs start at 1, so size+1 is the next unused one
|
2021-03-01 12:52:24 -08:00
|
|
|
size_t id = m_MessageTypeIdsByName.size() + 1;
|
|
|
|
|
RegisterMessageType((MessageTypeId)id, name.c_str());
|
|
|
|
|
m_ScriptInterface.SetGlobal(("MT_" + name).c_str(), (int)id);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_RegisterGlobal(const std::string& name, JS::HandleValue value)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
m_ScriptInterface.SetGlobal(name.c_str(), value, m_CurrentlyHotloading);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 11:13:34 -07:00
|
|
|
const CParamNode& CComponentManager::Script_GetTemplate(const std::string& templateName)
|
|
|
|
|
{
|
|
|
|
|
static CParamNode nullNode(false);
|
|
|
|
|
|
|
|
|
|
ICmpTemplateManager* cmpTemplateManager = static_cast<ICmpTemplateManager*> (QueryInterface(SYSTEM_ENTITY, IID_TemplateManager));
|
|
|
|
|
if (!cmpTemplateManager)
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("Template manager is not loaded");
|
|
|
|
|
return nullNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CParamNode* tmpl = cmpTemplateManager->GetTemplate(templateName);
|
|
|
|
|
if (!tmpl)
|
|
|
|
|
return nullNode;
|
|
|
|
|
return *tmpl;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
std::vector<int> CComponentManager::Script_GetEntitiesWithInterface(int iid)
|
2010-08-04 14:15:41 -07:00
|
|
|
{
|
|
|
|
|
std::vector<int> ret;
|
2021-03-01 12:52:24 -08:00
|
|
|
const InterfaceListUnordered& ents = GetEntitiesWithInterfaceUnordered(iid);
|
2011-03-02 16:16:14 -08:00
|
|
|
for (InterfaceListUnordered::const_iterator it = ents.begin(); it != ents.end(); ++it)
|
2013-09-10 05:27:59 -07:00
|
|
|
if (!ENTITY_IS_LOCAL(it->first))
|
|
|
|
|
ret.push_back(it->first);
|
2011-03-02 16:16:14 -08:00
|
|
|
std::sort(ret.begin(), ret.end());
|
2011-01-15 15:35:20 -08:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
std::vector<IComponent*> CComponentManager::Script_GetComponentsWithInterface(int iid)
|
2011-01-15 15:35:20 -08:00
|
|
|
{
|
|
|
|
|
std::vector<IComponent*> ret;
|
2021-03-01 12:52:24 -08:00
|
|
|
InterfaceList ents = GetEntitiesWithInterface(iid);
|
2011-03-02 16:16:14 -08:00
|
|
|
for (InterfaceList::const_iterator it = ents.begin(); it != ents.end(); ++it)
|
2011-01-15 15:35:20 -08:00
|
|
|
ret.push_back(it->second); // TODO: maybe we should exclude local entities
|
2010-08-04 14:15:41 -07:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-09 13:02:49 -07:00
|
|
|
CMessage* CComponentManager::ConstructMessage(int mtid, JS::HandleValue data)
|
2010-07-18 08:19:49 -07:00
|
|
|
{
|
|
|
|
|
if (mtid == MT__Invalid || mtid > (int)m_MessageTypeIdsByName.size()) // (IDs start at 1 so use '>' here)
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("PostMessage with invalid message type ID '%d'", mtid);
|
2010-07-18 08:19:49 -07:00
|
|
|
|
2023-06-21 00:50:00 -07:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2010-07-18 08:19:49 -07:00
|
|
|
if (mtid < MT__LastNative)
|
|
|
|
|
{
|
2023-06-21 00:50:00 -07:00
|
|
|
return CMessageFromJSVal(mtid, rq, data);
|
2010-07-18 08:19:49 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-21 00:50:00 -07:00
|
|
|
return new CMessageScripted(rq, mtid, m_MessageTypeNamesById[mtid], data);
|
2010-07-18 08:19:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_PostMessage(int ent, int mtid, JS::HandleValue data)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
CMessage* msg = ConstructMessage(mtid, data);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!msg)
|
|
|
|
|
return; // error
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
PostMessage(ent, *msg);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
void CComponentManager::Script_BroadcastMessage(int mtid, JS::HandleValue data)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-03-01 12:52:24 -08:00
|
|
|
CMessage* msg = ConstructMessage(mtid, data);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!msg)
|
|
|
|
|
return; // error
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
BroadcastMessage(*msg);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
int CComponentManager::Script_AddEntity(const std::wstring& templateName)
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
// TODO: should validate the string to make sure it doesn't contain scary characters
|
|
|
|
|
// that will let it access non-component-template files
|
2021-03-01 12:52:24 -08:00
|
|
|
return AddEntity(templateName, AllocateNewEntity());
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 12:52:24 -08:00
|
|
|
int CComponentManager::Script_AddLocalEntity(const std::wstring& templateName)
|
2010-01-24 09:24:35 -08:00
|
|
|
{
|
|
|
|
|
// TODO: should validate the string to make sure it doesn't contain scary characters
|
|
|
|
|
// that will let it access non-component-template files
|
2021-03-01 12:52:24 -08:00
|
|
|
return AddEntity(templateName, AllocateNewLocalEntity());
|
2014-12-31 01:31:41 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-14 12:36:29 -08:00
|
|
|
void CComponentManager::ResetState()
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-08-15 18:23:50 -07:00
|
|
|
// Delete all dynamic message subscriptions
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsync.clear();
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsyncByComponent.clear();
|
|
|
|
|
|
2025-01-19 06:19:25 -08:00
|
|
|
// Remove all items we were tracing.
|
|
|
|
|
m_TraceCache.clear();
|
|
|
|
|
|
2021-04-13 06:47:59 -07:00
|
|
|
// Delete all IComponents in reverse order of creation.
|
|
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::reverse_iterator iit = m_ComponentsByTypeId.rbegin();
|
|
|
|
|
for (; iit != m_ComponentsByTypeId.rend(); ++iit)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
std::map<entity_id_t, IComponent*>::iterator eit = iit->second.begin();
|
|
|
|
|
for (; eit != iit->second.end(); ++eit)
|
|
|
|
|
{
|
2011-01-16 06:08:38 -08:00
|
|
|
eit->second->Deinit();
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ComponentTypesById[iit->first].dealloc(eit->second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 06:30:25 -08:00
|
|
|
std::vector<std::unordered_map<entity_id_t, IComponent*> >::iterator ifcit = m_ComponentsByInterface.begin();
|
2011-03-02 16:16:14 -08:00
|
|
|
for (; ifcit != m_ComponentsByInterface.end(); ++ifcit)
|
|
|
|
|
ifcit->clear();
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ComponentsByTypeId.clear();
|
2010-01-14 12:36:29 -08:00
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
// Delete all SEntityComponentCaches
|
2017-02-22 11:27:58 -08:00
|
|
|
std::unordered_map<entity_id_t, SEntityComponentCache*>::iterator ccit = m_ComponentCaches.begin();
|
2013-09-11 13:41:53 -07:00
|
|
|
for (; ccit != m_ComponentCaches.end(); ++ccit)
|
|
|
|
|
free(ccit->second);
|
|
|
|
|
m_ComponentCaches.clear();
|
|
|
|
|
m_SystemEntity = CEntityHandle();
|
|
|
|
|
|
2010-01-14 12:36:29 -08:00
|
|
|
m_DestructionQueue.clear();
|
|
|
|
|
|
|
|
|
|
// Reset IDs
|
|
|
|
|
m_NextEntityId = SYSTEM_ENTITY + 1;
|
|
|
|
|
m_NextLocalEntityId = FIRST_LOCAL_ENTITY;
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-14 09:28:54 -07:00
|
|
|
void CComponentManager::SetRNGSeed(u32 seed)
|
|
|
|
|
{
|
|
|
|
|
m_RNG.seed(seed);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
void CComponentManager::RegisterComponentType(InterfaceId iid, ComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc,
|
2010-04-09 12:02:39 -07:00
|
|
|
const char* name, const std::string& schema)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2020-11-12 00:24:30 -08:00
|
|
|
ComponentType c{ CT_Native, iid, alloc, dealloc, name, schema, std::unique_ptr<JS::PersistentRootedValue>() };
|
2015-08-18 20:32:47 -07:00
|
|
|
m_ComponentTypesById.insert(std::make_pair(cid, std::move(c)));
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ComponentTypeIdsByName[name] = cid;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-30 02:31:07 -08:00
|
|
|
void CComponentManager::RegisterComponentTypeScriptWrapper(InterfaceId iid, ComponentTypeId cid,
|
|
|
|
|
AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema,
|
|
|
|
|
ClassInitFunc classInit)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2024-12-30 02:31:07 -08:00
|
|
|
ComponentType c{ CT_ScriptWrapper, iid, alloc, dealloc, name, schema,
|
|
|
|
|
std::unique_ptr<JS::PersistentRootedValue>(), classInit };
|
2015-08-18 20:32:47 -07:00
|
|
|
m_ComponentTypesById.insert(std::make_pair(cid, std::move(c)));
|
2010-01-09 11:20:14 -08:00
|
|
|
m_ComponentTypeIdsByName[name] = cid;
|
|
|
|
|
// TODO: merge with RegisterComponentType
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 06:44:08 -07:00
|
|
|
void CComponentManager::MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid)
|
|
|
|
|
{
|
|
|
|
|
m_ScriptedSystemComponents.push_back(cid);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
void CComponentManager::RegisterMessageType(MessageTypeId mtid, const char* name)
|
|
|
|
|
{
|
|
|
|
|
m_MessageTypeIdsByName[name] = mtid;
|
2010-07-18 08:19:49 -07:00
|
|
|
m_MessageTypeNamesById[mtid] = name;
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
void CComponentManager::SubscribeToMessageType(MessageTypeId mtid)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// TODO: verify mtid
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(m_CurrentComponent != CID__Invalid);
|
2010-01-22 12:03:14 -08:00
|
|
|
std::vector<ComponentTypeId>& types = m_LocalMessageSubscriptions[mtid];
|
|
|
|
|
types.push_back(m_CurrentComponent);
|
|
|
|
|
std::sort(types.begin(), types.end()); // TODO: just sort once at the end of LoadComponents
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::SubscribeGloballyToMessageType(MessageTypeId mtid)
|
|
|
|
|
{
|
|
|
|
|
// TODO: verify mtid
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(m_CurrentComponent != CID__Invalid);
|
2010-01-22 12:03:14 -08:00
|
|
|
std::vector<ComponentTypeId>& types = m_GlobalMessageSubscriptions[mtid];
|
|
|
|
|
types.push_back(m_CurrentComponent);
|
2010-01-09 11:20:14 -08:00
|
|
|
std::sort(types.begin(), types.end()); // TODO: just sort once at the end of LoadComponents
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-30 03:14:34 -08:00
|
|
|
bool CComponentManager::IsLocallySubscribed(MessageTypeId mtid)
|
|
|
|
|
{
|
|
|
|
|
ENSURE(m_CurrentComponent != CID__Invalid);
|
|
|
|
|
return PS::contains(m_LocalMessageSubscriptions[mtid], m_CurrentComponent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CComponentManager::IsGloballySubscribed(MessageTypeId mtid)
|
|
|
|
|
{
|
|
|
|
|
ENSURE(m_CurrentComponent != CID__Invalid);
|
|
|
|
|
return PS::contains(m_GlobalMessageSubscriptions[mtid], m_CurrentComponent);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 16:20:12 -07:00
|
|
|
void CComponentManager::FlattenDynamicSubscriptions()
|
|
|
|
|
{
|
|
|
|
|
std::map<MessageTypeId, CDynamicSubscription>::iterator it;
|
|
|
|
|
for (it = m_DynamicMessageSubscriptionsNonsync.begin();
|
|
|
|
|
it != m_DynamicMessageSubscriptionsNonsync.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
it->second.Flatten();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::DynamicSubscriptionNonsync(MessageTypeId mtid, IComponent* component, bool enable)
|
|
|
|
|
{
|
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
bool newlyInserted = m_DynamicMessageSubscriptionsNonsyncByComponent[component].insert(mtid).second;
|
|
|
|
|
if (newlyInserted)
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsync[mtid].Add(component);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
size_t numRemoved = m_DynamicMessageSubscriptionsNonsyncByComponent[component].erase(mtid);
|
|
|
|
|
if (numRemoved)
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsync[mtid].Remove(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::RemoveComponentDynamicSubscriptions(IComponent* component)
|
|
|
|
|
{
|
|
|
|
|
std::map<IComponent*, std::set<MessageTypeId> >::iterator it = m_DynamicMessageSubscriptionsNonsyncByComponent.find(component);
|
|
|
|
|
if (it == m_DynamicMessageSubscriptionsNonsyncByComponent.end())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::set<MessageTypeId>::iterator mtit;
|
|
|
|
|
for (mtit = it->second.begin(); mtit != it->second.end(); ++mtit)
|
|
|
|
|
{
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsync[*mtit].Remove(component);
|
|
|
|
|
|
|
|
|
|
// Need to flatten the subscription lists immediately to avoid dangling IComponent* references
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsync[*mtit].Flatten();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_DynamicMessageSubscriptionsNonsyncByComponent.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
CComponentManager::ComponentTypeId CComponentManager::LookupCID(const std::string& cname) const
|
|
|
|
|
{
|
|
|
|
|
std::map<std::string, ComponentTypeId>::const_iterator it = m_ComponentTypeIdsByName.find(cname);
|
|
|
|
|
if (it == m_ComponentTypeIdsByName.end())
|
|
|
|
|
return CID__Invalid;
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CComponentManager::LookupComponentTypeName(ComponentTypeId cid) const
|
|
|
|
|
{
|
|
|
|
|
std::map<ComponentTypeId, ComponentType>::const_iterator it = m_ComponentTypesById.find(cid);
|
|
|
|
|
if (it == m_ComponentTypesById.end())
|
|
|
|
|
return "";
|
|
|
|
|
return it->second.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CComponentManager::ComponentTypeId CComponentManager::GetScriptWrapper(InterfaceId iid)
|
|
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
if (iid >= IID__LastNative && iid <= (int)m_InterfaceIdsByName.size()) // use <= since IDs start at 1
|
|
|
|
|
return CID_UnknownScript;
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
std::map<ComponentTypeId, ComponentType>::const_iterator it = m_ComponentTypesById.begin();
|
|
|
|
|
for (; it != m_ComponentTypesById.end(); ++it)
|
|
|
|
|
if (it->second.iid == iid && it->second.type == CT_ScriptWrapper)
|
|
|
|
|
return it->first;
|
2014-12-22 16:29:14 -08:00
|
|
|
|
|
|
|
|
std::map<std::string, InterfaceId>::const_iterator iiit = m_InterfaceIdsByName.begin();
|
|
|
|
|
for (; iiit != m_InterfaceIdsByName.end(); ++iiit)
|
|
|
|
|
if (iiit->second == iid)
|
|
|
|
|
{
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGERROR("No script wrapper found for interface id %d '%s'", iid, iiit->first.c_str());
|
2014-12-22 16:29:14 -08:00
|
|
|
return CID__Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("No script wrapper found for interface id %d", iid);
|
2010-01-09 11:20:14 -08:00
|
|
|
return CID__Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-14 12:36:29 -08:00
|
|
|
entity_id_t CComponentManager::AllocateNewEntity()
|
|
|
|
|
{
|
|
|
|
|
entity_id_t id = m_NextEntityId++;
|
|
|
|
|
// TODO: check for overflow
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity_id_t CComponentManager::AllocateNewLocalEntity()
|
|
|
|
|
{
|
|
|
|
|
entity_id_t id = m_NextLocalEntityId++;
|
|
|
|
|
// TODO: check for overflow
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity_id_t CComponentManager::AllocateNewEntity(entity_id_t preferredId)
|
|
|
|
|
{
|
|
|
|
|
// TODO: ensure this ID hasn't been allocated before
|
|
|
|
|
// (this might occur with broken map files)
|
2014-12-22 16:46:00 -08:00
|
|
|
// Trying to actually add two entities with the same id will fail in AddEntitiy
|
2010-01-14 12:36:29 -08:00
|
|
|
entity_id_t id = preferredId;
|
|
|
|
|
|
|
|
|
|
// Ensure this ID won't be allocated again
|
|
|
|
|
if (id >= m_NextEntityId)
|
|
|
|
|
m_NextEntityId = id+1;
|
|
|
|
|
// TODO: check for overflow
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
bool CComponentManager::AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode& paramNode)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
IComponent* component = ConstructComponent(ent, cid);
|
|
|
|
|
if (!component)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-01-16 06:08:38 -08:00
|
|
|
component->Init(paramNode);
|
2010-01-09 11:20:14 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 06:44:08 -07:00
|
|
|
void CComponentManager::AddSystemComponents(bool skipScriptedComponents, bool skipAI)
|
|
|
|
|
{
|
|
|
|
|
CParamNode noParam;
|
|
|
|
|
AddComponent(m_SystemEntity, CID_TemplateManager, noParam);
|
2016-01-03 04:41:04 -08:00
|
|
|
AddComponent(m_SystemEntity, CID_CinemaManager, noParam);
|
2014-05-18 06:44:08 -07:00
|
|
|
AddComponent(m_SystemEntity, CID_CommandQueue, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_ObstructionManager, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_ParticleManager, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_Pathfinder, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_ProjectileManager, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_RangeManager, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_SoundManager, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_Terrain, noParam);
|
|
|
|
|
AddComponent(m_SystemEntity, CID_TerritoryManager, noParam);
|
2021-03-17 10:04:51 -07:00
|
|
|
AddComponent(m_SystemEntity, CID_UnitMotionManager, noParam);
|
2014-06-01 11:24:50 -07:00
|
|
|
AddComponent(m_SystemEntity, CID_UnitRenderer, noParam);
|
2014-05-18 06:44:08 -07:00
|
|
|
AddComponent(m_SystemEntity, CID_WaterManager, noParam);
|
|
|
|
|
|
|
|
|
|
// Add scripted system components:
|
|
|
|
|
if (!skipScriptedComponents)
|
|
|
|
|
{
|
|
|
|
|
for (uint32_t i = 0; i < m_ScriptedSystemComponents.size(); ++i)
|
|
|
|
|
AddComponent(m_SystemEntity, m_ScriptedSystemComponents[i], noParam);
|
|
|
|
|
if (!skipAI)
|
|
|
|
|
AddComponent(m_SystemEntity, CID_AIManager, noParam);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
IComponent* CComponentManager::ConstructComponent(CEntityHandle ent, ComponentTypeId cid)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
std::map<ComponentTypeId, ComponentType>::const_iterator it = m_ComponentTypesById.find(cid);
|
|
|
|
|
if (it == m_ComponentTypesById.end())
|
|
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Invalid component id %d", cid);
|
2010-01-09 11:20:14 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ComponentType& ct = it->second;
|
|
|
|
|
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE((size_t)ct.iid < m_ComponentsByInterface.size());
|
2011-03-02 16:16:14 -08:00
|
|
|
|
2019-11-25 06:30:25 -08:00
|
|
|
std::unordered_map<entity_id_t, IComponent*>& emap1 = m_ComponentsByInterface[ct.iid];
|
2013-09-11 13:41:53 -07:00
|
|
|
if (emap1.find(ent.GetId()) != emap1.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Multiple components for interface %d", ct.iid);
|
2010-01-09 11:20:14 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::map<entity_id_t, IComponent*>& emap2 = m_ComponentsByTypeId[cid];
|
|
|
|
|
|
|
|
|
|
// If this is a scripted component, construct the appropriate JS object first
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue obj(rq.cx);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (ct.type == CT_Script)
|
|
|
|
|
{
|
2020-11-12 00:24:30 -08:00
|
|
|
m_ScriptInterface.CallConstructor(*ct.ctor, JS::HandleValueArray::empty(), &obj);
|
2014-08-02 09:30:15 -07:00
|
|
|
if (obj.isNull())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Script component constructor failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct the new component
|
UnitMotion pushing improvements
The main change is the introduction of a 'pushing pressure' counter on
units. This counter increases when units get pushed around, and
decreases over time. In essence, units under high pressure move slower &
are harder to push around.
The major effect is that units can now get bogged down when very dense
groups start colliding. This makes movement more realistic, makes unit
movement more 'chokepointy', and generally improves the mathematical
soundness of the system (lower values are easier to handle for our 200ms
turns).
Other changes:
- The logic to detect units crossing each other's path has been
reworked. Units that run towards each other should not more obviously
avoid each other.
- New parameters: 'Spread' is a measure of how strong the pushing effect
is based on distance. With the current settings, static-pushing is
rather 'on/off', whereas moving-pushing is more gradual (and thus the
max influence distance was increased when moving).
- Default values have been tweaked for lower overlap.
- Units only looked at other units within their grid region. This led to
overlap near grid-borders. Units now look at neighboring grid elements,
which largely removes this issue. While this may be slower, the
performance of pushing was largely negligible before, so it is unlikely
to become a main cause of lag (and overlap was generally disliked by
players).
- Units no longer orient in the direction of pushing, but instead keep
facing their target. This can look slightly odd under very heavy pushing
forces, but vastly improves behaviour of very slow units such as rams
(since they spend much less time turning around). As a side-effect,
clean up angle code following acc780bcbb .
Engine changes:
- Add a debug rendering mode at compile-time to help understand what is
happening.
- Make it possible to constexpr initialise fractional fixed numbers by
using FromFraction
The 'pressure' change was inspired by alre's suggestion at
https://wildfiregames.com/forum/topic/56436-for-a-better-unit-movement/#comment-461987
Refs #6127
Differential Revision: https://code.wildfiregames.com/D4439
This was SVN commit r26245.
2022-01-24 07:36:13 -08:00
|
|
|
// NB: The unit motion manager relies on components not moving in memory once constructed.
|
2010-01-09 11:20:14 -08:00
|
|
|
IComponent* component = ct.alloc(m_ScriptInterface, obj);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(component);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
component->SetEntityHandle(ent);
|
2010-05-01 02:48:39 -07:00
|
|
|
component->SetSimContext(m_SimContext);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// Store a reference to the new component
|
2015-08-18 20:32:47 -07:00
|
|
|
emap1.insert(std::make_pair(ent.GetId(), component));
|
|
|
|
|
emap2.insert(std::make_pair(ent.GetId(), component));
|
2010-01-14 12:36:29 -08:00
|
|
|
// TODO: We need to more careful about this - if an entity is constructed by a component
|
|
|
|
|
// while we're iterating over all components, this will invalidate the iterators and everything
|
|
|
|
|
// will break.
|
|
|
|
|
// We probably need some kind of delayed addition, so they get pushed onto a queue and then
|
|
|
|
|
// inserted into the world later on. (Be careful about immediation deletion in that case, too.)
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
SEntityComponentCache* cache = ent.GetComponentCache();
|
|
|
|
|
ENSURE(cache != NULL && ct.iid < (int)cache->numInterfaces && cache->interfaces[ct.iid] == NULL);
|
|
|
|
|
cache->interfaces[ct.iid] = component;
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
void CComponentManager::AddMockComponent(CEntityHandle ent, InterfaceId iid, IComponent& component)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// Just add it into the by-interface map, not the by-component-type map,
|
|
|
|
|
// so it won't be considered for messages or deletion etc
|
|
|
|
|
|
2019-11-25 06:30:25 -08:00
|
|
|
std::unordered_map<entity_id_t, IComponent*>& emap1 = m_ComponentsByInterface.at(iid);
|
2013-09-11 13:41:53 -07:00
|
|
|
if (emap1.find(ent.GetId()) != emap1.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
debug_warn(L"Multiple components for interface");
|
2015-08-18 20:32:47 -07:00
|
|
|
emap1.insert(std::make_pair(ent.GetId(), &component));
|
2013-09-11 13:41:53 -07:00
|
|
|
|
|
|
|
|
SEntityComponentCache* cache = ent.GetComponentCache();
|
|
|
|
|
ENSURE(cache != NULL && iid < (int)cache->numInterfaces && cache->interfaces[iid] == NULL);
|
|
|
|
|
cache->interfaces[iid] = &component;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 06:19:25 -08:00
|
|
|
void CComponentManager::Trace(JSTracer* trc, void* data)
|
|
|
|
|
{
|
|
|
|
|
CComponentManager& cmpMgr = *(static_cast<CComponentManager*>(data));
|
|
|
|
|
for (auto& traceByEnt : cmpMgr.m_TraceCache)
|
|
|
|
|
for (JS::Heap<JS::Value>* ptr : traceByEnt.second)
|
|
|
|
|
JS::TraceEdge(trc, ptr, "ComponentManager::Trace");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::RegisterTrace(entity_id_t ent, const JS::Heap<JS::Value>& instance)
|
|
|
|
|
{
|
|
|
|
|
m_TraceCache.try_emplace(ent).first->second.push_back(const_cast<JS::Heap<JS::Value>*>(std::addressof(instance)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
CEntityHandle CComponentManager::AllocateEntityHandle(entity_id_t ent)
|
|
|
|
|
{
|
2019-05-25 08:59:43 -07:00
|
|
|
ENSURE(!EntityExists(ent));
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
// Interface IDs start at 1, and SEntityComponentCache is defined with a 1-sized array,
|
|
|
|
|
// so we need space for an extra m_InterfaceIdsByName.size() items
|
|
|
|
|
SEntityComponentCache* cache = (SEntityComponentCache*)calloc(1,
|
|
|
|
|
sizeof(SEntityComponentCache) + sizeof(IComponent*) * m_InterfaceIdsByName.size());
|
|
|
|
|
ENSURE(cache != NULL);
|
|
|
|
|
cache->numInterfaces = m_InterfaceIdsByName.size() + 1;
|
|
|
|
|
|
|
|
|
|
m_ComponentCaches[ent] = cache;
|
|
|
|
|
|
|
|
|
|
return CEntityHandle(ent, cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CEntityHandle CComponentManager::LookupEntityHandle(entity_id_t ent, bool allowCreate)
|
|
|
|
|
{
|
2017-02-22 11:27:58 -08:00
|
|
|
std::unordered_map<entity_id_t, SEntityComponentCache*>::iterator it;
|
2013-09-11 13:41:53 -07:00
|
|
|
it = m_ComponentCaches.find(ent);
|
|
|
|
|
if (it == m_ComponentCaches.end())
|
|
|
|
|
{
|
|
|
|
|
if (allowCreate)
|
|
|
|
|
return AllocateEntityHandle(ent);
|
|
|
|
|
else
|
|
|
|
|
return CEntityHandle(ent, NULL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return CEntityHandle(ent, it->second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::InitSystemEntity()
|
|
|
|
|
{
|
|
|
|
|
ENSURE(m_SystemEntity.GetId() == INVALID_ENTITY);
|
|
|
|
|
m_SystemEntity = AllocateEntityHandle(SYSTEM_ENTITY);
|
|
|
|
|
m_SimContext.SetSystemEntity(m_SystemEntity);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
entity_id_t CComponentManager::AddEntity(const std::wstring& templateName, entity_id_t ent)
|
|
|
|
|
{
|
2012-02-07 18:46:15 -08:00
|
|
|
ICmpTemplateManager *cmpTemplateManager = static_cast<ICmpTemplateManager*> (QueryInterface(SYSTEM_ENTITY, IID_TemplateManager));
|
|
|
|
|
if (!cmpTemplateManager)
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
debug_warn(L"No ICmpTemplateManager loaded");
|
|
|
|
|
return INVALID_ENTITY;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-15 10:41:53 -08:00
|
|
|
const CParamNode* tmpl = cmpTemplateManager->LoadTemplate(ent, utf8_from_wstring(templateName));
|
2010-01-22 12:03:14 -08:00
|
|
|
if (!tmpl)
|
|
|
|
|
return INVALID_ENTITY; // LoadTemplate will have reported the error
|
|
|
|
|
|
2014-12-22 16:46:00 -08:00
|
|
|
// This also ensures that ent does not exist
|
2013-09-11 13:41:53 -07:00
|
|
|
CEntityHandle handle = AllocateEntityHandle(ent);
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
// Construct a component for each child of the root element
|
|
|
|
|
const CParamNode::ChildrenMap& tmplChilds = tmpl->GetChildren();
|
|
|
|
|
for (CParamNode::ChildrenMap::const_iterator it = tmplChilds.begin(); it != tmplChilds.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
// Ignore attributes on the root element
|
|
|
|
|
if (it->first.length() && it->first[0] == '@')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
CComponentManager::ComponentTypeId cid = LookupCID(it->first);
|
|
|
|
|
if (cid == CID__Invalid)
|
|
|
|
|
{
|
2019-07-14 07:13:15 -07:00
|
|
|
LOGERROR("Unrecognized component type name '%s' in entity template '%s'", it->first, utf8_from_wstring(templateName));
|
2010-01-22 12:03:14 -08:00
|
|
|
return INVALID_ENTITY;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
if (!AddComponent(handle, cid, it->second))
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
2015-01-22 12:37:38 -08:00
|
|
|
LOGERROR("Failed to construct component type name '%s' in entity template '%s'", it->first, utf8_from_wstring(templateName));
|
2010-01-22 12:03:14 -08:00
|
|
|
return INVALID_ENTITY;
|
|
|
|
|
}
|
|
|
|
|
// TODO: maybe we should delete already-constructed components if one of them fails?
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-18 08:19:49 -07:00
|
|
|
CMessageCreate msg(ent);
|
|
|
|
|
PostMessage(ent, msg);
|
|
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
return ent;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 08:59:43 -07:00
|
|
|
bool CComponentManager::EntityExists(entity_id_t ent) const
|
|
|
|
|
{
|
|
|
|
|
return m_ComponentCaches.find(ent) != m_ComponentCaches.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-01-14 12:36:29 -08:00
|
|
|
void CComponentManager::DestroyComponentsSoon(entity_id_t ent)
|
|
|
|
|
{
|
|
|
|
|
m_DestructionQueue.push_back(ent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentManager::FlushDestroyedComponents()
|
|
|
|
|
{
|
2016-06-25 06:12:35 -07:00
|
|
|
PROFILE2("Flush Destroyed Components");
|
2013-09-11 13:41:53 -07:00
|
|
|
while (!m_DestructionQueue.empty())
|
2010-01-14 12:36:29 -08:00
|
|
|
{
|
2013-02-02 18:08:20 -08:00
|
|
|
// Make a copy of the destruction queue, so that the iterators won't be invalidated if the
|
|
|
|
|
// CMessageDestroy handlers try to destroy more entities themselves
|
|
|
|
|
std::vector<entity_id_t> queue;
|
|
|
|
|
queue.swap(m_DestructionQueue);
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
for (std::vector<entity_id_t>::iterator it = queue.begin(); it != queue.end(); ++it)
|
2010-01-14 12:36:29 -08:00
|
|
|
{
|
2013-02-02 18:08:20 -08:00
|
|
|
entity_id_t ent = *it;
|
2019-05-25 08:59:43 -07:00
|
|
|
|
|
|
|
|
// Do nothing if invalid, destroyed, etc.
|
|
|
|
|
if (!EntityExists(ent))
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
CEntityHandle handle = LookupEntityHandle(ent);
|
2013-02-02 18:08:20 -08:00
|
|
|
|
|
|
|
|
CMessageDestroy msg(ent);
|
|
|
|
|
PostMessage(ent, msg);
|
|
|
|
|
|
2017-04-17 01:00:41 -07:00
|
|
|
// Flatten all the dynamic subscriptions to ensure there are no dangling
|
|
|
|
|
// references in the 'removed' lists to components we're going to delete
|
|
|
|
|
// Some components may have dynamically unsubscribed following the Destroy message
|
|
|
|
|
FlattenDynamicSubscriptions();
|
|
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
// Destroy the components, and remove from m_ComponentsByTypeId:
|
|
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::iterator iit = m_ComponentsByTypeId.begin();
|
|
|
|
|
for (; iit != m_ComponentsByTypeId.end(); ++iit)
|
2010-01-14 12:36:29 -08:00
|
|
|
{
|
2013-02-02 18:08:20 -08:00
|
|
|
std::map<entity_id_t, IComponent*>::iterator eit = iit->second.find(ent);
|
|
|
|
|
if (eit != iit->second.end())
|
|
|
|
|
{
|
|
|
|
|
eit->second->Deinit();
|
2014-06-19 16:20:12 -07:00
|
|
|
RemoveComponentDynamicSubscriptions(eit->second);
|
2013-02-02 18:08:20 -08:00
|
|
|
m_ComponentTypesById[iit->first].dealloc(eit->second);
|
|
|
|
|
iit->second.erase(ent);
|
2013-09-11 13:41:53 -07:00
|
|
|
handle.GetComponentCache()->interfaces[m_ComponentTypesById[iit->first].iid] = NULL;
|
2013-02-02 18:08:20 -08:00
|
|
|
}
|
2010-01-14 12:36:29 -08:00
|
|
|
}
|
|
|
|
|
|
2013-09-11 13:41:53 -07:00
|
|
|
free(handle.GetComponentCache());
|
|
|
|
|
m_ComponentCaches.erase(ent);
|
2025-01-19 06:19:25 -08:00
|
|
|
|
|
|
|
|
auto hit = m_TraceCache.find(ent);
|
|
|
|
|
if (hit != m_TraceCache.end())
|
|
|
|
|
m_TraceCache.erase(hit);
|
2013-09-11 13:41:53 -07:00
|
|
|
|
2013-02-02 18:08:20 -08:00
|
|
|
// Remove from m_ComponentsByInterface
|
2019-11-25 06:30:25 -08:00
|
|
|
std::vector<std::unordered_map<entity_id_t, IComponent*> >::iterator ifcit = m_ComponentsByInterface.begin();
|
2013-02-02 18:08:20 -08:00
|
|
|
for (; ifcit != m_ComponentsByInterface.end(); ++ifcit)
|
|
|
|
|
{
|
|
|
|
|
ifcit->erase(ent);
|
|
|
|
|
}
|
2010-01-14 12:36:29 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
IComponent* CComponentManager::QueryInterface(entity_id_t ent, InterfaceId iid) const
|
|
|
|
|
{
|
2011-03-02 16:16:14 -08:00
|
|
|
if ((size_t)iid >= m_ComponentsByInterface.size())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-03-02 16:16:14 -08:00
|
|
|
// Invalid iid
|
2010-01-09 11:20:14 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 06:30:25 -08:00
|
|
|
std::unordered_map<entity_id_t, IComponent*>::const_iterator eit = m_ComponentsByInterface[iid].find(ent);
|
2011-03-02 16:16:14 -08:00
|
|
|
if (eit == m_ComponentsByInterface[iid].end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// This entity doesn't implement this interface
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eit->second;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-02 16:16:14 -08:00
|
|
|
CComponentManager::InterfaceList CComponentManager::GetEntitiesWithInterface(InterfaceId iid) const
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::pair<entity_id_t, IComponent*> > ret;
|
|
|
|
|
|
|
|
|
|
if ((size_t)iid >= m_ComponentsByInterface.size())
|
|
|
|
|
{
|
|
|
|
|
// Invalid iid
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.reserve(m_ComponentsByInterface[iid].size());
|
|
|
|
|
|
2019-11-25 06:30:25 -08:00
|
|
|
std::unordered_map<entity_id_t, IComponent*>::const_iterator it = m_ComponentsByInterface[iid].begin();
|
2011-03-02 16:16:14 -08:00
|
|
|
for (; it != m_ComponentsByInterface[iid].end(); ++it)
|
|
|
|
|
ret.push_back(*it);
|
|
|
|
|
|
|
|
|
|
std::sort(ret.begin(), ret.end()); // lexicographic pair comparison means this'll sort by entity ID
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static CComponentManager::InterfaceListUnordered g_EmptyEntityMap;
|
|
|
|
|
const CComponentManager::InterfaceListUnordered& CComponentManager::GetEntitiesWithInterfaceUnordered(InterfaceId iid) const
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-03-02 16:16:14 -08:00
|
|
|
if ((size_t)iid >= m_ComponentsByInterface.size())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-03-02 16:16:14 -08:00
|
|
|
// Invalid iid
|
2010-01-09 11:20:14 -08:00
|
|
|
return g_EmptyEntityMap;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-02 16:16:14 -08:00
|
|
|
return m_ComponentsByInterface[iid];
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 16:20:12 -07:00
|
|
|
void CComponentManager::PostMessage(entity_id_t ent, const CMessage& msg)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
// Send the message to components of ent, that subscribed locally to this message
|
|
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> >::const_iterator it;
|
|
|
|
|
it = m_LocalMessageSubscriptions.find(msg.GetType());
|
|
|
|
|
if (it != m_LocalMessageSubscriptions.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
std::vector<ComponentTypeId>::const_iterator ctit = it->second.begin();
|
|
|
|
|
for (; ctit != it->second.end(); ++ctit)
|
|
|
|
|
{
|
2011-01-12 04:29:00 -08:00
|
|
|
// Find the component instances of this type (if any)
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
|
|
|
|
|
if (emap == m_ComponentsByTypeId.end())
|
|
|
|
|
continue;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
// Send the message to all of them
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.find(ent);
|
|
|
|
|
if (eit != emap->second.end())
|
2011-01-16 06:08:38 -08:00
|
|
|
eit->second->HandleMessage(msg, false);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
SendGlobalMessage(ent, msg);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 16:20:12 -07:00
|
|
|
void CComponentManager::BroadcastMessage(const CMessage& msg)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
// Send the message to components of all entities that subscribed locally to this message
|
|
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> >::const_iterator it;
|
|
|
|
|
it = m_LocalMessageSubscriptions.find(msg.GetType());
|
|
|
|
|
if (it != m_LocalMessageSubscriptions.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
std::vector<ComponentTypeId>::const_iterator ctit = it->second.begin();
|
|
|
|
|
for (; ctit != it->second.end(); ++ctit)
|
|
|
|
|
{
|
2011-01-12 04:29:00 -08:00
|
|
|
// Find the component instances of this type (if any)
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
|
|
|
|
|
if (emap == m_ComponentsByTypeId.end())
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
// Send the message to all of them
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin();
|
|
|
|
|
for (; eit != emap->second.end(); ++eit)
|
2011-01-16 06:08:38 -08:00
|
|
|
eit->second->HandleMessage(msg, false);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
SendGlobalMessage(INVALID_ENTITY, msg);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 16:20:12 -07:00
|
|
|
void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
// (Common functionality for PostMessage and BroadcastMessage)
|
|
|
|
|
|
|
|
|
|
// Send the message to components of all entities that subscribed globally to this message
|
|
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> >::const_iterator it;
|
|
|
|
|
it = m_GlobalMessageSubscriptions.find(msg.GetType());
|
|
|
|
|
if (it != m_GlobalMessageSubscriptions.end())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-01-22 12:03:14 -08:00
|
|
|
std::vector<ComponentTypeId>::const_iterator ctit = it->second.begin();
|
|
|
|
|
for (; ctit != it->second.end(); ++ctit)
|
|
|
|
|
{
|
2014-11-04 16:22:14 -08:00
|
|
|
// Special case: Messages for local entities shouldn't be sent to script
|
2011-01-12 04:29:00 -08:00
|
|
|
// components that subscribed globally, so that we don't have to worry about
|
|
|
|
|
// them accidentally picking up non-network-synchronised data.
|
|
|
|
|
if (ENTITY_IS_LOCAL(ent))
|
|
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
std::map<ComponentTypeId, ComponentType>::const_iterator cit = m_ComponentTypesById.find(*ctit);
|
|
|
|
|
if (cit != m_ComponentTypesById.end() && cit->second.type == CT_Script)
|
2011-01-12 04:29:00 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the component instances of this type (if any)
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
|
|
|
|
|
if (emap == m_ComponentsByTypeId.end())
|
|
|
|
|
continue;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
// Send the message to all of them
|
2010-01-22 12:03:14 -08:00
|
|
|
std::map<entity_id_t, IComponent*>::const_iterator eit = emap->second.begin();
|
|
|
|
|
for (; eit != emap->second.end(); ++eit)
|
2011-01-16 06:08:38 -08:00
|
|
|
eit->second->HandleMessage(msg, true);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2010-04-09 12:02:39 -07:00
|
|
|
|
2014-06-19 16:20:12 -07:00
|
|
|
// Send the message to component instances that dynamically subscribed to this message
|
|
|
|
|
std::map<MessageTypeId, CDynamicSubscription>::iterator dit = m_DynamicMessageSubscriptionsNonsync.find(msg.GetType());
|
|
|
|
|
if (dit != m_DynamicMessageSubscriptionsNonsync.end())
|
|
|
|
|
{
|
|
|
|
|
dit->second.Flatten();
|
|
|
|
|
const std::vector<IComponent*>& dynamic = dit->second.GetComponents();
|
|
|
|
|
for (size_t i = 0; i < dynamic.size(); i++)
|
|
|
|
|
dynamic[i]->HandleMessage(msg, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-09 12:02:39 -07:00
|
|
|
|
2017-07-29 16:26:44 -07:00
|
|
|
std::string CComponentManager::GenerateSchema() const
|
2010-04-09 12:02:39 -07:00
|
|
|
{
|
|
|
|
|
std::string schema =
|
|
|
|
|
"<grammar xmlns='http://relaxng.org/ns/structure/1.0' xmlns:a='http://ns.wildfiregames.com/entity' datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes'>"
|
2015-12-05 09:02:25 -08:00
|
|
|
"<define name='decimal'>"
|
|
|
|
|
"<data type='decimal'/>"
|
|
|
|
|
"</define>"
|
2010-04-09 12:02:39 -07:00
|
|
|
"<define name='nonNegativeDecimal'>"
|
|
|
|
|
"<data type='decimal'><param name='minInclusive'>0</param></data>"
|
|
|
|
|
"</define>"
|
|
|
|
|
"<define name='positiveDecimal'>"
|
|
|
|
|
"<data type='decimal'><param name='minExclusive'>0</param></data>"
|
|
|
|
|
"</define>"
|
|
|
|
|
"<define name='anything'>"
|
|
|
|
|
"<zeroOrMore>"
|
|
|
|
|
"<choice>"
|
|
|
|
|
"<attribute><anyName/></attribute>"
|
|
|
|
|
"<text/>"
|
|
|
|
|
"<element>"
|
|
|
|
|
"<anyName/>"
|
|
|
|
|
"<ref name='anything'/>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</choice>"
|
|
|
|
|
"</zeroOrMore>"
|
|
|
|
|
"</define>";
|
|
|
|
|
|
|
|
|
|
std::map<InterfaceId, std::vector<std::string> > interfaceComponentTypes;
|
|
|
|
|
|
2010-04-23 09:09:03 -07:00
|
|
|
std::vector<std::string> componentTypes;
|
|
|
|
|
|
2010-04-09 12:02:39 -07:00
|
|
|
for (std::map<ComponentTypeId, ComponentType>::const_iterator it = m_ComponentTypesById.begin(); it != m_ComponentTypesById.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
schema +=
|
|
|
|
|
"<define name='component." + it->second.name + "'>"
|
|
|
|
|
"<element name='" + it->second.name + "'>"
|
|
|
|
|
"<interleave>" + it->second.schema + "</interleave>"
|
|
|
|
|
"</element>"
|
|
|
|
|
"</define>";
|
|
|
|
|
|
|
|
|
|
interfaceComponentTypes[it->second.iid].push_back(it->second.name);
|
2010-04-23 09:09:03 -07:00
|
|
|
componentTypes.push_back(it->second.name);
|
2010-04-09 12:02:39 -07:00
|
|
|
}
|
|
|
|
|
|
2010-04-23 09:09:03 -07:00
|
|
|
// Declare the implementation of each interface, for documentation
|
2010-04-09 12:02:39 -07:00
|
|
|
for (std::map<std::string, InterfaceId>::const_iterator it = m_InterfaceIdsByName.begin(); it != m_InterfaceIdsByName.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
schema += "<define name='interface." + it->first + "'><choice>";
|
|
|
|
|
std::vector<std::string>& cts = interfaceComponentTypes[it->second];
|
|
|
|
|
for (size_t i = 0; i < cts.size(); ++i)
|
|
|
|
|
schema += "<ref name='component." + cts[i] + "'/>";
|
|
|
|
|
schema += "</choice></define>";
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-23 09:09:03 -07:00
|
|
|
// List all the component types, in alphabetical order (to match the reordering performed by CParamNode).
|
|
|
|
|
// (We do it this way, rather than <interleave>ing all the interface definitions (which would additionally perform
|
|
|
|
|
// a check that we don't use multiple component types of the same interface in one file), because libxml2 gives
|
|
|
|
|
// useless error messages in the latter case; this way lets it report the real error.)
|
|
|
|
|
std::sort(componentTypes.begin(), componentTypes.end());
|
2010-04-09 12:02:39 -07:00
|
|
|
schema +=
|
|
|
|
|
"<start>"
|
2022-12-29 23:34:23 -08:00
|
|
|
"<element>"
|
|
|
|
|
"<anyName/>"
|
2010-04-23 09:09:03 -07:00
|
|
|
"<optional><attribute name='parent'/></optional>";
|
|
|
|
|
for (std::vector<std::string>::const_iterator it = componentTypes.begin(); it != componentTypes.end(); ++it)
|
|
|
|
|
schema += "<optional><ref name='component." + *it + "'/></optional>";
|
2010-04-09 12:02:39 -07:00
|
|
|
schema +=
|
|
|
|
|
"</element>"
|
|
|
|
|
"</start>";
|
|
|
|
|
|
|
|
|
|
schema += "</grammar>";
|
|
|
|
|
|
|
|
|
|
return schema;
|
|
|
|
|
}
|