Skip to content
Prev Previous commit
Next Next commit
Go through assertion list backwards to prevent the string assert pick…
…ing it all up.

Change what nut.type returns
Change target.getPlayer into a type check for /charunban
  • Loading branch information
Miyoglow committed May 17, 2024
commit 73ced8f969501cd5c33bebbb30527b5040e50aa2
14 changes: 8 additions & 6 deletions gamemode/core/libs/sh_command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ local COMMAND_PREFIX = "/"

function nut.command.add(name, data)
if (!isstring(name)) then
return ErrorNoHaltWithStack("nut.command.add expected string for #1 argument but got: " .. nut.type(name))
return ErrorNoHaltWithStack("nut.command.add expected string for #1 argument but got: " .. nut.type.getName(nut.type(name)))
end

if (!istable(data)) then
return ErrorNoHaltWithStack("nut.command.add(\"" .. name .. "\") expected table for #2 argument but got: " .. nut.type(data))
return ErrorNoHaltWithStack("nut.command.add(\"" .. name .. "\") expected table for #2 argument but got: " .. nut.type.getName(nut.type(data)))
end

if (!isfunction(data.onRun)) then
return ErrorNoHaltWithStack("nut.command.add(\"" .. name .. "\") expected an onRun function in #2 argument but got: " .. nut.type(data.onRun))
return ErrorNoHaltWithStack("nut.command.add(\"" .. name .. "\") expected an onRun function in #2 argument but got: " .. nut.type.getName(nut.type(data.onRun)))
end

-- new argument system
Expand Down Expand Up @@ -308,10 +308,12 @@ if (SERVER) then
end

if (argument) then
local multipleTypes = nut.type.getMultiple(nutType)
local multipleTypes = table.Reverse(nut.type.getMultiple(nutType))
local failed

for _, v in ipairs(multipleTypes) do
-- string types are an early bit value in types, but we want to parse/assert them last, so go through the types we have backwards
for i = #multipleTypes, 1, -1 do
local v = multipleTypes[i]
local assertion = nut.type.assert(v, argument)

if (assertion) then
Expand All @@ -326,7 +328,7 @@ if (SERVER) then
if (nut.type.getName(v) == "player" or nut.type.getName(v) == "character") then
failed = "Could not find the target \'" .. argument .. "\'"
else
failed = "Wrong type to #" .. i .. " argument, expected \'" .. nut.type.getName(nutType) .. "\' got \'" .. nut.type(argument) .. "\'"
failed = "Wrong type to #" .. i .. " argument, expected \'" .. nut.type.getName(nutType) .. "\' got \'" .. nut.type.getName(nut.type(argument)) .. "\'"
end
end
end
Expand Down
28 changes: 19 additions & 9 deletions gamemode/core/libs/sh_type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ nut.type.bitsum = nut.type.bitsum or 0
-- _G.type but for nut.type
function nut.type.type(...)
local value = select(1, ...)
value = (istable(value) and value == nut.type) and select(2, ...) or value

if (istable(value) and value == nut.type) then
value = select(2, ...)
end

if (istable(value)) then
if (value.nutType and nut.type.map[value.nutType]) then
return nut.type.types[nut.type.map[value.nutType]].name
if (value.nutType) then
return value.nutType
end
end

for _, v in ipairs(nut.type.types) do
if (v.assertion and v.assertion(value)) then
return v.name
end
-- basic types (strings, numbers, bools) are early in the bit values, go through the types list backwards to parse complex types first
-- as they could use isstring, isnumber, isbool calls to narrow the assertion and give useful returns
for i = #nut.type.types, 1, -1 do
local v = nut.type.types[i]
if (v.assertion and v.assertion(value)) then
return nut.type[v.name]
end
end

Expand Down Expand Up @@ -94,7 +100,7 @@ function nut.type.getName(nutType)
return nut.type.getName(xor)
end
-- could be multiple types, lets see if it is
else
elseif (isnumber(nutType)) then
local types = nut.type.getMultiple(nutType)

if (#types > 0) then
Expand All @@ -106,6 +112,8 @@ function nut.type.getName(nutType)

return table.concat(typeNames, "|")
end
elseif (isstring(nutType)) then
return nutType
end
end

Expand All @@ -116,6 +124,8 @@ function nut.type.isOptional(num)
return isnumber(num) and bit.band(num, nut.type.optional) == nut.type.optional
end

-- may move this kind of parsing/searching through values to the commands or util library instead, and reference it there

nut.type.add("string", function(value) return isstring(value) end)
nut.type.add("number", function(value) return isnumber(tonumber(value)) end)
nut.type.add("bool", function(value) return isbool(value) end)
Expand All @@ -131,7 +141,7 @@ nut.type.add("player", function(value)
end)
nut.type.add("character", function(value)
if (istable(value)) then
return GetMetaTable(value) == nut.meta.character and true
return getmetatable(value) == nut.meta.character and value
end

if (isentity(value)) then
Expand Down
2 changes: 1 addition & 1 deletion gamemode/core/sh_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ nut.command.add("charunban", {
return L("charSearching", client)
end

if (target.getPlayer) then
if (nut.type(target) == nut.type.character) then
if (target:getData("banned")) then
target:setData("banned", nil)
target:setData("permakilled", nil)
Expand Down