87 lines
1.4 KiB
C
87 lines
1.4 KiB
C
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <raylib.h>
|
|
|
|
#include <lua.h>
|
|
|
|
#include "types.h"
|
|
|
|
void lua_ontick(){
|
|
|
|
}
|
|
|
|
void lua_onframe(){
|
|
|
|
}
|
|
|
|
static int lua_add_machine(lua_State *L){
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int lua_mtprint(lua_State *L){
|
|
printf("(lua) %s\n", lua_tostring(L, 0));
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct LuaFn {
|
|
char *name;
|
|
lua_CFunction fn;
|
|
};
|
|
|
|
struct LuaFn exported_funcs[] = {
|
|
{"mtprint", lua_mtprint}
|
|
};
|
|
|
|
// Taken from the Lua manual
|
|
static void *l_alloc (void *ud, void *ptr, size_t osize,size_t nsize){
|
|
(void)ud; (void)osize; /* not used */
|
|
if (nsize == 0) {
|
|
free(ptr);
|
|
return NULL;
|
|
}
|
|
else
|
|
return realloc(ptr, nsize);
|
|
}
|
|
|
|
struct load_dat {
|
|
char *str;
|
|
int pos;
|
|
int len;
|
|
};
|
|
|
|
const char *file_reader(lua_State *L, void *data, size_t *size){
|
|
(void)L;
|
|
struct load_dat *dat = data;
|
|
if(dat->pos >= dat->len){
|
|
*size = 0;
|
|
return NULL;
|
|
}
|
|
else{
|
|
*size = dat->len;
|
|
dat->pos += *size;
|
|
return dat->str;
|
|
}
|
|
}
|
|
|
|
lua_State *gl_L;
|
|
|
|
void script_init(){
|
|
gl_L = lua_newstate(l_alloc, NULL);
|
|
|
|
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
|
lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn);
|
|
|
|
struct load_dat dat = {"mtprint(\"hello\")", 0};
|
|
dat.len = strlen(dat.str);
|
|
lua_load(gl_L, file_reader, &dat, "test", "t");
|
|
int results = 0;
|
|
lua_resume(gl_L, NULL, 0, &results);
|
|
}
|
|
|
|
void script_clean(){
|
|
lua_close(gl_L);
|
|
}
|