Skip to content

Commit

Permalink
Version 1.4.10
Browse files Browse the repository at this point in the history
* Replace global with storage
  • Loading branch information
jackiepmueller committed Oct 22, 2024
1 parent d0eb71c commit 0e1155f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stdlib",
"version": "1.4.9",
"version": "1.4.10",
"factorio_version": "2.0",
"title": "Factorio Standard Library",
"author": "Afforess",
Expand Down
26 changes: 13 additions & 13 deletions stdlib/entity/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ function Entity.has(entity, field_name)
end

--- Gets the user data that is associated with an entity.
-- The user data is stored in the global object and it persists between loads.
-- The user data is stored in the storage object and it persists between loads.
--> The user data will be removed from an entity when the entity becomes invalid.
-- @tparam LuaEntity entity the entity to look up
-- @treturn ?|nil|Mixed the user data, or nil if no data exists for the entity
function Entity.get_data(entity)
assert(entity, 'missing entity argument')
if not global._entity_data then
if not storage._entity_data then
return nil
end

local unit_number = entity.unit_number
if unit_number then
return global._entity_data[unit_number]
return storage._entity_data[unit_number]
else
local entity_name = entity.name
if not global._entity_data[entity_name] then
if not storage._entity_data[entity_name] then
return nil
end

local entity_category = global._entity_data[entity_name]
local entity_category = storage._entity_data[entity_name]
for _, entity_data in pairs(entity_category) do
if Entity._are_equal(entity_data.entity, entity) then
return entity_data.data
Expand All @@ -56,30 +56,30 @@ function Entity.get_data(entity)
end

--- Associates the user data to an entity.
-- The user data will be stored in the global object and it will persist between loads.
-- The user data will be stored in the storage object and it will persist between loads.
--> The user data will be removed from an entity when the entity becomes invalid.
-- @tparam LuaEntity entity the entity with which to associate the user data
-- @tparam ?|nil|Mixed data the data to set, or nil to delete the data associated with the entity
-- @treturn ?|nil|Mixed the previous data associated with the entity, or nil if the entity had no previous data
function Entity.set_data(entity, data)
assert(entity, 'missing entity argument')

if not global._entity_data then
global._entity_data = {}
if not storage._entity_data then
storage._entity_data = {}
end

local unit_number = entity.unit_number
if unit_number then
local prev = global._entity_data[unit_number]
global._entity_data[unit_number] = data
local prev = storage._entity_data[unit_number]
storage._entity_data[unit_number] = data
return prev
else
local entity_name = entity.name
if not global._entity_data[entity_name] then
global._entity_data[entity_name] = {}
if not storage._entity_data[entity_name] then
storage._entity_data[entity_name] = {}
end

local entity_category = global._entity_data[entity_name]
local entity_category = storage._entity_data[entity_name]

for i = #entity_category, 1, -1 do
local entity_data = entity_category[i]
Expand Down
12 changes: 6 additions & 6 deletions stdlib/event/changes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ end
function Changes.on_init()
for _, versions in pairs(Changes.mod_versions) do
local list = {}
local cur_version = game.active_mods[script.mod_name]
local cur_version = script.active_mods[script.mod_name]
for ver in pairs(versions) do
list[ver] = cur_version
end
global._changes = list
storage._changes = list
end
end

Expand All @@ -117,14 +117,14 @@ function Changes.on_configuration_changed(event)
end

function Changes.on_mod_changed(this_mod_changes)
global._changes = global._changes or {}
storage._changes = storage._changes or {}

local old = this_mod_changes.old_version
if old then -- Find the last installed version
local versions = {}
for _, path in pairs(Changes.mod_versions) do
for ver, fun in pairs(path) do
if not global._changes[ver] then
if not storage._changes[ver] then
versions[ver] = this_mod_changes.new_version
fun()
log('Migration completed for version ' .. ver)
Expand All @@ -134,7 +134,7 @@ function Changes.on_mod_changed(this_mod_changes)
table.each(
versions,
function(v, k)
global._changes[k] = v
storage._changes[k] = v
end
)
end
Expand All @@ -147,7 +147,7 @@ function Changes.dump_data()
'return ' .. inspect(Changes[change_type], { longkeys = true, arraykeys = true }))
end
end
game.write_file(Changes.get_file_path('Changes/global.lua'), 'return ' .. inspect(global._changes or nil, { longkeys = true, arraykeys = true }))
game.write_file(Changes.get_file_path('Changes/global.lua'), 'return ' .. inspect(storage._changes or nil, { longkeys = true, arraykeys = true }))
end

return Changes
28 changes: 14 additions & 14 deletions stdlib/event/force.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- Force global creation.
-- <p>All new forces will be added to the `global.forces` table.
-- <p>This modules events should be registered after any other Init functions but before any scripts needing `global.players`.
-- <p>All new forces will be added to the `storage.forces` table.
-- <p>This modules events should be registered after any other Init functions but before any scripts needing `storage.players`.
-- <p>This modules can register the following events: `on_force_created`, and `on_forces_merging`.
-- @module Event.Force
-- @usage
Expand Down Expand Up @@ -44,10 +44,10 @@ function Force.additional_data(...)
return Force
end

--- Get `game.forces[name]` & `global.forces[name]`, or create `global.forces[name]` if it doesn't exist.
--- Get `game.forces[name]` & `storage.forces[name]`, or create `storage.forces[name]` if it doesn't exist.
-- @tparam string|LuaForce force the force to get data for
-- @treturn LuaForce the force instance
-- @treturn table the force's global data
-- @treturn table the force's storage data
-- @usage
-- local Force = require('__stdlib__/stdlib/event/force')
-- local force_name, force_data = Force.get("player")
Expand All @@ -56,17 +56,17 @@ end
function Force.get(force)
force = Game.get_force(force)
assert(force, 'force is missing')
return game.forces[force.name], global.forces and global.forces[force.name] or Force.init(force.name)
return game.forces[force.name], storage.forces and storage.forces[force.name] or Force.init(force.name)
end

--- Merge a copy of the passed data to all forces in `global.forces`.
--- Merge a copy of the passed data to all forces in `storage.forces`.
-- @tparam table data a table containing variables to merge
-- @usage
-- local data = {a = "abc", b = "def"}
-- Force.add_data_all(data)
function Force.add_data_all(data)
table.each(
global.forces,
storage.forces,
function(v)
table.merge(v, table.deepcopy(data))
end
Expand All @@ -78,19 +78,19 @@ end
-- @tparam[opt] string|table event table or a string containing force name
-- @tparam[opt=false] boolean overwrite the force data
function Force.init(event, overwrite)
global.forces = global.forces or {}
storage.forces = storage.forces or {}

local force = Game.get_force(event)

if force then
if not global.forces[force.name] or (global.forces[force.name] and overwrite) then
global.forces[force.name] = new(force.name)
return global.forces[force.name]
if not storage.forces[force.name] or (storage.forces[force.name] and overwrite) then
storage.forces[force.name] = new(force.name)
return storage.forces[force.name]
end
else
for name in pairs(game.forces) do
if not global.forces[name] or (global.forces[name] and overwrite) then
global.forces[name] = new(name)
if not storage.forces[name] or (storage.forces[name] and overwrite) then
storage.forces[name] = new(name)
end
end
end
Expand All @@ -104,7 +104,7 @@ end

--- When forces are merged, just remove the original forces data
function Force.merged(event)
global.forces[event.source_name] = nil
storage.forces[event.source_name] = nil
end

function Force.register_init()
Expand Down
36 changes: 18 additions & 18 deletions stdlib/event/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ function Player.additional_data(...)
return Player
end

--- Get `game.players[index]` & `global.players[index]`, or create `global.players[index]` if it doesn't exist.
--- Get `game.players[index]` & `storage.players[index]`, or create `storage.players[index]` if it doesn't exist.
-- @tparam number|string|LuaPlayer player the player index to get data for
-- @treturn LuaPlayer the player instance
-- @treturn table the player's global data
-- @treturn table the player's storage data
-- @usage
-- local Player = require('__stdlib__/stdlib/event/player')
-- local player, player_data = Player.get(event.player_index)
function Player.get(player)
player = Game.get_player(player)
return player, global.players and global.players[player.index] or Player.init(player.index)
return player, storage.players and storage.players[player.index] or Player.init(player.index)
end

--- Get the players saved data table. Creates it if it doesn't exsist.
-- @tparam number index The player index to get data for
-- @treturn table the player's global data
-- @treturn table the player's storage data
function Player.pdata(index)
return global.players and global.players[index] or Player.init(index)
return storage.players and storage.players[index] or Player.init(index)
end

--- Merge a copy of the passed data to all players in `global.players`.
--- Merge a copy of the passed data to all players in `storage.players`.
-- @tparam table data a table containing variables to merge
-- @usage local data = {a = 'abc', b = 'def'}
-- Player.add_data_all(data)
function Player.add_data_all(data)
local pdata = global.players
local pdata = storage.players
table.each(
pdata,
function(v)
Expand All @@ -76,41 +76,41 @@ end
--- Remove data for a player when they are deleted.
-- @tparam table event event table containing the `player_index`
function Player.remove(event)
global.players[event.player_index] = nil
storage.players[event.player_index] = nil
end

--- Init or re-init a player or players.
-- Passing a `nil` event will iterate all existing players.
-- @tparam[opt] number|table|string|LuaPlayer event
-- @tparam[opt=false] boolean overwrite the player data
function Player.init(event, overwrite)
-- Create the global.players table if it doesn't exisit
global.players = global.players or {}
-- Create the storage.players table if it doesn't exisit
storage.players = storage.players or {}

--get a valid player object or nil
local player = Game.get_player(event)

if player then --If player is not nil then we are working with a valid player.
if not global.players[player.index] or (global.players[player.index] and overwrite) then
global.players[player.index] = new(player.index)
return global.players[player.index]
if not storage.players[player.index] or (storage.players[player.index] and overwrite) then
storage.players[player.index] = new(player.index)
return storage.players[player.index]
end
else --Check all players
for index in pairs(game.players) do
if not global.players[index] or (global.players[index] and overwrite) then
global.players[index] = new(index)
if not storage.players[index] or (storage.players[index] and overwrite) then
storage.players[index] = new(index)
end
end
end

if global._print_queue then
if storage._print_queue then
table.each(
global._print_queue,
storage._print_queue,
function(msg)
game.print(tostring(msg))
end
)
global._print_queue = nil
storage._print_queue = nil
end
return Player
end
Expand Down
20 changes: 10 additions & 10 deletions stdlib/event/surface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ end
--- Remove data for a surface when it is deleted.
-- @tparam table event event table containing the surface index
function Surface.remove(event)
global.surfaces[event.surface_index] = nil
storage.surfaces[event.surface_index] = nil
end

function Surface.rename(event)
global.surfaces[event.surface_index].name = event.new_name
storage.surfaces[event.surface_index].name = event.new_name
end

function Surface.import(event)
Expand All @@ -59,21 +59,21 @@ end
-- @tparam[opt] number|table|string|LuaSurface event
-- @tparam[opt=false] boolean overwrite the surface data
function Surface.init(event, overwrite)
-- Create the global.surfaces table if it doesn't exisit
global.surfaces = global.surfaces or {}
-- Create the storage.surfaces table if it doesn't exisit
storage.surfaces = storage.surfaces or {}

--get a valid surface object or nil
local surface = game.surfaces[event.surface_index]

if surface then
if not global.surfaces[surface.index] or (global.surfaces[surface.index] and overwrite) then
global.surfaces[surface.index] = new(surface.index)
return global.surfaces[surface.index]
if not storage.surfaces[surface.index] or (storage.surfaces[surface.index] and overwrite) then
storage.surfaces[surface.index] = new(surface.index)
return storage.surfaces[surface.index]
end
else --Check all surfaces
for index in pairs(game.surfaces) do
if not global.surfaces[index] or (global.surfaces[index] and overwrite) then
global.surfaces[index] = new(index)
if not storage.surfaces[index] or (storage.surfaces[index] and overwrite) then
storage.surfaces[index] = new(index)
end
end
end
Expand All @@ -82,7 +82,7 @@ end

function Surface.dump_data()
game.write_file(Surface.get_file_path('Surface/surface_data.lua'), inspect(Surface._new_surface_data, { longkeys = true, arraykeys = true }))
game.write_file(Surface.get_file_path('Surface/global.lua'), inspect(global.surfaces or nil, { longkeys = true, arraykeys = true }))
game.write_file(Surface.get_file_path('Surface/global.lua'), inspect(storage.surfaces or nil, { longkeys = true, arraykeys = true }))
end

function Surface.register_init()
Expand Down
Loading

0 comments on commit 0e1155f

Please sign in to comment.