1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-22 03:39:09 +02:00
RedMew/utils/global.lua
RedRafe 26e1c28dc0
Factorio 2.0 update (#1436)
* Init Factorio 2.0 update

* add credits

* fix test module

* I know luackeck, I know

* Fixes

* Fix bad event.player_index handling

* Hotfixes

* Remove all filter inserters

* Migrate removed items

* Deprecating spidertron control and landfill features
2024-10-22 20:22:35 +01:00

90 lines
1.9 KiB
Lua

local Event = require 'utils.event_core'
local Token = require 'utils.token'
local matching_path = '^.+__level__/(.+)$'
local Global = {}
function Global.register(tbl, callback)
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
local token = Token.register_global(tbl)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
function Global.register_init(tbl, init_handler, callback)
if _LIFECYCLE ~= _STAGE.control then
error('can only be called during the control stage', 2)
end
local token = Token.register_global(tbl)
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
end
)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
if _DEBUG then
local concat = table.concat
local names = {}
Global.names = names
function Global.register(tbl, callback)
local filepath = debug.getinfo(2, 'S').source:match(matching_path):sub(1, -5)
local token = Token.register_global(tbl)
names[token] = concat {token, ' - ', filepath}
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
function Global.register_init(tbl, init_handler, callback)
local filepath = debug.getinfo(2, 'S').source:match(matching_path):sub(1, -5)
local token = Token.register_global(tbl)
names[token] = concat {token, ' - ', filepath}
Event.on_init(
function()
init_handler(tbl)
callback(tbl)
end
)
Event.on_load(
function()
callback(Token.get_global(token))
end
)
return token
end
end
return Global