-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceExtManager.lua
More file actions
59 lines (59 loc) · 1.89 KB
/
InstanceExtManager.lua
File metadata and controls
59 lines (59 loc) · 1.89 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
InstanceExtManager = {}
local callbacks = {}
local enable_callbacks = {}
InstanceExtManager.callbacks = callbacks
function InstanceExtManager.add_callback(instance, callback, name, fn)
if not enable_callbacks[callback] then
log.error("Can't add non-existed callback", 2)
end
local id = type(instance) == "number" and instance or instance.id
local instance_callbacks = callbacks[id]
if not instance_callbacks then
instance_callbacks = {}
callbacks[id] = instance_callbacks
end
local callbacks = instance_callbacks[callback]
if not callbacks then
callbacks = {}
instance_callbacks[callback] = callbacks
end
-- if callbacks[name] then
-- log.warning("Try to override callback:", callback, name)
-- end
callbacks[name] = fn
end
function InstanceExtManager.enable_callback(callback)
if enable_callbacks[callback] then
log.error("Trying to enable an already enabled callback", 2)
else
enable_callbacks[callback] = true
return true
end
end
function InstanceExtManager.remove_callback(instance, callback, name)
local id = type(instance) == "number" and instance or instance.id
local instance_callbacks = callbacks[id]
if not instance_callbacks then
return
end
local callbacks = instance_callbacks[callback]
if not callbacks then
return
end
if not callbacks[name] then
log.warning("Try to remove a non-existent callback")
end
callbacks[name] = nil
end
function InstanceExtManager.callback_exists(instance, callback, name)
local id = type(instance) == "number" and instance or instance.id
local instance_callbacks = callbacks[id]
if not instance_callbacks then
return false
end
local callbacks = instance_callbacks[callback]
if not callbacks then
return false
end
return callbacks[name] ~= nil
end