fix: extra type conformity check for TechnologyUnit to prevent unexpected NPEs

This commit is contained in:
2025-04-17 00:01:31 -05:00
parent 5f2379afd0
commit d40a5a50c0
2 changed files with 28 additions and 9 deletions

View File

@@ -1,4 +1,10 @@
---------------------------------------------------------------------------------------------------
Date: 04. 16. 2025
Bugfixes:
- Fixed rare issue improper TechnologyUnit objects in other mods would cause a NPE crash.
Minor Features:
- Added error handling with 'xpcall' to prevent full crashes, instead logging exceptions.
---------------------------------------------------------------------------------------------------
Version: 0.1.0
Date: 12. 12. 2024
Major Features:

View File

@@ -136,9 +136,15 @@ function is_infinite_research(name)
return data.raw.technology[name].unit.count == nil;
end
--
-- This function expects that the technology exists, and follows the TechnologyUnit type specification properly
function is_interplanetary_research(name)
for _, ingredient in pairs(data.raw.technology[name].unit.ingredients) do
local technology = data.raw.technology[name];
if technology.unit == nil then
return false;
end
for _, ingredient in pairs(technology.unit.ingredients) do
if interplanetary_science_packs[ingredient[1]] then
return true;
end
@@ -161,10 +167,13 @@ function multiply(current, next)
end
end
for name, technology in pairs(data.raw.technology) do
-- skip trigger technology
function calculate(name, technology)
-- Skip trigger technology, or technologies that don't properly provide a `unit`, `unit.ingredients`, or `unit.count` property
if (technology.research_trigger ~= nil) then
goto continue;
return;
elseif (technology.unit == nil or technology.unit.ingredients == nil) then
log("Skipping non-trigger technology \"" ..
name .. "\" because it doesn't properly define a unit, it's ingredients, and/or a count.")
end
-- default to the global multiplier
@@ -213,10 +222,10 @@ for name, technology in pairs(data.raw.technology) do
-- don't apply multiplier if it would do nothing
if (multiplier == 1) then
goto continue;
return
elseif (multiplier <= 0) then
log("Multiplier is less than 0, skipping " .. name .. " (" .. multiplier .. ")")
goto continue;
return
end
-- Multiplier has been calculated, apply it
@@ -237,6 +246,10 @@ for name, technology in pairs(data.raw.technology) do
technology.unit.count = technology.unit.count*multiplier;
end
end
end
::continue::
end
for name, technology in pairs(data.raw.technology) do
xpcall(calculate, function(err)
log("Error in technology " .. name .. ": " .. err)
end, name, technology)
end