Implement standard deprecation system for Lua API

(as proposed by @DeFender1031)

All existing deprecation messages in the Lua code have been changed to
use the new deprecation system.

The goal is to eventually use an equivalent system for ALL areas of the API.
This commit is contained in:
Celtic Minstrel 2017-08-13 22:34:57 -04:00
parent 74cff96475
commit d192f07123
3 changed files with 109 additions and 51 deletions

View File

@ -1,9 +1,12 @@
local _ = wesnoth.textdomain "wesnoth-ai"
wesnoth.deprecation_message('data/ai/lua/patrol.lua', 1, nil, _"Use the Patrols Micro AI instead of patrol.lua.")
function patrol_gen(n, wp)
-- n is the name of the unit, like Kiressh
-- wp - a table of waypoint tables of form {x,y}
wesnoth.message('data/ai/lua/patrol.lua is deprecated. Use the Patrols Micro AI instead.')
local unit = wesnoth.get_units({name=n})[1]
local x, y = unit.x, unit.y

View File

@ -144,6 +144,86 @@ function wml.shallow_parsed(cfg)
end
end
--[========[Deprecation Helpers]========]
local _ = wesnoth.textdomain "wesnoth"
-- Note: When using version (for level 2 or 3 deprecation), specify the first version
-- in which the feature could be removed... NOT the version at which it was deprecated.
local function wesnoth.deprecation_message(elem_name, level, version, detail)
local message_params = {elem = elem_name}
local logger
local message
if level == 1 then
logger = function(msg) wesnoth.log("info", msg) end
message = wesnoth.format(_"$elem has been deprecated indefinitely.", message_params)
elseif level == 2 then
logger = function(msg) wesnoth.log("warn", msg) end
if wesnoth.compare_versions(game_config.version, "<", version) then
message_params.version = version
message = wesnoth.format(_"$elem has been deprecated and may be removed in version $version.", message_params)
else
message = wesnoth.format(_"$elem has been deprecated and may be removed at any time.", message_params)
end
elseif level == 3 then
logger = function(msg) wesnoth.log("err", msg) end
message = wesnoth.format(_"$elem has been deprecated and will be removed in the next version.", message_params)
elseif level == 4 then
logger = error
message = wesnoth.format(_"$elem has been deprecated and removed.", message_params)
else
error(_"Invalid deprecation level (should be 1-4)")
end
if len(detail) > 0 then
logger(message .. "\n " .. detail)
else
logger(message)
end
end
-- Usage: module.something = wesnoth.deprecate(module.something, "something", ...)
function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, detail_msg)
local message = detail_msg or ''
if replacement then
message = message .. " " .. wesnoth.format(_"(Note: You should use $replacement instead in new code)", {replacement = replacement})
end
if type(level) ~= "number" or level < 1 or level > 4 then
error("Invalid deprecation level! Must be 1-4.")
end
local msg_shown = false
if type(elem) == "function" then
return function(...)
if not msg_shown then
msg_shown = true
wesnoth.deprecation_message(elem_name, level, version, message)
end
return elem(...)
end
elseif type(elem) == "table" or type(elem) == "userdata" then
local mt = {
__index = function(key)
if not msg_shown then
msg_shown = true
wesnoth.deprecation_message(elem_name, level, version, message)
end
return elem[key]
end,
__newindex = function(key, val)
if not msg_shown then
msg_shown = true
wesnoth.deprecation_message(elem_name, level, version, message)
end
elem[key] = val
end,
}
return setmetatable({}, mt)
else
wesnoth.log('warn', "Attempted to deprecate something that is not a table or function: " ..
elem_name .. " -> " .. replacement .. ", where " .. elem_name .. " = " .. tostring(elem))
end
return elem
end
if wesnoth.kernel_type() == "Game Lua Kernel" then
--[========[Basic variable access]========]
@ -270,3 +350,12 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
return result
end
end
-- Some C++ functions are deprecated; apply the messages here.
-- Note: It must happen AFTER the C++ functions are reassigned above to their new location.
-- These deprecated functions will probably never be removed.
wesnoth.get_variable = wesnoth.deprecate_api('wesnoth.get_variable', 'wml.variable.get', 1, nil, wesnoth.get_variable)
wesnoth.set_variable = wesnoth.deprecate_api('wesnoth.set_variable', 'wml.variable.set', 1, nil, wesnoth.set_variable)
wesnoth.get_all_vars = wesnoth.deprecate_api('wesnoth.get_all_vars', 'wml.variable.get_all', 1, nil, wesnoth.get_all_vars)
wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wesnoth.tovconfig)
wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wesnoth.debug)

View File

@ -333,54 +333,20 @@ helper.parsed = wml.parsed
helper.shallow_literal = wml.shallow_literal
helper.shallow_parsed = wml.shallow_parsed
--[[ Uncomment after 1.14
helper.distance_between = helper.deprecate(
"helper.distance_between is deprecated; use wesnoth.map.distance_between instead",
wesnoth.map.distance_between)
helper.get_child = helper.deprecate(
"helper.get_child is deprecated; use wml.get_child instead", wml.get_child)
helper.get_nth_child = helper.deprecate(
"helper.get_nth_child is deprecated; use wml.get_nth_child instead", wml.get_nth_child)
helper.child_count = helper.deprecate(
"helper.child_count is deprecated; use wml.child_count instead", wml.child_count)
helper.child_range = helper.deprecate(
"helper.child_range is deprecated; use wml.child_range instead", wml.child_range)
helper.child_array = helper.deprecate(
"helper.child_array is deprecated; use wml.child_array instead", wml.child_array)
helper.get_variable_array = helper.deprecate(
"helper.get_variable_array is deprecated; use wml.variable.get_array instead",
wml.variable.get_array)
helper.set_variable_array = helper.deprecate(
"helper.set_variable_array is deprecated; use wml.variable.set_array instead",
wml.variable.set_array)
helper.get_variable_proxy_array = helper.deprecate(
"helper.get_variable_proxy_array is deprecated; use wml.variable.get_proxy_array instead",
wml.variable.get_proxy_array)
helper.literal = helper.deprecate(
"helper.literal is deprecated; use wml.literal instead", wml.literal)
helper.parsed = helper.deprecate(
"helper.parsed is deprecated; use wml.parsed instead", wml.parsed)
helper.shallow_literal = helper.deprecate(
"helper.shallow_literal is deprecated; use wml.shallow_literal instead", wml.shallow_literal)
helper.shallow_parsed = helper.deprecate(
"helper.shallow_parsed is deprecated; use wml.shallow_parsed instead", wml.shallow_parsed)
helper.set_wml_var_metatable = helper.deprecate(
"helper.set_wml_var_metatable is deprecated; use wml.variable.proxy instead " ..
"which has the metatable already set", helper.set_wml_var_metatable)
helper.set_wml_tag_metatable = helper.deprecate(
"helper.set_wml_tag_metatable is deprecated; use wml.tag instead " ..
"which has the metatable already set", helper.set_wml_tag_metatable)
wesnoth.get_variable = helper.deprecate(
"wesnoth.get_variable is deprecated; use wml.variable.get instead", wesnoth.get_variable)
wesnoth.set_variable = helper.deprecate(
"wesnoth.set_variable is deprecated; use wml.variable.set instead", wesnoth.set_variable)
wesnoth.get_all_vars = helper.deprecate(
"wesnoth.get_all_vars is deprecated; use wml.variable.get_all instead", wesnoth.get_all_vars)
wesnoth.tovconfig = helper.deprecate(
"wesnoth.tovconfig is deprecated; use wml.tovconfig instead", wesnoth.tovconfig)
wesnoth.debug = helper.deprecate(
"wesnoth.debug is deprecated; use wml.tostring instead", wesnoth.debug)
--]]
helper.distance_between = wesnoth.deprecate_api('helper.distance_between', 'wesnoth.map.distance_between', 1, nil, helper.distance_between)
helper.get_child = wesnoth.deprecate_api('helper.get_child', 'wml.get_child', 1, nil, wml.get_child)
helper.get_nth_child = wesnoth.deprecate('helper.get_nth_child', 'wml.get_nth_child', 1, nil, wml.get_nth_child)
helper.child_count = wesnoth.deprecate_api('helper.child_count', 'wml.child_count', 1, nil, wml.child_count)
helper.child_range = wesnoth.deprecate_api('helper.child_range', 'wml.child_range', 1, nil, wml.child_range)
helper.child_array = wesnoth.deprecate_api('helper.child_array', 'wml.child_array', 1, nil, wml.child_array)
helper.get_variable_array = wesnoth.deprecate_api('helper.get_variable_array', 'wml.variable.get_array', 1, nil, wml.variable.get_array)
helper.set_variable_array = wesnoth.deprecate_api('helper.set_variable_array', 'wml.variable.set_array', 1, nil, wml.variable.set_array)
helper.get_variable_proxy_array = wesnoth.deprecate_api('helper.get_variable_proxy_array', 'wml.variable.get_proxy_array', 1, nil, wml.variable.get_proxy_array)
helper.literal = wesnoth.deprecate_api('helper.literal', 'wml.literal', 1, nil, wml.literal)
helper.parsed = wesnoth.deprecate_api('helper.parsed', 'wml.parsed', 1, nil, wml.parsed)
helper.shallow_literal = wesnoth.deprecate_api('helper.shallow_literal', 'wml.shallow_literal', 1, nil, wml.shallow_literal)
helper.shallow_parsed = wesnoth.deprecate_api('helper.shallow_parsed', 'wml.shallow_parsed', 1, nil, wml.shallow_parsed)
helper.set_wml_var_metatable = wesnoth.deprecate_api('helper.set_wml_var_metatable', 'wml.variable.proxy', 1, nil, helper.set_wml_var_metatable)
helper.set_wml_tag_metatable = wesnoth.deprecate_api('helper.set_wml_tag_metatable', 'wml.tag', 1, nil, helper.set_wml_tag_metatable)
return helper