From 8e97de69797b1935b24df4138e53061bd3ed1522 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Sep 2014 23:42:40 +1000 Subject: [PATCH] Added the scripts directory to lua's path This will allow modules. --- rsc/scripts/map_maker.lua | 7 +++++++ rsc/scripts/setup_server.lua | 3 +++ server/server_logic.cpp | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 rsc/scripts/map_maker.lua diff --git a/rsc/scripts/map_maker.lua b/rsc/scripts/map_maker.lua new file mode 100644 index 0000000..ccbd0a8 --- /dev/null +++ b/rsc/scripts/map_maker.lua @@ -0,0 +1,7 @@ +local mapMaker = {} + +function mapMaker.foo() + print("--> Hello map maker! <--") +end + +return mapMaker \ No newline at end of file diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index ab851c1..4b24ba9 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -1,5 +1,8 @@ print("Lua script check") +mapMaker = require "map_maker" +mapMaker.foo() + --uber lazy declarations function math.sqr(x) return x*x end function math.dist(x, y, i, j) return math.sqrt(math.sqr(x - i) + math.sqr(y - j)) end diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 51ed785..6e45929 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include //------------------------- @@ -71,8 +72,27 @@ void ServerApplication::Init(int argc, char* argv[]) { throw(std::runtime_error("Failed to initialize lua")); } luaL_openlibs(luaState); + std::cout << "Initialized lua" << std::endl; + //append config["dir.scripts"] to the module path + if (config["dir.scripts"].size() > 0) { + //get the original path + lua_getglobal(luaState, "package"); + lua_getfield(luaState, -1, "path"); + + //build & push the message + std::ostringstream path; + path << lua_tostring(luaState, -1) << ";" << config["dir.scripts"] << "?.lua"; + lua_pushstring(luaState, path.str().c_str()); + + //set the new path and clean up the stack + lua_setfield(luaState, -3, "path"); + lua_pop(luaState, 2); + + std::cout << "\tLua script directory appended" << std::endl; + } + //------------------------- //Setup the objects //-------------------------