Line data Source code
1 : /* Copyright (C) 2026 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_SCRIPTCOMPONENT
19 : #define INCLUDED_SCRIPTCOMPONENT
20 :
21 : // These headers are included because they are required in component implementation,
22 : // so including them here transitively makes sense.
23 : #include "scriptinterface/FunctionWrapper.h"
24 : #include "simulation2/system/CmpPtr.h"
25 : #include "simulation2/system/Components.h"
26 : #include "simulation2/system/IComponent.h"
27 : #include "simulation2/system/ParamNode.h"
28 : #include "simulation2/system/SimContext.h"
29 : #include "simulation2/serialization/ISerializer.h"
30 : #include "simulation2/serialization/IDeserializer.h"
31 :
32 : #include "ps/CLogger.h"
33 :
34 : class CComponentTypeScript
35 : {
36 : NONCOPYABLE(CComponentTypeScript);
37 : public:
38 : CComponentTypeScript(const Script::Interface& scriptInterface, JS::HandleValue instance);
39 :
40 91916 : JS::HandleValue GetInstance() const { return JS::HandleValue::fromMarkedLocation(m_Instance.unsafeAddress()); }
41 21935 : JS::MutableHandleValue GetMutInstance() { return JS::MutableHandleValue::fromMarkedLocation(const_cast<JS::Value*>(m_Instance.unsafeAddress())); }
42 : static void Trace(JSTracer* trc, void* data);
43 :
44 : void Init(CComponentManager& cmpMgr, const CParamNode& paramNode, entity_id_t ent);
45 : void Deinit();
46 : bool HasMessageHandler(const CMessage& msg, const bool global);
47 : void HandleMessage(const CMessage& msg, bool global);
48 :
49 : void Serialize(ISerializer& serialize);
50 : void Deserialize(CComponentManager& cmpMgr, const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent);
51 :
52 : template<typename R, typename... Ts>
53 4941 : R Call(const char* funcname, const Ts&... params) const
54 : {
55 4341 : R ret;
56 4941 : Script::Request rq(m_ScriptInterface);
57 4941 : if (Script::Function::Call(rq, GetInstance(), funcname, ret, params...))
58 4941 : return ret;
59 0 : LOGERROR("Error calling component script function {}", funcname);
60 3339 : return R();
61 4941 : }
62 :
63 : // CallRef is mainly used for returning script values with correct stack rooting.
64 : template<typename R, typename... Ts>
65 3 : void CallRef(const char* funcname, R ret, const Ts&... params) const
66 : {
67 3 : Script::Request rq(m_ScriptInterface);
68 3 : if (!Script::Function::Call(rq, GetInstance(), funcname, ret, params...))
69 0 : LOGERROR("Error calling component script function {}", funcname);
70 3 : }
71 :
72 : template<typename... Ts>
73 3 : void CallVoid(const char* funcname, const Ts&... params) const
74 : {
75 3 : Script::Request rq(m_ScriptInterface);
76 3 : if (!Script::Function::CallVoid(rq, GetInstance(), funcname, params...))
77 0 : LOGERROR("Error calling component script function {}", funcname);
78 3 : }
79 :
80 : private:
81 : const Script::Interface& m_ScriptInterface;
82 : JS::Heap<JS::Value> m_Instance;
83 : };
84 :
85 : #define REGISTER_COMPONENT_SCRIPT_WRAPPER(cname) \
86 : void RegisterComponentType_##cname(CComponentManager& mgr) \
87 : { \
88 : IComponent::RegisterComponentTypeScriptWrapper(mgr, CCmp##cname::GetInterfaceId(), \
89 : CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, \
90 : CCmp##cname::GetSchema(), CCmp##cname::ClassInit); \
91 : }
92 :
93 :
94 : #define DEFAULT_SCRIPT_WRAPPER_BASIC(cname) \
95 : static IComponent* Allocate(const Script::Interface& scriptInterface, JS::HandleValue instance) \
96 : { \
97 : return new CCmp##cname(scriptInterface, instance); \
98 : } \
99 : static void Deallocate(IComponent* cmp) \
100 : { \
101 : delete static_cast<CCmp##cname*> (cmp); \
102 : } \
103 : CCmp##cname(const Script::Interface& scriptInterface, JS::HandleValue instance) : m_Script(scriptInterface, instance) { } \
104 : static std::string GetSchema() \
105 : { \
106 : return "<a:component type='script-wrapper'/><empty/>"; \
107 : } \
108 : void Init(const CParamNode& paramNode) override \
109 : { \
110 : m_Script.Init(GetSimContext().GetComponentManager(), paramNode, GetEntityId()); \
111 : } \
112 : void Deinit() override \
113 : { \
114 : m_Script.Deinit(); \
115 : } \
116 : JS::HandleValue GetJSInstance() const override \
117 : { \
118 : return m_Script.GetInstance(); \
119 : } \
120 : int GetComponentTypeId() const override \
121 : { \
122 : return CID_##cname; \
123 : } \
124 : private: \
125 : CComponentTypeScript m_Script; \
126 : public:
127 :
128 :
129 : #define DEFAULT_SCRIPT_WRAPPER(cname) \
130 : static void ClassInit(CComponentManager&) { } \
131 : void HandleMessage(const CMessage& msg, bool global) override \
132 : { \
133 : m_Script.HandleMessage(msg, global); \
134 : } \
135 : void Serialize(ISerializer& serialize) override \
136 : { \
137 : m_Script.Serialize(serialize); \
138 : } \
139 : void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override \
140 : { \
141 : m_Script.Deserialize(GetSimContext().GetComponentManager(), paramNode, deserialize, GetEntityId()); \
142 : } \
143 : DEFAULT_SCRIPT_WRAPPER_BASIC(cname)
144 :
145 : #endif // INCLUDED_SCRIPTCOMPONENT
|