summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_script_loader.cpp
blob: 3a3d88fbc75aed5b87f173d40f1ffb18cadd6895 (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
#include "stdafx.h"

CMLuaScriptLoader::CMLuaScriptLoader(lua_State *L) : L(L)
{
}

void CMLuaScriptLoader::LoadScript(const wchar_t *scriptDir, const wchar_t *file)
{
	wchar_t fullPath[MAX_PATH], path[MAX_PATH];
	mir_snwprintf(fullPath, L"%s\\%s", scriptDir, file);
	PathToRelativeT(fullPath, path);

	CMLuaScript *script = new CMLuaScript(L, path);
	g_mLua->Scripts.insert(script);

	if (db_get_b(NULL, MODULE, _T2A(file), 1) == FALSE)
	{
		Log(L"%s:PASS", path);
		return;
	}

	if (script->Load())
		Log(L"%s:OK", path);
}

void CMLuaScriptLoader::LoadScripts()
{
	wchar_t scriptDir[MAX_PATH];
	FoldersGetCustomPathT(g_hScriptsFolder, scriptDir, _countof(scriptDir), VARST(MIRLUA_PATHT));

	Log(L"Loading scripts from %s", scriptDir);

	wchar_t searchMask[MAX_PATH];
	mir_snwprintf(searchMask, L"%s\\%s", scriptDir, L"*.lua");

	WIN32_FIND_DATA fd;
	HANDLE hFind = FindFirstFile(searchMask, &fd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				continue;
			LoadScript(scriptDir, fd.cFileName);
		} while (FindNextFile(hFind, &fd));
		FindClose(hFind);
	}
}

void CMLuaScriptLoader::Load(lua_State *L)
{
	CMLuaScriptLoader(L).LoadScripts();
}