Plasma Engine  2.0
Loading...
Searching...
No Matches
LuaWrapper.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Logging/Log.h>
5#include <Foundation/Strings/String.h>
6
7#ifdef BUILDSYSTEM_ENABLE_LUA_SUPPORT
8
9# if PL_ENABLED(PL_PLATFORM_WINDOWS_UWP)
10// We compile Lua as C++ under UWP so we need to include the headers directly
11// to prevent the addition of extern "C" done by lua.hpp.
12# include <Lua/lauxlib.h>
13# include <Lua/lua.h>
14# include <Lua/luaconf.h>
15# include <Lua/lualib.h>
16# else
17# include <Lua/lua.hpp>
18# endif
19
32class PL_CORE_DLL plLuaWrapper
33{
34 PL_DISALLOW_COPY_AND_ASSIGN(plLuaWrapper);
35
36public:
39
41 plLuaWrapper(); // [tested]
42
44 plLuaWrapper(lua_State* s);
45
47 ~plLuaWrapper(); // [tested]
48
50 void Clear(); // [tested]
51
57 lua_State* GetLuaState();
58
68 plResult ExecuteString(const char* szString, const char* szDebugChunkName = "chunk",
69 plLogInterface* pLogInterface = nullptr) const; // [tested]
70
72
73
76
81 plResult OpenTable(const char* szTable); // [tested]
82
84 plResult OpenTableFromParameter(plUInt32 uiFunctionParameter); // [tested]
85
87 void CloseTable(); // [tested]
88
90 void CloseAllTables(); // [tested]
91
93 void PushTable(const char* szTableName, bool bGlobalTable); // [tested]
94
96
99
101 bool IsVariableAvailable(const char* szVariable) const; // [tested]
102
104 bool IsFunctionAvailable(const char* szFunction) const; // [tested]
105
107
110
112 int GetIntVariable(const char* szName, plInt32 iDefault = 0) const; // [tested]
113
115 bool GetBoolVariable(const char* szName, bool bDefault = false) const; // [tested]
116
118 float GetFloatVariable(const char* szName, float fDefault = 0.0f) const; // [tested]
119
121 const char* GetStringVariable(const char* szName, const char* szDefault = "") const; // [tested]
122
124
127
129 void SetVariableNil(const char* szName) const; // [tested]
130
132 void SetVariable(const char* szName, plInt32 iValue) const; // [tested]
133
135 void SetVariable(const char* szName, bool bValue) const; // [tested]
136
138 void SetVariable(const char* szName, float fValue) const; // [tested]
139
141 void SetVariable(const char* szName, const char* szValue) const; // [tested]
142
144 void SetVariable(const char* szName, const char* szValue, plUInt32 uiLen) const; // [tested]
145
147
150
152 void RegisterCFunction(const char* szFunctionName, lua_CFunction function, void* pLightUserData = nullptr) const; // [tested]
153
156 bool PrepareFunctionCall(const char* szFunctionName); // [tested]
157
164 plResult CallPreparedFunction(plUInt32 uiExpectedReturnValues = 0, plLogInterface* pLogInterface = nullptr); // [tested]
165
168 void DiscardReturnValues(); // [tested]
169
181 plInt32 ReturnToScript() const; // [tested]
182
184
187
190 void PushParameter(plInt32 iParam); // [tested]
191
194 void PushParameter(bool bParam); // [tested]
195
198 void PushParameter(float fParam); // [tested]
199
202 void PushParameter(const char* szParam); // [tested]
203
206 void PushParameter(const char* szParam, plUInt32 uiLength); // [tested]
207
210 void PushParameterNil(); // [tested]
211
213
216
218 void* GetFunctionLightUserData() const;
219
221 plUInt32 GetNumberOfFunctionParameters() const; // [tested]
222
224 bool IsParameterInt(plUInt32 uiParameter) const; // [tested]
225
227 bool IsParameterBool(plUInt32 uiParameter) const; // [tested]
228
230 bool IsParameterFloat(plUInt32 uiParameter) const; // [tested]
231
233 bool IsParameterTable(plUInt32 uiParameter) const; // [tested]
234
236 bool IsParameterString(plUInt32 uiParameter) const; // [tested]
237
239 bool IsParameterNil(plUInt32 uiParameter) const; // [tested]
240
242 int GetIntParameter(plUInt32 uiParameter) const; // [tested]
243
245 bool GetBoolParameter(plUInt32 uiParameter) const; // [tested]
246
248 float GetFloatParameter(plUInt32 uiParameter) const; // [tested]
249
251 const char* GetStringParameter(plUInt32 uiParameter) const; // [tested]
252
254
257
259 void PushReturnValue(plInt32 iParam); // [tested]
260
262 void PushReturnValue(bool bParam); // [tested]
263
265 void PushReturnValue(float fParam); // [tested]
266
268 void PushReturnValue(const char* szParam); // [tested]
269
271 void PushReturnValue(const char* szParam, plUInt32 uiLength); // [tested]
272
274 void PushReturnValueNil(); // [tested]
275
276
278 bool IsReturnValueInt(plUInt32 uiReturnValue) const; // [tested]
279
281 bool IsReturnValueBool(plUInt32 uiReturnValue) const; // [tested]
282
284 bool IsReturnValueFloat(plUInt32 uiReturnValue) const; // [tested]
285
287 bool IsReturnValueString(plUInt32 uiReturnValue) const; // [tested]
288
290 bool IsReturnValueNil(plUInt32 uiReturnValue) const; // [tested]
291
292
294 int GetIntReturnValue(plUInt32 uiReturnValue) const; // [tested]
295
297 bool GetBoolReturnValue(plUInt32 uiReturnValue) const; // [tested]
298
300 float GetFloatReturnValue(plUInt32 uiReturnValue) const; // [tested]
301
303 const char* GetStringReturnValue(plUInt32 uiReturnValue) const; // [tested]
304
306
307
308private:
310 static void* lua_allocator(void* ud, void* ptr, size_t osize, size_t nsize);
311
313 lua_State* m_pState;
314
316 bool m_bReleaseOnExit;
317
318 struct plScriptStates
319 {
320 plScriptStates()
321
322 = default;
323
325 plInt32 m_iParametersPushed = 0;
326
328 plInt32 m_iOpenTables = 0;
329
331 plInt32 m_iLuaReturnValues = 0;
332 };
333
334 plScriptStates m_States;
335
336 static constexpr plInt32 s_iParamOffset = 1; // should be one, to start counting at 0, instead of 1
337};
338
339# include <Core/Scripting/LuaWrapper/LuaWrapper.inl>
340
341#endif // BUILDSYSTEM_ENABLE_LUA_SUPPORT
Base class for all logging classes.
Definition Log.h:77
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54