From 218e356979467deb6008f04eab514a98dc7ea379 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: Mon, 11 Jul 2022 23:49:30 +0300 Subject: [PATCH] Lua function "dofile" rewritten. Now it works in 2 modes: 1) Default mode: execute files only from mpqs; 2) Developer mode: execute files from mpqs and game folder. --- 3rd/lua/Features/extended lua.cpp | 17 ----- 3rd/lua/Features/extended lua.h | 120 ------------------------------ README.md | 4 +- Src/LuaHooks.cpp | 52 ++++++++++++- Src/pch.h | 2 +- 5 files changed, 51 insertions(+), 144 deletions(-) delete mode 100644 3rd/lua/Features/extended lua.cpp delete mode 100644 3rd/lua/Features/extended lua.h diff --git a/3rd/lua/Features/extended lua.cpp b/3rd/lua/Features/extended lua.cpp deleted file mode 100644 index 1f65146..0000000 --- a/3rd/lua/Features/extended lua.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "extended lua.h" - -LUA_API void lua_push(lua_State* L, UINT value, int type) { - lua_lock(L); - setvalue(s2v(L->top), value, type); - api_incr_top(L); - lua_unlock(L); -} - -//LUA_API lua_Integer lua_to(lua_State* L, int idx, int type) { -// lua_Integer res = 0; -// const TValue* o = index2value(L, idx); -// int isnum = tointeger(o, &res); -// if (pisnum) -// *pisnum = isnum; -// return res; -//} \ No newline at end of file diff --git a/3rd/lua/Features/extended lua.h b/3rd/lua/Features/extended lua.h deleted file mode 100644 index 78e1467..0000000 --- a/3rd/lua/Features/extended lua.h +++ /dev/null @@ -1,120 +0,0 @@ -#pragma once - -#include "./lobject.h" -#include "./lapi.h" -typedef unsigned int UINT; - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - -// Warcraft Types -#define LUA_TAGENT -#define LUA_TEVENT -#define LUA_TPLAYER -#define LUA_TWIDGET -#define LUA_TUNIT -#define LUA_TDESTRUCTABLE -#define LUA_TITEM -#define LUA_TABILITY -#define LUA_TBUFF -#define LUA_TFORCE -#define LUA_TGROUP -#define LUA_TTRIGGER -#define LUA_TTRIGGERCONDITION -#define LUA_TTRIGGERACTION -#define LUA_TTIMER -#define LUA_TLOCATION -#define LUA_TREGION -#define LUA_TRECT -#define LUA_TBOOLEXPR -#define LUA_TSOUND -#define LUA_TCONDITIONFUNC -#define LUA_TFILTERFUNC -#define LUA_TUNITPOOL -#define LUA_TITEMPOOL -#define LUA_TRACE -#define LUA_TALLIANCETYPE -#define LUA_TRACEPREFERENCE -#define LUA_TGAMESTATE -#define LUA_TIGAMESTATE -#define LUA_TFGAMESTATE -#define LUA_TPLAYERSTATE -#define LUA_TPLAYERSCORE -#define LUA_TPLAYERGAMERESULT -#define LUA_TUNITSTATE -#define LUA_TAIDIFFICULTY - -#define LUA_TEVENTID -#define LUA_TGAMEEVENT -#define LUA_TPLAYEREVENT -#define LUA_TPLAYERUNITEVENT -#define LUA_TUNITEVENT -#define LUA_TLIMITOP -#define LUA_TWIDGETEVENT -#define LUA_TDIALOGEVENT -#define LUA_TUNITTYPE - -#define LUA_TGAMESPEED -#define LUA_TGAMEDIFFICULTY -#define LUA_TGAMETYPE -#define LUA_TMAPFLAG -#define LUA_TMAPVISIBILITY -#define LUA_TMAPSETTING -#define LUA_TMAPDENSITY -#define LUA_TMAPCONTROL -#define LUA_TPLAYERSLOTSTATE -#define LUA_TVOLUMEGROUP -#define LUA_TCAMERAFIELD -#define LUA_TCAMERASETUP -#define LUA_TPLAYERCOLOR -#define LUA_TPLACEMENT -#define LUA_TSTARTLOCPRIO -#define LUA_TRARITYCONTROL -#define LUA_TBLENDMODE -#define LUA_TTEXMAPFLAGS -#define LUA_TEFFECT -#define LUA_TEFFECTTYPE -#define LUA_TWEATHEREFFECT -#define LUA_TTERRAINDEFORMATION -#define LUA_TFOGSTATE -#define LUA_TFOGMODIFIER -#define LUA_TDIALOG -#define LUA_TBUTTON -#define LUA_TQUEST -#define LUA_TQUESTITEM -#define LUA_TDEFEATCONDITION -#define LUA_TTIMERDIALOG -#define LUA_TLEADERBOARD -#define LUA_TMULTIBOARD -#define LUA_TMULTIBOARDITEM -#define LUA_TTRACKABLE -#define LUA_TGAMECACHE -#define LUA_TVERSION -#define LUA_TITEMTYPE -#define LUA_TTEXTTAG -#define LUA_TATTACKTYPE -#define LUA_TDAMAGETYPE -#define LUA_TWEAPONTYPE -#define LUA_TSOUNDTYPE -#define LUA_TLIGHTNING -#define LUA_TPATHINGTYPE -#define LUA_TIMAGE -#define LUA_TUBERSPLAT -#define LUA_THASHTABLE - -#define LUA_NUMTYPES 9 // default 9 - -#define setvalue(obj,x,t) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, makevariant(t, 0)); } \ No newline at end of file diff --git a/README.md b/README.md index 6778b25..a3c999b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# War3 Lua - 1.1.6 (Outland) +# War3 Lua - 1.1.7 (Outland) [![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml) @@ -25,5 +25,5 @@ Lua version: **5.4.4** ### Recommended build parameters * Configuration **Release** * Windows platform **x86** -* Platform **10.0.19041.**0**** +* Platform **10.0.19041.0** * Build tools **Visual Studio 2015 (v140)** diff --git a/Src/LuaHooks.cpp b/Src/LuaHooks.cpp index c9f2af9..d42ec16 100644 --- a/Src/LuaHooks.cpp +++ b/Src/LuaHooks.cpp @@ -20,7 +20,7 @@ namespace LuaHooks { int searcher_Lua(lua_State* l) { Storm::Archive map; std::string scriptName = luaL_checkstring(l, 1); - if (!scriptName.compare("war3map")) { + if (scriptName == "war3map") { map.Connect(*pMapMpq); } @@ -46,6 +46,51 @@ namespace LuaHooks { return 1; } + int dofilecont(lua_State* l, int d1, lua_KContext d2) { + return lua_gettop(l) - 1; + } + + int lua_dofile(lua_State* l) { + std::string scriptName = luaL_optstring(l, 1, NULL); + lua_settop(l, 1); + + 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]; + if (!script.empty()) { + if (luaL_loadbuffer(l, script.c_str(), script.size(), ("@(" + mapPath + "):\\" + scriptName).c_str()) != LUA_OK) { + if (developerMode) { + lua_pop(l, 1); + + ifDeveloperMode: + if (luaL_loadfile(l, scriptName.c_str()) != LUA_OK) { + return lua_error(l); + } + } + else { + return lua_error(l); + } + } + } + else { + if (developerMode) { + goto ifDeveloperMode; + } + + return luaL_error(l, "cannot open %s: No such file or directory", scriptName.c_str()); + } + + lua_callk(l, 0, LUA_MULTRET, 0, dofilecont); + + return dofilecont(l, 0, 0); + } + //-------------------------------------------------------------- void lua_replaceSearchers(lua_State* l) { @@ -83,6 +128,8 @@ namespace LuaHooks { lua_pop(l, 1); searchers.clear(); + + lua_register(l, "dofile", lua_dofile); } // -------------------------------------------------------------------------------- - @@ -111,9 +158,6 @@ namespace LuaHooks { lua_pop(l, 1); - lua_pushnil(l); - lua_setglobal(l, "dofile"); - lua_pushnil(l); lua_setglobal(l, "debug"); } diff --git a/Src/pch.h b/Src/pch.h index 5cbece3..308b27a 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 "6" +#define WAR3_LUA_RELEASE "7" #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