From f8187e5e5eb63bc7cd1bd7382a242d0418d3c161 Mon Sep 17 00:00:00 2001 From: deflinhec Date: Tue, 11 Jul 2023 04:30:31 +0200 Subject: [PATCH] Add lua package search path for better module folder structure organize. --- server/runtime_lua_loadlib.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/runtime_lua_loadlib.go b/server/runtime_lua_loadlib.go index c8c47b0231..b101ac789c 100644 --- a/server/runtime_lua_loadlib.go +++ b/server/runtime_lua_loadlib.go @@ -28,6 +28,7 @@ import ( "github.com/yuin/gopher-lua" "os" "path/filepath" + "regexp" "strings" ) @@ -53,7 +54,22 @@ func OpenPackage(moduleCache *RuntimeLuaModuleCache) func(L *lua.LState) int { return func(L *lua.LState) int { loLoaderCache := func(L *lua.LState) int { name := L.CheckString(1) - module, ok := moduleCache.Modules[name] + var ok bool + var module *RuntimeLuaModule + dir := lua.LuaLDir + lua.LuaDirSep + packagemod := L.RegisterModule(lua.LoadLibName, loFuncs) + searchpath := L.GetField(packagemod, "path").String() + pattern := regexp.MustCompile(`^\.` + lua.LuaDirSep) + for _, path := range strings.Split(searchpath, ";") { + path = pattern.ReplaceAllString(path, dir) + path = strings.TrimPrefix(path, dir) + path = strings.TrimSuffix(path, ".lua") + path = strings.ReplaceAll(path, "?", name) + path = strings.ReplaceAll(path, lua.LuaDirSep, ".") + if module, ok = moduleCache.Modules[path]; ok { + break + } + } if !ok { L.Push(lua.LString(fmt.Sprintf("no cached module '%s'", name))) return 1