This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.lua
executable file
·92 lines (76 loc) · 2.5 KB
/
loader.lua
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
assert(sys.provides "nested-nodes", "nested nodes feature missing")
local GLOBAL_CONTENTS, GLOBAL_CHILDS = node.make_nested()
local M = {}
local modules = {}
local modules_content_versions = {}
local function module_event(child, event_name, content, ...)
if not modules[child] then
return
end
local event = modules[child][event_name]
if event then
-- print('-> event', event_name, content)
return event(content, ...)
end
end
local function module_unload(child)
for content, version in pairs(modules_content_versions[child]) do
module_event(child, 'content_remove', content)
end
module_event(child, 'unload')
modules[child] = nil
node.gc()
end
local function module_update(child, module_func)
module_unload(child)
local module = module_func(function(name)
return child .. "/" .. name
end, GLOBAL_CHILDS[child], GLOBAL_CONTENTS[child])
modules[child] = module
for content, version in pairs(modules_content_versions[child]) do
module_event(child, 'content_update', content)
end
node.gc()
end
local function module_update_content(child, content, version)
local mcv = modules_content_versions[child]
if not mcv[content] or mcv[content] < version then
mcv[content] = version
return module_event(child, 'content_update', content)
end
end
local function module_delete_content(child, content)
local mcv = modules_content_versions[child]
modules_content_versions[child][content] = nil
return module_event(child, 'content_remove', content)
end
node.event("child_add", function(child)
modules_content_versions[child] = {}
end)
node.event("child_remove", function(child)
modules_content_versions[child] = nil
end)
node.event("content_update", function(name, obj)
local child, content = util.splitpath(name)
if child == '' then -- not interested in top level events
return
elseif content == 'module.lua' then
return module_update(child, assert(
loadstring(resource.load_file(obj), "=" .. name)
))
else
return module_update_content(child, content, GLOBAL_CONTENTS[child][name])
end
end)
node.event("content_remove", function(name)
local child, content = util.splitpath(name)
if child == '' then -- not interested in top level events
return
elseif content == 'module.lua' then
return module_unload(child)
else
return module_delete_content(child, content)
end
end)
M.modules = modules
return M