Plasma Engine  2.0
Loading...
Searching...
No Matches
TsBinding.h
1#pragma once
2
3#include <TypeScriptPlugin/TypeScriptPluginDLL.h>
4
5#include <Core/Console/ConsoleFunction.h>
6#include <Core/ResourceManager/ResourceHandle.h>
7#include <Core/Scripting/DuktapeContext.h>
8#include <Core/World/Declarations.h>
9#include <Core/World/World.h>
10#include <Foundation/Configuration/CVar.h>
11#include <Foundation/Containers/HashTable.h>
12#include <Foundation/Math/Declarations.h>
13#include <Foundation/Types/Uuid.h>
14#include <TypeScriptPlugin/Resources/ScriptCompendiumResource.h>
15#include <TypeScriptPlugin/Transpiler/Transpiler.h>
16
17class plWorld;
20
22
23enum plTypeScriptBindingIndexProperty
24{
25 ComponentHandle,
26 GameObjectHandle
27};
28
29class PL_TYPESCRIPTPLUGIN_DLL plTypeScriptBinding
30{
31public:
33 {
34 const plRTTI* m_pMessageType = nullptr;
35 plUInt32 m_uiMessageTypeNameHash = 0;
36 plString m_sHandlerFunc;
37 };
38
40 {
41 plString m_sComponentTypeName;
42 plHybridArray<TsMessageHandler, 4> m_MessageHandlers;
43 };
44
46
49
50public:
53
54 plResult Initialize(plWorld& ref_world);
55 plResult LoadComponent(const plUuid& typeGuid, TsComponentTypeInfo& out_typeInfo);
56
57 plResult FindScriptComponentInfo(const char* szComponentType, TsComponentTypeInfo& out_typeInfo);
58
59 void RegisterMessageHandlersForComponentType(const char* szComponent, const plUuid& componentType);
60
61 PL_ALWAYS_INLINE plDuktapeContext& GetDukTapeContext() { return m_Duk; }
62 PL_ALWAYS_INLINE duk_context* GetDukContext() { return m_Duk.GetContext(); }
63
64 void Update();
65
66private:
67 static void GetTsName(const plRTTI* pRtti, plStringBuilder& out_sName);
68
69 plDuktapeContext m_Duk;
70 bool m_bInitialized = false;
71 plMap<plUuid, bool> m_LoadedComponents;
72 plScriptCompendiumResourceHandle m_hScriptCompendium;
73
77public:
78 static plResult SetupProjectCode();
79
80private:
81 static void GenerateComponentCode(plStringBuilder& out_Code, const plRTTI* pRtti);
82 static void GenerateExposedFunctionsCode(plStringBuilder& out_Code, const plRTTI* pRtti);
83 static void GeneratePropertiesCode(plStringBuilder& out_Code, const plRTTI* pRtti);
84 static void GenerateAllComponentsCode(plStringBuilder& out_Code);
85 static void GenerateComponentsFile(const char* szFile);
86 static void InjectComponentImportExport(plStringBuilder& content, const char* szComponentFile);
87 static void InjectMessageImportExport(plStringBuilder& content, const char* szComponentFile);
88 static void GenerateEnumsFile(const char* szFile, const plSet<const plRTTI*>& items);
89 static void InjectEnumImportExport(plStringBuilder& content, const char* szEnumFile);
90 static void InjectFlagsImportExport(plStringBuilder& content, const char* szEnumFile);
91 static void GenerateConstructorString(plStringBuilder& out_String, const plVariant& value);
92
93 static void RemoveAutoGeneratedCode(plStringBuilder& content);
94 static void AppendToTextFile(plStringBuilder& content, plStringView text);
95
96 static plSet<const plRTTI*> s_RequiredEnums;
97 static plSet<const plRTTI*> s_RequiredFlags;
98
102
103public:
105 {
106 const plAbstractFunctionProperty* m_pFunc = nullptr;
107 };
108
109 static const FunctionBinding* FindFunctionBinding(plUInt32 uiFunctionHash);
110
111private:
112 static plUInt32 ComputeFunctionBindingHash(const plRTTI* pType, const plAbstractFunctionProperty* pFunc);
113 static void SetupRttiFunctionBindings();
114 static const char* TsType(const plRTTI* pRtti);
115 static int __CPP_Binding_RegisterMessageHandler(duk_context* pDuk);
116
117 static plHashTable<plUInt32, FunctionBinding> s_BoundFunctions;
118
119
123
124public:
126 {
127 const plAbstractMemberProperty* m_pMember = nullptr;
128 };
129
130 static const PropertyBinding* FindPropertyBinding(plUInt32 uiHash);
131
132 static void SyncPlObjectToTsObject(duk_context* pDuk, const plRTTI* pRtti, const void* pObject, plInt32 iObjIdx);
133 static void SyncTsObjectPlTsObject(duk_context* pDuk, const plRTTI* pRtti, void* pObject, plInt32 iObjIdx);
134
135private:
136 static plUInt32 ComputePropertyBindingHash(const plRTTI* pType, const plAbstractMemberProperty* pMember);
137 static void SetupRttiPropertyBindings();
138
139 static plHashTable<plUInt32, PropertyBinding> s_BoundProperties;
140
144
145public:
146 plUniquePtr<plMessage> MessageFromParameter(duk_context* pDuk, plInt32 iObjIdx, plTime delay);
147 static void DukPutMessage(duk_context* pDuk, const plMessage& msg);
148
149 bool HasMessageHandler(const TsComponentTypeInfo& typeInfo, const plRTTI* pMsgRtti) const;
150 bool DeliverMessage(const TsComponentTypeInfo& typeInfo, plTypeScriptComponent* pComponent, plMessage& ref_msg, bool bSynchronizeAfterwards);
151 bool DeliverTsMessage(const TsComponentTypeInfo& typeInfo, plTypeScriptComponent* pComponent, const plMsgTypeScriptMsgProxy& msg);
152
153private:
154 static void GenerateMessagesFile(const char* szFile);
155 static void GenerateAllMessagesCode(plStringBuilder& out_Code);
156 static void GenerateMessageCode(plStringBuilder& out_Code, const plRTTI* pRtti);
157 static void GenerateMessagePropertiesCode(plStringBuilder& out_Code, const plRTTI* pRtti);
158
159 plInt32 m_iMsgDeliveryRecursion = 0;
160 plUuid m_CurrentTsMsgHandlerRegistrator;
161 plMap<plUuid, TsComponentInfo> m_TsComponentTypes;
162
163
167private:
168 static int DukSearchModule(duk_context* pDuk);
169
173private:
174 plResult Init_RequireModules();
175 plResult Init_Log();
176 plResult Init_Utils();
177 plResult Init_Time();
178 plResult Init_GameObject();
179 plResult Init_Component();
180 plResult Init_World();
181 plResult Init_Clock();
182 plResult Init_Random();
183 plResult Init_FunctionBinding();
184 plResult Init_PropertyBinding();
185 plResult Init_Debug();
186 plResult Init_Physics();
187
188
192public:
193 static plWorld* RetrieveWorld(duk_context* pDuk);
194 static plTypeScriptBinding* RetrieveBinding(duk_context* pDuk);
195
196 plWorld* GetWorld() { return m_pWorld; }
197
198private:
199 void StoreWorld(plWorld* pWorld);
200
201 plWorld* m_pWorld = nullptr;
202 static plHashTable<duk_context*, plWorld*> s_DukToWorld;
203
207public:
208 static plGameObjectHandle RetrieveGameObjectHandle(duk_context* pDuk, plInt32 iObjIdx = 0 /* use 0, if the game object is passed in as the 'this' object (first parameter) */);
209 static plGameObject* ExpectGameObject(duk_context* pDuk, plInt32 iObjIdx = 0 /* use 0, if the game object is passed in as the 'this' object (first parameter) */);
210 bool DukPutGameObject(const plGameObjectHandle& hObject);
211 void DukPutGameObject(const plGameObject* pObject);
212
216public:
217 void DukPutComponentObject(plComponent* pComponent);
218 static plComponentHandle RetrieveComponentHandle(duk_context* pDuk, plInt32 iObjIdx = 0 /* use 0, if the component is passed in as the 'this' object (first parameter) */);
219
220 template <typename ComponentType>
221 static ComponentType* ExpectComponent(duk_context* pDuk, plInt32 iObjIdx = 0 /* use 0, if the game object is passed in as the 'this' object (first parameter) */);
222
226
227 static void PushVec2(duk_context* pDuk, const plVec2& value);
228 static void SetVec2(duk_context* pDuk, plInt32 iObjIdx, const plVec2& value);
229 static void SetVec2Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plVec2& value);
230 static plVec2 GetVec2(duk_context* pDuk, plInt32 iObjIdx, const plVec2& vFallback = plVec2::MakeZero());
231 static plVec2 GetVec2Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plVec2& vFallback = plVec2::MakeZero());
232
233 static void PushVec3(duk_context* pDuk, const plVec3& value);
234 static void SetVec3(duk_context* pDuk, plInt32 iObjIdx, const plVec3& value);
235 static void SetVec3Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plVec3& value);
236 static plVec3 GetVec3(duk_context* pDuk, plInt32 iObjIdx, const plVec3& vFallback = plVec3::MakeZero());
237 static plVec3 GetVec3Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plVec3& vFallback = plVec3::MakeZero());
238
239 static void PushMat3(duk_context* pDuk, const plMat3& value);
240 static void SetMat3(duk_context* pDuk, plInt32 iObjIdx, const plMat3& value);
241 static void SetMat3Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plMat3& value);
242 static plMat3 GetMat3(duk_context* pDuk, plInt32 iObjIdx, const plMat3& mFallback = plMat3::MakeIdentity());
243 static plMat3 GetMat3Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plMat3& mFallback = plMat3::MakeIdentity());
244
245 static void PushMat4(duk_context* pDuk, const plMat4& value);
246 static void SetMat4(duk_context* pDuk, plInt32 iObjIdx, const plMat4& value);
247 static void SetMat4Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plMat4& value);
248 static plMat4 GetMat4(duk_context* pDuk, plInt32 iObjIdx, const plMat4& mFallback = plMat4::MakeIdentity());
249 static plMat4 GetMat4Property(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plMat4& mFallback = plMat4::MakeIdentity());
250
251 static void PushQuat(duk_context* pDuk, const plQuat& value);
252 static void SetQuat(duk_context* pDuk, plInt32 iObjIdx, const plQuat& value);
253 static void SetQuatProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plQuat& value);
254 static plQuat GetQuat(duk_context* pDuk, plInt32 iObjIdx, plQuat qFallback = plQuat::MakeIdentity());
255 static plQuat GetQuatProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, plQuat qFallback = plQuat::MakeIdentity());
256
257 static void PushColor(duk_context* pDuk, const plColor& value);
258 static void SetColor(duk_context* pDuk, plInt32 iObjIdx, const plColor& value);
259 static void SetColorProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plColor& value);
260 static plColor GetColor(duk_context* pDuk, plInt32 iObjIdx, const plColor& fallback = plColor::White);
261 static plColor GetColorProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plColor& fallback = plColor::White);
262
263 static void PushTransform(duk_context* pDuk, const plTransform& value);
264 static void SetTransform(duk_context* pDuk, plInt32 iObjIdx, const plTransform& value);
265 static void SetTransformProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plTransform& value);
266 static plTransform GetTransform(duk_context* pDuk, plInt32 iObjIdx, const plTransform& fallback = plTransform::MakeIdentity());
267 static plTransform GetTransformProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plTransform& fallback = plTransform::MakeIdentity());
268
269 static void PushVariant(duk_context* pDuk, const plVariant& value);
270 static void SetVariantProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plVariant& value);
271 static plVariant GetVariant(duk_context* pDuk, plInt32 iObjIdx, const plRTTI* pType);
272 static plVariant GetVariantProperty(duk_context* pDuk, const char* szPropertyName, plInt32 iObjIdx, const plRTTI* pType);
273
277
278public:
280
282 {
283 plComponentHandle m_hOwner;
284 plUInt32 m_uiFuncStashIdx = 0;
285 };
286
292
294
295 void StoreConsoleFuncCall(plConsoleFunctionBase* pFunc, const plArrayPtr<plVariant>& params);
296
297private:
298 void ExecuteConsoleFuncs();
299
300 struct ConsoleFuncCall
301 {
302 plConsoleFunctionBase* m_pFunc;
303 plHybridArray<plVariant, 4> m_Arguments;
304 };
305
307
311public:
312 bool RegisterGameObject(plGameObjectHandle hHandle, plUInt32& out_uiStashIdx);
313 plResult RegisterComponent(plStringView sTypeName, plComponentHandle hHandle, plUInt32& out_uiStashIdx, bool bIsNativeComponent);
314
316 void CleanupStash(plUInt32 uiNumIterations);
317
318 plUInt32 AcquireStashObjIndex();
319 void ReleaseStashObjIndex(plUInt32 uiIdx);
320
321private:
322 static void StoreReferenceInStash(duk_context* pDuk, plUInt32 uiStashIdx);
323 static bool DukPushStashObject(duk_context* pDuk, plUInt32 uiStashIdx);
324
325 static constexpr plUInt32 c_uiMaxMsgStash = 512;
326 static constexpr plUInt32 c_uiFirstStashMsgIdx = 512;
327 static constexpr plUInt32 c_uiLastStashMsgIdx = c_uiFirstStashMsgIdx + c_uiFirstStashMsgIdx;
328 static constexpr plUInt32 c_uiFirstStashObjIdx = c_uiLastStashMsgIdx;
329 plUInt32 m_uiNextStashMsgIdx = c_uiFirstStashMsgIdx;
330 plUInt32 m_uiNextStashObjIdx = c_uiFirstStashObjIdx;
331 plMap<plGameObjectHandle, plUInt32> m_GameObjectToStashIdx;
332 plMap<plComponentHandle, plUInt32> m_ComponentToStashIdx;
333 plDeque<plUInt32> m_FreeStashObjIdx;
336 plDynamicArray<plTime> m_StashedMsgDelivery;
337
339};
340
341template <typename ComponentType>
342ComponentType* plTypeScriptBinding::ExpectComponent(duk_context* pDuk, plInt32 iObjIdx /*= 0 */)
343{
344 plComponentHandle hOwnHandle = plTypeScriptBinding::RetrieveComponentHandle(pDuk, iObjIdx);
345
346 ComponentType* pComponent = nullptr;
347 plWorld* pWorld = plTypeScriptBinding::RetrieveWorld(pDuk);
348 PL_VERIFY(pWorld->TryGetComponent(hOwnHandle, pComponent), "Invalid component parameter");
349
350 return pComponent;
351}
The base class for a property that represents a function.
Definition AbstractProperty.h:535
This is the base class for all properties that are members of a class. It provides more information a...
Definition AbstractProperty.h:237
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
static const plColor White
#FFFFFF
Definition Color.h:194
Base class of all component types.
Definition Component.h:25
Base class for all types of plConsoleFunction, represents functions to be exposed to plConsole.
Definition ConsoleFunction.h:37
Definition Deque.h:270
Definition DynamicArray.h:81
This class represents an object inside the world.
Definition GameObject.h:32
Definition HashTable.h:333
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition Map.h:408
static plMat3Template< float > MakeIdentity()
Definition Mat3_inl.h:48
static plMat4Template< float > MakeIdentity()
Definition Mat4_inl.h:75
Base class for all message types. Each message type has it's own id which is used to dispatch message...
Definition Message.h:22
static const plQuatTemplate< float > MakeIdentity()
Definition Quat_inl.h:29
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition Set.h:238
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
static plTransformTemplate< float > MakeIdentity()
Definition Transform_inl.h:25
Definition TsBinding.h:30
Definition TypeScriptComponent.h:47
The plTypedResourceHandle controls access to an plResource.
Definition ResourceHandle.h:136
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
static constexpr plVec2Template< float > MakeZero()
Definition Vec2.h:49
static plVec3Template< float > MakeZero()
Definition Vec3.h:38
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
bool TryGetComponent(const plComponentHandle &hComponent, ComponentType *&out_pComponent)
Returns whether a component with the given handle exists and if so writes out the corresponding point...
Definition World_inl.h:264
A handle to a component.
Definition Declarations.h:138
A handle to a game object.
Definition Declarations.h:76
Base class for all iterators.
Definition Map.h:11
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
Definition TypeScriptComponent.h:17
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Definition TsBinding.h:288
Definition TsBinding.h:105
Definition TsBinding.h:126
Definition TsBinding.h:40
Definition TsBinding.h:33