From 79a27c2c90823f1f9212d80c6b2d63cde620855c Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Mon, 30 May 2011 16:22:39 +0100 Subject: [PATCH] Basics for Lua --- includes/luaconsole.h | 17 ++++++++++++ src/luaconsole.c | 61 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 29 ++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 includes/luaconsole.h create mode 100644 src/luaconsole.c diff --git a/includes/luaconsole.h b/includes/luaconsole.h new file mode 100644 index 000000000..c8b6b1958 --- /dev/null +++ b/includes/luaconsole.h @@ -0,0 +1,17 @@ +#ifndef LUACONSOLEH +#define LUACONSOLEH +#include +#include +#include +#include + +void luacon_open(); +int luacon_step(); +int luacon_keypress(char key); +int luacon_eval(char *command); +void luacon_close(); +int process_command_lua(pixel *vid_buf, char *console, char *console_error); + +//TPT Interface +int luatpt_test(lua_State* l); +#endif diff --git a/src/luaconsole.c b/src/luaconsole.c new file mode 100644 index 000000000..4c216137f --- /dev/null +++ b/src/luaconsole.c @@ -0,0 +1,61 @@ +#ifdef LUACONSOLE +#include + +lua_State *l; +void luacon_open(){ + const static struct luaL_reg tptluaapi [] = { + {"test", &luatpt_test}, + {NULL,NULL} + }; + + + l = lua_open(); + luaL_openlibs(l); + luaL_openlib(l, "tpt", tptluaapi, 0); +} +int luacon_step(){ + //Nothing here yet + return 0; +} +int luacon_keypress(char key){ + //Nothing here yet + return 0; +} +int luacon_eval(char *command){ + return luaL_dostring (l, command); +} +void luacon_close(){ + lua_close(l); +} +int process_command_lua(pixel *vid_buf, char *console, char *console_error) +{ + int commandret; + char console2[15]; + char console3[15]; + char console4[15]; + char console5[15]; + //sprintf(console_error, "%s", console); + if (console && strcmp(console, "")!=0 && strncmp(console, " ", 1)!=0) + { + sscanf(console,"%14s %14s %14s %14s", console2, console3, console4, console5); + if (strcmp(console2, "quit")==0) + { + return -1; + } else { + commandret = luacon_eval(console); + if (commandret) + strcpy(console_error,"failed to execute code."); + } + } + return 1; +} +//Being TPT interface methods: +int luatpt_test(lua_State* l) +{ + int testint = 0; + testint = luaL_optint(l, 1, 0); + printf("Test successful, got %d\n", testint); + return 1; +} + +#endif diff --git a/src/main.c b/src/main.c index 41a6c74fd..c7e325d91 100644 --- a/src/main.c +++ b/src/main.c @@ -58,6 +58,9 @@ #ifdef PYCONSOLE #include "pyconsole.h" #endif +#ifdef LUACONSOLE +#include "luaconsole.h" +#endif pixel *vid_buf; @@ -1560,6 +1563,9 @@ int main(int argc, char *argv[]) fmt.callback = mixaudio; fmt.userdata = NULL; +#ifdef LUACONSOLE + luacon_open(); +#endif #ifdef PYCONSOLE //initialise python console Py_Initialize(); @@ -2366,6 +2372,9 @@ int main(int argc, char *argv[]) } } } +#ifdef LUACONSOLE + luacon_keypress(sdl_key); +#endif #ifdef PYCONSOLE if (pyready==1 && pygood==1) if (pkey!=NULL && sdl_key!=NULL) @@ -3331,6 +3340,20 @@ int main(int argc, char *argv[]) if (!console_mode) hud_enable = 1; } +#elif defined LUACONSOLE + char *console; + sys_pause = 1; + console = console_ui(vid_buf, console_error, console_more); + console = mystrdup(console); + strcpy(console_error,""); + if (process_command_lua(vid_buf, console, console_error)==-1) + { + free(console); + break; + } + free(console); + if (!console_mode) + hud_enable = 1; #else char *console; sys_pause = 1; @@ -3349,6 +3372,9 @@ int main(int argc, char *argv[]) } //execute python step hook +#ifdef LUACONSOLE + luacon_step(); +#endif #ifdef PYCONSOLE if (pyready==1 && pygood==1) if (pstep!=NULL) @@ -3384,6 +3410,9 @@ int main(int argc, char *argv[]) } SDL_CloseAudio(); http_done(); +#ifdef LUACONSOLE + luacon_close(); +#endif #ifdef PYCONSOLE PyRun_SimpleString("import os,tempfile,os.path\ntry:\n os.remove(os.path.join(tempfile.gettempdir(),'tpt_console.py'))\nexcept:\n pass");