FAForever / fa

Lua code for FAF
221 stars 228 forks source link

Improve guards for `table.copy` to fix blueprint reloading crash #6288

Closed lL1l1 closed 1 week ago

lL1l1 commented 1 week ago

Description of the proposed changes

Fixes in-game blueprint reloading crash caused by #6273 (specific diff) from blueprints-units.lua:311 due to spawn dummies having CommandCaps = "0". Instead of checking for nil, checks for the value to be a table.

The function could use a better annotation if possible.

Testing done on the proposed changes

The game no longer crashes when reloading blueprints during a session.

Checklist

Garanas commented 1 week ago

What about:

--- table.copy(t) returns a shallow copy of t.
---@generic T
---@param t T
---@return T
function table.copy(t)
  (...)
end
lL1l1 commented 1 week ago

I misunderstood what a shallow copy is. I thought it would only copy 1 deep (which doesn't preserve type) but it just copies old memory references into the new indexes of the new table (which does preserve type, but makes mutating it need more care).