Skip to content

Commit da0fd2f

Browse files
committed
Refactor item-uses.lua: Strip unnecessary pcall wrappers for performance
1 parent a6843e0 commit da0fd2f

1 file changed

Lines changed: 46 additions & 64 deletions

File tree

item-uses.lua

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,24 @@ end
4343
-- Helper: check material reaction product
4444
local function mat_has_product(mat, pid)
4545
if not mat then return false end
46-
local ok, r = pcall(function()
47-
for i = 0, #mat.reaction_product.id - 1 do
48-
if tostring(mat.reaction_product.id[i].value) == pid then return true end
49-
end
50-
return false
51-
end)
52-
return ok and r or false
46+
for i = 0, #mat.reaction_product.id - 1 do
47+
if tostring(mat.reaction_product.id[i].value) == pid then return true end
48+
end
49+
return false
5350
end
5451

5552
-- Helper: check material reaction class
5653
local function mat_has_class(mat, cname)
5754
if not mat then return false end
58-
local ok, r = pcall(function()
59-
for i = 0, #mat.reaction_class - 1 do
60-
if tostring(mat.reaction_class[i].value) == cname then return true end
61-
end
62-
return false
63-
end)
64-
return ok and r or false
55+
for i = 0, #mat.reaction_class - 1 do
56+
if tostring(mat.reaction_class[i].value) == cname then return true end
57+
end
58+
return false
6559
end
6660

6761
local function has_flag(name)
6862
if not material then return false end
69-
local ok, v = pcall(function() return material.flags[name] end)
70-
return ok and v
63+
return material.flags[name]
7164
end
7265

7366
-- Map enum names to readable workshop names
@@ -93,33 +86,30 @@ local BTYPE_WORKSHOP = tonumber(df.building_type.Workshop)
9386
local BTYPE_FURNACE = tonumber(df.building_type.Furnace)
9487

9588
local function get_workshop_name(r)
96-
local count = 0
97-
pcall(function() count = #r.building.type end)
89+
local count = #r.building.type
9890
if count == 0 then return 'Unknown workshop' end
9991

10092
for idx = 0, count - 1 do
10193
local name = nil
102-
pcall(function()
103-
local btype = tonumber(r.building.type[idx])
104-
local st = tonumber(r.building.subtype[idx])
105-
local custom = tonumber(r.building.custom[idx])
106-
107-
if btype == BTYPE_WORKSHOP then
108-
local enum_name = df.workshop_type[st]
109-
if enum_name and enum_name ~= 'Custom' then
110-
name = workshop_readable[enum_name] or enum_name
111-
elseif custom and custom >= 0 then
112-
name = df.global.world.raws.buildings.all[custom].name
113-
end
114-
elseif btype == BTYPE_FURNACE then
115-
local enum_name = df.furnace_type[st]
116-
if enum_name and enum_name ~= 'Custom' then
117-
name = furnace_readable[enum_name] or enum_name
118-
elseif custom and custom >= 0 then
119-
name = df.global.world.raws.buildings.all[custom].name
120-
end
94+
local btype = tonumber(r.building.type[idx])
95+
local st = tonumber(r.building.subtype[idx])
96+
local custom = tonumber(r.building.custom[idx])
97+
98+
if btype == BTYPE_WORKSHOP then
99+
local enum_name = df.workshop_type[st]
100+
if enum_name and enum_name ~= 'Custom' then
101+
name = workshop_readable[enum_name] or enum_name
102+
elseif custom and custom >= 0 then
103+
name = df.global.world.raws.buildings.all[custom].name
104+
end
105+
elseif btype == BTYPE_FURNACE then
106+
local enum_name = df.furnace_type[st]
107+
if enum_name and enum_name ~= 'Custom' then
108+
name = furnace_readable[enum_name] or enum_name
109+
elseif custom and custom >= 0 then
110+
name = df.global.world.raws.buildings.all[custom].name
121111
end
122-
end)
112+
end
123113
if name and #name > 0 then return name end
124114
end
125115
return 'Unknown workshop'
@@ -201,8 +191,7 @@ end
201191
---------------------------------------------------------------------------
202192
if mi and mi.plant then
203193
local function has_pflag(n)
204-
local ok, v = pcall(function() return mi.plant.flags[n] end)
205-
return ok and v
194+
return mi.plant.flags[n]
206195
end
207196
-- These plant flags only apply to PLANT items (not growths)
208197
local is_plant = (item_type == df.item_type.PLANT)
@@ -222,17 +211,15 @@ end
222211

223212
-- Growth material checks
224213
if item_type == df.item_type.PLANT_GROWTH then
225-
pcall(function()
226-
local plant_raw = df.global.world.raws.plants.all[mat_index]
227-
local growth = plant_raw.growths[item_subtype]
228-
local gmi = dfhack.matinfo.decode(growth.mat_type, growth.mat_index)
229-
if gmi and gmi.material then
230-
-- DYE_MAT milling only applies to PLANT items, not growths
231-
if mat_has_product(gmi.material, 'DRINK_MAT') then
232-
add_use('Still', 'Brew drink from growth')
233-
end
214+
local plant_raw = df.global.world.raws.plants.all[mat_index]
215+
local growth = plant_raw.growths[item_subtype]
216+
local gmi = dfhack.matinfo.decode(growth.mat_type, growth.mat_index)
217+
if gmi and gmi.material then
218+
-- DYE_MAT milling only applies to PLANT items, not growths
219+
if mat_has_product(gmi.material, 'DRINK_MAT') then
220+
add_use('Still', 'Brew drink from growth')
234221
end
235-
end)
222+
end
236223
end
237224

238225
---------------------------------------------------------------------------
@@ -293,8 +280,7 @@ for _, r in ipairs(df.global.world.raws.reactions.reactions) do
293280
local primary_ir = nil
294281
for _, reagent in ipairs(r.reagents) do
295282
if df.reaction_reagent_itemst:is_instance(reagent) then
296-
local code = ''
297-
pcall(function() code = reagent.code end)
283+
local code = reagent.code
298284
if not skip_codes[code] then
299285
primary_ir = reagent
300286
break
@@ -322,21 +308,17 @@ for _, r in ipairs(df.global.world.raws.reactions.reactions) do
322308
-- require at least has_material_reaction_product or reaction_class
323309
local has_hmrp = false
324310
local hmrp_val = nil
325-
pcall(function()
326-
if ir.has_material_reaction_product and #ir.has_material_reaction_product > 0 then
327-
has_hmrp = true
328-
hmrp_val = ir.has_material_reaction_product
329-
end
330-
end)
311+
if ir.has_material_reaction_product and #ir.has_material_reaction_product > 0 then
312+
has_hmrp = true
313+
hmrp_val = ir.has_material_reaction_product
314+
end
331315

332316
local has_rc = false
333317
local rc_val = nil
334-
pcall(function()
335-
if ir.reaction_class and #ir.reaction_class > 0 then
336-
has_rc = true
337-
rc_val = ir.reaction_class
338-
end
339-
end)
318+
if ir.reaction_class and #ir.reaction_class > 0 then
319+
has_rc = true
320+
rc_val = ir.reaction_class
321+
end
340322

341323
-- Skip overly generic reagents (both type and mat are wildcard, no extra filters)
342324
if ir.item_type == -1 and ir.mat_type == -1 and not has_hmrp and not has_rc then

0 commit comments

Comments
 (0)