summaryrefslogtreecommitdiffstats
path: root/source/LuaScript.cpp
blob: 8d92c238f5dfda374df7d4da07e48a7ad2927025 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

// LuaScript.cpp

// Implements the cLuaScript class that loads a Lua script file to produce a web template out of it

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "LuaScript.h"

extern "C"
{
#include "lualib.h"
}

#include "tolua++.h"
#include "Bindings.h"
#include "ManualBindings.h"

// fwd: SQLite/lsqlite3.c
extern "C"
{
	LUALIB_API int luaopen_lsqlite3(lua_State * L);
}

// fwd: LuaExpat/lxplib.c:
extern "C"
{
	int luaopen_lxp(lua_State * L);
}





cLuaScript::cLuaScript()
	: m_LuaState(NULL)
{

}





cLuaScript::~cLuaScript()
{
	if( m_LuaState )
	{
		lua_close( m_LuaState );
		m_LuaState = 0;
	}
}





void cLuaScript::Initialize()
{
	// Check to see if this script has not been initialized before
	ASSERT(!m_LuaState);

	// Create a Lua state and bind all libraries to it
	m_LuaState = lua_open();
	luaL_openlibs(m_LuaState);
	tolua_AllToLua_open(m_LuaState);
	ManualBindings::Bind(m_LuaState);
	luaopen_lsqlite3(m_LuaState);
	luaopen_lxp(m_LuaState);
}





bool cLuaScript::LoadFile( const char* a_FilePath )
{
	// Make sure the plugin is initialized
	ASSERT(m_LuaState);

	// Load the file into the Lua state
	int s = luaL_loadfile(m_LuaState, a_FilePath );
	if (ReportErrors(s))
	{
		return false;
	}
	return true;
}





bool cLuaScript::Execute()
{
	// Make sure we got a Lua state
	ASSERT(m_LuaState);

	// Execute the script as it is right now
	int s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
	if( ReportErrors( s ) )
	{
		return false;
	}
	return true;
}





bool cLuaScript::ReportErrors( int a_Status )
{
	if (a_Status == 0)
	{
		// No error to report
		return false;
	}

	// Status was set to error so get the error from the Lua state and log it
	LOGERROR("LUA: %s", lua_tostring(m_LuaState, -1));
	lua_pop(m_LuaState, 1);

	// Return true to indicate that an error was returned
	return true;
}





bool cLuaScript::LuaPushFunction( const char * a_FunctionName, bool a_bLogError /*= true*/ )
{
	ASSERT(m_LuaState);

	// Find and push the function on the Lua stack
	lua_getglobal(m_LuaState, a_FunctionName);

	// Make sure we found a function
	if (!lua_isfunction(m_LuaState, -1))
	{
		if (a_bLogError)
		{
			LOGWARN("LUA: Could not find function %s()", a_FunctionName);
		}

		// Pop the pushed 'object' back
		lua_pop(m_LuaState, 1);
		return false;
	}

	// Successfully pushed a function to the Lua stack
	return true;
}





bool cLuaScript::LuaCallFunction( int a_NumArgs, int a_NumResults, const char * a_FunctionName )
{
	ASSERT(m_LuaState);

	// Make sure there's a lua function on the stack
	ASSERT(lua_isfunction(m_LuaState, -a_NumArgs - 1));

	// Call the desired function
	int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);

	// Check for errors
	if (ReportErrors(s))
	{
		LOGWARN("LUA: Error calling function %s()", a_FunctionName);
		return false;
	}

	// Successfully executed function
	return true;
}





bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString )
{
	// Make sure we have the required things to call a function
	ASSERT(m_LuaState);
	ASSERT(a_Function);

	// Push the desired function on the stack
	if (!LuaPushFunction(a_Function))
	{
		return false;
	}

	if (!LuaCallFunction(0, 1, a_Function))
	{
		return false;
	}

	if (lua_isstring(m_LuaState, -1))
	{
		ReturnedString = tolua_tostring(m_LuaState, -1, "");
	}
	lua_pop(m_LuaState, 1);
	return true;
}





bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString )
{
	// Make sure we have the required things to call a function
	ASSERT(m_LuaState);
	ASSERT(a_Function);

	// Push the desired function on the stack
	if (!LuaPushFunction(a_Function))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_UserType.Object, a_UserType.ClassName);

	if (!LuaCallFunction(1, 1, a_Function))
	{
		return false;
	}

	if (lua_isstring(m_LuaState, -1))
	{
		ReturnedString = tolua_tostring(m_LuaState, -1, "");
	}
	lua_pop(m_LuaState, 1);
	return true;
}





bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString )
{
	// Make sure we have the required things to call a function
	ASSERT(m_LuaState);
	ASSERT(a_Function);

	// Push the desired function on the stack
	if (!LuaPushFunction(a_Function))
	{
		return false;
	}

	tolua_pushusertype(m_LuaState, a_UserType1.Object, a_UserType1.ClassName);
	tolua_pushusertype(m_LuaState, a_UserType2.Object, a_UserType2.ClassName);

	if (!LuaCallFunction(2, 1, a_Function))
	{
		return false;
	}

	if (lua_isstring(m_LuaState, -1))
	{
		ReturnedString = tolua_tostring(m_LuaState, -1, "");
	}
	lua_pop(m_LuaState, 1);
	return true;
}