Add some helper functions for deprecated WML.

Added the code here instead of in the config object, to avoid dragging
more stuff in the core libraries. The deprectated warnings are only used
for the game stuff, so no need to put it in the server as well.
This commit is contained in:
Mark de Wever 2011-01-10 20:38:34 +00:00
parent 93695626ed
commit b1069876a8
2 changed files with 120 additions and 0 deletions

View file

@ -27,6 +27,7 @@
#include "gettext.hpp"
#include "gui/dialogs/message.hpp"
#include "formula_string_utils.hpp"
#include "log.hpp"
void wml_exception(
const char* cond
@ -80,3 +81,68 @@ std::string missing_mandatory_wml_key(const std::string &section, const std::str
"mandatory key '$key|' isn't set.", symbols);
}
}
std::string deprecate_wml_key_warning(
const std::string& key
, const std::string& removal_version)
{
assert(!key.empty());
assert(!removal_version.empty());
utils::string_map symbols;
symbols["key"] = key;
symbols["removal_version"] = removal_version;
return vgettext("The key '$key' is deprecated and support "
"will be removed in version $removal_version.", symbols);
}
std::string deprecated_renamed_wml_key_warning(
const std::string& deprecated_key
, const std::string& key
, const std::string& removal_version)
{
assert(!deprecated_key.empty());
assert(!key.empty());
assert(!removal_version.empty());
utils::string_map symbols;
symbols["deprecated_key"] = deprecated_key;
symbols["key"] = key;
symbols["removal_version"] = removal_version;
return vgettext(
"The key '$deprecated_key' has been renamed to '$key'. "
"Support for '$deprecated_key' will be removed in version "
"$removal_version."
, symbols);
}
const config::attribute_value& get_renamed_config_attribute(
const config& cfg
, const std::string& deprecated_key
, const std::string& key
, const std::string& removal_version)
{
const config::attribute_value* result = cfg.get(key);
if(result) {
return *result;
}
result = cfg.get(deprecated_key);
if(result) {
lg::wml_error
<< deprecated_renamed_wml_key_warning(
deprecated_key
, key
, removal_version)
<< '\n';
return *result;
}
static const config::attribute_value empty_attribute;
return empty_attribute;
}

View file

@ -22,6 +22,8 @@
#ifndef WML_EXCEPTION_HPP_INCLUDED
#define WML_EXCEPTION_HPP_INCLUDED
#include "config.hpp"
#include <string>
class display;
@ -125,6 +127,58 @@ struct twml_exception
*/
std::string missing_mandatory_wml_key(const std::string& section, const std::string& key,
const std::string& primary_key = "", const std::string& primary_value = "");
/**
* Returns a standard warning message for using a deprecated wml key.
*
* @param key The deprecated key.
* @param removal_version The version in which the key will be
* removed key.
*
* @returns The warning message.
*/
std::string deprecate_wml_key_warning(
const std::string& key
, const std::string& removal_version);
/**
* Returns a standard warning message for using a deprecated renamed wml key.
*
* @param deprecated_key The deprecated key.
* @param key The new key to be used.
* @param removal_version The version in which the key will be
* removed key.
*
* @returns The warning message.
*/
std::string deprecated_renamed_wml_key_warning(
const std::string& deprecated_key
, const std::string& key
, const std::string& removal_version);
/**
* Returns a config attribute, using either the old name or the new one.
*
* The function first tries the find the attribute using @p key and if that
* doesn't find the attribute it tries @p deprecated_key. If that test finds
* an attribute it will issue a warning and return the result. Else returns
* an empty attribute.
*
* @note This function is not a member of @ref config, since that would add
* additional dependencies to the core library.
*
* @param cfg The config to get the attribute from.
* @param deprecated_key The deprecated key.
* @param key The new key to be used.
* @param removal_version The version in which the key will be
* removed key.
*
* @returns The attribute found as described above.
*/
const config::attribute_value& get_renamed_config_attribute(
const config& cfg
, const std::string& deprecated_key
, const std::string& key
, const std::string& removal_version);
#endif