diff --git a/source/simulation2/scripting/ScriptComponent.cpp b/source/simulation2/scripting/ScriptComponent.cpp index 9b504388fa..1ea4fa241d 100644 --- a/source/simulation2/scripting/ScriptComponent.cpp +++ b/source/simulation2/scripting/ScriptComponent.cpp @@ -25,8 +25,9 @@ #include "simulation2/serialization/IDeserializer.h" CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) : - m_ScriptInterface(scriptInterface), m_Instance(scriptInterface.GetGeneralJSContext(), instance) + m_ScriptInterface(scriptInterface) { + m_Instance.init(ScriptRequest(m_ScriptInterface).cx, instance); } void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent) diff --git a/source/simulation2/scripting/ScriptComponent.h b/source/simulation2/scripting/ScriptComponent.h index f82e0435da..8a9e62341b 100644 --- a/source/simulation2/scripting/ScriptComponent.h +++ b/source/simulation2/scripting/ScriptComponent.h @@ -18,8 +18,16 @@ #ifndef INCLUDED_SCRIPTCOMPONENT #define INCLUDED_SCRIPTCOMPONENT +// These headers are included because they are required in component implementation, +// so including them here transitively makes sense. #include "scriptinterface/FunctionWrapper.h" -#include "simulation2/system/Component.h" +#include "simulation2/system/CmpPtr.h" +#include "simulation2/system/Components.h" +#include "simulation2/system/IComponent.h" +#include "simulation2/system/ParamNode.h" +#include "simulation2/system/SimContext.h" +#include "simulation2/serialization/ISerializer.h" +#include "simulation2/serialization/IDeserializer.h" #include "ps/CLogger.h" @@ -74,7 +82,7 @@ private: #define REGISTER_COMPONENT_SCRIPT_WRAPPER(cname) \ void RegisterComponentType_##cname(CComponentManager& mgr) \ { \ - mgr.RegisterComponentTypeScriptWrapper(CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \ + IComponent::RegisterComponentTypeScriptWrapper(mgr, CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \ CCmp##cname::ClassInit(mgr); \ } diff --git a/source/simulation2/system/Component.h b/source/simulation2/system/Component.h index 9efcdde29e..4d3ea63415 100644 --- a/source/simulation2/system/Component.h +++ b/source/simulation2/system/Component.h @@ -18,6 +18,8 @@ #ifndef INCLUDED_COMPONENT #define INCLUDED_COMPONENT +// These headers are included because they are required in component implementation, +// so including them here transitively makes sense. #include "simulation2/system/CmpPtr.h" #include "simulation2/system/Components.h" #include "simulation2/system/ComponentManager.h" @@ -30,7 +32,7 @@ #define REGISTER_COMPONENT_TYPE(cname) \ void RegisterComponentType_##cname(CComponentManager& mgr) \ { \ - mgr.RegisterComponentType(CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \ + IComponent::RegisterComponentType(mgr, CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \ CCmp##cname::ClassInit(mgr); \ } diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h index e59a2e6cee..b2f3dac2a6 100644 --- a/source/simulation2/system/ComponentManager.h +++ b/source/simulation2/system/ComponentManager.h @@ -23,6 +23,7 @@ #include "simulation2/helpers/Player.h" #include "simulation2/system/Components.h" #include "simulation2/system/Entity.h" +#include "simulation2/system/IComponent.h" #include #include @@ -46,9 +47,8 @@ public: typedef int MessageTypeId; private: - // Component allocation types - typedef IComponent* (*AllocFunc)(const ScriptInterface& scriptInterface, JS::HandleValue ctor); - typedef void (*DeallocFunc)(IComponent*); + using AllocFunc = IComponent::AllocFunc; + using DeallocFunc = IComponent::DeallocFunc; // ComponentTypes come in three types: // Native: normal C++ component diff --git a/source/simulation2/system/IComponent.cpp b/source/simulation2/system/IComponent.cpp index 1bf3aba9e6..76c63483bf 100644 --- a/source/simulation2/system/IComponent.cpp +++ b/source/simulation2/system/IComponent.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,6 +19,8 @@ #include "IComponent.h" +#include "simulation2/system/ComponentManager.h" + #include IComponent::~IComponent() @@ -31,6 +33,16 @@ std::string IComponent::GetSchema() return ""; } +void IComponent::RegisterComponentType(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema) +{ + mgr.RegisterComponentType(iid, cid, alloc, dealloc, name, schema); +} + +void IComponent::RegisterComponentTypeScriptWrapper(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema) +{ + mgr.RegisterComponentTypeScriptWrapper(iid, cid, alloc, dealloc, name, schema); +} + void IComponent::HandleMessage(const CMessage& UNUSED(msg), bool UNUSED(global)) { } diff --git a/source/simulation2/system/IComponent.h b/source/simulation2/system/IComponent.h index 0d5515b2f1..63a887573c 100644 --- a/source/simulation2/system/IComponent.h +++ b/source/simulation2/system/IComponent.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -32,10 +32,17 @@ class IDeserializer; class IComponent { public: + // Component allocation types + using AllocFunc = IComponent* (*)(const ScriptInterface& scriptInterface, JS::HandleValue ctor); + using DeallocFunc = void (*)(IComponent*); + virtual ~IComponent(); static std::string GetSchema(); + static void RegisterComponentType(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema); + static void RegisterComponentTypeScriptWrapper(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema); + virtual void Init(const CParamNode& paramNode) = 0; virtual void Deinit() = 0; diff --git a/source/simulation2/system/Interface.h b/source/simulation2/system/Interface.h index 00180d6c30..c0410c248e 100644 --- a/source/simulation2/system/Interface.h +++ b/source/simulation2/system/Interface.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -23,6 +23,6 @@ #define DECLARE_INTERFACE_TYPE(iname) \ virtual bool NewJSObject(const ScriptInterface& scriptInterface, JS::MutableHandleObject out) const; \ static void InterfaceInit(ScriptInterface& scriptInterface); \ - static int GetInterfaceId() { return IID_##iname; } + static EInterfaceId GetInterfaceId() { return IID_##iname; } #endif // INCLUDED_INTERFACE