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
|
#include "stdafx.h"
CMLuaScript::CMLuaScript(lua_State *L, const TCHAR* path)
: L(L), unloadRef(0)
{
mir_tstrcpy(filePath, path);
fileName = _tcsrchr(filePath, '\\') + 1;
size_t length = mir_tstrlen(fileName) - 3;
ptrT name((TCHAR*)mir_calloc(sizeof(TCHAR) * length));
mir_tstrncpy(name, fileName, mir_tstrlen(fileName) - 3);
moduleName = mir_utf8encodeT(name);
}
CMLuaScript::~CMLuaScript()
{
mir_free(this->moduleName);
}
const char* CMLuaScript::GetModuleName() const
{
return moduleName;
}
const TCHAR* CMLuaScript::GetFilePath() const
{
return filePath;
}
const TCHAR* CMLuaScript::GetFileName() const
{
return fileName;
}
bool CMLuaScript::Load()
{
if (luaL_loadfile(L, T2Utf(filePath)))
{
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
return false;
}
if (lua_pcall(L, 0, 1, 0))
{
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
return false;
}
isLoaded = true;
if (!lua_istable(L, -1))
return true;
lua_pushliteral(L, "Load");
lua_gettable(L, -2);
if (lua_isfunction(L, -1))
{
if (lua_pcall(L, 0, 0, 0))
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
}
else
lua_pop(L, 1);
lua_pushliteral(L, "Unload");
lua_gettable(L, -2);
if (lua_isfunction(L, -1))
{
lua_pushvalue(L, -1);
unloadRef = luaL_ref(L, LUA_REGISTRYINDEX);
}
lua_pop(L, 1);
return true;
}
void CMLuaScript::Unload()
{
if (isLoaded && unloadRef)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef);
if (lua_isfunction(L, -1) && lua_pcall(L, 0, 0, 0))
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
luaL_unref(L, LUA_REGISTRYINDEX, unloadRef);
isLoaded = false;
}
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
lua_pushnil(L);
lua_setfield(L, -2, moduleName);
lua_pop(L, 1);
lua_pushnil(L);
lua_setglobal(L, moduleName);
}
|