renamed eval_precondition_function to eval_and_execute_function, moved to fs_edit_general.lua and used for results as well

This commit is contained in:
Sokomine 2021-06-10 18:06:21 +02:00
parent 4e4948923a
commit 312b9a49df
3 changed files with 72 additions and 106 deletions

View File

@ -1,4 +1,74 @@
-- evaluate those preconditions of type "function" (set by the staff)
-- and execute those effects/results of type "function" (set by the staff)
-- WARNING: This is extremly powerful!
-- The code is taken out of the function
-- calculate_displayable_options(..)
-- (written by AliasAlreadyTaken).
-- It requires the npc_master priv to add or edit this prereq.
-- The function is called by
-- * yl_speak_up.eval_precondition and
-- * yl_speak_up.eval_effect.
yl_speak_up.eval_and_execute_function = function(player, x_v, id_prefix)
local pname = player:get_player_name()
--minetest.chat_send_all("this is in a single prereq or effect: "..dump(x_v))
local x_id = x_v[ id_prefix.. "id" ]
if x_v[ id_prefix.."type" ] ~= "function" then
return true
end
local code = x_v[ id_prefix.."value" ]
if code:byte(1) == 27 then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..x_id.." :"..dump(code) .. " because of illegal bytecode for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..x_id.." :"..dump(code) .. " for player unknown because of illegal bytecode")
end
end
local param = "playername"
if( id_prefix == "r_") then
param = "player"
end
local f, msg = loadstring("return function("..param..") " .. code .. " end")
if not f then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..x_id.." :"..dump(code) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..x_id.." :"..dump(code) .. " for player unknown")
end
else
local func = f()
local ok, ret = pcall(func,pname)
if not ok then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..x_id.." :"..dump(code) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..x_id.." :"..dump(code) .. " for player unknown")
end
end
if type(ret) == "boolean" then
return ret
end
end
-- fallback
return false
end
-- These two functions
-- * yl_speak_up.input_fs_edit_option_related and

View File

@ -237,7 +237,7 @@ yl_speak_up.eval_precondition = function(player, n_id, p)
elseif(p.p_type == "function") then
-- a precondition set by using the staff;
-- extremly powerful (executes any lua code)
return yl_speak_up.eval_precondition_function(player, p)
return yl_speak_up.eval_and_execute_function(player, p, "p_")
elseif(p.p_type == "state") then
local var_val = false
if(not(p.p_variable) or p.p_variable == "") then

View File

@ -349,70 +349,6 @@ local function delete_option(n_id, d_id, o_id)
end
-- evaluate those preconditions of type "function" (set by the staff)
-- WARNING: This is extremly powerful!
-- The code is taken out of the function
-- calculate_displayable_options(..)
-- (written by AliasAlreadyTaken).
-- It requires the npc_master priv to add or edit this prereq.
-- The function is called by yl_speak_up.eval_precondition
yl_speak_up.eval_precondition_function = function(player, p_v)
local pname = player:get_player_name()
--minetest.chat_send_all("this is in a single prereq: "..dump(p_v))
local p_id = p_v.p_id
if p_v.p_type ~= "function" then
return true
end
local code = p_v.p_value
if code:byte(1) == 27 then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..p_id.." :"..dump(code) .. " because of illegal bytecode for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..p_id.." :"..dump(code) .. " for player unknown because of illegal bytecode")
end
end
local f, msg = loadstring("return function(playername) " .. code .. " end")
if not f then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..p_id.." :"..dump(code) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..p_id.." :"..dump(code) .. " for player unknown")
end
else
local func = f()
local ok, ret = pcall(func,pname)
if not ok then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..p_id.." :"..dump(code) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..p_id.." :"..dump(code) .. " for player unknown")
end
end
if type(ret) == "boolean" then
return ret
end
end
-- fallback
return false
end
local function calculate_displayable_options(pname, d_options)
-- Let's go through all the options and see if we need to display them to the user
@ -2653,47 +2589,7 @@ yl_speak_up.input_talk = function(player, formname, fields)
end
if not(edit_mode) and v.r_type == "function" then
local code = v.r_value
if code:byte(1) == 27 then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " because of illegal bytecode for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown because of illegal bytecode")
end
end
local f, msg = loadstring("return function(player) " .. code .. " end")
if not f then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not compile the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown")
end
else
local func = f()
local ok, ret = pcall(func,pname)
if not ok then
local obj = yl_speak_up.speak_to[pname].obj
local n_id = yl_speak_up.speak_to[pname].n_id
local npc = get_number_from_id(n_id)
if obj:get_luaentity() and tonumber(npc) then
minetest.log("error","[MOD] yl_speak_up: NPC with ID n_"..npc.." at position "..minetest.pos_to_string(obj:get_pos(),0).." could not execute the content of "..v.r_id.." :"..dump(v.r_value) .. " for player "..pname)
else
minetest.log("error","[MOD] yl_speak_up: NPC with ID unknown could not execute the content of "..v.r_id.." :"..dump(v.r_value) .. " for player unknown")
end
end
end
yl_speak_up.eval_and_execute_function(player, v, "r_")
end
if v.r_type == "dialog" then