Basics for Lua

This commit is contained in:
Simon Robertshaw 2011-05-30 16:22:39 +01:00
parent 5dcc3dbb44
commit 79a27c2c90
3 changed files with 107 additions and 0 deletions

17
includes/luaconsole.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef LUACONSOLEH
#define LUACONSOLEH
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>
#include <defines.h>
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

61
src/luaconsole.c Normal file
View File

@ -0,0 +1,61 @@
#ifdef LUACONSOLE
#include <luaconsole.h>
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

View File

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