From c4b142f770df2cbc4c2319c141f230708686aec2 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 23 Oct 2020 22:27:36 +0800 Subject: [PATCH 1/2] Load the Lua socket library dynamically if it's available, necessary for MobDebug --- nel/include/nel/gui/lua_helper.h | 4 +++ nel/src/gui/lua_helper.cpp | 53 ++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/nel/include/nel/gui/lua_helper.h b/nel/include/nel/gui/lua_helper.h index da5c33e6a..8a0f985a6 100644 --- a/nel/include/nel/gui/lua_helper.h +++ b/nel/include/nel/gui/lua_helper.h @@ -379,6 +379,10 @@ namespace NLGUI TSmallScriptCache _SmallScriptCache; static const char * _NELSmallScriptTableName; +#ifdef _WIN32 + HMODULE m_LuaSocket; +#endif + private: // this object isn't intended to be copied CLuaState(const CLuaState &/* other */):NLMISC::CRefCount() { nlassert(0); } diff --git a/nel/src/gui/lua_helper.cpp b/nel/src/gui/lua_helper.cpp index d2b6a68d3..c27c6aeba 100644 --- a/nel/src/gui/lua_helper.cpp +++ b/nel/src/gui/lua_helper.cpp @@ -216,16 +216,55 @@ namespace NLGUI // *** Load base libs { CLuaStackChecker lsc(this); - #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501 +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501 luaL_openlibs(_State); - #else +#else luaopen_base (_State); luaopen_table (_State); luaopen_io (_State); luaopen_string (_State); luaopen_math (_State); luaopen_debug (_State); - #endif +#endif + +#ifdef _WIN32 + // Lua socket library for MobDebug, optional + if (NLMISC::CFile::fileExists("socket\\core.dll")) + { + // Load socket\core.dll dynamically + m_LuaSocket = LoadLibraryW(L"socket\\core.dll"); + if (!m_LuaSocket) + { + nlwarning("Lua socket library found, but failed to load"); + } + else + { + void *luaopen_socket_core = (void *)GetProcAddress(m_LuaSocket, "luaopen_socket_core"); + if (!luaopen_socket_core) + { + nlwarning("Lua socket library loaded, but `luaopen_socket_core` not found"); + FreeLibrary(m_LuaSocket); + m_LuaSocket = NULL; + } + else + { + // preload['socket.core'] = luaopen_socket_core +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501 + lua_getglobal(_State, "package"); + lua_getfield(_State, -1, "preload"); + lua_pushcfunction(_State, (lua_CFunction)luaopen_socket_core); + lua_setfield(_State, -2, "socket.core"); + lua_pop(_State, 2); + nlinfo("Lua socket library preloaded"); +#endif + } + } + } + else + { + m_LuaSocket = NULL; + } +#endif // open are buggy???? clear(); @@ -313,6 +352,14 @@ namespace NLGUI // Clear Small Script Cache _SmallScriptPool= 0; _SmallScriptCache.clear(); + +#ifdef _WIN32 + if (m_LuaSocket) + { + FreeLibrary(m_LuaSocket); + m_LuaSocket = NULL; + } +#endif } // *************************************************************************** From 6531fb0a2eb1e40f4cd792de0916b8203ae1b8b2 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 23 Oct 2020 23:18:20 +0800 Subject: [PATCH 2/2] Lua debugger requires properly standardized paths --- nel/src/gui/lua_helper.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/nel/src/gui/lua_helper.cpp b/nel/src/gui/lua_helper.cpp index c27c6aeba..a07ba3b71 100644 --- a/nel/src/gui/lua_helper.cpp +++ b/nel/src/gui/lua_helper.cpp @@ -511,7 +511,23 @@ namespace NLGUI // execute the script text, with dbgSrc==filename (use @ for lua internal purpose) - executeScriptInternal(script, string("@") + CFile::getFilename(pathName)); +#ifdef _WIN32 + // Paths need to be correct for debugging to work + std::string pathNameStandardized = pathName; + if (pathNameStandardized.size() > 1) + { + if (pathNameStandardized[1] == ':' && pathNameStandardized[0] >= 'a' && pathNameStandardized[0] <= 'z') + pathNameStandardized[0] -= 'a' - 'A'; + for (ptrdiff_t i = 0; i < (ptrdiff_t)pathNameStandardized.size(); ++i) + { + if (pathNameStandardized[i] == '/') + pathNameStandardized[i] = '\\'; + } + } +#else + const std::string &pathNameStandardized = pathName; +#endif + executeScriptInternal(script, string("@") + pathNameStandardized); return true; }