VERSIÓ 1.5.6:

- [FIX] Acallats warnings en findloader de lua
- [FIX] Acallats alguns 'Illegal music handle' innecesaris
- [NEW] Ara detecta que no s'ha conectat al debuger de vscode i trau els missatges per consola com abans
- [NEW] Missatges de error més clars
- [NEW] Ara també trau els missatges de debug per consola en la versió release
This commit is contained in:
2026-05-13 11:57:08 +02:00
parent ea7d7ba19f
commit 49cb0af228
9 changed files with 171 additions and 78 deletions
+11 -2
View File
@@ -649,6 +649,13 @@ static void findloader (lua_State *L, const char *name) {
// [RZC 12/03/2026] ================================== // [RZC 12/03/2026] ==================================
// Soport per a rutes relatives i absolutes // Soport per a rutes relatives i absolutes
// //
static void safe_concat3(char *out, size_t outsz, const char *a, const char *b, const char *c) {
out[0] = '\0';
strncat(out, a, outsz - 1);
strncat(out, b, outsz - strlen(out) - 1);
strncat(out, c, outsz - strlen(out) - 1);
}
static void resolve_module_name(lua_State *L, char *out, size_t outsz) { static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
const char *req = luaL_checkstring(L, 1); const char *req = luaL_checkstring(L, 1);
@@ -730,7 +737,8 @@ static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
// Hemos llegado a la raíz // Hemos llegado a la raíz
strncpy(out, rest, outsz - 1); strncpy(out, rest, outsz - 1);
} else { } else {
snprintf(out, outsz, "%s.%s", temp, rest); //snprintf(out, outsz, "%s.%s", temp, rest);
safe_concat3(out, outsz, temp, ".", rest);
} }
out[outsz - 1] = '\0'; out[outsz - 1] = '\0';
@@ -742,7 +750,8 @@ static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
// Estamos en la raíz // Estamos en la raíz
strncpy(out, req, outsz - 1); strncpy(out, req, outsz - 1);
} else { } else {
snprintf(out, outsz, "%s.%s", caller, req); //snprintf(out, outsz, "%s.%s", caller, req);
safe_concat3(out, outsz, caller, ".", req);
} }
out[outsz - 1] = '\0'; out[outsz - 1] = '\0';
+8 -8
View File
@@ -256,8 +256,8 @@ namespace jail
void pause() void pause()
{ {
if (!music::enabled) return; if (!music::enabled || (current<0)) return;
if (current<0 || current>static_cast<int>(musics.size())) { if (current>static_cast<int>(musics.size())) {
log_msg(LOG_FAIL, "music::pause: Illegal music handle: %i\n", current); log_msg(LOG_FAIL, "music::pause: Illegal music handle: %i\n", current);
return; return;
} }
@@ -274,8 +274,8 @@ namespace jail
void resume() void resume()
{ {
if (!music::enabled) return; if (!music::enabled || (current<0)) return;
if (current<0 || current>static_cast<int>(musics.size())) { if (current>static_cast<int>(musics.size())) {
log_msg(LOG_FAIL, "music::resume: Illegal music handle: %i\n", current); log_msg(LOG_FAIL, "music::resume: Illegal music handle: %i\n", current);
return; return;
} }
@@ -292,8 +292,8 @@ namespace jail
void stop() void stop()
{ {
if (!music::enabled) return; if (!music::enabled || (current<0)) return;
if (current<0 || current>static_cast<int>(musics.size())) { if (current>static_cast<int>(musics.size())) {
log_msg(LOG_FAIL, "music::stop: Illegal music handle: %i\n", current); log_msg(LOG_FAIL, "music::stop: Illegal music handle: %i\n", current);
return; return;
} }
@@ -312,8 +312,8 @@ namespace jail
void fadeOut(int milliseconds) void fadeOut(int milliseconds)
{ {
if (!music::enabled) return; if (!music::enabled || (current<0)) return;
if (current<0 || current>static_cast<int>(musics.size())) { if (current>static_cast<int>(musics.size())) {
log_msg(LOG_FAIL, "music::fadeOut: Illegal music handle: %i\n", current); log_msg(LOG_FAIL, "music::fadeOut: Illegal music handle: %i\n", current);
return; return;
} }
+46 -13
View File
@@ -1,6 +1,7 @@
#include "lua.h" #include "lua.h"
#include "lua.wrappers.h" #include "lua.wrappers.h"
#include "lua.debug.h" #include "lua.debug.h"
#include "lua.utils.h"
#include "external/lua/lua.hpp" #include "external/lua/lua.hpp"
#include "mini/file/file.h" #include "mini/file/file.h"
#include "other/log.h" #include "other/log.h"
@@ -22,6 +23,38 @@ namespace mini
return is_playing; return is_playing;
} }
std::string assemble_error_message(const char *lua_error_string) {
std::string error = lua_error_string; // tu cadena original
std::string nombre;
int linea = 0;
std::string mensaje;
// 1. Buscar el nombre entre comillas
auto p1 = error.find('"');
auto p2 = error.find('"', p1 + 1);
if (p1 != std::string::npos && p2 != std::string::npos)
nombre = error.substr(p1 + 1, p2 - (p1 + 1));
// 2. Buscar la línea después de "]:"
auto p3 = error.find("]:", p2);
if (p3 != std::string::npos) {
size_t start = p3 + 2;
linea = std::stoi(error.substr(start));
}
// 3. Buscar el mensaje después del siguiente ':'
auto p4 = error.find(':', p3 + 2);
if (p4 != std::string::npos)
mensaje = error.substr(p4 + 2); // saltar ": "
// Ahora tienes:
// nombre → "main"
// linea → 18
// mensaje → "unexpected symbol near '+'"
return debug::chunkToPath(nombre) + ":" + std::to_string(linea) + ":" + mensaje;
}
int MiniLoader(lua_State *L) { int MiniLoader(lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
@@ -58,7 +91,7 @@ namespace mini
lua_setfield(L, -2, registerpath.c_str()); lua_setfield(L, -2, registerpath.c_str());
lua_pop(L, 2); lua_pop(L, 2);
} else { } else {
log_msg(LOG_LUALD, "Error cargando %s: %s\n", fullpath.c_str(), lua_tostring(L, -1)); log_msg(LOG_LUALD, "Error cargando %s: %s\n", fullpath.c_str(), assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L, 1); lua_pop(L, 1);
} }
@@ -85,7 +118,7 @@ namespace mini
} }
if (luaL_loadbuffer(L, buffer, size, name)) { if (luaL_loadbuffer(L, buffer, size, name)) {
log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1)); log_msg(LOG_LUALD, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L, 1); lua_pop(L, 1);
free(buffer); free(buffer);
lua_pushnil(L); lua_pushnil(L);
@@ -107,14 +140,14 @@ namespace mini
int size; int size;
char* buffer = file::getfilebuffer(main_lua_file, size); char* buffer = file::getfilebuffer(main_lua_file, size);
if (luaL_loadbuffer(L, buffer, size, "main")) { if (luaL_loadbuffer(L, buffer, size, "main")) {
log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1)); log_msg(LOG_LUALD, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L,1); lua_pop(L,1);
return; return;
} }
free(buffer); free(buffer);
if (lua_pcall(L,0, LUA_MULTRET, 0)) { if (lua_pcall(L,0, LUA_MULTRET, 0)) {
//luaL_traceback(L, L, NULL, 1); //luaL_traceback(L, L, NULL, 1);
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L,1); lua_pop(L,1);
return; return;
} }
@@ -135,7 +168,7 @@ namespace mini
debug::init(L); debug::init(L);
//std::thread(debugCommandThread).detach(); //std::thread(debugCommandThread).detach();
printf("stdin isatty: %d\n", isatty(0)); //printf("stdin isatty: %d\n", isatty(0));
is_playing = true; is_playing = true;
} }
@@ -157,15 +190,15 @@ namespace mini
lua_getglobal(L, "mini"); lua_getglobal(L, "mini");
lua_getfield(L, -1, "init"); lua_getfield(L, -1, "init");
#if defined(DEBUG) && defined(__linux__) if (debug::is_enabled()) {
is_playing = debug::call_and_handle_exceptions(L); is_playing = debug::call_and_handle_exceptions(L);
#else } else {
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L,1); lua_pop(L,1);
is_playing = false; is_playing = false;
} }
#endif }
} }
void update() { void update() {
@@ -173,15 +206,15 @@ namespace mini
lua_getglobal(L, "mini"); lua_getglobal(L, "mini");
lua_getfield(L, -1, "update"); lua_getfield(L, -1, "update");
#if defined(DEBUG) && defined(__linux__) if (debug::is_enabled()) {
is_playing = debug::call_and_handle_exceptions(L); is_playing = debug::call_and_handle_exceptions(L);
#else } else {
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1)); log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
lua_pop(L,1); lua_pop(L,1);
is_playing = false; is_playing = false;
} }
#endif }
} }
} }
+21 -50
View File
@@ -1,6 +1,7 @@
#if defined(DEBUG) && defined(__linux__) #if defined(DEBUG) && defined(__linux__)
#include "lua.debug.h" #include "lua.debug.h"
#include "lua.utils.h"
#include "external/lua/lua.hpp" #include "external/lua/lua.hpp"
#include "mini/win/win.h" #include "mini/win/win.h"
@@ -63,7 +64,7 @@ namespace mini
int g_pauseLine = 0; int g_pauseLine = 0;
bool function_has_breakpoints = false; bool function_has_breakpoints = false;
std::stack<bool> funBreakStack; std::stack<bool> funBreakStack;
bool debug_enabled = true; bool debug_enabled = false;
enum StepMode { enum StepMode {
STEP_NONE, STEP_NONE,
@@ -77,52 +78,6 @@ namespace mini
using json = nlohmann::json; using json = nlohmann::json;
std::string pathToChunk(const std::string& path) {
std::filesystem::path p(path);
// 1. Normalizar la ruta
p = p.lexically_normal();
// 2. Buscar el directorio "data"
auto it = std::find(p.begin(), p.end(), "data");
if (it == p.end())
return ""; // no es un script Lua válido
// 3. Construir la parte relativa después de "data"
std::filesystem::path rel;
for (++it; it != p.end(); ++it)
rel /= *it;
// 4. Quitar ".lua"
std::string s = rel.string();
if (s.ends_with(".lua"))
s = s.substr(0, s.size() - 4);
// 5. Convertir "/" → "."
for (char& c : s)
if (c == '/')
c = '.';
return s;
}
std::string chunkToPath(const std::string& chunk) {
// 1. Convertir "ia.test" → "ia/test"
std::string rel;
rel.reserve(chunk.size() + 10);
for (char c : chunk)
rel += (c == '.' ? '/' : c);
// 2. Añadir prefijo y sufijo
rel = "data/" + rel + ".lua";
// 3. Convertir a ruta absoluta
std::filesystem::path abs = std::filesystem::current_path() / rel;
return abs.lexically_normal().string();
}
int getStackDepth(lua_State* L) { int getStackDepth(lua_State* L) {
lua_Debug ar; lua_Debug ar;
int depth = 0; int depth = 0;
@@ -880,8 +835,11 @@ namespace mini
void processDebugCommand(lua_State* L, const std::string& line) { void processDebugCommand(lua_State* L, const std::string& line) {
//printf("COMANDO PROCESADO: %s\n", line.c_str()); //printf("COMANDO PROCESADO: %s\n", line.c_str());
if (!line.starts_with("@@DEBUGCMD@@")) if (!line.starts_with("@@DEBUGCMD@@")) {
disable(L);
return; return;
}
enable(L);
json j = json::parse(line.substr(12)); json j = json::parse(line.substr(12));
@@ -1348,7 +1306,7 @@ namespace mini
} }
void init(lua_State* L) { void init(lua_State* L) {
lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0); //lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0);
} }
void kill_thread() { void kill_thread() {
@@ -1366,6 +1324,19 @@ namespace mini
} }
} }
void enable(lua_State* L) {
if (debug_enabled) return;
debug_enabled = true;
lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0);
}
void disable(lua_State* L) {
if (!debug_enabled) return;
debug_enabled = false;
lua_sethook(L, NULL,0, 0);
}
bool is_enabled() { return debug_enabled; }
} }
} }
} }
+7
View File
@@ -1,4 +1,5 @@
#pragma once #pragma once
struct lua_State; struct lua_State;
namespace mini namespace mini
@@ -13,12 +14,18 @@ namespace mini
void kill_thread(); void kill_thread();
void process_commands(lua_State* L); void process_commands(lua_State* L);
void toggle(lua_State* L); void toggle(lua_State* L);
void enable(lua_State* L);
void disable(lua_State* L);
bool is_enabled();
#else #else
bool call_and_handle_exceptions(lua_State* L) { return true; }; bool call_and_handle_exceptions(lua_State* L) { return true; };
void init(lua_State* L) {}; void init(lua_State* L) {};
void kill_thread() {}; void kill_thread() {};
void process_commands(lua_State* L) {}; void process_commands(lua_State* L) {};
void toggle(lua_State* L) {}; void toggle(lua_State* L) {};
void enable(lua_State* L) {};
void disable(lua_State* L) {};
bool is_enabled() { return false; };
#endif #endif
} }
+59
View File
@@ -0,0 +1,59 @@
#include "lua.utils.h"
#include <string>
#include <filesystem>
#include <algorithm>
namespace mini
{
namespace lua
{
namespace debug
{
std::string pathToChunk(const std::string& path) {
std::filesystem::path p(path);
// 1. Normalizar la ruta
p = p.lexically_normal();
// 2. Buscar el directorio "data"
auto it = std::find(p.begin(), p.end(), "data");
if (it == p.end())
return ""; // no es un script Lua válido
// 3. Construir la parte relativa después de "data"
std::filesystem::path rel;
for (++it; it != p.end(); ++it)
rel /= *it;
// 4. Quitar ".lua"
std::string s = rel.string();
if (s.ends_with(".lua"))
s = s.substr(0, s.size() - 4);
// 5. Convertir "/" → "."
for (char& c : s)
if (c == '/')
c = '.';
return s;
}
std::string chunkToPath(const std::string& chunk) {
// 1. Convertir "ia.test" → "ia/test"
std::string rel;
rel.reserve(chunk.size() + 10);
for (char c : chunk)
rel += (c == '.' ? '/' : c);
// 2. Añadir prefijo y sufijo
rel = "data/" + rel + ".lua";
// 3. Convertir a ruta absoluta
std::filesystem::path abs = std::filesystem::current_path() / rel;
return abs.lexically_normal().string();
}
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <string>
namespace mini
{
namespace lua
{
namespace debug
{
std::string pathToChunk(const std::string& path);
std::string chunkToPath(const std::string& chunk);
}
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
#pragma once #pragma once
#define MINI_VERSION "1.5.5" #define MINI_VERSION "1.5.6"
+4 -4
View File
@@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#ifdef DEBUG //#ifdef DEBUG
enum LogLevel { LOG_OK, LOG_FAIL, LOG_WARN, LOG_INFO, LOG_LUART, LOG_LUALD, LOG_VERBOSE, LOG_UNSALTED }; enum LogLevel { LOG_OK, LOG_FAIL, LOG_WARN, LOG_INFO, LOG_LUART, LOG_LUALD, LOG_VERBOSE, LOG_UNSALTED };
static inline void log_msg(enum LogLevel level, const char *fmt, ...) { static inline void log_msg(enum LogLevel level, const char *fmt, ...) {
@@ -22,6 +22,6 @@ static inline void log_msg(enum LogLevel level, const char *fmt, ...) {
vprintf(fmt, args); vprintf(fmt, args);
va_end(args); va_end(args);
} }
#else //#else
#define log_msg(...) ((void)0) //#define log_msg(...) ((void)0)
#endif //#endif