Skip to content

Commit

Permalink
Merge branch 'ft-lua-package-path'
Browse files Browse the repository at this point in the history
* ft-lua-package-path:
  Add lua package search path for better module folder structure organize.
  • Loading branch information
deflinhec committed Jul 11, 2023
2 parents e08c03e + f8187e5 commit 513fb4a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion server/runtime_lua_loadlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

lua "github.com/heroiclabs/nakama/v3/internal/gopher-lua"
Expand Down Expand Up @@ -54,7 +55,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
Expand Down

0 comments on commit 513fb4a

Please sign in to comment.