Skip to content

Commit

Permalink
Added require from map.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ev3nt committed Dec 1, 2021
1 parent 2c226b4 commit a16a986
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# War3 Lua - 1.1.1
# War3 Lua - 1.1.2 (Beta)

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

Expand Down
99 changes: 98 additions & 1 deletion Src/LuaMachine.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LuaMachine.h"

#include <fstream>
#include <Storm.h>
#pragma comment(lib, "storm.lib")

Expand All @@ -18,6 +19,60 @@ void lua_preload(lua_State* l, LPCSTR name, lua_CFunction function)
lua_pop(l, 1);
}

//-----------------------------------------------------------------------------

static int checkload(lua_State* L, int stat, const char* filename) {
if (stat) {
lua_pushstring(L, filename);
return 2;
}
else
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}

static int searcher_LuaInMpq(lua_State* l) {
HANDLE map = *(HANDLE*)MakePtr(hGame, _MapMPQ);

std::string filename = luaL_checkstring(l, 1) + std::string(".lua");
lua_pop(l, 1);

HANDLE file;
if (SFileOpenFileEx(map, filename.c_str(), NULL, &file)) {
int lenght = SFileGetFileSize(file, NULL);
char* buffer = new char[lenght + 1];

ZeroMemory(buffer, lenght + 1);

SFileReadFile(file, buffer, lenght, NULL, NULL);
SFileCloseFile(file);

int result = checkload(l, (luaL_loadstring(l, buffer) == LUA_OK), filename.c_str());
delete[] buffer;

return result;
}

char mapname[MAX_PATH] = { 0 };
SFileGetArchiveName(map, mapname, sizeof(mapname));
std::string mapnamestring = mapname;

for (size_t i = mapnamestring.size(); i > 0; i--) {
if (mapnamestring[i] == '\\') {
mapnamestring = mapnamestring.substr(i + 1);

break;
}
}


lua_pushstring(l, std::string("no file '(" + mapnamestring + "):\\" + filename + "'").c_str());

return 1;
}

//-----------------------------------------------------------------------------

lua_State* GetMainLuaState()
{
if (!MainLuaState)
Expand All @@ -26,6 +81,48 @@ lua_State* GetMainLuaState()

luaL_openlibs(l);
lua_open_jassnatives(l);

std::vector<lua_CFunction> searchers;

lua_getglobal(l, "package");
lua_getfield(l, -1, "searchers");

lua_rawgeti(l, -1, 1);
searchers.push_back(lua_tocfunction(l, -1));
lua_pop(l, 1);

// for (int i = 1; ; i++) {
// if (lua_rawgeti(l, -1, i) == LUA_TNIL) {
// lua_pop(l, 1);
//
// break;
// }
//
// searchers.push_back(lua_tocfunction(l, -1));
// lua_pop(l, 1);
// }
//
// searchers.pop_back();
// searchers.pop_back();
//
//searchers.insert(searchers.begin() + 1, searcher_LuaInMpq);

searchers.push_back(searcher_LuaInMpq);

for (size_t i = 0; i < searchers.size(); i++) {
lua_pushvalue(l, -2);
lua_pushcclosure(l, searchers[i], 1);
lua_rawseti(l, -2, i + 1);
}

lua_pushnil(l);
lua_rawseti(l, -2, 3);

lua_pushnil(l);
lua_rawseti(l, -2, 4);

lua_pop(l, 2);
std::vector<lua_CFunction>().swap(searchers);
}

return MainLuaState;
Expand Down Expand Up @@ -158,7 +255,7 @@ void StartLua()
HANDLE hFile;
if (SFileOpenFileEx(hMapMPQ, "war3map.lua", NULL, &hFile))
{
int lenght = 65535;
int lenght = SFileGetFileSize(hFile, NULL);
char* buffer = new char[lenght];

ZeroMemory(buffer, lenght);
Expand Down
2 changes: 1 addition & 1 deletion Src/Variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#define WAR3_LUA_MAJOR "1"
#define WAR3_LUA_MINOR "1"
#define WAR3_LUA_RELEASE "1"
#define WAR3_LUA_RELEASE "2 (Beta)"

#define WAR3_LUA_VERSION WAR3_LUA_MAJOR "." WAR3_LUA_MINOR "." WAR3_LUA_RELEASE
#define WAR3_LUA "War3 Lua " WAR3_LUA_VERSION
Expand Down

0 comments on commit a16a986

Please sign in to comment.