Lunatic: add 'lua' OSD command, permitting to execute arbitrary code.

That is, everything that would be possible via scripting.

git-svn-id: https://svn.eduke32.com/eduke32@4134 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-11-04 22:56:03 +00:00
parent e132073640
commit 794edf18ff
4 changed files with 43 additions and 2 deletions

View File

@ -9552,7 +9552,7 @@ static int32_t registerosdcommands(void)
#endif
#ifdef LUNATIC
OSD_RegisterFunction("lua", "lua \"lua code...\": runs Lua code", osdcmd_lua);
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);

View File

@ -194,6 +194,7 @@ finish:
#define EL_MAXERRORS 20
static int32_t el_numErrors=0, el_tooMuchErrors;
static char *el_errorMsgs[EL_MAXERRORS];
int8_t el_addNewErrors = 1; // add new errors to display?
// Compare against all other error messages.
// Strictly seen, this is quadratic-time, but EL_MAXERRORS is small and
@ -209,7 +210,7 @@ static int32_t cmp_against_others(const char *str, int32_t slen)
LUNATIC_EXTERN void El_OnError(const char *str)
{
if (!el_tooMuchErrors)
if (el_addNewErrors && !el_tooMuchErrors)
{
char *errstr = NULL;
const char *nl = Bstrchr(str, '\n');

View File

@ -39,6 +39,7 @@ void El_DestroyState(L_State *estate);
int32_t El_CallEvent(L_State *estate, int32_t eventidx, int32_t iActor, int32_t iPlayer, int32_t lDist, int32_t *iReturn);
int32_t El_CallActor(L_State *estate, int32_t actortile, int32_t iActor, int32_t iPlayer, int32_t lDist);
extern int8_t el_addNewErrors; // add new errors to display?
void El_OnError(const char *str);
int32_t (*El_RestoreGamevars)(const char *savecode);

View File

@ -39,6 +39,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <math.h>
#include "enet/enet.h"
#ifdef LUNATIC
# include "lunatic_game.h"
#endif
extern int32_t voting, g_doQuickSave;
struct osdcmd_cheatsinfo osdcmd_cheatsinfo_stat;
float r_ambientlight = 1.0, r_ambientlightrecip = 1.0;
@ -612,6 +616,39 @@ static int32_t osdcmd_setactorvar(const osdfuncparm_t *parm)
Gv_SetVar(i, varval, ID, -1);
return OSDCMD_OK;
}
#else
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 (!L_IsInitialized(&g_ElState))
{
OSD_Printf("Lua state is not initialized.\n");
return OSDCMD_OK;
}
// TODO: "=<expr>" as shorthand for "print(<expr>)", like in the
// stand-alone Lua interpreter?
// TODO: reserve some table to explicitly store stuff on the top level, for
// debugging convenience?
// For the 'lua' OSD command, don't make errors appear on-screen:
el_addNewErrors = 0;
ret = L_RunString(&g_ElState, (char *)parm->parms[0], 0, -1, "console");
el_addNewErrors = 1;
if (ret != 0)
OSD_Printf("Error running the Lua code (error code %d)\n", ret);
return OSDCMD_OK;
}
#endif
static int32_t osdcmd_addpath(const osdfuncparm_t *parm)
@ -1595,6 +1632,8 @@ int32_t registerosdcommands(void)
OSD_RegisterFunction("setvar","setvar <gamevar> <value>: sets the value of a gamevar", osdcmd_setvar);
OSD_RegisterFunction("setvarvar","setvarvar <gamevar1> <gamevar2>: sets the value of <gamevar1> to <gamevar2>", osdcmd_setvar);
OSD_RegisterFunction("setactorvar","setactorvar <actor#> <gamevar> <value>: sets the value of <actor#>'s <gamevar> to <value>", osdcmd_setactorvar);
#else
OSD_RegisterFunction("lua", "lua \"Lua code...\": runs Lunatic code", osdcmd_lua);
#endif
OSD_RegisterFunction("screenshot","screenshot: takes a screenshot. See r_scrcaptureformat.", osdcmd_screenshot);