-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_util.lua
More file actions
209 lines (185 loc) · 5.38 KB
/
fs_util.lua
File metadata and controls
209 lines (185 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local flib_gui = require "__flib__/gui"
local flib_table = require "__flib__/table"
local M = {}
---Returns an index of first element that matches the condition.
---@generic K, V
---@param tbl table<K, V>
---@param fun fun(value: V, key: K): boolean
---@return K?
function M.find(tbl, fun)
for key, value in pairs(tbl) do
if fun(value, key) then
return key
end
end
return nil
end
---Returns a table containing arrays with elements grouped
---according to values returned by the callback function.
---@generic K, V, R
---@param tbl table<K, V>
---@param fun fun(value: V, key: K): R
---@return table<R, V[]>
function M.group_by(tbl, fun)
local grouping = {}
for key, value in pairs(tbl) do
local group_key = fun(value, key)
if not grouping[group_key] then
grouping[group_key] = {}
end
flib_table.insert(grouping[group_key], value)
end
return grouping
end
---Converts the table to list.
---@generic V
---@param tbl table<any, V>
---@return V[]
function M.to_list(tbl)
local list = {}
for _, value in pairs(tbl) do
flib_table.insert(list, value)
end
return list
end
---Sorts prototypes by standard method of factorio.
---@generic V
---@param list (V | { order: string, name: string })[]
---@return V[]
function M.sort_prototypes(list)
flib_table.sort(list, function(a, b)
if a.order ~= b.order then
return a.order < b.order
else
return a.name < b.name
end
end)
return list
end
---comment
---@param value number
---@return boolean
function M.is_nan(value)
return value ~= value
end
---comment
---@param value number
---@return boolean
function M.is_infinite(value)
return value == math.huge or value == -math.huge
end
---Add a new child or children to the given GUI element.
---@param parent LuaGuiElement
---@param def flib.GuiElemDef
---@param append_tags table?
---@return table<string, LuaGuiElement> elems
---@return LuaGuiElement first
function M.add_gui(parent, def, append_tags)
local res_elems, first = flib_gui.add(parent, def)
first.tags = flib_table.shallow_merge{
first.tags,
append_tags,
}
M.dispatch_to_subtree(first, "on_added")
return res_elems, first
end
M.add_handlers = flib_gui.add_handlers
---Collect necessary data and create an event.
---@param element LuaGuiElement
---@param event_name string?
---@param data table?
---@return EventDataTrait
function M.create_gui_event(element, event_name, data)
local event = {
element = element,
mod_name = element.get_mod(),
name = event_name or "",
player_index = element.player_index,
tick = game.tick,
}
event = flib_table.deep_merge { event, data }
return event
end
---Dispatch an event to the element.
---@param element LuaGuiElement
---@param event_name string
---@param data table?
function M.dispatch(element, event_name, data)
local event = M.create_gui_event(element, event_name, data)
---@diagnostic disable-next-line: param-type-mismatch
flib_gui.dispatch(event)
end
---Dispatches an event to all lower elements on the root element.
---@param root_element LuaGuiElement
---@param event_name string
---@param append_data table?
function M.dispatch_to_subtree(root_element, event_name, append_data)
local elements = {}
for e in M.dfs_lower(root_element) do
flib_table.insert(elements, e)
end
for _, e in ipairs(elements) do
if e.valid then
local event = M.create_gui_event(e, event_name, append_data)
---@diagnostic disable-next-line: param-type-mismatch
flib_gui.dispatch(event)
end
end
end
---Returns an element with matching name from upper elements.
---@param start_element LuaGuiElement
---@param name string
---@return LuaGuiElement?
function M.find_upper(start_element, name)
for element in M.follow_upper(start_element) do
if element.name == name then
return element
end
end
return nil
end
---Iterate over all lower elements of the starting element in depth-first search.
---Note that if elements are added or deleted in progress of iteration, they will not be iterated correctly.
---@param start_element LuaGuiElement
---@return fun(): LuaGuiElement?
function M.dfs_lower(start_element)
local element_stack = { start_element }
local index_stack = { 0 }
local function it()
local tail = #element_stack
if tail == 0 then
return nil
end
local current = element_stack[tail]
local children = current.children
local index = index_stack[tail]
index_stack[tail] = index + 1
if index == 0 then
return current
elseif index <= #children then
table.insert(element_stack, children[index])
table.insert(index_stack, 0)
return it()
else
table.remove(element_stack)
table.remove(index_stack)
return it()
end
end
return it
end
---Iterates elements from the start element to the root element.
---@param start_element LuaGuiElement
---@return fun(): LuaGuiElement?
function M.follow_upper(start_element)
local current = start_element
function it()
local ret = current
if current then
current = current.parent
end
return ret
end
return it
end
return M