Skip to content

Commit 1a57e10

Browse files
Version 1.5.0
* Replace global with storage
1 parent 93f75e4 commit 1a57e10

9 files changed

Lines changed: 80 additions & 80 deletions

File tree

‎info.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "stdlib",
3-
"version": "1.4.9",
4-
"factorio_version": "1.1",
3+
"version": "1.5.0",
4+
"factorio_version": "2.0",
55
"title": "Factorio Standard Library",
66
"author": "Afforess",
77
"contact": "",
88
"dependencies": [
9-
"base >= 1.1.0"
9+
"base >= 2.0.8"
1010
],
1111
"homepage": "https://github.com/Afforess/Factorio-Stdlib",
1212
"description": "The Factorio Standard Library is a project to bring Factorio modders high-quality, commonly-required utilities and tools.",

‎stdlib/entity/entity.lua‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ function Entity.has(entity, field_name)
2626
end
2727

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

3939
local unit_number = entity.unit_number
4040
if unit_number then
41-
return global._entity_data[unit_number]
41+
return storage._entity_data[unit_number]
4242
else
4343
local entity_name = entity.name
44-
if not global._entity_data[entity_name] then
44+
if not storage._entity_data[entity_name] then
4545
return nil
4646
end
4747

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

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

67-
if not global._entity_data then
68-
global._entity_data = {}
67+
if not storage._entity_data then
68+
storage._entity_data = {}
6969
end
7070

7171
local unit_number = entity.unit_number
7272
if unit_number then
73-
local prev = global._entity_data[unit_number]
74-
global._entity_data[unit_number] = data
73+
local prev = storage._entity_data[unit_number]
74+
storage._entity_data[unit_number] = data
7575
return prev
7676
else
7777
local entity_name = entity.name
78-
if not global._entity_data[entity_name] then
79-
global._entity_data[entity_name] = {}
78+
if not storage._entity_data[entity_name] then
79+
storage._entity_data[entity_name] = {}
8080
end
8181

82-
local entity_category = global._entity_data[entity_name]
82+
local entity_category = storage._entity_data[entity_name]
8383

8484
for i = #entity_category, 1, -1 do
8585
local entity_data = entity_category[i]

‎stdlib/event/changes.lua‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ end
9292
function Changes.on_init()
9393
for _, versions in pairs(Changes.mod_versions) do
9494
local list = {}
95-
local cur_version = game.active_mods[script.mod_name]
95+
local cur_version = script.active_mods[script.mod_name]
9696
for ver in pairs(versions) do
9797
list[ver] = cur_version
9898
end
99-
global._changes = list
99+
storage._changes = list
100100
end
101101
end
102102

@@ -117,14 +117,14 @@ function Changes.on_configuration_changed(event)
117117
end
118118

119119
function Changes.on_mod_changed(this_mod_changes)
120-
global._changes = global._changes or {}
120+
storage._changes = storage._changes or {}
121121

122122
local old = this_mod_changes.old_version
123123
if old then -- Find the last installed version
124124
local versions = {}
125125
for _, path in pairs(Changes.mod_versions) do
126126
for ver, fun in pairs(path) do
127-
if not global._changes[ver] then
127+
if not storage._changes[ver] then
128128
versions[ver] = this_mod_changes.new_version
129129
fun()
130130
log('Migration completed for version ' .. ver)
@@ -134,7 +134,7 @@ function Changes.on_mod_changed(this_mod_changes)
134134
table.each(
135135
versions,
136136
function(v, k)
137-
global._changes[k] = v
137+
storage._changes[k] = v
138138
end
139139
)
140140
end
@@ -147,7 +147,7 @@ function Changes.dump_data()
147147
'return ' .. inspect(Changes[change_type], { longkeys = true, arraykeys = true }))
148148
end
149149
end
150-
game.write_file(Changes.get_file_path('Changes/global.lua'), 'return ' .. inspect(global._changes or nil, { longkeys = true, arraykeys = true }))
150+
game.write_file(Changes.get_file_path('Changes/global.lua'), 'return ' .. inspect(storage._changes or nil, { longkeys = true, arraykeys = true }))
151151
end
152152

153153
return Changes

‎stdlib/event/event.lua‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ if not config.skip_script_protections then -- Protections for post and pre regis
6666
error('Detected attempt to add the STDLIB event module after using script.on_event')
6767
end
6868
end
69-
for name in pairs(Event.script) do
70-
_G.script[name] = function()
71-
error('Detected attempt to register an event using script.' .. name .. ' while using the STDLIB event system ')
72-
end
73-
end
69+
--for name in pairs(Event.script) do
70+
-- _G.script[name] = function()
71+
-- error('Detected attempt to register an event using script.' .. name .. ' while using the STDLIB event system ')
72+
-- end
73+
--end
7474
end
7575

7676
local bootstrap_events = {

‎stdlib/event/force.lua‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--- Force global creation.
2-
-- <p>All new forces will be added to the `global.forces` table.
3-
-- <p>This modules events should be registered after any other Init functions but before any scripts needing `global.players`.
2+
-- <p>All new forces will be added to the `storage.forces` table.
3+
-- <p>This modules events should be registered after any other Init functions but before any scripts needing `storage.players`.
44
-- <p>This modules can register the following events: `on_force_created`, and `on_forces_merging`.
55
-- @module Event.Force
66
-- @usage
@@ -44,10 +44,10 @@ function Force.additional_data(...)
4444
return Force
4545
end
4646

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

62-
--- Merge a copy of the passed data to all forces in `global.forces`.
62+
--- Merge a copy of the passed data to all forces in `storage.forces`.
6363
-- @tparam table data a table containing variables to merge
6464
-- @usage
6565
-- local data = {a = "abc", b = "def"}
6666
-- Force.add_data_all(data)
6767
function Force.add_data_all(data)
6868
table.each(
69-
global.forces,
69+
storage.forces,
7070
function(v)
7171
table.merge(v, table.deepcopy(data))
7272
end
@@ -78,19 +78,19 @@ end
7878
-- @tparam[opt] string|table event table or a string containing force name
7979
-- @tparam[opt=false] boolean overwrite the force data
8080
function Force.init(event, overwrite)
81-
global.forces = global.forces or {}
81+
storage.forces = storage.forces or {}
8282

8383
local force = Game.get_force(event)
8484

8585
if force then
86-
if not global.forces[force.name] or (global.forces[force.name] and overwrite) then
87-
global.forces[force.name] = new(force.name)
88-
return global.forces[force.name]
86+
if not storage.forces[force.name] or (storage.forces[force.name] and overwrite) then
87+
storage.forces[force.name] = new(force.name)
88+
return storage.forces[force.name]
8989
end
9090
else
9191
for name in pairs(game.forces) do
92-
if not global.forces[name] or (global.forces[name] and overwrite) then
93-
global.forces[name] = new(name)
92+
if not storage.forces[name] or (storage.forces[name] and overwrite) then
93+
storage.forces[name] = new(name)
9494
end
9595
end
9696
end
@@ -104,7 +104,7 @@ end
104104

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

110110
function Force.register_init()

‎stdlib/event/player.lua‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,31 @@ function Player.additional_data(...)
4040
return Player
4141
end
4242

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

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

62-
--- Merge a copy of the passed data to all players in `global.players`.
62+
--- Merge a copy of the passed data to all players in `storage.players`.
6363
-- @tparam table data a table containing variables to merge
6464
-- @usage local data = {a = 'abc', b = 'def'}
6565
-- Player.add_data_all(data)
6666
function Player.add_data_all(data)
67-
local pdata = global.players
67+
local pdata = storage.players
6868
table.each(
6969
pdata,
7070
function(v)
@@ -76,41 +76,41 @@ end
7676
--- Remove data for a player when they are deleted.
7777
-- @tparam table event event table containing the `player_index`
7878
function Player.remove(event)
79-
global.players[event.player_index] = nil
79+
storage.players[event.player_index] = nil
8080
end
8181

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

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

9393
if player then --If player is not nil then we are working with a valid player.
94-
if not global.players[player.index] or (global.players[player.index] and overwrite) then
95-
global.players[player.index] = new(player.index)
96-
return global.players[player.index]
94+
if not storage.players[player.index] or (storage.players[player.index] and overwrite) then
95+
storage.players[player.index] = new(player.index)
96+
return storage.players[player.index]
9797
end
9898
else --Check all players
9999
for index in pairs(game.players) do
100-
if not global.players[index] or (global.players[index] and overwrite) then
101-
global.players[index] = new(index)
100+
if not storage.players[index] or (storage.players[index] and overwrite) then
101+
storage.players[index] = new(index)
102102
end
103103
end
104104
end
105105

106-
if global._print_queue then
106+
if storage._print_queue then
107107
table.each(
108-
global._print_queue,
108+
storage._print_queue,
109109
function(msg)
110110
game.print(tostring(msg))
111111
end
112112
)
113-
global._print_queue = nil
113+
storage._print_queue = nil
114114
end
115115
return Player
116116
end

‎stdlib/event/surface.lua‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ end
4040
--- Remove data for a surface when it is deleted.
4141
-- @tparam table event event table containing the surface index
4242
function Surface.remove(event)
43-
global.surfaces[event.surface_index] = nil
43+
storage.surfaces[event.surface_index] = nil
4444
end
4545

4646
function Surface.rename(event)
47-
global.surfaces[event.surface_index].name = event.new_name
47+
storage.surfaces[event.surface_index].name = event.new_name
4848
end
4949

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

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

6868
if surface then
69-
if not global.surfaces[surface.index] or (global.surfaces[surface.index] and overwrite) then
70-
global.surfaces[surface.index] = new(surface.index)
71-
return global.surfaces[surface.index]
69+
if not storage.surfaces[surface.index] or (storage.surfaces[surface.index] and overwrite) then
70+
storage.surfaces[surface.index] = new(surface.index)
71+
return storage.surfaces[surface.index]
7272
end
7373
else --Check all surfaces
7474
for index in pairs(game.surfaces) do
75-
if not global.surfaces[index] or (global.surfaces[index] and overwrite) then
76-
global.surfaces[index] = new(index)
75+
if not storage.surfaces[index] or (storage.surfaces[index] and overwrite) then
76+
storage.surfaces[index] = new(index)
7777
end
7878
end
7979
end
@@ -82,7 +82,7 @@ end
8282

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

8888
function Surface.register_init()

0 commit comments

Comments
 (0)