summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/script.cpp
blob: a02467b6a3079b6a596b8b49154d795152a497f9 (plain)
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
#include "stdafx.h"

#define MT_SCRIPT "SCRIPT"

CMLuaScript::CMLuaScript(lua_State *L, const wchar_t *path)
	: CMLuaEnvironment(L),
	isBinary(false),
	status(ScriptStatus::None),
	unloadRef(LUA_NOREF)
{
	mir_wstrcpy(filePath, path);

	const wchar_t *fileName = wcsrchr(filePath, L'\\') + 1;
	const wchar_t *dot = wcsrchr(fileName, '.');

	size_t length = mir_wstrlen(fileName) - mir_wstrlen(dot) + 1;

	scriptName = (wchar_t*)mir_calloc(sizeof(wchar_t) * (length + 1));
	mir_wstrncpy(scriptName, fileName, length);

	m_szModuleName = mir_utf8encodeW(scriptName);

	isBinary = mir_wstrcmpi(dot + 1, LUAPRECSCRIPTEXT) == 0;
}

CMLuaScript::CMLuaScript(const CMLuaScript &script)
	: CMLuaEnvironment(script.L), isBinary(script.isBinary),
	status(ScriptStatus::None), unloadRef(LUA_NOREF)
{
	mir_wstrcpy(filePath, script.filePath);
	scriptName = mir_wstrdup(script.scriptName);
	m_szModuleName = mir_strdup(script.m_szModuleName);
}

CMLuaScript::~CMLuaScript()
{
	//Unload();
	mir_free((void*)m_szModuleName);
	mir_free((void*)scriptName);
}

const wchar_t* CMLuaScript::GetFilePath() const
{
	return filePath;
}

const wchar_t* CMLuaScript::GetName() const
{
	return scriptName;
}

bool CMLuaScript::IsBinary() const
{
	return isBinary;
}

bool CMLuaScript::IsEnabled() const
{
	return g_plugin.getByte(_T2A(scriptName), 1);
}

void CMLuaScript::Enable()
{
	g_plugin.delSetting(_T2A(scriptName));
}

void CMLuaScript::Disable()
{
	g_plugin.setByte(_T2A(scriptName), 0);
}

ScriptStatus CMLuaScript::GetStatus() const
{
	return status;
}

int CMLuaScript::Load()
{
	status = ScriptStatus::Failed;

	if (luaL_loadfile(L, _T2A(filePath))) {
		ReportError(L);
		return false;
	}

	CMLuaEnvironment::CreateEnvironmentTable();

	if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
		ReportError(L);
		return false;
	}

	status = ScriptStatus::Loaded;

	if (lua_isnoneornil(L, -1))
		return true;

	luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
	lua_getfield(L, -1, m_szModuleName);
	if (!lua_toboolean(L, -1)) {
		lua_pop(L, 1);
		lua_pushvalue(L, -2);
		lua_setfield(L, -2, m_szModuleName);
		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);
		unloadRef = luaL_ref(L, LUA_REGISTRYINDEX);
	}
	lua_pop(L, 1);

	lua_pop(L, 1);

	return true;
}

int CMLuaScript::Unload()
{
	if (status == ScriptStatus::Loaded) {
		lua_rawgeti(L, LUA_REGISTRYINDEX, unloadRef);
		if (lua_isfunction(L, -1))
			luaM_pcall(L);
		lua_pushnil(L);
		lua_rawsetp(L, LUA_REGISTRYINDEX, this);
		status = ScriptStatus::None;
	}

	luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
	lua_pushnil(L);
	lua_setfield(L, -2, m_szModuleName);
	lua_pop(L, 1);
	
	return CMLuaEnvironment::Unload();
}

bool CMLuaScript::Reload()
{
	Log(L"Reloading script %s", filePath);
	Unload();
	return Load();
}

static int luc_Writer(lua_State* /*L*/, const void *p, size_t sz, void *u)
{
	return (fwrite(p, sz, 1, (FILE*)u) != 1) && (sz != 0);
}

bool CMLuaScript::Compile()
{
	Unload();

	Log(L"Compiling script %s", filePath);

	wchar_t scriptDir[MAX_PATH];
	FoldersGetCustomPathW(g_hScriptsFolder, scriptDir, _countof(scriptDir), VARSW(MIRLUA_PATHT));
	wchar_t fullPath[MAX_PATH];
	mir_snwprintf(fullPath, L"%s\\%s.%s", scriptDir, scriptName, LUAPRECSCRIPTEXT);
	wchar_t path[MAX_PATH];
	PathToRelativeW(fullPath, path);

	FILE *file = _wfopen(path, L"wb");
	if (file == nullptr) {
		Log(L"Failed to save compiled script to %s", file);
		return false;
	}

	if (luaL_loadfile(L, _T2A(filePath))) {
		ReportError(L);
		fclose(file);
		return false;
	}

	int res = lua_dump(L, luc_Writer, file, 1);
	if (res != 0) {
		fclose(file);
		return false;
	}

	fclose(file);

	ptrW newPath(mir_wstrdup(filePath));
	newPath[mir_wstrlen(newPath) - 1] = L'_';
	MoveFile(filePath, newPath);
	mir_wstrcpy(filePath, path);

	return Load();
}