Deprecate special handling of ${key} syntax in metadata values (#12970)

This commit is contained in:
Jude Melton-Houghton 2022-11-24 17:56:43 -05:00 committed by GitHub
parent 3c7f26d937
commit 8817af07fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 6 deletions

@ -250,11 +250,12 @@ local formspec_escapes = {
["["] = "\\[", ["["] = "\\[",
["]"] = "\\]", ["]"] = "\\]",
[";"] = "\\;", [";"] = "\\;",
[","] = "\\," [","] = "\\,",
["$"] = "\\$",
} }
function core.formspec_escape(text) function core.formspec_escape(text)
-- Use explicit character set instead of dot here because it doubles the performance -- Use explicit character set instead of dot here because it doubles the performance
return text and string.gsub(text, "[\\%[%];,]", formspec_escapes) return text and string.gsub(text, "[\\%[%];,$]", formspec_escapes)
end end

@ -9,3 +9,4 @@ This document contains a list of breaking changes to be made in the next major v
* remove `depends.txt` / `description.txt` (would simplify ContentDB and Minetest code a little) * remove `depends.txt` / `description.txt` (would simplify ContentDB and Minetest code a little)
* rotate moon texture by 180°, making it coherent with the sun (see https://github.com/minetest/minetest/pull/11902) * rotate moon texture by 180°, making it coherent with the sun (see https://github.com/minetest/minetest/pull/11902)
* remove undocumented `set_physics_override(num, num, num)` * remove undocumented `set_physics_override(num, num, num)`
* remove special handling of `${key}` syntax in metadata values

@ -6861,6 +6861,11 @@ Can be obtained via `item:get_meta()`.
Base class used by [`StorageRef`], [`NodeMetaRef`], [`ItemStackMetaRef`], Base class used by [`StorageRef`], [`NodeMetaRef`], [`ItemStackMetaRef`],
and [`PlayerMetaRef`]. and [`PlayerMetaRef`].
Note: If a metadata value is in the format `${k}`, an attempt to get the value
will return the value associated with key `k`. There is a low recursion limit.
This behavior is **deprecated** and will be removed in a future version. Usage
of the `${k}` syntax in formspecs is not deprecated.
### Methods ### Methods
* `contains(key)`: Returns true if key present, otherwise false. * `contains(key)`: Returns true if key present, otherwise false.

@ -51,7 +51,7 @@ const std::string &IMetadata::getString(const std::string &name, std::string *pl
return empty_string; return empty_string;
} }
return resolveString(*raw, place, recursion); return resolveString(*raw, place, recursion, true);
} }
bool IMetadata::getStringToRef(const std::string &name, bool IMetadata::getStringToRef(const std::string &name,
@ -61,16 +61,21 @@ bool IMetadata::getStringToRef(const std::string &name,
if (!raw) if (!raw)
return false; return false;
const std::string &resolved = resolveString(*raw, &str, recursion); const std::string &resolved = resolveString(*raw, &str, recursion, true);
if (&resolved != &str) if (&resolved != &str)
str = resolved; str = resolved;
return true; return true;
} }
const std::string &IMetadata::resolveString(const std::string &str, std::string *place, const std::string &IMetadata::resolveString(const std::string &str, std::string *place,
u16 recursion) const u16 recursion, bool deprecated) const
{ {
if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') { if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
if (deprecated) {
warningstream << "Deprecated use of recursive resolution syntax in metadata: ";
safe_print_string(warningstream, str);
warningstream << std::endl;
}
// It may be the case that &str == place, but that's fine. // It may be the case that &str == place, but that's fine.
return getString(str.substr(2, str.length() - 3), place, recursion + 1); return getString(str.substr(2, str.length() - 3), place, recursion + 1);
} }

@ -65,7 +65,7 @@ public:
// Add support for variable names in values. Uses place like getString. // Add support for variable names in values. Uses place like getString.
const std::string &resolveString(const std::string &str, std::string *place, const std::string &resolveString(const std::string &str, std::string *place,
u16 recursion = 0) const; u16 recursion = 0, bool deprecated = false) const;
protected: protected:
// Returns nullptr to indicate absence of value. Uses place like getString. // Returns nullptr to indicate absence of value. Uses place like getString.

@ -459,6 +459,7 @@ inline void str_formspec_escape(std::string &str)
str_replace(str, "[", "\\["); str_replace(str, "[", "\\[");
str_replace(str, ";", "\\;"); str_replace(str, ";", "\\;");
str_replace(str, ",", "\\,"); str_replace(str, ",", "\\,");
str_replace(str, "$", "\\$");
} }
/** /**