Line data Source code
1 : /* Copyright (C) 2021 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_ICOMPONENT
19 : #define INCLUDED_ICOMPONENT
20 :
21 : #include "Components.h"
22 : #include "Message.h"
23 : #include "Entity.h"
24 : #include "SimContext.h"
25 : #include "scriptinterface/ScriptForward.h"
26 :
27 : class CParamNode;
28 : class CMessage;
29 : class ISerializer;
30 : class IDeserializer;
31 :
32 170 : class IComponent
33 : {
34 : public:
35 : // Component allocation types
36 : using AllocFunc = IComponent* (*)(const ScriptInterface& scriptInterface, JS::HandleValue ctor);
37 : using DeallocFunc = void (*)(IComponent*);
38 :
39 : virtual ~IComponent();
40 :
41 : static std::string GetSchema();
42 :
43 : static void RegisterComponentType(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema);
44 : static void RegisterComponentTypeScriptWrapper(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema);
45 :
46 : virtual void Init(const CParamNode& paramNode) = 0;
47 : virtual void Deinit() = 0;
48 :
49 : virtual void HandleMessage(const CMessage& msg, bool global);
50 :
51 0 : CEntityHandle GetEntityHandle() const { return m_EntityHandle; }
52 151 : void SetEntityHandle(CEntityHandle ent) { m_EntityHandle = ent; }
53 :
54 135 : entity_id_t GetEntityId() const { return m_EntityHandle.GetId(); }
55 :
56 58 : CEntityHandle GetSystemEntity() const { return m_SimContext->GetSystemEntity(); }
57 :
58 203 : const CSimContext& GetSimContext() const { return *m_SimContext; }
59 151 : void SetSimContext(const CSimContext& context) { m_SimContext = &context; }
60 :
61 : static u8 GetSerializationVersion() { return 0; }
62 : virtual void Serialize(ISerializer& serialize) = 0;
63 : virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) = 0;
64 :
65 : /**
66 : * Returns false by default, indicating that a scripted wrapper of this IComponent is not supported.
67 : * Derrived classes should return true if they implement such a wrapper.
68 : */
69 : virtual bool NewJSObject(const ScriptInterface& scriptInterface, JS::MutableHandleObject out) const;
70 : virtual JS::Value GetJSInstance() const;
71 : virtual int GetComponentTypeId() const = 0;
72 :
73 : private:
74 : CEntityHandle m_EntityHandle;
75 : const CSimContext* m_SimContext;
76 : };
77 :
78 : #endif // INCLUDED_ICOMPONENT
|