diff --git a/libretro-db/Makefile b/libretro-db/Makefile index df9b6654b9..224df910ce 100644 --- a/libretro-db/Makefile +++ b/libretro-db/Makefile @@ -70,7 +70,7 @@ TESTLIB_FLAGS = ${CFLAGS} ${LUA_FLAGS} -shared -fpic .PHONY: all clean check -all: rmsgpack_test libretrodb_tool plain_converter +all: rmsgpack_test libretrodb_tool plain_converter lua_converter %.o: %.c ${CC} $(INCFLAGS) $< -c ${CFLAGS} -o $@ diff --git a/libretro-db/dat_converter b/libretro-db/dat_converter index 1113b3869e..17f0d28a79 100755 --- a/libretro-db/dat_converter +++ b/libretro-db/dat_converter @@ -1,5 +1,5 @@ #!/bin/sh rdb_file="$1" shift 1 -# ./lua_converter "$rdb_file" dat_converter.lua "$@" -./plain_converter "$rdb_file" "$@" + ./lua_converter "$rdb_file" dat_converter.lua "$@" +#./plain_converter "$rdb_file" "$@" diff --git a/libretro-db/lua_converter.c b/libretro-db/lua_converter.c new file mode 100644 index 0000000000..e2ad50d8d4 --- /dev/null +++ b/libretro-db/lua_converter.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "libretrodb.h" +#include "lua_common.h" + +int master_key = 1; + +const char * LUA_COMMON = " \ +function binary(s) if s ~= nil then return {binary = s} else return nil end end \ +function uint(s) if s ~= nil then return {uint = s} else return nil end end \ +"; + +static int call_init(lua_State * L, int argc, const char ** argv) +{ + int rv = -1; + int i; + + lua_getglobal(L, "init"); + for (i = 0; i < argc; i++) + lua_pushstring(L, argv[i]); + + if (lua_pcall(L, argc, 0, 0) != 0) + { + printf( + "error running function `init': %s\n", + lua_tostring(L, -1) + ); + } + + return rv; +} + +static int value_provider(void * ctx, struct rmsgpack_dom_value *out) +{ + int rv = 0; + lua_State * L = ctx; + + lua_getglobal(L, "get_value"); + + if (lua_pcall(L, 0, 1, 0) != 0) + { + printf( + "error running function `get_value': %s\n", + lua_tostring(L, -1) + ); + } + + if (lua_isnil(L, -1)) + rv = 1; + else if (lua_istable(L, -1)) + rv = libretrodb_lua_to_rmsgpack_value(L, -1, out); + else + printf("function `get_value' must return a table or nil\n"); + lua_pop(L, 1); + return rv; +} + +int main(int argc, char ** argv) +{ + lua_State *L; + const char *db_file; + const char *lua_file; + RFILE *dst; + int rv = 0; + + if (argc < 3) + { + printf("usage:\n%s [args ...]\n", argv[0]); + return 1; + } + + db_file = argv[1]; + lua_file = argv[2]; + L = luaL_newstate(); + + luaL_openlibs(L); + luaL_dostring(L, LUA_COMMON); + + if (luaL_dofile(L, lua_file) != 0) + return 1; + + call_init(L, argc - 2, (const char **) argv + 2); + + dst = retro_fopen(db_file, RFILE_MODE_WRITE, -1); + if (!dst) + { + printf( + "Could not open destination file '%s': %s\n", + db_file, + strerror(errno) + ); + rv = errno; + goto clean; + } + + rv = libretrodb_create(dst, &value_provider, L); + +clean: + lua_close(L); + retro_fclose(dst); + return rv; +}