diff options
author | aunsane <aunsane@gmail.com> | 2018-05-20 21:10:10 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-05-20 21:34:31 +0300 |
commit | a955d18f62f335f84e0926f351eb85d78224cba6 (patch) | |
tree | 7bd4d30040ec32bba5949dd2245cb68617fb4192 /plugins/MirLua/src/script_loader.cpp | |
parent | 46363eef857b69761f1d6d28da5a53a954f76900 (diff) |
MirLua: project reordering
Diffstat (limited to 'plugins/MirLua/src/script_loader.cpp')
-rw-r--r-- | plugins/MirLua/src/script_loader.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/plugins/MirLua/src/script_loader.cpp b/plugins/MirLua/src/script_loader.cpp new file mode 100644 index 0000000000..0092cca372 --- /dev/null +++ b/plugins/MirLua/src/script_loader.cpp @@ -0,0 +1,70 @@ +#include "stdafx.h"
+
+CMLuaScriptLoader::CMLuaScriptLoader(lua_State *L)
+ : L(L)
+{
+}
+
+void CMLuaScriptLoader::SetPaths()
+{
+ wchar_t path[MAX_PATH];
+
+ lua_getglobal(L, LUA_LOADLIBNAME);
+
+ FoldersGetCustomPathT(g_hCLibsFolder, path, _countof(path), VARSW(MIRLUA_PATHT));
+ lua_pushfstring(L, "%s\\?.dll", T2Utf(path));
+ lua_setfield(L, -2, "cpath");
+
+ FoldersGetCustomPathT(g_hScriptsFolder, path, _countof(path), VARSW(MIRLUA_PATHT));
+ lua_pushfstring(L, "%s\\?.lua", T2Utf(path));
+ lua_setfield(L, -2, "path");
+
+ lua_pop(L, 1);
+}
+
+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);
+ PathToRelativeW(fullPath, path);
+
+ CMLuaScript *script = new CMLuaScript(L, path);
+ g_plugin.Scripts.insert(script);
+
+ if (!script->IsEnabled()) {
+ Log(L"%s:PASS", path);
+ return;
+ }
+
+ if (script->Load())
+ Log(L"%s:OK", path);
+}
+
+void CMLuaScriptLoader::LoadScripts()
+{
+ SetPaths();
+
+ wchar_t scriptDir[MAX_PATH];
+ FoldersGetCustomPathT(g_hScriptsFolder, scriptDir, _countof(scriptDir), VARSW(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();
+}
\ No newline at end of file |