-
Notifications
You must be signed in to change notification settings - Fork 1
/
CallbackHandler.lua
120 lines (96 loc) · 3.18 KB
/
CallbackHandler.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
-- CallbackHandler.lua
local CallbackHandler = {}
local registeredCallbacks = {}
local DEBUG = true
local function log(...)
if DEBUG then
print("[CallbackHandler]", ...)
end
end
function CallbackHandler.registerFile(filePath)
log("Attempting to register callbacks from:", filePath)
if not love.filesystem.getInfo(filePath) then
log("ERROR: File not found:", filePath)
return false, "File not found: " .. filePath
end
local content = love.filesystem.read(filePath)
if not content then
log("ERROR: Failed to read file:", filePath)
return false, "Failed to read file"
end
-- Clear existing callbacks before registering new ones
registeredCallbacks = {}
local env = setmetatable({}, {__index = _G})
local chunk, err = load(content, filePath, "t", env)
if not chunk then
log("ERROR: Failed to load callback file:", err)
return false, err
end
local success, result = pcall(chunk)
if not success then
log("ERROR: Failed to execute callback file:", result)
return false, result
end
if type(result) ~= "table" then
log("ERROR: Callback file must return a table of functions")
return false, "Invalid callback file format"
end
-- Print all available callbacks for debugging
log("Available callbacks in file:")
for name, _ in pairs(result) do
log(" -", name)
end
local count = 0
for name, func in pairs(result) do
if type(func) == "function" then
log("Registered callback:", name)
registeredCallbacks[name] = func
count = count + 1
else
log("WARNING: Skipping invalid callback:", name, "- not a function")
end
end
-- Print all registered callbacks for verification
log("Currently registered callbacks:")
for name, _ in pairs(registeredCallbacks) do
log(" -", name)
end
log("Successfully registered", count, "callbacks")
return true, count
end
function CallbackHandler.getCallback(name)
log("Getting callback:", name)
local callback = registeredCallbacks[name]
if not callback then
log("WARNING: Callback not found:", name)
return nil, "Callback not found: " .. name
end
log("Successfully retrieved callback:", name)
return callback
end
function CallbackHandler.executeCallback(name, ...)
log("Executing callback:", name)
local callback = CallbackHandler.getCallback(name)
if not callback then
return false, "Callback not found: " .. name
end
local success, result = pcall(callback, ...)
if not success then
log("ERROR: Failed to execute callback:", name, "-", result)
return false, result
end
log("Successfully executed callback:", name)
return true, result
end
function CallbackHandler.listCallbacks()
local callbacks = {}
for name, _ in pairs(registeredCallbacks) do
table.insert(callbacks, name)
end
return callbacks
end
function CallbackHandler.clear()
log("Clearing all callbacks")
registeredCallbacks = {}
end
return CallbackHandler