From 2851f412d2c9965016f86b7314d145f1e33227c6 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 7 Oct 2012 15:26:04 +0000 Subject: [PATCH] Lunatic-m32: add an OSD command to be able to interface with stuff. git-svn-id: https://svn.eduke32.com/eduke32@3058 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/astub.c | 29 ++++++++++++++++ polymer/eduke32/source/lunatic/lunatic.c | 1 + polymer/eduke32/source/lunatic/lunatic_m32.c | 8 ++++- polymer/eduke32/source/lunatic/lunatic_m32.h | 1 + polymer/eduke32/source/lunatic/lunatic_priv.h | 34 ++++++++++++++++--- 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/polymer/eduke32/source/astub.c b/polymer/eduke32/source/astub.c index a6c808ea7..c54042490 100644 --- a/polymer/eduke32/source/astub.c +++ b/polymer/eduke32/source/astub.c @@ -9184,6 +9184,32 @@ static int32_t osdcmd_tint(const osdfuncparm_t *parm) } #endif +#ifdef LUNATIC +static int32_t osdcmd_lua(const osdfuncparm_t *parm) +{ + // Should be used like + // lua "lua code..." + // (the quotes making the whole string passed as one argument) + + int32_t ret; + + if (parm->numparms != 1) + return OSDCMD_SHOWHELP; + + if (g_EmState == NULL) + { + OSD_Printf("Lua state is not initialized.\n"); + return OSDCMD_OK; + } + + ret = Em_RunStringOnce(g_EmState, parm->parms[0]); + if (ret != 0) + OSD_Printf("Error running the Lua code (error code %d)\n", ret); + + return OSDCMD_OK; +} +#endif + // M32 script vvv static int32_t osdcmd_include(const osdfuncparm_t *parm) { @@ -9430,6 +9456,9 @@ static int32_t registerosdcommands(void) OSD_RegisterFunction("tint", "tint : queries or sets hightile tinting", osdcmd_tint); #endif +#ifdef LUNATIC + OSD_RegisterFunction("lua", "lua \"lua code...\": runs Lua code", osdcmd_lua); +#endif // M32 script OSD_RegisterFunction("include", "include : compiles one or more M32 script files", osdcmd_include); OSD_RegisterFunction("do", "do (m32 script ...): executes M32 script statements", osdcmd_do); diff --git a/polymer/eduke32/source/lunatic/lunatic.c b/polymer/eduke32/source/lunatic/lunatic.c index d89ba4f07..bbee4a3f2 100644 --- a/polymer/eduke32/source/lunatic/lunatic.c +++ b/polymer/eduke32/source/lunatic/lunatic.c @@ -142,6 +142,7 @@ void El_DestroyState(El_State *estate) // 2: couldn't read whole file // 3: syntax error in lua file // 4: runtime error while executing lua file +// 5: empty file int32_t El_RunOnce(El_State *estate, const char *fn) { return lunatic_run_once(estate->L, fn, estate->name); diff --git a/polymer/eduke32/source/lunatic/lunatic_m32.c b/polymer/eduke32/source/lunatic/lunatic_m32.c index 3b3564dbb..39e895105 100644 --- a/polymer/eduke32/source/lunatic/lunatic_m32.c +++ b/polymer/eduke32/source/lunatic/lunatic_m32.c @@ -29,7 +29,13 @@ Em_State *Em_CreateState(void) // 2: couldn't read whole file // 3: syntax error in lua file // 4: runtime error while executing lua file +// 5: empty file int32_t Em_RunOnce(Em_State *L, const char *fn) { - return lunatic_run_once(L, fn, "test"); + return lunatic_run_once(L, fn, "file"); +} + +int32_t Em_RunStringOnce(Em_State *L, const char *str) +{ + return lunatic_run_string(L, (char *)str, 0, "string"); } diff --git a/polymer/eduke32/source/lunatic/lunatic_m32.h b/polymer/eduke32/source/lunatic/lunatic_m32.h index 00e7a6efc..e3ac00e5c 100644 --- a/polymer/eduke32/source/lunatic/lunatic_m32.h +++ b/polymer/eduke32/source/lunatic/lunatic_m32.h @@ -15,5 +15,6 @@ static inline void Em_DestroyState(Em_State *L) } int32_t Em_RunOnce(Em_State *L, const char *fn); +int32_t Em_RunStringOnce(Em_State *L, const char *str); #endif diff --git a/polymer/eduke32/source/lunatic/lunatic_priv.h b/polymer/eduke32/source/lunatic/lunatic_priv.h index c51638041..e61d55d2b 100644 --- a/polymer/eduke32/source/lunatic/lunatic_priv.h +++ b/polymer/eduke32/source/lunatic/lunatic_priv.h @@ -1,4 +1,6 @@ -/* Private, game/editor-common Lunatic routines. */ +/* Private, game/editor-common Lunatic routines. + * This file is intended to be included exactly ONCE in the game and editor + * Lunatic sources. */ #include #include @@ -30,11 +32,14 @@ static void setup_debug_traceback(lua_State *L) lua_pop(L, 2); } -static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *statename) + +static int32_t read_whole_file(const char *fn, char **retbufptr) { int32_t fid, flen, i; char *buf; + *retbufptr = NULL; + fid = kopen4load(fn, 0); // TODO: g_loadFromGroupOnly, kopen4loadfrommod ? if (fid < 0) @@ -42,7 +47,7 @@ static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *staten flen = kfilelength(fid); if (flen == 0) - return 0; // empty script ... + return 5; buf = Bmalloc(flen+1); if (!buf) @@ -61,6 +66,14 @@ static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *staten } buf[flen] = 0; + *retbufptr = buf; + + return 0; +} + +static int32_t lunatic_run_string(lua_State *L, char *buf, int32_t dofreebuf, const char *statename) +{ + int32_t i; // -- lua -- Bassert(lua_gettop(L)==0); @@ -72,7 +85,8 @@ static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *staten i = luaL_loadstring(L, buf); Bassert(lua_gettop(L)==2); - Bfree(buf); + if (dofreebuf) + Bfree(buf); if (i == LUA_ERRMEM) { @@ -115,3 +129,15 @@ static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *staten return 0; } + +static int32_t lunatic_run_once(lua_State *L, const char *fn, const char *statename) +{ + int32_t i; + char *buf; + + i = read_whole_file(fn, &buf); + if (i != 0) + return i; + + return lunatic_run_string(L, buf, 1, statename); +}