From 06e9c8aa2fde9836541ffefb6af340992b01d227 Mon Sep 17 00:00:00 2001 From: djoslin0 Date: Sun, 22 Jun 2025 03:48:42 -0700 Subject: [PATCH] Fix substring bug in require() (#867) * Fix substring bug in require() When require() was searching for a matching module, it would only consider the end of the filename. So foobar.lua and bar.lua could both match a require('bar'). This commit fixes that. * remove debug logs --------- Co-authored-by: MysterD --- src/pc/lua/smlua_require.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pc/lua/smlua_require.c b/src/pc/lua/smlua_require.c index 96552339a..196f8cb76 100644 --- a/src/pc/lua/smlua_require.c +++ b/src/pc/lua/smlua_require.c @@ -38,10 +38,12 @@ static struct ModFile* smlua_find_mod_file(const char* moduleName) { int bestTotalDepth = INT_MAX; bool foundRelativeFile = false; + char rawName[SYS_MAX_PATH] = ""; char luaName[SYS_MAX_PATH] = ""; char luacName[SYS_MAX_PATH] = ""; - snprintf(luaName, SYS_MAX_PATH, "%s.lua", moduleName); - snprintf(luacName, SYS_MAX_PATH, "%s.luac", moduleName); + snprintf(rawName, SYS_MAX_PATH, "/%s", moduleName); + snprintf(luaName, SYS_MAX_PATH, "/%s.lua", moduleName); + snprintf(luacName, SYS_MAX_PATH, "/%s.luac", moduleName); for (int i = 0; i < gLuaActiveMod->fileCount; i++) { struct ModFile* file = &gLuaActiveMod->files[i]; @@ -57,7 +59,7 @@ static struct ModFile* smlua_find_mod_file(const char* moduleName) { } // check for match - if (!str_ends_with(file->relativePath, moduleName) && !str_ends_with(file->relativePath, luaName) && !str_ends_with(file->relativePath, luacName)) { + if (!str_ends_with(file->relativePath, rawName) && !str_ends_with(file->relativePath, luaName) && !str_ends_with(file->relativePath, luacName)) { continue; }