mirror of
https://github.com/factoriolib/flib.git
synced 2025-09-04 08:26:22 +00:00
Update require syntax and add require guards
This commit is contained in:
@@ -12,7 +12,7 @@ creating Factorio mods.
|
||||
Download the latest release from the
|
||||
[mod portal](https://mods.factorio.com/mod/flib) unzip it, and put it in your
|
||||
mods directory. You can access libraries provided by flib with
|
||||
`require("__flib__/position")`, etc.
|
||||
`require("__flib__.position")`, etc.
|
||||
|
||||
Add the flib directory to your language server's library. We recommend
|
||||
installing the [Factorio modding
|
||||
|
4
area.lua
4
area.lua
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.area" then
|
||||
return require("__flib__.area")
|
||||
end
|
||||
|
||||
--- @diagnostic disable
|
||||
--- @deprecated Use `bounding-box` instead.
|
||||
local flib_area = {}
|
||||
|
@@ -1,10 +1,14 @@
|
||||
local position = require("__flib__/position")
|
||||
if ... ~= "__flib__.bounding-box" then
|
||||
return require("__flib__.bounding-box")
|
||||
end
|
||||
|
||||
local position = require("__flib__.position")
|
||||
|
||||
--- Utilities for manipulating bounding boxes. All functions support both the shorthand and explicit syntaxes for boxes
|
||||
--- and positions, and will preserve the syntax that was passed in. Boxes are considered immutable; all functions will
|
||||
--- return new boxes.
|
||||
--- ```lua
|
||||
--- local flib_bounding_box = require("__flib__/bounding-box")
|
||||
--- local flib_bounding_box = require("__flib__.bounding-box")
|
||||
--- ```
|
||||
--- @class flib_bounding_box
|
||||
local flib_bounding_box = {}
|
||||
@@ -64,8 +68,10 @@ end
|
||||
function flib_bounding_box.contains_position(box, pos)
|
||||
local box = flib_bounding_box.ensure_explicit(box)
|
||||
local pos = position.ensure_explicit(pos)
|
||||
return
|
||||
box.left_top.x <= pos.x and box.left_top.y <= pos.y and box.right_bottom.x >= pos.x and box.right_bottom.y >= pos.y
|
||||
return box.left_top.x <= pos.x
|
||||
and box.left_top.y <= pos.y
|
||||
and box.right_bottom.x >= pos.x
|
||||
and box.right_bottom.y >= pos.y
|
||||
end
|
||||
|
||||
--- Return the box in explicit form.
|
||||
@@ -219,11 +225,10 @@ end
|
||||
function flib_bounding_box.intersects_box(box1, box2)
|
||||
local box1 = flib_bounding_box.ensure_explicit(box1)
|
||||
local box2 = flib_bounding_box.ensure_explicit(box2)
|
||||
return
|
||||
box1.left_top.x < box2.right_bottom.x
|
||||
and box2.left_top.x < box1.right_bottom.x
|
||||
and box1.left_top.y < box2.right_bottom.y
|
||||
and box2.left_top.y < box1.right_bottom.y
|
||||
return box1.left_top.x < box2.right_bottom.x
|
||||
and box2.left_top.x < box1.right_bottom.x
|
||||
and box1.left_top.y < box2.right_bottom.y
|
||||
and box2.left_top.y < box1.right_bottom.y
|
||||
end
|
||||
|
||||
--- Return a new box with the same dimensions, moved by the given delta.
|
||||
|
@@ -6,6 +6,7 @@ Date: ????
|
||||
- Added `gui-templates` module for building various common and/or annoying GUI components, including technology slots.
|
||||
- Added `technology` module with various runtime technology-related utilities.
|
||||
- Added technology slot styles.
|
||||
- Added require guards to allow requiring flib modules with any syntax without breaking upvalues.
|
||||
Bugfixes:
|
||||
- Fixed `data_util.get_energy_value` not accepting capital `K` as a unit suffix. (#59)
|
||||
---------------------------------------------------------------------------------------------------
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.data-util" then
|
||||
return require("__flib__.data-util")
|
||||
end
|
||||
|
||||
--- Utilities for data stage prototype manipulation.
|
||||
--- ```lua
|
||||
--- local flib_data_util = require("__flib__/data-util")
|
||||
--- local flib_data_util = require("__flib__.data-util")
|
||||
--- ```
|
||||
--- @class flib_data_util
|
||||
local flib_data_util = {}
|
||||
|
6
data.lua
6
data.lua
@@ -1,3 +1,3 @@
|
||||
require("prototypes/sprite")
|
||||
require("prototypes/style")
|
||||
require("prototypes/technology-slot-style")
|
||||
require("prototypes.sprite")
|
||||
require("prototypes.style")
|
||||
require("prototypes.technology-slot-style")
|
||||
|
@@ -1,6 +1,10 @@
|
||||
local gui = require("__flib__/gui-lite")
|
||||
local mod_gui = require("__core__/lualib/mod-gui")
|
||||
local table = require("__flib__/table")
|
||||
if ... ~= "__flib__.dictionary-lite" then
|
||||
return require("__flib__.dictionary-lite")
|
||||
end
|
||||
|
||||
local gui = require("__flib__.gui-lite")
|
||||
local mod_gui = require("__core__.lualib.mod-gui")
|
||||
local table = require("__flib__.table")
|
||||
|
||||
--- @class FlibDictionaryGlobal
|
||||
--- @field init_ran boolean
|
||||
@@ -25,7 +29,7 @@ local table = require("__flib__/table")
|
||||
|
||||
--- Utilities for creating dictionaries of localised string translations.
|
||||
--- ```lua
|
||||
--- local flib_dictionary = require("__flib__/dictionary-lite")
|
||||
--- local flib_dictionary = require("__flib__.dictionary-lite")
|
||||
--- ```
|
||||
--- @class flib_dictionary
|
||||
local flib_dictionary = {}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
local gui = require("__flib__/gui-lite")
|
||||
local mod_gui = require("__core__/lualib/mod-gui")
|
||||
local table = require("__flib__/table")
|
||||
if ... ~= "__flib__.dictionary" then
|
||||
return require("__flib__.dictionary")
|
||||
end
|
||||
|
||||
local gui = require("__flib__.gui-lite")
|
||||
local mod_gui = require("__core__.lualib.mod-gui")
|
||||
local table = require("__flib__.table")
|
||||
|
||||
--- @diagnostic disable
|
||||
--- @deprecated Use 'dictionary-lite' instead.
|
||||
|
@@ -1,8 +1,12 @@
|
||||
local flib_math = require("__flib__/math")
|
||||
if ... ~= "__flib__.direction" then
|
||||
return require("__flib__.direction")
|
||||
end
|
||||
|
||||
local flib_math = require("__flib__.math")
|
||||
|
||||
--- Functions for working with directions.
|
||||
--- ```lua
|
||||
--- local flib_direction = require("__flib__/direction")
|
||||
--- local flib_direction = require("__flib__.direction")
|
||||
--- ```
|
||||
--- @class flib_direction
|
||||
local flib_direction = {}
|
||||
|
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.event" then
|
||||
return require("__flib__.event")
|
||||
end
|
||||
|
||||
--- @diagnostic disable
|
||||
--- @deprecated use `script` directly
|
||||
local flib_event = {}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.format" then
|
||||
return require("__flib__.format")
|
||||
end
|
||||
|
||||
--- Various string formatting functions.
|
||||
--- ```lua
|
||||
--- local flib_format = require("__flib__/format")
|
||||
--- local flib_format = require("__flib__.format")
|
||||
--- ```
|
||||
--- @class flib_format
|
||||
local flib_format = {}
|
||||
|
@@ -1,2 +1,2 @@
|
||||
--- @deprecated use `gui` or `gui-lite` instead.`
|
||||
return require("__flib__/gui")
|
||||
return require("__flib__.gui")
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.gui-lite" then
|
||||
return require("__flib__.gui-lite")
|
||||
end
|
||||
|
||||
--- Utilities for building GUIs and handling GUI events.
|
||||
--- ```lua
|
||||
--- local flib_gui = require("__flib__/gui-lite")
|
||||
--- local flib_gui = require("__flib__.gui-lite")
|
||||
--- ```
|
||||
--- @class flib_gui
|
||||
local flib_gui = {}
|
||||
|
@@ -1,7 +1,11 @@
|
||||
local flib_math = require("__flib__/math")
|
||||
local flib_gui = require("__flib__/gui-lite")
|
||||
local flib_table = require("__flib__/table")
|
||||
local flib_technology = require("__flib__/technology")
|
||||
if ... ~= "__flib__.gui-templates" then
|
||||
return require("__flib__.gui-templates")
|
||||
end
|
||||
|
||||
local flib_math = require("__flib__.math")
|
||||
local flib_gui = require("__flib__.gui-lite")
|
||||
local flib_table = require("__flib__.table")
|
||||
local flib_technology = require("__flib__.technology")
|
||||
|
||||
local flib_gui_templates = {}
|
||||
|
||||
@@ -26,8 +30,10 @@ function flib_gui_templates.technology_slot(parent, technology, level, research_
|
||||
name = technology.name,
|
||||
style = style,
|
||||
elem_tooltip = { type = "technology", name = technology.name },
|
||||
tags = flib_gui.format_handlers({ [defines.events.on_gui_click] = on_click }),
|
||||
})
|
||||
if on_click then
|
||||
base.tags = flib_gui.format_handlers({ [defines.events.on_gui_click] = on_click })
|
||||
end
|
||||
base
|
||||
.add({ type = "flow", style = "flib_technology_slot_sprite_flow", ignored_by_interaction = true })
|
||||
.add({ type = "sprite", style = "flib_technology_slot_sprite", sprite = "technology/" .. technology.name })
|
||||
|
4
gui.lua
4
gui.lua
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.gui" then
|
||||
return require("__flib__.gui")
|
||||
end
|
||||
|
||||
--- @diagnostic disable
|
||||
|
||||
local mod_name = script.mod_name
|
||||
|
6
math.lua
6
math.lua
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.math" then
|
||||
return require("__flib__.math")
|
||||
end
|
||||
|
||||
--- Extension of the Lua 5.2 math library.
|
||||
--- ```lua
|
||||
--- local flib_math = require("__flib__/math")
|
||||
--- local flib_math = require("__flib__.math")
|
||||
--- ```
|
||||
--- @class flib_math: factorio.mathlib
|
||||
local flib_math = {}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.migration" then
|
||||
return require("__flib__.migration")
|
||||
end
|
||||
|
||||
--- Mod migration and version comparison functions.
|
||||
--- ```lua
|
||||
--- local flib_migration = require("__flib__/migration")
|
||||
--- local flib_migration = require("__flib__.migration")
|
||||
--- ```
|
||||
--- @class flib_migration
|
||||
local flib_migration = {}
|
||||
|
4
misc.lua
4
misc.lua
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.misc" then
|
||||
return require("__flib__.misc")
|
||||
end
|
||||
|
||||
--- @diagnostic disable
|
||||
--- @deprecated use `format` and `position` modules instead.`
|
||||
local flib_misc = {}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.on-tick-n" then
|
||||
return require("__flib__.on-tick-n")
|
||||
end
|
||||
|
||||
--- Schedule tasks to be executed later.
|
||||
--- ```lua
|
||||
--- local flib_on_tick_n = require("__flib__/on-tick-n")
|
||||
--- local flib_on_tick_n = require("__flib__.on-tick-n")
|
||||
--- ```
|
||||
--- @class flib_on_tick_n
|
||||
local on_tick_n = {}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.orientation" then
|
||||
return require("__flib__.orientation")
|
||||
end
|
||||
|
||||
--- Functions for working with orientations.
|
||||
--- ```lua
|
||||
--- local flib_orientation = require("__flib__/orientation")
|
||||
--- local flib_orientation = require("__flib__.orientation")
|
||||
--- ```
|
||||
--- @class flib_orientation
|
||||
local flib_orientation = {}
|
||||
|
@@ -1,9 +1,13 @@
|
||||
local flib_math = require("__flib__/math")
|
||||
if ... ~= "__flib__.position" then
|
||||
return require("__flib__.position")
|
||||
end
|
||||
|
||||
local flib_math = require("__flib__.math")
|
||||
|
||||
--- Utilities for manipulating positions. All functions support both the shorthand and explicit syntaxes and will
|
||||
--- preserve the syntax that was passed in.
|
||||
--- ```lua
|
||||
--- local flib_position = require("__flib__/position")
|
||||
--- local flib_position = require("__flib__.position")
|
||||
--- ```
|
||||
--- @class flib_position
|
||||
local flib_position = {}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local data_util = require("__flib__/data-util")
|
||||
local data_util = require("__flib__.data-util")
|
||||
|
||||
local styles = data.raw["gui-style"].default
|
||||
|
||||
|
@@ -1,8 +1,12 @@
|
||||
if ... ~= "__flib__.queue" then
|
||||
return require("__flib__.queue")
|
||||
end
|
||||
|
||||
--- Lua queue implementation.
|
||||
---
|
||||
--- Based on "Queues and Double Queues" from [Programming in Lua](http://www.lua.org/pil/11.4.html).
|
||||
--- ```lua
|
||||
--- local flib_queue = require("__flib__/queue")
|
||||
--- local flib_queue = require("__flib__.queue")
|
||||
--- ```
|
||||
--- @class flib_queue
|
||||
local flib_queue = {}
|
||||
|
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.reverse-defines" then
|
||||
return require("__flib__.reverse-defines")
|
||||
end
|
||||
|
||||
--- Defines reverse lookup table.
|
||||
---
|
||||
--- NOTE: Type intellisense simply does not work for this module, and there is no easy way to fix
|
||||
|
@@ -1,10 +1,14 @@
|
||||
if ... ~= "__flib__.table" then
|
||||
return require("__flib__.table")
|
||||
end
|
||||
|
||||
--- Extension of the Lua 5.2 table library.
|
||||
---
|
||||
--- **NOTE:** Several functions in this module will only work with [arrays](https://www.lua.org/pil/11.1.html),
|
||||
--- which are tables with sequentially numbered keys. All table functions will work with arrays as well, but
|
||||
--- array functions **will not** work with tables.
|
||||
--- ```lua
|
||||
--- local flib_table: = require("__flib__/table")
|
||||
--- local flib_table: = require("__flib__.table")
|
||||
--- ```
|
||||
--- @class flib_table: tablelib
|
||||
local flib_table = {}
|
||||
|
@@ -1,3 +1,7 @@
|
||||
if ... ~= "__flib__.technology" then
|
||||
return require("__flib__.technology")
|
||||
end
|
||||
|
||||
--- @class flib_technology
|
||||
local flib_technology = {}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local flib_dictionary = require("__flib__/dictionary-lite")
|
||||
local flib_dictionary = require("__flib__.dictionary-lite")
|
||||
|
||||
-- Build demo dictionaries for various kinds of prototypes. Dictionaries don't need to be sorted like this, they can
|
||||
-- contain anything and everything. For example, in many of my mods I will make a "search" dictionary containing all
|
||||
|
@@ -3,8 +3,8 @@
|
||||
|
||||
-- GUI
|
||||
|
||||
local flib_gui = require("__flib__/gui-lite")
|
||||
local mod_gui = require("__core__/lualib/mod-gui")
|
||||
local flib_gui = require("__flib__.gui-lite")
|
||||
local mod_gui = require("__core__.lualib.mod-gui")
|
||||
|
||||
--- @alias FlibTestGuiMode
|
||||
--- | "all"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local position = require("__flib__/position")
|
||||
local position = require("__flib__.position")
|
||||
|
||||
local res = position.add({ 1, 3 }, { -2, 1 })
|
||||
assert(res[1] == -1)
|
||||
|
@@ -1,5 +1,5 @@
|
||||
local Test = require("tests/factorio_luaunit")
|
||||
local math = require("__flib__/math")
|
||||
local Test = require("tests.factorio_luaunit")
|
||||
local math = require("__flib__.math")
|
||||
-- FIXME: Use Factorio testing framework
|
||||
--- @diagnostic disable-next-line
|
||||
math.randomseed(os.clock())
|
||||
|
@@ -1,6 +1,10 @@
|
||||
if ... ~= "__flib__.train" then
|
||||
return require("__flib__.train")
|
||||
end
|
||||
|
||||
--- Functions for working with trains.
|
||||
--- ```lua
|
||||
--- local flib_train = require("__flib__/train")
|
||||
--- local flib_train = require("__flib__.train")
|
||||
--- ```
|
||||
--- @class flib_train
|
||||
local flib_train = {}
|
||||
|
@@ -1,8 +1,12 @@
|
||||
if ... ~= "__flib__.translation" then
|
||||
return require("__flib__.translation")
|
||||
end
|
||||
|
||||
--- @diagnostic disable
|
||||
--- @deprecated use `dictionary` instead
|
||||
local flib_translation = {}
|
||||
|
||||
local table = require("__flib__/table")
|
||||
local table = require("__flib__.table")
|
||||
|
||||
local math = math
|
||||
local next = next
|
||||
|
Reference in New Issue
Block a user