-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting.cpp
More file actions
185 lines (171 loc) · 3.63 KB
/
Scripting.cpp
File metadata and controls
185 lines (171 loc) · 3.63 KB
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
#include <apollo/Scripting/Scripting.h>
#include <apollo/Utilities/ResourceManager.h>
#include <apollo/Logging.h>
#include <apollo/Scripting/Compile.h>
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
extern void __LuaBind ( lua_State* L );
#define LUA_READER_BUFFER_SIZE 1024
static const char* luaReader ( lua_State* L, void* data, size_t* size )
{
static char buffer[LUA_READER_BUFFER_SIZE];
size_t len = SDL_RWread((SDL_RWops*)data, buffer, 1, LUA_READER_BUFFER_SIZE);
if (len <= 0)
{
*size = 0;
return NULL;
}
else
{
*size = len;
return buffer;
}
}
static void luaHandleError ( lua_State* L )
{
const char* message = lua_tostring(L, -1);
LOG("Scripting::Driver", LOG_WARNING, "Script error: %s", message);
lua_pop(L, 1);
}
static void luaLoad ( lua_State* L, const std::string& path )
{
std::string fullpath = "Scripts/" + path + ".lua";
#ifdef NDEBUG
if (ResourceManager::FileExists("Scripts/" + path + ".lo"))
{
fullpath = "Scripts/" + path + ".lo";
}
else
{
CompileScript(path);
}
#endif
SDL_RWops* rwops = ResourceManager::OpenFile(fullpath);
if (rwops)
{
int rc = lua_load(L, luaReader, (void*)rwops, path.c_str());
SDL_RWclose(rwops);
if (rc != 0)
{
luaHandleError(L);
return;
}
rc = lua_pcall(L, 0, 0, 0);
if (rc != 0)
{
luaHandleError(L);
return;
}
}
else
{
LOG("Scripting::Driver", LOG_WARNING, "Unable to load script: %s", path.c_str());
}
}
LuaScript::LuaScript ( const std::string& filename )
{
L = luaL_newstate();
luaL_openlibs(L);
__LuaBind(L);
luaLoad(L, "System/Class");
luaLoad(L, filename);
}
LuaScript::~LuaScript ()
{
lua_close(L);
}
void LuaScript::InvokeSubroutine ( const std::string& name )
{
lua_getglobal(L, name.c_str());
if (!lua_isnoneornil(L, -1))
{
int rc = lua_pcall(L, 0, 0, 0);
if (rc > 0)
{
luaHandleError(L);
}
}
else
{
lua_pop(L, 1);
}
}
void LuaScript::InvokeSubroutine ( const std::string& name, const std::string& p )
{
lua_getglobal(L, name.c_str());
if (!lua_isnoneornil(L, -1))
{
lua_pushlstring(L, p.data(), p.length());
int rc = lua_pcall(L, 1, 0, 0);
if (rc > 0)
{
luaHandleError(L);
}
}
else
{
lua_pop(L, 1);
}
}
void LuaScript::InvokeSubroutine ( const std::string& name, int reference )
{
lua_getglobal(L, name.c_str());
if (!lua_isnoneornil(L, -1))
{
lua_rawgeti(L, reference, -1);
int rc = lua_pcall(L, 1, 0, 0);
if (rc > 0)
{
luaHandleError(L);
}
}
else
{
lua_pop(L, 1);
}
}
void LuaScript::InvokeSubroutine ( const std::string& name, float x, float y )
{
lua_getglobal(L, name.c_str());
if (!lua_isnoneornil(L, -1))
{
lua_pushnumber(L, x);
lua_pushnumber(L, y);
int rc = lua_pcall(L, 2, 0, 0);
if (rc > 0)
{
luaHandleError(L);
}
}
else
{
lua_pop(L, 1);
}
}
void LuaScript::InvokeSubroutine ( const std::string& name, const std::string& p, float x, float y )
{
lua_getglobal(L, name.c_str());
if (!lua_isnoneornil(L, -1))
{
lua_pushlstring(L, p.data(), p.length());
lua_pushnumber(L, x);
lua_pushnumber(L, y);
int rc = lua_pcall(L, 3, 0, 0);
if (rc > 0)
{
luaHandleError(L);
}
}
else
{
lua_pop(L, 1);
}
}
void LuaScript::RawImport ( lua_State* L, const std::string& module )
{
luaLoad(L, "Modules/" + module);
}