diff --git a/data/logo.gif b/data/gfx/logo.gif similarity index 100% rename from data/logo.gif rename to data/gfx/logo.gif diff --git a/data/other.lua b/data/ia/other.lua similarity index 66% rename from data/other.lua rename to data/ia/other.lua index 4ca5ae3..11d3092 100644 --- a/data/other.lua +++ b/data/ia/other.lua @@ -1,6 +1,9 @@ -return { +require "ia.test" + +other = { peiv = function() pal.color(1, 1, 1, 1) + test2() return "HOLA OTHER UNIT" end } diff --git a/data/ia/test.lua b/data/ia/test.lua new file mode 100644 index 0000000..aeaa9cf --- /dev/null +++ b/data/ia/test.lua @@ -0,0 +1,3 @@ +function test2() + draw.text("THIS WORKS!",1,140,4) +end \ No newline at end of file diff --git a/data/main.lua b/data/main.lua index d99ca3f..37f2822 100644 --- a/data/main.lua +++ b/data/main.lua @@ -1,14 +1,21 @@ -other = require "other" +require "ia.other" x=0 function mini.init() - s = surf.load("logo.gif") + s = surf.load("gfx/logo.gif") surf.source(s) - p = pal.load("logo.gif") + p = pal.load("gfx/logo.gif") pal.set(p) pal.trans(255) --surf.save(s, "prova.gif", p) + + print("=== PACKAGES LOADED ===") + for name, value in pairs(package.loaded) do + print(name, value) + end + print("========================") + end function mini.update() @@ -34,4 +41,5 @@ function mini.update() local mx, my = mouse.pos() draw.rectf(mx, my, 4, 4, 8) draw.text(mx .. " " .. my, 1, 8, 8) + draw.text(other.peiv(),1,100,4) end diff --git a/jfile.cpp b/jfile.cpp index 334cf1d..1baf2c4 100644 --- a/jfile.cpp +++ b/jfile.cpp @@ -9,7 +9,9 @@ #include #include #include -#include + +#include +#include // Para opendir/readdir en SOURCE_FOLDER #ifndef _WIN32 #include @@ -277,4 +279,101 @@ bool file_createFolder(const char* name) { strcpy(tmp, "./"); strcat(tmp, name); return mkdir(tmp, 0755)==0; -} \ No newline at end of file +} + +static bool has_extension(const std::string &name, const char *ext) +{ + if (!ext) return true; // sin filtro + + std::string e = ext; + std::string suffix = "." + e; + + if (name.size() < suffix.size()) + return false; + + return (name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0); +} + +std::vector file_listdir(const char *folder, const char *extension) +{ + std::vector result; + std::string base(folder); + + // Normalizar: quitar "/" final si existe + if (!base.empty() && base.back() == '/') + base.pop_back(); + + // ------------------------------- + // 1. MODO: ARCHIVOS SUELTOS + // ------------------------------- + if (file_source == SOURCE_FOLDER) + { + std::string fullpath = std::string(resource_folder) + base; + + DIR *dir = opendir(fullpath.c_str()); + if (!dir) + return result; + + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) + { + std::string name = entry->d_name; + + // Ignorar "." y ".." + if (name == "." || name == "..") + continue; + + // Ignorar subdirectorios + std::string full = fullpath + "/" + name; + DIR *test = opendir(full.c_str()); + if (test) + { + closedir(test); + continue; // es un directorio + } + + // Filtrar por extensión + if (!has_extension(name, extension)) + continue; + + result.push_back(name); + } + + closedir(dir); + return result; + } + + // ------------------------------- + // 2. MODO: ARCHIVO CONTENEDOR + // ------------------------------- + if (file_source == SOURCE_FILE) + { + std::string prefix = base + "/"; + + for (auto &f : toc) + { + const std::string &path = f.path; + + // Debe empezar por "folder/" + if (path.compare(0, prefix.size(), prefix) != 0) + continue; + + // Extraer la parte después de "folder/" + std::string rest = path.substr(prefix.size()); + + // Ignorar subdirectorios + if (rest.find('/') != std::string::npos) + continue; + + // Filtrar por extensión + if (!has_extension(rest, extension)) + continue; + + result.push_back(rest); + } + + return result; + } + + return result; +} diff --git a/jfile.h b/jfile.h index be55c8d..63c5141 100644 --- a/jfile.h +++ b/jfile.h @@ -1,5 +1,7 @@ #pragma once #include +#include +#include #define SOURCE_FILE 0 #define SOURCE_FOLDER 1 @@ -21,3 +23,4 @@ const char* file_getconfigvalue(const char *key); void file_setconfigvalue(const char* key, const char* value); bool file_createFolder(const char* name); +std::vector file_listdir(const char *folder, const char *extension=NULL); \ No newline at end of file diff --git a/lua.cpp b/lua.cpp index b9e5fd5..53f61ac 100644 --- a/lua.cpp +++ b/lua.cpp @@ -1276,6 +1276,7 @@ void push_lua_funcs() { } +/* int MiniLoader(lua_State *L) { const char *name = luaL_checkstring(L, 1); char filename[strlen(name)+5]; @@ -1291,6 +1292,79 @@ int MiniLoader(lua_State *L) { free(buffer); return 1; } +*/ + +int MiniLoader(lua_State *L) { + const char *name = luaL_checkstring(L, 1); + + // 1. Convertir puntos en barras + std::string path(name); + std::replace(path.begin(), path.end(), '.', '/'); + + // 2. Detectar comodín "/*" + bool load_all = false; + if (path.size() >= 2 && path.substr(path.size()-2) == "/*") { + load_all = true; + path = path.substr(0, path.size()-2); // quitar "/*" + } + + if (load_all) { + std::vector files = file_listdir(path.c_str(), "lua"); + + // Ejecutar todos los módulos + for (auto &f : files) { + std::string fullpath = path + "/" + f; + std::string registerpath = std::string(path + "." + f.substr(0,f.size()-4)); + + int size; + char* buffer = file_getfilebuffer(fullpath.c_str(), size); + if (!buffer) continue; + + if (luaL_loadbuffer(L, buffer, size, registerpath.c_str()) == LUA_OK) { + lua_pcall(L, 0, 0, 0); // ejecutar módulo, sin devolver nada + lua_getglobal(L, "package"); + lua_getfield(L, -1, "loaded"); + lua_pushboolean(L, 1); + lua_setfield(L, -2, registerpath.c_str()); + lua_pop(L, 2); + } else { + log_msg(LOG_LUALD, "Error cargando %s: %s\n", fullpath.c_str(), lua_tostring(L, -1)); + lua_pop(L, 1); + } + + free(buffer); + } + + // Devolver un loader vacío + lua_pushcfunction(L, [](lua_State* L) -> int { + lua_pushboolean(L, 1); // require devuelve true + return 1; + }); + + return 1; + } + + // 3. Cargar un único archivo + std::string filename = path + ".lua"; + + int size; + char* buffer = file_getfilebuffer(filename.c_str(), size); + if (!buffer) { + lua_pushnil(L); + return 1; + } + + if (luaL_loadbuffer(L, buffer, size, name)) { + log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + free(buffer); + lua_pushnil(L); + return 1; + } + + free(buffer); + return 1; +} void lua_init(const char *main_lua_file) { L = luaL_newstate(); diff --git a/version.h b/version.h index d83e778..24a390b 100644 --- a/version.h +++ b/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.3.16" +#define MINI_VERSION "1.4"