mirror of
https://github.com/Xevion/research-multipliers.git
synced 2025-12-14 20:12:44 -06:00
fix: extra type conformity check for TechnologyUnit to prevent unexpected NPEs
This commit is contained in:
@@ -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
|
Version: 0.1.0
|
||||||
Date: 12. 12. 2024
|
Date: 12. 12. 2024
|
||||||
Major Features:
|
Major Features:
|
||||||
|
|||||||
@@ -136,9 +136,15 @@ function is_infinite_research(name)
|
|||||||
return data.raw.technology[name].unit.count == nil;
|
return data.raw.technology[name].unit.count == nil;
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
-- This function expects that the technology exists, and follows the TechnologyUnit type specification properly
|
||||||
function is_interplanetary_research(name)
|
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
|
if interplanetary_science_packs[ingredient[1]] then
|
||||||
return true;
|
return true;
|
||||||
end
|
end
|
||||||
@@ -161,10 +167,13 @@ function multiply(current, next)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for name, technology in pairs(data.raw.technology) do
|
function calculate(name, technology)
|
||||||
-- skip trigger 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
|
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
|
end
|
||||||
|
|
||||||
-- default to the global multiplier
|
-- 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
|
-- don't apply multiplier if it would do nothing
|
||||||
if (multiplier == 1) then
|
if (multiplier == 1) then
|
||||||
goto continue;
|
return
|
||||||
elseif (multiplier <= 0) then
|
elseif (multiplier <= 0) then
|
||||||
log("Multiplier is less than 0, skipping " .. name .. " (" .. multiplier .. ")")
|
log("Multiplier is less than 0, skipping " .. name .. " (" .. multiplier .. ")")
|
||||||
goto continue;
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Multiplier has been calculated, apply it
|
-- 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;
|
technology.unit.count = technology.unit.count*multiplier;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
::continue::
|
|
||||||
|
for name, technology in pairs(data.raw.technology) do
|
||||||
|
xpcall(calculate, function(err)
|
||||||
|
log("Error in technology " .. name .. ": " .. err)
|
||||||
|
end, name, technology)
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user