Skip to content

Commit

Permalink
Lua function "dofile" rewritten. Now it works in 2 modes:
Browse files Browse the repository at this point in the history
1) Default mode: execute files only from mpqs;
2) Developer mode: execute files from mpqs and game folder.
  • Loading branch information
Ev3nt committed Jul 11, 2022
1 parent 7a0915a commit 218e356
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 144 deletions.
17 changes: 0 additions & 17 deletions 3rd/lua/Features/extended lua.cpp

This file was deleted.

120 changes: 0 additions & 120 deletions 3rd/lua/Features/extended lua.h

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# War3 Lua - 1.1.6 (<span style="color:orange">Outland</span>)
# War3 Lua - 1.1.7 (<span style="color:orange">Outland</span>)

[![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml)

Expand All @@ -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)**
52 changes: 48 additions & 4 deletions Src/LuaHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -83,6 +128,8 @@ namespace LuaHooks {

lua_pop(l, 1);
searchers.clear();

lua_register(l, "dofile", lua_dofile);
}

// -------------------------------------------------------------------------------- -
Expand Down Expand Up @@ -111,9 +158,6 @@ namespace LuaHooks {

lua_pop(l, 1);

lua_pushnil(l);
lua_setglobal(l, "dofile");

lua_pushnil(l);
lua_setglobal(l, "debug");
}
Expand Down
2 changes: 1 addition & 1 deletion Src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 218e356

Please sign in to comment.