From 0e1155f236d2c690c3d0786a5ae6fa611eae52a4 Mon Sep 17 00:00:00 2001 From: Jackie Mueller Date: Tue, 22 Oct 2024 19:30:55 -0400 Subject: [PATCH] Version 1.4.10 * Replace global with storage --- info.json | 2 +- stdlib/entity/entity.lua | 26 ++++++++++++------------- stdlib/event/changes.lua | 12 ++++++------ stdlib/event/force.lua | 28 +++++++++++++-------------- stdlib/event/player.lua | 36 +++++++++++++++++------------------ stdlib/event/surface.lua | 20 +++++++++---------- stdlib/game.lua | 18 +++++++++--------- stdlib/scripts/quickstart.lua | 4 ++-- 8 files changed, 73 insertions(+), 73 deletions(-) diff --git a/info.json b/info.json index 79422ea8..59417ccc 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "stdlib", - "version": "1.4.9", + "version": "1.4.10", "factorio_version": "2.0", "title": "Factorio Standard Library", "author": "Afforess", diff --git a/stdlib/entity/entity.lua b/stdlib/entity/entity.lua index 685e8435..173528c4 100644 --- a/stdlib/entity/entity.lua +++ b/stdlib/entity/entity.lua @@ -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 @@ -56,7 +56,7 @@ 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 @@ -64,22 +64,22 @@ end 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] diff --git a/stdlib/event/changes.lua b/stdlib/event/changes.lua index 97f216c4..b9f577a3 100644 --- a/stdlib/event/changes.lua +++ b/stdlib/event/changes.lua @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/stdlib/event/force.lua b/stdlib/event/force.lua index e0b6f0eb..a14c882d 100644 --- a/stdlib/event/force.lua +++ b/stdlib/event/force.lua @@ -1,6 +1,6 @@ --- Force global creation. ---

All new forces will be added to the `global.forces` table. ---

This modules events should be registered after any other Init functions but before any scripts needing `global.players`. +--

All new forces will be added to the `storage.forces` table. +--

This modules events should be registered after any other Init functions but before any scripts needing `storage.players`. --

This modules can register the following events: `on_force_created`, and `on_forces_merging`. -- @module Event.Force -- @usage @@ -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") @@ -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 @@ -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 @@ -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() diff --git a/stdlib/event/player.lua b/stdlib/event/player.lua index 93648f13..55a61d13 100644 --- a/stdlib/event/player.lua +++ b/stdlib/event/player.lua @@ -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) @@ -76,7 +76,7 @@ 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. @@ -84,33 +84,33 @@ end -- @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 diff --git a/stdlib/event/surface.lua b/stdlib/event/surface.lua index 4e67a84d..c15315b4 100644 --- a/stdlib/event/surface.lua +++ b/stdlib/event/surface.lua @@ -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) @@ -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 @@ -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() diff --git a/stdlib/game.lua b/stdlib/game.lua index 49014bce..49cef39c 100644 --- a/stdlib/game.lua +++ b/stdlib/game.lua @@ -55,7 +55,7 @@ end --- Messages all players currently connected to the game. --> Offline players are not counted as having received the message. --- If no players exist msg is stored in the `global._print_queue` table. +-- If no players exist msg is stored in the `storage._print_queue` table. -- @tparam string msg the message to send to players -- @tparam[opt] ?|nil|boolean condition the condition to be true for a player to be messaged -- @treturn uint the number of players who received the message. @@ -70,12 +70,12 @@ function Game.print_all(msg, condition) end return num else - global._print_queue = global._print_queue or {} - global._print_queue[#global._print_queue + 1] = msg + storage._print_queue = storage._print_queue or {} + storage._print_queue[#storage._print_queue + 1] = msg end end ---- Gets or sets data in the global variable. +--- Gets or sets data in the storage variable. -- @tparam string sub_table the name of the table to use to store data. -- @tparam[opt] mixed index an optional index to use for the sub_table -- @tparam mixed key the key to store the data in @@ -84,13 +84,13 @@ end -- @treturn mixed the chunk value stored at the key or the previous value function Game.get_or_set_data(sub_table, index, key, set, value) assert(type(sub_table) == 'string', 'sub_table must be a string') - global[sub_table] = global[sub_table] or {} + storage[sub_table] = storage[sub_table] or {} local this if index then - global[sub_table][index] = global[sub_table][index] or {} - this = global[sub_table][index] + storage[sub_table][index] = storage[sub_table][index] or {} + this = storage[sub_table][index] else - this = global[sub_table] + this = storage[sub_table] end local previous @@ -106,7 +106,7 @@ function Game.get_or_set_data(sub_table, index, key, set, value) end function Game.write_mods() - game.write_file('Mods.lua', 'return ' .. inspect(game.active_mods)) + game.write_file('Mods.lua', 'return ' .. inspect(script.active_mods)) end function Game.write_statistics() diff --git a/stdlib/scripts/quickstart.lua b/stdlib/scripts/quickstart.lua index eb762bf8..3da9fd48 100644 --- a/stdlib/scripts/quickstart.lua +++ b/stdlib/scripts/quickstart.lua @@ -81,9 +81,9 @@ function quickstart.on_player_created(event) end local power_armor = QS.get('power_armor', 'fake') - if player.character and game.item_prototypes[power_armor] then + if player.character and prototypes.item[power_armor] then --Put on power armor, install equipment - player.get_inventory(defines.inventory.character_armor).insert(power_armor) + player.character.get_inventory(defines.inventory.character_armor).insert(power_armor) local grid = player.character.grid if grid then for _, eq in pairs(QS.get('equipment', { 'fusion-reactor-equipment' })) do