mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-02-07 13:31:40 +02:00
Global and tokens
Fixes an issue where if you unloaded a module midgame, the whole save broke.
This commit is contained in:
parent
720feaf13b
commit
3f582a5cd1
@ -2,38 +2,51 @@ local Event = require 'utils.event_core'
|
||||
local Token = require 'utils.token'
|
||||
|
||||
local Global = {}
|
||||
local concat = table.concat
|
||||
|
||||
local names = {}
|
||||
Global.names = names
|
||||
local concat = table.concat
|
||||
|
||||
function Global.register(tbl, callback)
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
error('can only be called during the control stage', 2)
|
||||
end
|
||||
|
||||
local filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
|
||||
local name = filepath:gsub('/', '_')
|
||||
local token = Token.register_global(tbl)
|
||||
local token_name = Token.register_global_with_name(name, tbl)
|
||||
|
||||
names[token] = concat {token, ' - ', filepath}
|
||||
if token then
|
||||
names[token] = concat {token, ' - ', filepath}
|
||||
else
|
||||
names[token_name] = concat {Token.get_global_index(token_name), ' - ', filepath}
|
||||
end
|
||||
|
||||
Event.on_load(
|
||||
function()
|
||||
callback(Token.get_global(token))
|
||||
if token then
|
||||
callback(Token.get_global(token))
|
||||
else
|
||||
callback(Token.get_global_with_name(name))
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
return token
|
||||
if token then
|
||||
return token
|
||||
else
|
||||
return token_name
|
||||
end
|
||||
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 filepath = debug.getinfo(2, 'S').source:match('^.+/currently%-playing/(.+)$'):sub(1, -5)
|
||||
local name = filepath:gsub('/', '_')
|
||||
local token = Token.register_global(tbl)
|
||||
local token_name = Token.register_global_with_name(name, tbl)
|
||||
|
||||
names[token] = concat {token, ' - ', filepath}
|
||||
if token then
|
||||
names[token] = concat {token, ' - ', filepath}
|
||||
else
|
||||
names[token_name] = concat {Token.get_global_index(token_name), ' - ', filepath}
|
||||
end
|
||||
|
||||
Event.on_init(
|
||||
function()
|
||||
@ -44,11 +57,18 @@ function Global.register_init(tbl, init_handler, callback)
|
||||
|
||||
Event.on_load(
|
||||
function()
|
||||
callback(Token.get_global(token))
|
||||
if token then
|
||||
callback(Token.get_global(token))
|
||||
else
|
||||
callback(Token.get_global_with_name(name))
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
return token
|
||||
if token then
|
||||
return token
|
||||
else
|
||||
return token_name
|
||||
end
|
||||
end
|
||||
|
||||
return Global
|
||||
|
@ -26,9 +26,16 @@ function Token.get(token_id)
|
||||
return tokens[token_id]
|
||||
end
|
||||
|
||||
global.tokens = {}
|
||||
global.tokens = {
|
||||
index = {},
|
||||
counter = 0
|
||||
}
|
||||
|
||||
function Token.register_global(var)
|
||||
if #global.tokens == 0 then
|
||||
return -- migration to newer version
|
||||
end
|
||||
|
||||
local c = #global.tokens + 1
|
||||
|
||||
global.tokens[c] = var
|
||||
@ -36,6 +43,27 @@ function Token.register_global(var)
|
||||
return c
|
||||
end
|
||||
|
||||
function Token.register_global_with_name(name, var)
|
||||
if not global.tokens[name] then
|
||||
global.tokens[name] = var
|
||||
end
|
||||
|
||||
if not global.tokens.index[name] then
|
||||
global.tokens.counter = global.tokens.counter + 1
|
||||
global.tokens.index[name] = global.tokens.counter
|
||||
end
|
||||
|
||||
return name
|
||||
end
|
||||
|
||||
function Token.get_global_index(name)
|
||||
return global.tokens.index[name]
|
||||
end
|
||||
|
||||
function Token.get_global_with_name(name)
|
||||
return global.tokens[name]
|
||||
end
|
||||
|
||||
function Token.get_global(token_id)
|
||||
return global.tokens[token_id]
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user