From ec875b93570ebfeb61ca59abfd3bc6d21d4f347d Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Sat, 31 Oct 2020 16:01:15 -0400 Subject: [PATCH 1/4] Changed Lua console message buffer to allow for strings that are much longer than 256 characters. --- src/drivers/Qt/LuaControl.cpp | 47 +++++++++++------------------------ 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/drivers/Qt/LuaControl.cpp b/src/drivers/Qt/LuaControl.cpp index 338b5d55..7e48c54b 100644 --- a/src/drivers/Qt/LuaControl.cpp +++ b/src/drivers/Qt/LuaControl.cpp @@ -27,60 +27,43 @@ static bool updateLuaDisplay = false; static bool openLuaKillMsgBox = false; static int luaKillMsgBoxRetVal = 0; -struct luaConsoleOutputLine -{ - char text[256]; - - luaConsoleOutputLine(void) - { - memset( text, 0, sizeof(text) ); - } - - void clear(void) - { - memset( text, 0, sizeof(text) ); - } - - void setText( const char *txt ) - { - strncpy( text, txt, sizeof(text)-1 ); - text[sizeof(text)-1] = 0; - } -}; - struct luaConsoleOutputBuffer { int head; int tail; int size; - struct luaConsoleOutputLine *line; + char *buf; luaConsoleOutputBuffer(void) { tail = head = 0; - size = 64; + size = 4096; - line = new luaConsoleOutputLine[size]; + buf = (char*)malloc(size); } ~luaConsoleOutputBuffer(void) { - if ( line ) + if ( buf ) { - delete [] line; line = NULL; + free(buf); buf = NULL; } } void addLine( const char *l ) { + int i=0; //printf("Adding Line %i: '%s'\n", head, l ); - line[head].setText( l ); + while ( l[i] != 0 ) + { + buf[head] = l[i]; i++; - head = (head + 1) % size; + head = (head + 1) % size; - if ( head == tail ) - { - tail = (tail + 1) % size; + if ( head == tail ) + { + tail = (tail + 1) % size; + } } } @@ -349,7 +332,7 @@ void LuaControlDialog_t::refreshState(void) while ( i != outBuf.head ) { - luaOutputText.append( outBuf.line[i].text ); + luaOutputText.append( 1, outBuf.buf[i] ); i = (i + 1) % outBuf.size; } From 0cca02e765e5d7568bf79f9770b6a8ca6302170c Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Sat, 31 Oct 2020 16:08:22 -0400 Subject: [PATCH 2/4] Bug fix for issue #215. MacOS was not changing the current working directory to the lua script location when loading a lua script. --- src/lua-engine.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 2e7d21c2..4b27d8ff 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -7,6 +7,7 @@ #include #elif __APPLE__ #include +#include #include #include #endif @@ -616,7 +617,18 @@ static int emu_loadrom(lua_State *L) #else const char *nameo2 = luaL_checkstring(L,1); char nameo[2048]; - strncpy(nameo, nameo2, sizeof(nameo)); + + if ( nameo2[0] == '/' ) + { + strncpy(nameo, nameo2, sizeof(nameo)); + } + else + { + char cwd[1024]; + getcwd( cwd, sizeof(cwd) ); + snprintf( nameo, sizeof(nameo), "%s/%s", cwd, nameo2 ); + } + //printf("Load ROM: '%s'\n", nameo ); if (!LoadGame(nameo, true)) { reloadLastGame(); @@ -6183,7 +6195,8 @@ void FCEU_LuaFrameBoundary() * * Returns true on success, false on failure. */ -int FCEU_LoadLuaCode(const char *filename, const char *arg) { +int FCEU_LoadLuaCode(const char *filename, const char *arg) +{ if (!DemandLua()) { return 0; @@ -6195,13 +6208,11 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) { luaScriptName = strdup(filename); } -#if defined(WIN32) || defined(__linux) std::string getfilepath = filename; getfilepath = getfilepath.substr(0,getfilepath.find_last_of("/\\") + 1); SetCurrentDir(getfilepath.c_str()); -#endif //stop any lua we might already have had running FCEU_LuaStop(); From ddb879b21a7e36fb736cd71a6a46f450ad7eb5d6 Mon Sep 17 00:00:00 2001 From: mjbudd77 Date: Sat, 31 Oct 2020 16:12:40 -0400 Subject: [PATCH 3/4] Build fix for MacOS. --- src/lua-engine.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 4b27d8ff..76ecfc06 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -10,6 +10,7 @@ #include #include #include +#define SetCurrentDir chdir #endif #ifdef WIN32 From 5c29bdbd8d06e3c7a810152518c964a35478d3f5 Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Sat, 31 Oct 2020 16:25:29 -0400 Subject: [PATCH 4/4] Lua scripting pathing improvements for Linux/MacOS. Use realpath when possible to always have a absolute path for scripts and roms internally. --- src/drivers/Qt/fceuWrapper.cpp | 3 ++- src/lua-engine.cpp | 8 +------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/drivers/Qt/fceuWrapper.cpp b/src/drivers/Qt/fceuWrapper.cpp index d823ae04..4ab56df4 100644 --- a/src/drivers/Qt/fceuWrapper.cpp +++ b/src/drivers/Qt/fceuWrapper.cpp @@ -753,7 +753,8 @@ int fceuWrapperInit( int argc, char *argv[] ) g_config->setOption("SDL.LuaScript", ""); if (s != "") { -#ifdef __linux +#if defined(__linux) || defined(__APPLE__) + // Resolve absolute path to file char fullpath[2048]; if ( realpath( s.c_str(), fullpath ) != NULL ) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 4b27d8ff..e0316e2a 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -618,16 +618,10 @@ static int emu_loadrom(lua_State *L) const char *nameo2 = luaL_checkstring(L,1); char nameo[2048]; - if ( nameo2[0] == '/' ) + if ( realpath( nameo2, nameo ) == NULL ) { strncpy(nameo, nameo2, sizeof(nameo)); } - else - { - char cwd[1024]; - getcwd( cwd, sizeof(cwd) ); - snprintf( nameo, sizeof(nameo), "%s/%s", cwd, nameo2 ); - } //printf("Load ROM: '%s'\n", nameo ); if (!LoadGame(nameo, true)) {