From 49cb0af2280d04c305625c1c2c0100f6a7896eb4 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 13 May 2026 11:57:08 +0200 Subject: [PATCH] =?UTF-8?q?VERSI=C3=93=201.5.6:=20-=20[FIX]=20Acallats=20w?= =?UTF-8?q?arnings=20en=20findloader=20de=20lua=20-=20[FIX]=20Acallats=20a?= =?UTF-8?q?lguns=20'Illegal=20music=20handle'=20innecesaris=20-=20[NEW]=20?= =?UTF-8?q?Ara=20detecta=20que=20no=20s'ha=20conectat=20al=20debuger=20de?= =?UTF-8?q?=20vscode=20i=20trau=20els=20missatges=20per=20consola=20com=20?= =?UTF-8?q?abans=20-=20[NEW]=20Missatges=20de=20error=20m=C3=A9s=20clars?= =?UTF-8?q?=20-=20[NEW]=20Ara=20tamb=C3=A9=20trau=20els=20missatges=20de?= =?UTF-8?q?=20debug=20per=20consola=20en=20la=20versi=C3=B3=20release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/external/lua/loadlib.c | 13 +++++- source/mini/audio/jail_audio.cpp | 16 +++---- source/mini/lua/lua.cpp | 59 ++++++++++++++++++++------ source/mini/lua/lua.debug.cpp | 71 ++++++++++---------------------- source/mini/lua/lua.debug.h | 7 ++++ source/mini/lua/lua.utils.cpp | 59 ++++++++++++++++++++++++++ source/mini/lua/lua.utils.h | 14 +++++++ source/mini/version.h | 2 +- source/other/log.h | 8 ++-- 9 files changed, 171 insertions(+), 78 deletions(-) create mode 100644 source/mini/lua/lua.utils.cpp create mode 100644 source/mini/lua/lua.utils.h diff --git a/source/external/lua/loadlib.c b/source/external/lua/loadlib.c index 3fd26c5..f144b41 100644 --- a/source/external/lua/loadlib.c +++ b/source/external/lua/loadlib.c @@ -649,6 +649,13 @@ static void findloader (lua_State *L, const char *name) { // [RZC 12/03/2026] ================================== // 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) { 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 strncpy(out, rest, outsz - 1); } 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'; @@ -742,7 +750,8 @@ static void resolve_module_name(lua_State *L, char *out, size_t outsz) { // Estamos en la raíz strncpy(out, req, outsz - 1); } 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'; diff --git a/source/mini/audio/jail_audio.cpp b/source/mini/audio/jail_audio.cpp index 7a60fb8..0fcbe4e 100644 --- a/source/mini/audio/jail_audio.cpp +++ b/source/mini/audio/jail_audio.cpp @@ -256,8 +256,8 @@ namespace jail void pause() { - if (!music::enabled) return; - if (current<0 || current>static_cast(musics.size())) { + if (!music::enabled || (current<0)) return; + if (current>static_cast(musics.size())) { log_msg(LOG_FAIL, "music::pause: Illegal music handle: %i\n", current); return; } @@ -274,8 +274,8 @@ namespace jail void resume() { - if (!music::enabled) return; - if (current<0 || current>static_cast(musics.size())) { + if (!music::enabled || (current<0)) return; + if (current>static_cast(musics.size())) { log_msg(LOG_FAIL, "music::resume: Illegal music handle: %i\n", current); return; } @@ -292,8 +292,8 @@ namespace jail void stop() { - if (!music::enabled) return; - if (current<0 || current>static_cast(musics.size())) { + if (!music::enabled || (current<0)) return; + if (current>static_cast(musics.size())) { log_msg(LOG_FAIL, "music::stop: Illegal music handle: %i\n", current); return; } @@ -312,8 +312,8 @@ namespace jail void fadeOut(int milliseconds) { - if (!music::enabled) return; - if (current<0 || current>static_cast(musics.size())) { + if (!music::enabled || (current<0)) return; + if (current>static_cast(musics.size())) { log_msg(LOG_FAIL, "music::fadeOut: Illegal music handle: %i\n", current); return; } diff --git a/source/mini/lua/lua.cpp b/source/mini/lua/lua.cpp index b014d81..9a1e5ae 100644 --- a/source/mini/lua/lua.cpp +++ b/source/mini/lua/lua.cpp @@ -1,6 +1,7 @@ #include "lua.h" #include "lua.wrappers.h" #include "lua.debug.h" +#include "lua.utils.h" #include "external/lua/lua.hpp" #include "mini/file/file.h" #include "other/log.h" @@ -22,6 +23,38 @@ namespace mini 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) { const char *name = luaL_checkstring(L, 1); @@ -58,7 +91,7 @@ namespace mini 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)); + 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); } @@ -85,7 +118,7 @@ namespace mini } 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); free(buffer); lua_pushnil(L); @@ -107,14 +140,14 @@ namespace mini int size; char* buffer = file::getfilebuffer(main_lua_file, size); 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); return; } free(buffer); if (lua_pcall(L,0, LUA_MULTRET, 0)) { //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); return; } @@ -135,7 +168,7 @@ namespace mini debug::init(L); //std::thread(debugCommandThread).detach(); - printf("stdin isatty: %d\n", isatty(0)); + //printf("stdin isatty: %d\n", isatty(0)); is_playing = true; } @@ -157,15 +190,15 @@ namespace mini lua_getglobal(L, "mini"); lua_getfield(L, -1, "init"); - #if defined(DEBUG) && defined(__linux__) + if (debug::is_enabled()) { is_playing = debug::call_and_handle_exceptions(L); - #else + } else { 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); is_playing = false; } - #endif + } } void update() { @@ -173,15 +206,15 @@ namespace mini lua_getglobal(L, "mini"); lua_getfield(L, -1, "update"); - #if defined(DEBUG) && defined(__linux__) + if (debug::is_enabled()) { is_playing = debug::call_and_handle_exceptions(L); - #else + } else { 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); is_playing = false; } - #endif + } } } diff --git a/source/mini/lua/lua.debug.cpp b/source/mini/lua/lua.debug.cpp index 0b3bfec..9f1f8fc 100644 --- a/source/mini/lua/lua.debug.cpp +++ b/source/mini/lua/lua.debug.cpp @@ -1,6 +1,7 @@ #if defined(DEBUG) && defined(__linux__) - #include "lua.debug.h" +#include "lua.utils.h" + #include "external/lua/lua.hpp" #include "mini/win/win.h" @@ -63,7 +64,7 @@ namespace mini int g_pauseLine = 0; bool function_has_breakpoints = false; std::stack funBreakStack; - bool debug_enabled = true; + bool debug_enabled = false; enum StepMode { STEP_NONE, @@ -77,52 +78,6 @@ namespace mini 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) { lua_Debug ar; int depth = 0; @@ -880,8 +835,11 @@ namespace mini void processDebugCommand(lua_State* L, const std::string& line) { //printf("COMANDO PROCESADO: %s\n", line.c_str()); - if (!line.starts_with("@@DEBUGCMD@@")) + if (!line.starts_with("@@DEBUGCMD@@")) { + disable(L); return; + } + enable(L); json j = json::parse(line.substr(12)); @@ -1348,7 +1306,7 @@ namespace mini } 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() { @@ -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; } } } } diff --git a/source/mini/lua/lua.debug.h b/source/mini/lua/lua.debug.h index 5cd063d..bba6565 100644 --- a/source/mini/lua/lua.debug.h +++ b/source/mini/lua/lua.debug.h @@ -1,4 +1,5 @@ #pragma once + struct lua_State; namespace mini @@ -13,12 +14,18 @@ namespace mini void kill_thread(); void process_commands(lua_State* L); void toggle(lua_State* L); + void enable(lua_State* L); + void disable(lua_State* L); + bool is_enabled(); #else bool call_and_handle_exceptions(lua_State* L) { return true; }; void init(lua_State* L) {}; void kill_thread() {}; void process_commands(lua_State* L) {}; void toggle(lua_State* L) {}; + void enable(lua_State* L) {}; + void disable(lua_State* L) {}; + bool is_enabled() { return false; }; #endif } diff --git a/source/mini/lua/lua.utils.cpp b/source/mini/lua/lua.utils.cpp new file mode 100644 index 0000000..96045c2 --- /dev/null +++ b/source/mini/lua/lua.utils.cpp @@ -0,0 +1,59 @@ +#include "lua.utils.h" +#include +#include +#include + +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(); + } + } + } +} diff --git a/source/mini/lua/lua.utils.h b/source/mini/lua/lua.utils.h new file mode 100644 index 0000000..114a8c0 --- /dev/null +++ b/source/mini/lua/lua.utils.h @@ -0,0 +1,14 @@ +#pragma once +#include + +namespace mini +{ + namespace lua + { + namespace debug + { + std::string pathToChunk(const std::string& path); + std::string chunkToPath(const std::string& chunk); + } + } +} diff --git a/source/mini/version.h b/source/mini/version.h index 5328fef..76034af 100644 --- a/source/mini/version.h +++ b/source/mini/version.h @@ -1,3 +1,3 @@ #pragma once -#define MINI_VERSION "1.5.5" +#define MINI_VERSION "1.5.6" diff --git a/source/other/log.h b/source/other/log.h index 1daa14d..88bbf18 100644 --- a/source/other/log.h +++ b/source/other/log.h @@ -2,7 +2,7 @@ #include #include -#ifdef DEBUG +//#ifdef DEBUG 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, ...) { @@ -22,6 +22,6 @@ static inline void log_msg(enum LogLevel level, const char *fmt, ...) { vprintf(fmt, args); va_end(args); } -#else -#define log_msg(...) ((void)0) -#endif \ No newline at end of file +//#else +//#define log_msg(...) ((void)0) +//#endif \ No newline at end of file