Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. -- The complete package library as relevant to possible SRB2 implementation --
  2. -- (pseudocode, but reference Lua 5.3)
  3.  
  4. -- global package table
  5. package = {
  6. preload = {},
  7. loaded = {},
  8. searchers = {}
  9. }
  10.  
  11. -- These searcher functions will be used below.
  12. -- They are intended to return a Lua function that represents the script file, and an optional error message.
  13.  
  14. local function search_preload(name)
  15. return package.preload[name]
  16. end
  17.  
  18. local function search_lumps(name)
  19. -- load Lua script lump from loaded wads
  20. local chunk, msg = load(name)
  21. return chunk, msg or "lump '"..name.."' not found"
  22. end
  23.  
  24. -- Lua users can add their own script finders to the list.
  25. -- This is probably not useful to SRB2.
  26. package.searchers = { search_preload, search_lumps }
  27.  
  28. function require(name)
  29. -- check package.loaded first
  30. if package.loaded[name] then
  31. -- return previous value
  32. return package.loaded[name]
  33. end
  34.  
  35. local chunk, elist = nil, "Search for '"..name.."' failed:"
  36. for i, search in ipairs(package.searchers) do
  37. local msg
  38.  
  39. chunk, msg = search(name)
  40.  
  41. if chunk then
  42. -- found a chunk!
  43. break
  44. end
  45.  
  46. if msg then
  47. -- append an error
  48. elist = elist .. "\n\t" .. msg
  49. end
  50. end
  51.  
  52. -- if we've exhausted our list of searchers and not found a chunk,
  53. -- then admit failure and dump all the combined errors we've been saving.
  54. if not chunk then
  55. error(elist)
  56. end
  57.  
  58. -- Do make sure to check that chunk is a function,
  59. -- and not some random value a user threw into package.preload
  60. assert(type(chunk) == "function")
  61.  
  62. -- run the script and save its return value
  63. package.loaded[name] = chunk()
  64.  
  65. -- package.loaded must only contain non-false values,
  66. -- even if the script explicitly returns false or 0
  67. if not package.loaded[name] then
  68. package.loaded[name] = true
  69. end
  70.  
  71. -- return the value the script gave us
  72. return package.loaded[name]
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement