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
|
#include "stdafx.h"
#define SCRIPT "Script"
CMLuaScript::CMLuaScript(lua_State *L, const TCHAR *path)
: L(L), status(None)
{
mir_tstrcpy(filePath, path);
fileName = _tcsrchr(filePath, '\\') + 1;
TCHAR *dot = _tcsrchr(fileName, '.');
size_t length = mir_tstrlen(fileName) - mir_tstrlen(dot) + 1;
ptrT name((TCHAR*)mir_calloc(sizeof(TCHAR) * (length + 1)));
mir_tstrncpy(name, fileName, length);
moduleName = mir_utf8encodeT(name);
MUUID muidLast = MIID_LAST;
id = GetPluginLangId(muidLast, 0);
}
CMLuaScript::~CMLuaScript()
{
mir_free(moduleName);
}
bool CMLuaScript::GetScriptEnviroment(lua_State *L, int n)
{
lua_Debug ar;
if (lua_getstack(L, 1, &ar) == 0 || lua_getinfo(L, "f", &ar) == 0 || lua_iscfunction(L, -1))
{
lua_pop(L, 1);
return false;
}
const char *env = lua_getupvalue(L, n, 1);
if (!env || mir_strcmp(env, "_ENV") != 0)
{
lua_pop(L, 1);
return false;
}
return true;
}
CMLuaScript* CMLuaScript::GetScriptFromEnviroment(lua_State *L, int n)
{
if (!GetScriptEnviroment(L, n))
return NULL;
lua_getfield(L, -1, SCRIPT);
CMLuaScript *script = (CMLuaScript*)lua_touserdata(L, -1);
lua_pop(L, 3);
return script;
}
int CMLuaScript::GetScriptIdFromEnviroment(lua_State *L, int n)
{
CMLuaScript *script = GetScriptFromEnviroment(L, n);
if (script != NULL)
return script->id;
return hLangpack;
}
const char* CMLuaScript::GetModuleName() const
{
return moduleName;
}
const TCHAR* CMLuaScript::GetFilePath() const
{
return filePath;
}
const TCHAR* CMLuaScript::GetFileName() const
{
return fileName;
}
const CMLuaScript::Status CMLuaScript::GetStatus() const
{
return status;
}
bool CMLuaScript::Load()
{
status = Failed;
if (luaL_loadfile(L, T2Utf(filePath)))
{
Log(lua_tostring(L, -1));
return false;
}
lua_createtable(L, 0, 2);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "_G");
lua_pushlightuserdata(L, this);
lua_setfield(L, -2, "Script");
lua_createtable(L, 0, 2);
lua_getglobal(L, "_G");
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setupvalue(L, -2, 1);
if (luaM_pcall(L, 0, 1))
return false;
status = Loaded;
if (lua_isnoneornil(L, -1))
return true;
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, -1, moduleName);
if (!lua_toboolean(L, -1))
{
lua_pop(L, 1);
lua_pushvalue(L, -2);
lua_setfield(L, -2, moduleName);
lua_pop(L, 1);
}
else
lua_remove(L, -2);
if (!lua_istable(L, -1))
return true;
lua_getfield(L, -1, "Load");
if (lua_isfunction(L, -1))
luaM_pcall(L);
else
lua_pop(L, 1);
lua_getfield(L, -1, "Unload");
if (lua_isfunction(L, -1))
{
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, this);
//unloadRef = luaL_ref(L, LUA_REGISTRYINDEX);
}
lua_pop(L, 1);
lua_pop(L, 1);
return true;
}
void CMLuaScript::Unload()
{
if (status == Loaded)
{
lua_rawgetp(L, LUA_REGISTRYINDEX, this);
//lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef);
if (lua_isfunction(L, -1))
luaM_pcall(L);
lua_pushnil(L);
lua_rawsetp(L, LUA_REGISTRYINDEX, this);
status = None;
}
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);
KillModuleIcons(id);
KillModuleSounds(id);
KillModuleMenus(id);
KillModuleHotkeys(id);
KillObjectEventHooks(this);
KillObjectServices(this);
}
|