String checking

This commit is contained in:
Jean-Patrick Guerrero 2019-09-25 16:12:33 +02:00
parent 3f9d7bc12d
commit f184341663

View File

@ -119,8 +119,16 @@ local function table_replace(t, val, new)
end end
end end
local function err(str)
return log("error", str)
end
local function clean_str(str)
return match(str, "%S*")
end
local function is_str(x) local function is_str(x)
return type(x) == "string" return type(x) == "string" and clean_str(x)
end end
local function is_table(x) local function is_table(x)
@ -138,8 +146,8 @@ end
local craft_types = {} local craft_types = {}
function craftguide.register_craft_type(name, def) function craftguide.register_craft_type(name, def)
if not is_str(name) or name == "" then if not is_str(name) then
return log("error", "craftguide.register_craft_type(): name missing") return err("craftguide.register_craft_type(): name missing")
end end
if not is_str(def.description) then if not is_str(def.description) then
@ -153,25 +161,21 @@ function craftguide.register_craft_type(name, def)
craft_types[name] = def craft_types[name] = def
end end
local function clean_name(item)
return match(item, "%S*")
end
function craftguide.register_craft(def) function craftguide.register_craft(def)
def.custom = true def.custom = true
def.width = 0 def.width = 0
local c = 0 local c = 0
if not is_table(def) or not next(def) then if not is_table(def) or not next(def) then
return log("error", "craftguide.register_craft(): craft definition missing") return err("craftguide.register_craft(): craft definition missing")
end end
if def.result then if def.result then
def.output = def.result -- Backward compatibility def.output = def.result -- Backward compatibility
end end
if not is_str(def.output) or def.output == "" then if not is_str(def.output) then
return log("error", "craftguide.register_craft(): output missing") return err("craftguide.register_craft(): output missing")
end end
if not is_table(def.items) then if not is_table(def.items) then
@ -225,11 +229,11 @@ function craftguide.register_craft(def)
for name in gmatch(concat(items, ","), "[%s%w_:]+") do for name in gmatch(concat(items, ","), "[%s%w_:]+") do
c = c + 1 c = c + 1
def.items[c] = clean_name(name) def.items[c] = clean_str(name)
end end
end end
local output = clean_name(def.output) local output = clean_str(def.output)
recipes_cache[output] = recipes_cache[output] or {} recipes_cache[output] = recipes_cache[output] or {}
insert(recipes_cache[output], def) insert(recipes_cache[output], def)
end end
@ -237,10 +241,10 @@ end
local recipe_filters = {} local recipe_filters = {}
function craftguide.add_recipe_filter(name, f) function craftguide.add_recipe_filter(name, f)
if not is_str(name) or name == "" then if not is_str(name) then
return log("error", "craftguide.add_recipe_filter(): name missing") return err("craftguide.add_recipe_filter(): name missing")
elseif not is_func(f) then elseif not is_func(f) then
return log("error", "craftguide.add_recipe_filter(): function missing") return err("craftguide.add_recipe_filter(): function missing")
end end
recipe_filters[name] = f recipe_filters[name] = f
@ -265,10 +269,10 @@ end
local search_filters = {} local search_filters = {}
function craftguide.add_search_filter(name, f) function craftguide.add_search_filter(name, f)
if not is_str(name) or name == "" then if not is_str(name) then
return log("error", "craftguide.add_search_filter(): name missing") return err("craftguide.add_search_filter(): name missing")
elseif not is_func(f) then elseif not is_func(f) then
return log("error", "craftguide.add_search_filter(): function missing") return err("craftguide.add_search_filter(): function missing")
end end
search_filters[name] = f search_filters[name] = f
@ -550,7 +554,7 @@ local function get_output_fs(fs, L)
1.1, 1.1, PNG.fire) 1.1, 1.1, PNG.fire)
else else
local item = L.recipe.output local item = L.recipe.output
local name = clean_name(item) local name = clean_str(item)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
output_X, YOFFSET + (sfinv_only and 0.7 or 0), output_X, YOFFSET + (sfinv_only and 0.7 or 0),
@ -659,7 +663,7 @@ local function get_recipe_fs(data, fs)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y + (sfinv_only and 0.7 or 0), X, Y + (sfinv_only and 0.7 or 0),
btn_size, btn_size, item, clean_name(item), ESC(label)) btn_size, btn_size, item, clean_str(item), ESC(label))
local info = { local info = {
unknown = not reg_items[item], unknown = not reg_items[item],
@ -885,9 +889,9 @@ core.register_craft = function(def)
toolrepair = def.additional_wear * -100 toolrepair = def.additional_wear * -100
end end
local output = def.output or (is_str(def.recipe) and def.recipe or "") local output = def.output or is_str(def.recipe)
if output == "" then return end if not output then return end
output = {clean_name(output)} output = {clean_str(output)}
local groups local groups
@ -1517,8 +1521,8 @@ register_command("craft", {
}) })
function craftguide.show(name, item, show_usages) function craftguide.show(name, item, show_usages)
if not is_str(name) or name == "" then if not is_str(name) then
return log("error", "craftguide.show(): player name missing") return err("craftguide.show(): player name missing")
end end
local data = pdata[name] local data = pdata[name]