From 1b7b60cac21ea9c9e83bc4c246d70f1a9b8a906a Mon Sep 17 00:00:00 2001 From: nveid Date: Sun, 26 Dec 2010 21:11:28 +0000 Subject: [PATCH] Initial lua import --- game/lua/.gitify-empty | 0 game/lua/README | 1 + game/lua/test.lua | 4 +++ hdrs/mushlua.h | 6 ++++ lua/.gitify-empty | 0 src/bsd.c | 8 +++++ src/lua.c | 81 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 game/lua/.gitify-empty create mode 100644 game/lua/README create mode 100644 game/lua/test.lua create mode 100644 hdrs/mushlua.h create mode 100644 lua/.gitify-empty create mode 100644 src/lua.c diff --git a/game/lua/.gitify-empty b/game/lua/.gitify-empty new file mode 100644 index 0000000..e69de29 diff --git a/game/lua/README b/game/lua/README new file mode 100644 index 0000000..ac66777 --- /dev/null +++ b/game/lua/README @@ -0,0 +1 @@ +All MUSH lua scripts should be placed in here with *.lua set as the file suffix diff --git a/game/lua/test.lua b/game/lua/test.lua new file mode 100644 index 0000000..b732dc1 --- /dev/null +++ b/game/lua/test.lua @@ -0,0 +1,4 @@ +-- Test lua script file +do + mush.notify(1, "Hi", test, "long" , " ", "string") +end diff --git a/hdrs/mushlua.h b/hdrs/mushlua.h new file mode 100644 index 0000000..8ba4839 --- /dev/null +++ b/hdrs/mushlua.h @@ -0,0 +1,6 @@ +#ifndef _MUSHLUA_H_ +#define _MUSHLUA_H_ + +void mush_lua_start(void); +void mush_lua_stop(void); +#endif /* _MUSHLUA_H_ */ diff --git a/lua/.gitify-empty b/lua/.gitify-empty new file mode 100644 index 0000000..e69de29 diff --git a/src/bsd.c b/src/bsd.c index 6ac6c96..603a869 100644 --- a/src/bsd.c +++ b/src/bsd.c @@ -126,6 +126,7 @@ #include "game.h" #include "dbio.h" #include "confmagic.h" +#include "mushlua.h" #ifdef HAS_WAITPID /** What does wait*() return? */ #define WAIT_TYPE int @@ -546,6 +547,10 @@ main(int argc, char **argv) fclose(id); #endif + /* Start lua enivonrment + * Eventually we'll conver the config file to a big lua script, so we'll start the lua envionrment before it */ + mush_lua_start(); + strncpy(confname, argv[1], BUFFER_LEN - 1); confname[BUFFER_LEN - 1] = '\0'; init_game_config(confname); @@ -565,6 +570,8 @@ main(int argc, char **argv) } init_qids(); + + /* load databases */ if (init_game_dbs() < 0) { do_rawlog(LT_ERR, T("ERROR: Couldn't load databases! Exiting.")); exit(2); @@ -619,6 +626,7 @@ main(int argc, char **argv) close_sockets(); sql_shutdown(); + mush_lua_stop(); /* shutdown the lua environment */ #ifndef COMPILE_CONSOLE #ifdef INFO_SLAVE diff --git a/src/lua.c b/src/lua.c new file mode 100644 index 0000000..5d11b9d --- /dev/null +++ b/src/lua.c @@ -0,0 +1,81 @@ +/* lua API for CobraMUSH */ +#include "config.h" +#include +#include "conf.h" +#include "dbio.h" +#include "externs.h" +#include "parse.h" +#include "htab.h" +#include "command.h" +#include "confmagic.h" + + +#include "mushlua.h" +#include "lua.h" +#include "lualib.h" +#include "lauxlib.c" + +static lua_State *mush_lua_env = NULL; +static lua_CFunction mlua_notify(lua_State *); +void mlua_test(dbref); + + +static const luaL_reg mlua_lib[] = { + {"notify", mlua_notify}, + {NULL, NULL} +}; + +void mush_lua_start() { + + mush_lua_env = lua_open(); + + luaL_register(mush_lua_env, "mush", mlua_lib); + + luaL_openlibs(mush_lua_env); +} + +void mush_lua_stop() { + + if(!mush_lua_env) + return; + + lua_close(mush_lua_env); +} + +/* Until we have real stuff.. this tests my lua scripts.. */ +void mlua_test(dbref enactor) { + int s; + + s = luaL_loadfile(mush_lua_env, "lua/test.lua"); + + if(s==0) { + s = lua_pcall(mush_lua_env, 0, LUA_MULTRET, 0); + } + + if(s != 0) + notify_format(enactor, "Lua Error: %s\n", lua_tostring(mush_lua_env, -1)); +} + +/* lua to MUSH API functions */ +static lua_CFunction mlua_notify(lua_State *L) { + int nargs; + dbref obj; + + nargs = lua_gettop(L); + + if(nargs < 2) { + lua_pushstring(L, "incorrect number of arguments"); + lua_error(L); + + } else if(!lua_isnumber(L, 1)) { + lua_pushstring(L, "first argument must be an integer"); + lua_error(L); + } else { + + lua_concat(L, nargs-1); + obj = lua_tonumber(L, 1); + + notify(obj, lua_tostring(L, -1) ); + } +} + -- 2.30.2