VERSIÓ 1.4

- [NEW] jfile_listdir()
- [NEW] ara els moduls es deuen carregar amb 'require "directori.modul" '
- [NEW] Ara es poden carregar directoris sencers amb 'require "directori.*" '
This commit is contained in:
2026-03-12 14:58:39 +01:00
parent 33bbf940ea
commit 4084cb64f2
8 changed files with 197 additions and 7 deletions

74
lua.cpp
View File

@@ -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<std::string> 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();