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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#define LUA_USE_POSIX
#include "cPlugin_NewLua.h"
#include "cMCLogger.h"
#include "cWebPlugin_Lua.h"
extern "C"
{
#include "lualib.h"
}
#include "tolua++.h"
#include "Bindings.h"
#include "ManualBindings.h"
#ifdef _WIN32
#include "wdirent.h"
#else
#include <dirent.h>
#endif
extern bool report_errors(lua_State* lua, int status);
cPlugin_NewLua::cPlugin_NewLua( const char* a_PluginName )
: m_LuaState( 0 )
{
m_Directory = a_PluginName;
}
cPlugin_NewLua::~cPlugin_NewLua()
{
for( WebPluginList::iterator itr = m_WebPlugins.begin(); itr != m_WebPlugins.end(); ++itr )
{
delete *itr;
}
m_WebPlugins.clear();
if( m_LuaState )
{
lua_close( m_LuaState );
m_LuaState = 0;
}
}
bool cPlugin_NewLua::Initialize()
{
if( !m_LuaState )
{
m_LuaState = lua_open();
luaL_openlibs( m_LuaState );
tolua_AllToLua_open(m_LuaState);
ManualBindings::Bind( m_LuaState );
}
std::string PluginPath = std::string("Plugins/") + m_Directory + "/";
// Load all files for this plugin, and execute them
DIR* dp;
struct dirent *entry;
if(dp = opendir( PluginPath.c_str() ))
{
while(entry = readdir(dp))
{
std::string FileName = entry->d_name;
if( FileName.find(".lua") != std::string::npos )
{
std::string Path = PluginPath + FileName;
int s = luaL_loadfile(m_LuaState, Path.c_str() );
if( report_errors( m_LuaState, s ) )
{
LOGERROR("Can't load plugin %s because of an error in file %s", m_Directory.c_str(), Path.c_str() );
lua_close( m_LuaState );
m_LuaState = 0;
return false;
}
s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
if( report_errors( m_LuaState, s ) )
{
LOGERROR("Error in plugin %s in file %s", m_Directory.c_str(), Path.c_str() );
lua_close( m_LuaState );
m_LuaState = 0;
return false;
}
}
}
closedir( dp );
}
// Call intialize function
if( !PushFunction("Initialize") )
{
lua_close( m_LuaState );
m_LuaState = 0;
return false;
}
tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
if( !CallFunction(1, 1, "Initialize") )
{
lua_close( m_LuaState );
m_LuaState = 0;
return false;
}
if( !lua_isboolean( m_LuaState, -1 ) )
{
LOGWARN("Error in plugin %s Initialize() must return a boolean value!", m_Directory.c_str() );
lua_close( m_LuaState );
m_LuaState = 0;
return false;
}
bool bSuccess = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bSuccess;
}
void cPlugin_NewLua::OnDisable()
{
if( !PushFunction("OnDisable", false) ) // false = don't log error if not found
return;
CallFunction(0, 0, "OnDisable");
}
void cPlugin_NewLua::Tick(float a_Dt)
{
if( !PushFunction("Tick") )
return;
tolua_pushnumber( m_LuaState, a_Dt );
CallFunction(1, 0, "Tick");
}
bool cPlugin_NewLua::OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player )
{
if( !PushFunction("OnCollectItem") )
return false;
tolua_pushusertype(m_LuaState, a_Pickup, "cPickup");
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(2, 1, "OnCollectItem") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnDisconnect( std::string a_Reason, cPlayer* a_Player )
{
if( !PushFunction("OnDisconnect") )
return false;
tolua_pushstring( m_LuaState, a_Reason.c_str() );
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(2, 1, "OnDisconnect") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player )
{
if( !PushFunction("OnBlockPlace") )
return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockPlace");
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(2, 1, "OnBlockPlace") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem )
{
if( !PushFunction("OnBlockDig") )
return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockDig");
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
tolua_pushusertype(m_LuaState, a_PickupItem, "cItem");
if( !CallFunction(3, 1, "OnBlockDig") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnChat( const char* a_Chat, cPlayer* a_Player )
{
if( !PushFunction("OnChat") )
return false;
tolua_pushstring( m_LuaState, a_Chat );
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(2, 1, "OnChat") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData )
{
if( !PushFunction("OnLogin") )
return false;
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_Login");
if( !CallFunction(1, 1, "OnLogin") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
void cPlugin_NewLua::OnPlayerSpawn( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerSpawn") )
return;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
CallFunction(1, 0, "OnPlayerSpawn");
}
bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerJoin") )
return false;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
if( !CallFunction(1, 1, "OnPlayerJoin") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
void cPlugin_NewLua::OnPlayerMove( cPlayer* a_Player )
{
if( !PushFunction("OnPlayerMove") )
return;
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
CallFunction(1, 0, "OnPlayerMove");
}
void cPlugin_NewLua::OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo )
{
if( !PushFunction("OnTakeDamage") )
return;
tolua_pushusertype(m_LuaState, a_Pawn, "cPawn");
tolua_pushusertype(m_LuaState, a_TakeDamageInfo, "TakeDamageInfo");
CallFunction(2, 0, "OnTakeDamage");
}
bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
{
if( !PushFunction("OnKilled") )
return false;
tolua_pushusertype(m_LuaState, a_Killed, "cPawn");
tolua_pushusertype(m_LuaState, a_Killer, "cEntity");
if( !CallFunction(2, 1, "OnKilled") )
return false;
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
return bRetVal;
}
cWebPlugin_Lua* cPlugin_NewLua::CreateWebPlugin(lua_State* a_LuaState)
{
if( a_LuaState != m_LuaState )
{
LOGERROR("Not allowed to create a WebPlugin from another plugin but your own!");
return 0;
}
cWebPlugin_Lua* WebPlugin = new cWebPlugin_Lua( this );
m_WebPlugins.push_back( WebPlugin );
return WebPlugin;
}
// Helper functions
bool cPlugin_NewLua::PushFunction( const char* a_FunctionName, bool a_bLogError /* = true */ )
{
lua_getglobal(m_LuaState, a_FunctionName);
if(!lua_isfunction(m_LuaState,-1))
{
if( a_bLogError )
{
LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName );
}
lua_pop(m_LuaState,1);
return false;
}
return true;
}
bool cPlugin_NewLua::CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName )
{
int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);
if( report_errors( m_LuaState, s ) )
{
LOGWARN("Error in plugin %s calling function %s()", m_Directory.c_str(), a_FunctionName );
return false;
}
return true;
}
|