From bf988aa9ef469e6ac9ab77ae5cc5be78b10b9f97 Mon Sep 17 00:00:00 2001 From: EvilFactory Date: Sat, 6 May 2023 13:38:52 -0300 Subject: [PATCH] Correctly handle errors in util methods --- .../Lua/DefaultLib/Utils/Math.lua | 19 +++++++++++++++++++ .../Lua/DefaultLib/Utils/String.lua | 6 ++++++ .../Lua/DefaultLib/Utils/Util.lua | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Math.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Math.lua index 5c39ea7ff..84698a5df 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Math.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Math.lua @@ -1,21 +1,40 @@ math.lerp = function (a, b, t) + if type(a) ~= "number" then error(string.format("bad argument #1 to 'lerp' (number expected, got %s)", type(a)), 2) end + if type(b) ~= "number" then error(string.format("bad argument #2 to 'lerp' (number expected, got %s)", type(b)), 2) end + if type(t) ~= "number" then error(string.format("bad argument #3 to 'lerp' (number expected, got %s)", type(t)), 2) end + return a * (1 - t) + b * t end math.clamp = function (value, min, max) + if type(value) ~= "number" then error(string.format("bad argument #1 to 'clamp' (number expected, got %s)", type(value)), 2) end + if type(min) ~= "number" then error(string.format("bad argument #2 to 'clamp' (number expected, got %s)", type(min)), 2) end + if type(max) ~= "number" then error(string.format("bad argument #3 to 'clamp' (number expected, got %s)", type(max)), 2) end + return math.max(min, math.min(max, value)) end math.round = function (value, decimals) + if type(value) ~= "number" then error(string.format("bad argument #1 to 'round' (number expected, got %s)", type(value)), 2) end + if type(decimals) ~= "number" then error(string.format("bad argument #2 to 'round' (number expected, got %s)", type(decimals)), 2) end + decimals = decimals or 0 local mult = 10 ^ decimals return math.floor(value * mult + 0.5) / mult end math.sign = function (value) + if type(value) ~= "number" then error(string.format("bad argument #1 to 'sign' (number expected, got %s)", type(value)), 2) end + return value >= 0 and 1 or -1 end math.remap = function (value, inMin, inMax, outMin, outMax) + if type(value) ~= "number" then error(string.format("bad argument #1 to 'remap' (number expected, got %s)", type(value)), 2) end + if type(inMin) ~= "number" then error(string.format("bad argument #2 to 'remap' (number expected, got %s)", type(inMin)), 2) end + if type(inMax) ~= "number" then error(string.format("bad argument #3 to 'remap' (number expected, got %s)", type(inMax)), 2) end + if type(outMin) ~= "number" then error(string.format("bad argument #4 to 'remap' (number expected, got %s)", type(outMin)), 2) end + if type(outMax) ~= "number" then error(string.format("bad argument #5 to 'remap' (number expected, got %s)", type(outMax)), 2) end + return outMin + (outMax - outMin) * ((value - inMin) / (inMax - inMin)) end \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/String.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/String.lua index 3215602a2..3a79448c3 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/String.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/String.lua @@ -1,7 +1,13 @@ string.startsWith = function(str, start) + if type(str) ~= "string" then error(string.format("bad argument #1 to 'startsWith' (string expected, got %s)", type(str)), 2) end + if type(start) ~= "string" then error(string.format("bad argument #2 to 'startsWith' (string expected, got %s)", type(start)), 2) end + return string.sub(str, 1, string.len(start)) == start end string.endsWith = function(str, ending) + if type(str) ~= "string" then error(string.format("bad argument #1 to 'endsWith' (string expected, got %s)", type(str)), 2) end + if type(ending) ~= "string" then error(string.format("bad argument #2 to 'endsWith' (string expected, got %s)", type(ending)), 2) end + return ending == "" or string.sub(str, -string.len(ending)) == ending end \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua index 897f2445c..ed6f89dbf 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib/Utils/Util.lua @@ -24,6 +24,10 @@ for _, item in pairs(Item.ItemList) do end Util.GetItemsById = function(id) + if id == nil then + error(string.format("bad argument #1 to 'GetItemsById' (string expected, got %s)", type(id)), 2) + end + return itemDictionary[id] end