loader.lua 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local args = {}
  2. for word in string.gmatch(..., "%S+") do
  3. table.insert(args, word)
  4. end
  5. SERVICE_NAME = args[1]
  6. local main, pattern
  7. local err = {}
  8. for pat in string.gmatch(LUA_SERVICE, "([^;]+);*") do
  9. local filename = string.gsub(pat, "?", SERVICE_NAME)
  10. local f, msg = loadfile(filename)
  11. if not f then
  12. table.insert(err, msg)
  13. else
  14. pattern = pat
  15. main = f
  16. break
  17. end
  18. end
  19. if not main then
  20. error(table.concat(err, "\n"))
  21. end
  22. LUA_SERVICE = nil
  23. package.path , LUA_PATH = LUA_PATH
  24. package.cpath , LUA_CPATH = LUA_CPATH
  25. local service_path = string.match(pattern, "(.*/)[^/?]+$")
  26. if service_path then
  27. service_path = string.gsub(service_path, "?", args[1])
  28. package.path = service_path .. "?.lua;" .. package.path
  29. SERVICE_PATH = service_path
  30. else
  31. local p = string.match(pattern, "(.*/).+$")
  32. SERVICE_PATH = p
  33. end
  34. if LUA_PRELOAD then
  35. local f = assert(loadfile(LUA_PRELOAD))
  36. f(table.unpack(args))
  37. LUA_PRELOAD = nil
  38. end
  39. _G.require = (require "skynet.require").require
  40. main(select(2, table.unpack(args)))