-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototypes.lua
More file actions
107 lines (97 loc) · 1.99 KB
/
prototypes.lua
File metadata and controls
107 lines (97 loc) · 1.99 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function object(fn)
local __ENV = getfenv()
return function(...)
local _ENV = {}
for k,v in pairs(__ENV) do
_ENV[k] = v
end
local interface = {}
local obj = {}
local properties = {}
properties.setup = function(name, get, set)
properties[name] = {
get = get,
set = set,
}
end
_ENV.properties = properties
setmetatable(_ENV, {
__index = function(t, k)
if k=='self'then
return obj
elseif k=='interface' then
return interface
else
return interface[k]
end
end,
__newindex = function(t, k, v)
if k=='self'then
obj = v
else
interface[k] = v
end
end,
})
setfenv(fn, _ENV)
fn(...)
setmetatable(obj, {
__index = function(t, k)
if k=="interface" then
return interface
else
local property = properties[k]
if type(property)=='table' and type(property.get)=='function' then
return property.get()
else
local fn = rawget(interface, 'get')
if type(fn)=='function' then
return fn(k)
else
return interface[k]
end
end
end
end,
__newindex = function(t, k, v)
local property = properties[k]
if type(property)=='table' and type(property.set)=='function' then
property.set(v)
else
local fn = rawget(interface, 'set')
if type(fn)=='function' then
fn(k, v)
else
interface[k] = v
end
end
end,
})
return obj
end
end
local cachedPrototypes = {}
local function prototype(name)
assert(type(name)=='string')
local p = cachedPrototypes[name]
if not f then
local f, msg = loadfile(('./prototypes/%s.lua'):format(name))
if not f then
f, msg = loadfile(('./prototypes/components/%s.lua'):format(name))
assert(f, "Component not found\n"..tostring(msg))
end
assert(type(f)=='function')
p = f()
cachedPrototypes[name]=p
end
assert(type(p)=='function')
return p
end
local M = {
}
setmetatable(M, {
__index = function(t, k)
return prototype(k)
end,
})
return M