From 206d537c3e293633429c0ee94f56241c6de1a7c2 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Sat, 18 Nov 2023 18:38:51 -0600 Subject: [PATCH] Replaced module injection with proper module searcher for Lua. --- CMakeLists.txt | 1 - assets/controls.cfg | 2 +- build-all.sh | 19 ++++++ src/embedded.c | 28 --------- src/embedded.h | 19 ++++++ src/singe.c | 137 +++++++++++++++++++++++++++++++------------- 6 files changed, 135 insertions(+), 71 deletions(-) delete mode 100644 src/embedded.c diff --git a/CMakeLists.txt b/CMakeLists.txt index bf5d3b237..8d25c1519 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,7 +47,6 @@ set(SINGE_SOURCE src/frameFile.c src/videoPlayer.h src/util.c - src/embedded.c ) diff --git a/assets/controls.cfg b/assets/controls.cfg index 6c6f7575f..bfb835797 100644 --- a/assets/controls.cfg +++ b/assets/controls.cfg @@ -23,7 +23,7 @@ -- Default Mappings -DEAD_ZONE = 8000 +DEAD_ZONE = 15000 INPUT_UP = { SCANCODE.UP, SCANCODE.KP_8, GAMEPAD_0.AXIS_LEFT_Y_U, GAMEPAD_0.AXIS_RIGHT_Y_U, GAMEPAD_0.DPAD_UP } INPUT_LEFT = { SCANCODE.LEFT, SCANCODE.KP_4, GAMEPAD_0.AXIS_LEFT_X_L, GAMEPAD_0.AXIS_RIGHT_X_L, GAMEPAD_0.DPAD_LEFT } diff --git a/build-all.sh b/build-all.sh index 95cb64e88..d13f37f6a 100755 --- a/build-all.sh +++ b/build-all.sh @@ -295,6 +295,25 @@ if [[ 0 == 1 ]]; then createEmbeddedBinary ${G_TARGET}/menuBackground.mkv ${G_GENERATED}/menuBackground_mkv.h MENUBACKGROUND_MKV_H rm ${G_TARGET}/temp1.mkv ${G_TARGET}/temp2.mkv ${G_TARGET}/menuBackground.mkv + # === LuaSocket === + createEmbeddedBinary thirdparty/luasocket/src/ftp.lua ${G_GENERATED}/ftp_lua.h FTP_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/headers.lua ${G_GENERATED}/headers_lua.h HEADERS_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/http.lua ${G_GENERATED}/http_lua.h HTTP_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/ltn12.lua ${G_GENERATED}/ltn12_lua.h LTN12_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/mbox.lua ${G_GENERATED}/mbox_lua.h MBOX_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/mime.lua ${G_GENERATED}/mime_lua.h MIME_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/smtp.lua ${G_GENERATED}/smtp_lua.h SMTP_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/socket.lua ${G_GENERATED}/socket_lua.h SOCKET_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/tp.lua ${G_GENERATED}/tp_lua.h TP_LUA_H + createEmbeddedBinary thirdparty/luasocket/src/url.lua ${G_GENERATED}/url_lua.h URL_LUA_H + + # === LuaSec === + createEmbeddedBinary thirdparty/luasec/src/https.lua ${G_GENERATED}/https_lua.h HTTPS_LUA_H + createEmbeddedBinary thirdparty/luasec/src/ssl.lua ${G_GENERATED}/ssl_lua.h SSL_LUA_H + + # === LuaRS232 === + createEmbeddedBinary thirdparty/librs232/bindings/lua/rs232.lua ${G_GENERATED}/rs232_lua.h RS232_LUA_H + :< - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - - -#define EMBED_HERE - -#undef EMBEDDED_H -#include "embedded.h" - -#undef EMBED_HERE diff --git a/src/embedded.h b/src/embedded.h index 08cf22032..c9caaf163 100644 --- a/src/embedded.h +++ b/src/embedded.h @@ -38,5 +38,24 @@ #include "generated/menuBackground_mkv.h" //#include "generated/Manual_pdf.h" +// LuaSocket +#include "generated/ftp_lua.h" +#include "generated/headers_lua.h" +#include "generated/http_lua.h" +#include "generated/ltn12_lua.h" +#include "generated/mbox_lua.h" +#include "generated/mime_lua.h" +#include "generated/smtp_lua.h" +#include "generated/socket_lua.h" +#include "generated/tp_lua.h" +#include "generated/url_lua.h" + +// LuaSec +#include "generated/https_lua.h" +#include "generated/ssl_lua.h" + +// LuaRS232 +#include "generated/rs232_lua.h" + #endif // EMBEDDED_H diff --git a/src/singe.c b/src/singe.c index 3e1c4bad3..89d6a2620 100644 --- a/src/singe.c +++ b/src/singe.c @@ -52,6 +52,11 @@ LSEC_API int luaopen_ssl_config(lua_State *L); #include "frameFile.h" #include "videoPlayer.h" #include "singe.h" + +// We have to do the embedding here so the Lua module +// definitions can find their lenght properly. They +// can't be external to this source file. +#define EMBED_HERE #include "embedded.h" @@ -67,6 +72,16 @@ LSEC_API int luaopen_ssl_config(lua_State *L); #define AXIS_KEY_DOWN 0 #define AXIS_KEY_UP 1 + +typedef struct { + char *name; + union { + char *source; + lua_CFunction openf; + }; + size_t length; +} luaModuleT; + typedef struct MouseS { int32_t x; int32_t y; @@ -236,6 +251,44 @@ typedef struct GlobalS { GlobalT _global; +#define MODL(name, array) { name, { (char *)array }, sizeof(array) } +#define MODC(name, openf) { name, { (char *)openf }, 0 } + + +// Lua Modules +static const luaModuleT luaModules[] = { + // LuaFileSystem + MODC("lfs", luaopen_lfs), + // LuaSocket + MODC("mime.core", luaopen_mime_core), + MODC("socket.core", luaopen_socket_core), + MODL("ltn12", ltn12_lua), + MODL("mbox", mbox_lua), + MODL("mime", mime_lua), + MODL("socket", socket_lua), + MODL("socket.ftp", ftp_lua), + MODL("socket.headers", headers_lua), + MODL("socket.http", http_lua), + MODL("socket.smtp", smtp_lua), + MODL("socket.tp", tp_lua), + MODL("socket.url", url_lua), +#ifndef _WIN32 + MODC("socket.unix", luaopen_socket_unix), + MODC("socket.serial", luaopen_socket_serial), +#endif + // LuaSec + MODC("ssl.core", luaopen_ssl_core), + MODC("ssl.context", luaopen_ssl_context), + MODC("ssl.x509", luaopen_ssl_x509), + MODC("ssl.config", luaopen_ssl_config), + MODL("ssl.https", https_lua), + MODL("ssl", ssl_lua), + // LuaRS232 + MODC("rs232.core", luaopen_luars232), + MODL("rs232", rs232_lua), +}; + + int32_t apiColorBackground(lua_State *L); int32_t apiColorForeground(lua_State *L); @@ -352,7 +405,7 @@ void channelFinished(int channel); void line(int32_t x1, int32_t y1, int32_t x2, int32_t y2, SDL_Color *c); void luaDie(lua_State *L, char *method, char *fmt, ...); int32_t luaError(lua_State *L); -void luaPreload(lua_State *L, const char *name, lua_CFunction func); +int luaSearcher(lua_State *L); void luaTrace(lua_State *L, char *method, char *fmt, ...); void processKey(bool down, int keysym, int32_t scancode); void progTrace(char *fmt, ...); @@ -3341,11 +3394,35 @@ void luaStackDump(lua_State *L) { #endif -void luaPreload(lua_State *L, const char *name, lua_CFunction func) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - lua_pushcfunction(L, func); - lua_setfield(L, -2, name); - lua_pop(L, 1); // remove PRELOAD table +int luaSearcher(lua_State *L) { + // https://leiradel.github.io/2020/03/01/Embedding-Lua-Modules.html + char *modname = (char *)lua_tostring(L, 1); + size_t i; + int res; + + // Iterates over all modules we know. + for (i = 0; i < sizeof(luaModules) / sizeof(luaModules[0]); i++) { + if (strcmp(modname, luaModules[i].name) == 0) { + // Found the module. + if (luaModules[i].length != 0) { + // It's a Lua module, return the chunk that defines the module. + res = luaL_loadbufferx(L, luaModules[i].source, luaModules[i].length, modname, "t"); + if (res != LUA_OK) { + // Compilation error. + return lua_error(L); + } + } else { + // It's a native module, return the native function that defines the module. + lua_pushcfunction(L, luaModules[i].openf); + } + return 1; + } + } + + // Oops... + lua_pushfstring(L, "Unknown Lua module: \"%s\"", modname); + + return 1; } @@ -4389,46 +4466,24 @@ void startControllers(void) { void startLuaContext(lua_State *L) { + size_t length; + luaL_openlibs(L); lua_atpanic(L, luaError); - luaPreload(L, "lfs", luaopen_lfs); - luaPreload(L, "socket.core", luaopen_socket_core); - luaPreload(L, "mime.core", luaopen_mime_core); - luaPreload(L, "rs232", luaopen_luars232); -#ifndef _WIN32 - luaPreload(L, "socket.unix", luaopen_socket_unix); - luaPreload(L, "socket.serial", luaopen_socket_serial); -#endif - luaPreload(L, "ssl.core", luaopen_ssl_core); - luaPreload(L, "ssl.context", luaopen_ssl_context); - luaPreload(L, "ssl.x509", luaopen_ssl_x509); - luaPreload(L, "ssl.config", luaopen_ssl_config); + // Get the package global table. + lua_getglobal(L, "package"); + // Get the list of searchers in the package table. + lua_getfield(L, -1, "searchers"); + // Get the number of existing searchers in the table. + length = lua_rawlen(L, -1); + // Add our own searcher to the list. + lua_pushcfunction(L, luaSearcher); + lua_rawseti(L, -2, length + 1); + // Remove the seachers and the package tables from the stack. + lua_pop(L, 2); /* - ***TODO*** Load these Lua modules: - - Lua Socket: - socket.lua - ltn12.lua - tp.lua - url.lua - ftp.lua - mime.lua - headers.lua - http.lua - - mbox.lua - smtp.lua - - luasec: - https.lua - ssl.lua - - librs232: - rs232.lua - - --[[ http = require("socket.http") http.request{