summaryrefslogtreecommitdiffstats
path: root/source/LuaScript.cpp
blob: f27d1793f9bc8e896987ee9d9d384a2936b656ca (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

// 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"





cLuaScript::cLuaScript()
	: m_LuaState("cLuaScript")
{

}





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

	// Create a Lua state and bind all libraries to it
	m_LuaState.Create();
}





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

	return m_LuaState.LoadFile(a_FilePath);
}





bool cLuaScript::CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString)
{
	ASSERT(m_LuaState.IsValid());
	
	m_LuaState.PushFunction("ShowPage");
	m_LuaState.PushUserType(&a_WebAdmin, "cWebAdmin");
	m_LuaState.PushUserType(&a_Request, "HTTPTemplateRequest");
	if (!m_LuaState.CallFunction(1))
	{
		return false;
	}
	if (lua_isstring(m_LuaState, -1))
	{
		a_ReturnedString.assign(tolua_tostring(m_LuaState, -1, ""));
	}
	lua_pop(m_LuaState, 1);
	return true;
}