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:
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
@@ -1,6 +1,9 @@
|
||||
return {
|
||||
require "ia.test"
|
||||
|
||||
other = {
|
||||
peiv = function()
|
||||
pal.color(1, 1, 1, 1)
|
||||
test2()
|
||||
return "HOLA OTHER UNIT"
|
||||
end
|
||||
}
|
||||
3
data/ia/test.lua
Normal file
3
data/ia/test.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
function test2()
|
||||
draw.text("THIS WORKS!",1,140,4)
|
||||
end
|
||||
@@ -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
|
||||
|
||||
103
jfile.cpp
103
jfile.cpp
@@ -9,7 +9,9 @@
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <algorithm>
|
||||
#include <dirent.h> // Para opendir/readdir en SOURCE_FOLDER
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pwd.h>
|
||||
@@ -277,4 +279,101 @@ bool file_createFolder(const char* name) {
|
||||
strcpy(tmp, "./");
|
||||
strcat(tmp, name);
|
||||
return mkdir(tmp, 0755)==0;
|
||||
}
|
||||
}
|
||||
|
||||
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<std::string> file_listdir(const char *folder, const char *extension)
|
||||
{
|
||||
std::vector<std::string> 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;
|
||||
}
|
||||
|
||||
3
jfile.h
3
jfile.h
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#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<std::string> file_listdir(const char *folder, const char *extension=NULL);
|
||||
74
lua.cpp
74
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<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();
|
||||
|
||||
Reference in New Issue
Block a user