moved execution of lua code from fs_edit_general.lua to eval_and_execute_function.lua

This commit is contained in:
Sokomine 2022-09-09 23:29:16 +02:00
parent 97275128d0
commit f714fd426b
3 changed files with 60 additions and 57 deletions

View File

@ -0,0 +1,58 @@
-- 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.
-- The npc also needs a priv to execute this.
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
yl_speak_up.log_error_with_position(pname, n_id,
"could not compile the content of "..tostring(x_id).." :"..dump(code)..
" because of illegal bytecode for player "..tostring(pname))
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
yl_speak_up.log_error_with_position(pname, n_id,
"could not compile the content of "..tostring(x_id).." :"..dump(code)..
" for player "..tostring(pname))
else
local func = f()
local ok, ret = pcall(func,pname)
if not ok then
yl_speak_up.log_error_with_position(pname, n_id,
"could not execute the content of "..tostring(x_id).." :"..dump(code)..
" for player "..tostring(pname))
end
if type(ret) == "boolean" then
return ret
end
end
-- fallback
return false
end

View File

@ -39,63 +39,6 @@ yl_speak_up.get_node_inv_lists = function(pos, search_for_list_name)
end
-- 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.
-- The npc also needs a priv to execute this.
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
yl_speak_up.log_error_with_position(pname, n_id,
"could not compile the content of "..tostring(x_id).." :"..dump(code)..
" because of illegal bytecode for player "..tostring(pname))
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
yl_speak_up.log_error_with_position(pname, n_id,
"could not compile the content of "..tostring(x_id).." :"..dump(code)..
" for player "..tostring(pname))
else
local func = f()
local ok, ret = pcall(func,pname)
if not ok then
yl_speak_up.log_error_with_position(pname, n_id,
"could not execute the content of "..tostring(x_id).." :"..dump(code)..
" for player "..tostring(pname))
end
if type(ret) == "boolean" then
return ret
end
end
-- fallback
return false
end
-- helper function for yl_speak_up.input_fs_edit_option_related
yl_speak_up.delete_element_p_or_a_or_e = function(
player, pname, n_id, d_id, o_id, x_id, id_prefix,

View File

@ -68,6 +68,8 @@ dofile(modpath .. "formspec_helpers.lua")
dofile(modpath .. "fs_alternate_text.lua")
-- implementation of chat command npc_talk_debug:
dofile(modpath .. "command_npc_talk_debug.lua")
-- execute lua code directly (preconditions and effects) - requires priv
dofile(modpath .. "eval_and_execute_function.lua")
-- common functions for editing preconditions and effects
dofile(modpath .. "fs_edit_general.lua")
-- edit preconditions (can be reached through edit options dialog)