-
Notifications
You must be signed in to change notification settings - Fork 3
/
lua_addons.cpp
82 lines (72 loc) · 2.2 KB
/
lua_addons.cpp
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
#include "stdafx.h"
// Include converted lua files
#include "Libs/luasocket/src/socket_main.h"
#include "Libs/json.h"
#include "lua/funcs.h"
#include "lua/require.h"
// Reference Lua C functions
extern "C" int luaopen_socket_core(lua_State *L);
// Handy functions
void registerLuaModule(const lua_CFunction cfunction, const char* modulename)
{
lua_getfield(L, LUA_GLOBALSINDEX, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(L, cfunction);
lua_setfield(L, -2, modulename);
}
void registerLuaModule(const unsigned char* buffer, size_t length, const char* modulename)
{
lua_getfield(L, LUA_GLOBALSINDEX, "package");
lua_getfield(L, -1, "preload");
int error = luaL_loadbuffer(L, (const char*)buffer, length, modulename);
if (error != 0)
{
const char* errorMsg = lua_tostring(L, -1);
MessageBoxA(NULL, errorMsg, "LUA load error! The module won't be loaded!", MB_OK);
return;
}
lua_setfield(L, -2, modulename);
}
void runEmbeddedLuaFile(const unsigned char* buff, size_t length, const char* name)
{
int err = luaL_loadbuffer(L, (const char*)buff, length, name);
if (err == 0)
{
err = lua_pcall(L, 0, 0, 0);
if (err != 0)
{
const char* errorMsg = lua_tostring(L, -1);
MessageBoxA(NULL, errorMsg, "LUA load error! The file won't be loaded!", MB_OK);
return;
}
}
else
{
const char* errorMsg = lua_tostring(L, -1);
MessageBoxA(NULL, errorMsg, "LUA load error! The file won't be loaded!", MB_OK);
return;
}
}
// Init addons
void luaAddonsLoad()
{
// Init useful functions globally
runEmbeddedLuaFile(funcs_lua, funcs_lua_len, "funcs.lua");
// Load the patch of the require function so it sets the current environement correctly
// -1
int err = luaL_loadbuffer(L, (const char*)require_lua, require_lua_len, "require.lua");
if (err != 0)
{
const char* errorMsg = lua_tostring(L, -1);
MessageBoxA(NULL, errorMsg, "LUA load error! The file won't be loaded!", MB_OK);
return;
}
lua_pushstring(L, "require.lua");
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
// Init json engine
registerLuaModule(json_lua, json_lua_len, "json");
// Init the LuaSocket
registerLuaModule(luaopen_socket_core, "socket.core");
registerLuaModule(socket_lua, socket_lua_len, "socket");
}