From 7a9d5e2a383608f01eb4b1a7f46e28f56796749d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D0=B3=D0=B4=D0=B0=D0=BD=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D0=B3=D0=B5=D1=80?= Date: Tue, 12 Jul 2022 01:39:27 +0300 Subject: [PATCH] Lua function "dofile" fixed, "loadfile" rewritten as "dofile". --- README.md | 2 +- Src/LuaHooks.cpp | 73 +++++++++++++++++++++++++++++++++++++----------- Src/pch.h | 2 +- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a3c999b..8efe14a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# War3 Lua - 1.1.7 (Outland) +# War3 Lua - 1.1.8 (Outland) [![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml) diff --git a/Src/LuaHooks.cpp b/Src/LuaHooks.cpp index d42ec16..445f1f7 100644 --- a/Src/LuaHooks.cpp +++ b/Src/LuaHooks.cpp @@ -50,6 +50,14 @@ namespace LuaHooks { return lua_gettop(l) - 1; } + int luaL_loadbufferasfile(lua_State* l, LPCSTR buffer, size_t size, std::string name, LPCSTR mode = NULL) { + int status = luaL_loadbufferx(l, buffer, size, ("@" + name).c_str(), mode); + + !size ? lua_pop(l, 1), lua_pushfstring(l, "cannot open %s: No such file or directory", name.c_str()) : NULL; // strerror(2) + + return size ? status : LUA_ERRFILE; + } + int lua_dofile(lua_State* l) { std::string scriptName = luaL_optstring(l, 1, NULL); lua_settop(l, 1); @@ -63,27 +71,17 @@ namespace LuaHooks { } std::string script = map[scriptName]; - if (!script.empty()) { - if (luaL_loadbuffer(l, script.c_str(), script.size(), ("@(" + mapPath + "):\\" + scriptName).c_str()) != LUA_OK) { - if (developerMode) { - lua_pop(l, 1); + if (luaL_loadbufferasfile(l, script.c_str(), script.size(), ("(" + mapPath + "):\\" + scriptName)) != LUA_OK) { + if (developerMode) { + lua_pop(l, 1); - ifDeveloperMode: - if (luaL_loadfile(l, scriptName.c_str()) != LUA_OK) { - return lua_error(l); - } - } - else { + if (luaL_loadfile(l, scriptName.c_str()) != LUA_OK) { return lua_error(l); } } - } - else { - if (developerMode) { - goto ifDeveloperMode; + else { + return lua_error(l); } - - return luaL_error(l, "cannot open %s: No such file or directory", scriptName.c_str()); } lua_callk(l, 0, LUA_MULTRET, 0, dofilecont); @@ -91,6 +89,46 @@ namespace LuaHooks { return dofilecont(l, 0, 0); } + int load_aux(lua_State* l, int status, int envidx) { + if (status == LUA_OK) { + if (envidx) { + lua_pushvalue(l, envidx); + + if (!lua_setupvalue(l, -2, 1)) { + lua_pop(l, 1); + } + } + + return 1; + } + else { + luaL_pushfail(l); + lua_insert(l, -2); + + return 2; + } + } + + int lua_loadfile(lua_State* l) { + std::string scriptName = luaL_optstring(l, 1, NULL); + LPCSTR mode = luaL_optstring(l, 2, NULL); + int env = (!lua_isnone(l, 3) ? 3 : 0); + + Storm::Archive map; + std::string mapPath = map.GetArchiveName(scriptName); + if (mapPath.empty()) { + map.Connect(*pMapMpq); + mapPath = map.GetArchiveName(); + map.Close(); + } + + std::string script = map[scriptName]; + int status = luaL_loadbufferasfile(l, script.c_str(), script.size(), ("(" + mapPath + "):\\" + scriptName)); + status != LUA_OK && developerMode ? lua_pop(l, 1), status = luaL_loadfilex(l, scriptName.c_str(), mode) : NULL; + + return load_aux(l, status, env); + } + //-------------------------------------------------------------- void lua_replaceSearchers(lua_State* l) { @@ -130,10 +168,11 @@ namespace LuaHooks { searchers.clear(); lua_register(l, "dofile", lua_dofile); + lua_register(l, "loadfile", lua_loadfile); } // -------------------------------------------------------------------------------- - -// Disabled functions + // Disabled functions void lua_disableFunctions(lua_State* l) { lua_getglobal(l, "os"); diff --git a/Src/pch.h b/Src/pch.h index 308b27a..da0baa3 100644 --- a/Src/pch.h +++ b/Src/pch.h @@ -23,7 +23,7 @@ #define WAR3_LUA_MAJOR "1" #define WAR3_LUA_MINOR "1" -#define WAR3_LUA_RELEASE "7" +#define WAR3_LUA_RELEASE "8" #define WAR3_LUA_VERSION_NAME ANSI_COLOR_YELLOW "Outland" ANSI_COLOR_RESET #define WAR3_LUA_VERSION WAR3_LUA_MAJOR "." WAR3_LUA_MINOR "." WAR3_LUA_RELEASE