From fea89f2e3cf06906d24a9ded75ad8965f00c666d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 28 Sep 2020 01:31:15 +0100 Subject: [PATCH] //maze, //maze3d: Allow non-number seeds This uses a simple (non-crypto!) hash function to hash non-number strings to a number. Existing seeds are *not* affected - they will still work as before. --- worldeditadditions/utils/strings.lua | 19 +++++++++++++++++++ worldeditadditions_commands/commands/maze.lua | 5 +---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/worldeditadditions/utils/strings.lua b/worldeditadditions/utils/strings.lua index f7a2ab9..2616484 100644 --- a/worldeditadditions/utils/strings.lua +++ b/worldeditadditions/utils/strings.lua @@ -268,3 +268,22 @@ function worldeditadditions.human_time(ms) end end end + + +--- Makes a seed from a string. +-- If the input is a number, it is returned as-is. +-- If the input is a string and can be converted to a number with tonumber(), +-- the output of tonumber() is returned. +-- Otherwise, the string is converted to a number via a simple hashing algorithm +-- (caution: certainlly NOT crypto-secure!). +-- @param {string} str The string to convert. +-- @source https://stackoverflow.com/a/2624210/1460422 The idea came from here +function worldeditadditions.makeseed(str) + if type(str) == "number" then return str end + if tonumber(str) ~= nil then return tonumber(str) end + local result = 0 + for i = 1, #str do + result = (result*91) + (string.byte(str:sub(i, i)) * 31) + end + return result +end diff --git a/worldeditadditions_commands/commands/maze.lua b/worldeditadditions_commands/commands/maze.lua index fbd9cda..fabac5e 100644 --- a/worldeditadditions_commands/commands/maze.lua +++ b/worldeditadditions_commands/commands/maze.lua @@ -27,10 +27,7 @@ local function parse_params_maze(params_text, is_3d) path_depth = tonumber(parts[4]) end if #parts >= param_index_seed then - seed = tonumber(parts[param_index_seed]) - if not seed then - return false, "Error: Invalid seed value (seeds may only be integers)." - end + seed = worldeditadditions.makeseed(parts[param_index_seed]) end replace_node = worldedit.normalize_nodename(replace_node)