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
This commit is contained in:
helixhorned 2012-10-07 15:26:04 +00:00
parent f9e61ce37e
commit 2851f412d2
5 changed files with 68 additions and 5 deletions

View file

@ -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 <pal> <r> <g> <b> <flags>: 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 <filenames...>: compiles one or more M32 script files", osdcmd_include);
OSD_RegisterFunction("do", "do (m32 script ...): executes M32 script statements", osdcmd_do);

View file

@ -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);

View file

@ -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");
}

View file

@ -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

View file

@ -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 <lua.h>
#include <lauxlib.h>
@ -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);
}