-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequire.lua
More file actions
76 lines (68 loc) · 1.97 KB
/
require.lua
File metadata and controls
76 lines (68 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
--[[
Require module for Playdate SDK
© Dimitri Barronmore
License: MIT/X11
https://mit-license.org/
--]]
-- Check if it's already been defined, so that this can
-- be safely used as a built-in dependency in libraries
-- and for cross-platform code.
if require then
return
end
package = {}
--package.preload = {}
package.loaded = {}
package.path = "./?.lua;./?/init.lua"
package.config = "/\n;\n?\n!\n-\n"
local pdz_filepath = function(filepath)
for dirpath in package.path:gmatch("[^;]+") do
local fixed_name = filepath:gsub("%.", "/")
local fixed_path = dirpath:gsub("%.lua", ".pdz")
fixed_path = fixed_path:gsub("%?", fixed_name)
if playdate.file.exists(fixed_path) then
return fixed_path
end
end
end
local pdz_searcher = function(modulepath)
local filepath = pdz_filepath(modulepath)
if not filepath then
return ("\n\tno file '%s.pdz' in package.path"):format(modulepath, 2)
end
-- Shortcutting because I'm lazy.
-- return function(reqpath)
return playdate.file.load(filepath)
-- end
end
package.searchers = { pdz_searcher }
function require(modulename)
if package.loaded[modulename] then
return table.unpack(package.loaded[modulename])
end
local loader
local errors = {}
for _, searcher in ipairs(package.searchers) do
local res = searcher(modulename)
if type(res) == "function" then
loader = res
break
else
table.insert(errors, res)
end
end
if not loader then
error(table.concat(errors))
end
-- shortcutting because I'm lazy.
local res = { pcall(loader, modulename) }
local status = table.remove(res, 1)
if status == false then
error("error loading module '" .. modulename .. "'\n" .. res[1], 2)
end
if #res == 0 then
res = {true}
end
package.loaded[modulename] = res
return table.unpack(res)
end