1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/utils/global.lua

55 lines
1.2 KiB
Lua
Raw Normal View History

2019-03-10 00:14:30 +02:00
local Event = require 'utils.event_core'
local Token = require 'utils.token'
2018-09-19 06:51:25 +02:00
2021-04-04 13:51:27 +02:00
local Global = {}
2020-06-24 12:40:18 +02:00
local concat = table.concat
2021-04-04 13:51:27 +02:00
local names = {}
Global.names = names
2018-09-19 06:51:25 +02:00
function Global.register(tbl, callback)
2019-03-10 00:14:30 +02:00
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
2020-06-24 12:40:18 +02:00
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
2018-09-19 06:51:25 +02:00
local token = Token.register_global(tbl)
2019-03-10 00:14:30 +02:00
2021-04-04 13:51:27 +02:00
names[token] = concat {token, ' - ', filepath}
2020-06-24 12:40:18 +02:00
2019-03-10 00:14:30 +02:00
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
2018-09-19 06:51:25 +02:00
end
function Global.register_init(tbl, init_handler, callback)
2019-03-10 00:14:30 +02:00
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
2020-06-24 12:40:18 +02:00
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
2018-09-19 06:51:25 +02:00
local token = Token.register_global(tbl)
2021-04-04 13:51:27 +02:00
names[token] = concat {token, ' - ', filepath}
2020-06-24 12:40:18 +02:00
2019-03-10 00:14:30 +02:00
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
end
)
2018-09-19 06:51:25 +02:00
2019-03-10 00:14:30 +02:00
Event.on_load(
function()
callback(Token.get_global(token))
2018-09-19 06:51:25 +02:00
end
2019-03-10 00:14:30 +02:00
)
return token
2019-03-10 00:14:30 +02:00
end
2018-09-19 06:51:25 +02:00
return Global