Rename global to storage

This commit is contained in:
Caleb Heuer
2024-09-17 20:03:25 -06:00
parent 981bc00af4
commit ea256547dd
7 changed files with 48 additions and 50 deletions

View File

@@ -6,7 +6,7 @@ local gui = require("__flib__.gui-lite")
local mod_gui = require("__core__.lualib.mod-gui")
local table = require("__flib__.table")
--- @class FlibDictionaryGlobal
--- @class FlibDictionaryStorage
--- @field init_ran boolean
--- @field raw table<string, Dictionary>
--- @field raw_count integer
@@ -36,12 +36,12 @@ local flib_dictionary = {}
local request_timeout_ticks = (60 * 5)
--- @param init_only boolean?
--- @return FlibDictionaryGlobal
--- @return FlibDictionaryStorage
local function get_data(init_only)
if not global.__flib or not global.__flib.dictionary then
if not storage.__flib or not storage.__flib.dictionary then
error("Dictionary module was not properly initialized - ensure that all lifecycle events are handled.")
end
local data = global.__flib.dictionary
local data = storage.__flib.dictionary
if init_only and data.init_ran then
error("Dictionaries cannot be modified after initialization.")
end
@@ -58,7 +58,7 @@ local function get_translator(language)
end
end
--- @param data FlibDictionaryGlobal
--- @param data FlibDictionaryStorage
local function update_gui(data)
local wip = data.wip
for _, player in pairs(game.players) do
@@ -141,7 +141,7 @@ local function update_gui(data)
end
end
--- @param data FlibDictionaryGlobal
--- @param data FlibDictionaryStorage
--- @return boolean success
local function request_next_batch(data)
local raw = data.raw
@@ -203,7 +203,7 @@ local function request_next_batch(data)
return true
end
--- @param data FlibDictionaryGlobal
--- @param data FlibDictionaryStorage
local function handle_next_language(data)
if not next(data.raw) then
-- This can happen if handle_next_language is called during on_init or on_configuration_changed
@@ -249,12 +249,11 @@ flib_dictionary.on_player_dictionaries_ready = script.generate_event_name()
-- Lifecycle handlers
function flib_dictionary.on_init()
-- Initialize global data
if not global.__flib then
global.__flib = {}
if not storage.__flib then
storage.__flib = {}
end
--- @type FlibDictionaryGlobal
global.__flib.dictionary = {
--- @type FlibDictionaryStorage
storage.__flib.dictionary = {
init_ran = false,
player_language_requests = {},
player_languages = {},
@@ -264,7 +263,6 @@ function flib_dictionary.on_init()
translated = {},
wip = nil,
}
-- Initialize all existing players
for player_index, player in pairs(game.players) do
if player.connected then
flib_dictionary.on_player_joined_game({

View File

@@ -81,10 +81,10 @@ end
--- @deprecated Use 'dictionary-lite' instead.
function flib_dictionary.init()
if not global.__flib then
global.__flib = {}
if not storage.__flib then
storage.__flib = {}
end
global.__flib.dictionary = {
storage.__flib.dictionary = {
in_process = {},
players = {},
raw = { _total_strings = 0 },
@@ -93,14 +93,14 @@ function flib_dictionary.init()
if use_local_storage then
raw = { _total_strings = 0 }
else
raw = global.__flib.dictionary.raw
raw = storage.__flib.dictionary.raw
end
end
--- @deprecated Use 'dictionary-lite' instead.
function flib_dictionary.load()
if not use_local_storage and global.__flib and global.__flib.dictionary then
raw = global.__flib.dictionary.raw
if not use_local_storage and storage.__flib and storage.__flib.dictionary then
raw = storage.__flib.dictionary.raw
end
end
@@ -110,11 +110,11 @@ function flib_dictionary.translate(player)
error("Player must be connected to the game before this function can be called!")
end
local player_data = global.__flib.dictionary.players[player.index]
local player_data = storage.__flib.dictionary.players[player.index]
if player_data then
return
end
global.__flib.dictionary.players[player.index] = {
storage.__flib.dictionary.players[player.index] = {
player = player,
status = "get_language",
requested_tick = game.tick,
@@ -155,7 +155,7 @@ end
--- @deprecated Use 'dictionary-lite' instead.
function flib_dictionary.check_skipped()
local script_data = global.__flib.dictionary
local script_data = storage.__flib.dictionary
local tick = game.tick
for _, player_data in pairs(script_data.players) do
-- If it's been longer than the timeout, request the string again
@@ -211,7 +211,7 @@ function flib_dictionary.process_translation(event_data)
if not event_data.translated then
return
end
local script_data = global.__flib.dictionary
local script_data = storage.__flib.dictionary
if string.find(event_data.result, "FLIB_DICTIONARY_NAME") then
local _, _, dict_name, dict_lang, string_index, translation =
string.find(event_data.result, dictionary_match_string)
@@ -372,7 +372,7 @@ end
--- @deprecated Use 'dictionary-lite' instead.
function flib_dictionary.cancel_translation(player_index)
local script_data = global.__flib.dictionary
local script_data = storage.__flib.dictionary
local player_data = script_data.players[player_index]
if not player_data then
return

View File

@@ -123,16 +123,16 @@ return flib_migration
--- ```lua
--- {
--- ["1.0.1"] = function()
--- global.foo = nil
--- for _, player_table in pairs(global.players) do
--- storage.foo = nil
--- for _, player_table in pairs(storage.players) do
--- player_table.bar = "Lorem ipsum"
--- end
--- end,
--- ["1.0.7"] = function()
--- global.bar = {}
--- storage.bar = {}
--- end
--- ["1.1.0"] = function(arg)
--- global.foo = arg
--- storage.foo = arg
--- end
--- }
--- ```

View File

@@ -13,11 +13,11 @@ local on_tick_n = {}
---
--- Must be called at the **beginning** of `on_init`. Can also be used to delete all current tasks.
function on_tick_n.init()
if not global.__flib then
global.__flib = {}
if not storage.__flib then
storage.__flib = {}
end
--- @type table<number, Tasks>
global.__flib.on_tick_n = {}
storage.__flib.on_tick_n = {}
end
--- Retrieve the tasks for the given tick, if any.
@@ -27,12 +27,12 @@ end
--- @return Tasks?
function on_tick_n.retrieve(tick)
-- Failsafe for rare cases where on_tick can fire before on_init
if not global.__flib or not global.__flib.on_tick_n then
if not storage.__flib or not storage.__flib.on_tick_n then
return
end
local actions = global.__flib.on_tick_n[tick]
local actions = storage.__flib.on_tick_n[tick]
if actions then
global.__flib.on_tick_n[tick] = nil
storage.__flib.on_tick_n[tick] = nil
return actions
end
end
@@ -42,7 +42,7 @@ end
--- @param task any The data representing this task. This can be anything except for a `function`.
--- @return TaskIdent ident An identifier for the task. Save this if you might remove the task before execution.
function on_tick_n.add(tick, task)
local list = global.__flib.on_tick_n
local list = storage.__flib.on_tick_n
local tick_list = list[tick]
if tick_list then
local index = #tick_list + 1
@@ -57,7 +57,7 @@ end
--- Remove a scheduled task.
--- @param ident TaskIdent The identifier object for the task, as returned from `on-tick-n.add`.
function on_tick_n.remove(ident)
local tick_list = global.__flib.on_tick_n[ident.tick]
local tick_list = storage.__flib.on_tick_n[ident.tick]
if not tick_list or not tick_list[ident.index] then
return false
end

View File

@@ -256,7 +256,7 @@ end
--- [1] = 1000,
--- }
--- event.on_tick(function()
--- global.from_k = table.for_n_of(extremely_large_table, global.from_k, 10, function(v) game.print(v) end)
--- storage.from_k = table.for_n_of(extremely_large_table, storage.from_k, 10, function(v) game.print(v) end)
--- end)
--- ```
--- @generic K, V, C

View File

@@ -168,7 +168,7 @@ function gui.build(player)
player.opened = elems.flib_todo_window
--- @type FlibTestGui
global.guis[player.index] = {
storage.guis[player.index] = {
elems = elems,
player = player,
-- State variables
@@ -321,7 +321,7 @@ end
-- The second argument is an optional wrapper function that will be called in lieu of the specified handler of an
-- element. It is used in this case to get the GUI table for the corresponding player before calling the handler.
flib_gui.add_handlers(gui, function(e, handler)
local self = global.guis[e.player_index]
local self = storage.guis[e.player_index]
if self then
handler(self, e)
end
@@ -336,7 +336,7 @@ flib_gui.handle_events()
-- Initalize guis table
script.on_init(function()
global.guis = {}
storage.guis = {}
end)
-- Create the GUI when a player is created
@@ -354,5 +354,5 @@ script.on_event(defines.events.on_player_created, function(e)
end)
-- For a real mod, you would also want to handle on_configuration_changed to rebuild your GUIs, and on_player_removed
-- to remove the GUI table from global. You would also want to ensure that the GUI is valid before running methods.
-- to remove the GUI table from storage. You would also want to ensure that the GUI is valid before running methods.
-- For the sake of brevity, these things were not covered in this demo.

View File

@@ -15,10 +15,10 @@ local type = type
--- @deprecated use `dictionary` instead
function flib_translation.init()
if not global.__flib then
global.__flib = {}
if not storage.__flib then
storage.__flib = {}
end
global.__flib.translation = {
storage.__flib.translation = {
players = {},
translating_players_count = 0,
}
@@ -26,7 +26,7 @@ end
--- @deprecated use `dictionary` instead
function flib_translation.iterate_batch(event_data)
local __translation = global.__flib.translation
local __translation = storage.__flib.translation
if __translation.translating_players_count == 0 then
return
end
@@ -108,7 +108,7 @@ end
--- @deprecated use `dictionary` instead
function flib_translation.process_result(event_data)
local __translation = global.__flib.translation
local __translation = storage.__flib.translation
if __translation.translating_players_count == 0 then
return
end
@@ -140,7 +140,7 @@ end
--- @deprecated use `dictionary` instead
function flib_translation.add_requests(player_index, strings)
local __translation = global.__flib.translation
local __translation = storage.__flib.translation
local player_table = __translation.players[player_index]
if player_table then
player_table.state = "sort"
@@ -179,7 +179,7 @@ end
--- @deprecated use `dictionary` instead
function flib_translation.cancel(player_index)
local __translation = global.__flib.translation
local __translation = storage.__flib.translation
local player_table = __translation.players[player_index]
if not player_table then
log("Tried to cancel translations for player [" .. player_index .. "] when no translations were running!")
@@ -191,12 +191,12 @@ end
--- @deprecated use `dictionary` instead
function flib_translation.is_translating(player_index)
return global.__flib.translation.players[player_index] and true or false
return storage.__flib.translation.players[player_index] and true or false
end
--- @deprecated use `dictionary` instead
function flib_translation.translating_players_count()
return global.__flib.translation.translating_players_count
return storage.__flib.translation.translating_players_count
end
--- @deprecated use `dictionary` instead