2023-05-14 18:40:19 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import toml
|
|
|
|
typesIn = {
|
|
|
|
"__unknown": "lua_touserdata",
|
|
|
|
"char": "luaL_checkinteger",
|
|
|
|
"int": "luaL_checkinteger",
|
|
|
|
"long long": "luaL_checkinteger",
|
2023-06-13 04:06:42 +00:00
|
|
|
"unsigned char": "luaL_checkinteger",
|
2023-05-14 18:40:19 +00:00
|
|
|
"char *": "(char *)luaL_checkstring"
|
|
|
|
}
|
|
|
|
|
|
|
|
typesOut = {
|
|
|
|
"__unknown": "lua_pushlightuserdata",
|
|
|
|
"char": "lua_pushinteger",
|
|
|
|
"int": "lua_pushinteger",
|
|
|
|
"long long": "lua_pushinteger",
|
2023-06-13 04:06:42 +00:00
|
|
|
"unsigned char": "lua_pushinteger",
|
2023-05-14 18:40:19 +00:00
|
|
|
"char *": "lua_pushstring"
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:05:07 +00:00
|
|
|
statics = toml.loads(open("src/values/statics.toml").read())
|
|
|
|
functions = toml.loads(open("src/values/functions.toml").read())
|
|
|
|
ofile = open("src/lua.c","w")
|
2023-05-14 18:40:19 +00:00
|
|
|
|
|
|
|
ofile.write('''\
|
|
|
|
#include <lua5.3/lua.h>
|
|
|
|
#include <lua5.3/lualib.h>
|
|
|
|
#include <lua5.3/lauxlib.h>
|
2023-05-15 13:28:58 +00:00
|
|
|
lua_State * engine_lua_state;
|
2023-05-15 08:42:02 +00:00
|
|
|
#include "lua_manual.c"
|
2023-05-14 18:40:19 +00:00
|
|
|
|
|
|
|
''')
|
|
|
|
|
|
|
|
for func in functions:
|
2023-06-12 14:05:07 +00:00
|
|
|
if "lua" in functions[func]:
|
|
|
|
if functions[func]["lua"] in ["no","manual"]: continue
|
2023-05-14 18:40:19 +00:00
|
|
|
invarCount = 1
|
|
|
|
funcnew = "engine_luaf_" +func.replace("engine_","",1)
|
|
|
|
ofile.write('int ' +funcnew+ '(lua_State *L) {\n')
|
|
|
|
for arg in functions[func]["arguments"]:
|
|
|
|
if not arg in typesIn:
|
|
|
|
checkfunc = typesIn["__unknown"]
|
|
|
|
else:
|
|
|
|
checkfunc = typesIn[arg]
|
2023-05-14 20:13:21 +00:00
|
|
|
ofile.write("\t" +arg+ ' ' +functions[func]["argNames"][invarCount - 1]+ ' = ' +checkfunc+ '(L,' +str(invarCount)+ ');\n')
|
2023-05-14 18:40:19 +00:00
|
|
|
invarCount += 1
|
|
|
|
|
2023-05-15 08:01:55 +00:00
|
|
|
argstring = "(" +",".join(functions[func]["argNames"])+ ")"
|
2023-05-14 18:40:19 +00:00
|
|
|
|
|
|
|
outtype = functions[func]["type"]
|
|
|
|
if outtype == "void":
|
|
|
|
ofile.write('\t' + func + argstring + ";")
|
|
|
|
ofile.write('\n\treturn 0;')
|
|
|
|
else:
|
|
|
|
if not outtype in typesOut:
|
|
|
|
pushfunc = typesOut["__unknown"]
|
|
|
|
else:
|
|
|
|
pushfunc = typesOut[arg]
|
|
|
|
|
|
|
|
ofile.write('\t' +outtype+ ' outvar = ' +func + argstring + ";")
|
|
|
|
ofile.write("\n\t" +pushfunc+ '(L,outvar);')
|
|
|
|
ofile.write('\n\treturn 1;')
|
|
|
|
|
|
|
|
ofile.write('\n}\n\n')
|
|
|
|
|
|
|
|
ofile.write('''\
|
2023-05-15 09:08:45 +00:00
|
|
|
void engine_lua_init() {
|
2023-05-14 18:40:19 +00:00
|
|
|
engine_lua_state = luaL_newstate();
|
|
|
|
luaL_openlibs(engine_lua_state);
|
|
|
|
|
|
|
|
''')
|
|
|
|
|
2023-05-15 13:28:58 +00:00
|
|
|
for static in statics:
|
|
|
|
ofile.write('\tlua_pushinteger(engine_lua_state,' +static+ ');\n')
|
|
|
|
ofile.write('\tlua_setglobal(engine_lua_state,"' +static+ '");\n')
|
|
|
|
|
2023-05-14 18:40:19 +00:00
|
|
|
for func in functions:
|
2023-06-12 14:05:07 +00:00
|
|
|
if "lua" in functions[func]:
|
|
|
|
if functions[func]["lua"] in ["no","manual"]: continue
|
2023-05-14 18:40:19 +00:00
|
|
|
funcnew = "engine_luaf_" +func.replace("engine_","",1)
|
|
|
|
ofile.write('\tlua_pushcfunction(engine_lua_state,' +funcnew+ ');\n')
|
|
|
|
ofile.write('\tlua_setglobal (engine_lua_state,"' +func+ '");\n')
|
|
|
|
|
|
|
|
ofile.write('''\
|
2023-05-15 09:08:45 +00:00
|
|
|
engine_lua_init_manual();
|
2023-05-16 10:29:12 +00:00
|
|
|
luaL_loadfile(engine_lua_state,"mods/main/script/main.lua");
|
2023-05-14 18:40:19 +00:00
|
|
|
lua_call(engine_lua_state,0,0);
|
|
|
|
}''')
|