Browse Source

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

Jude Melton-Houghton 1 year ago
parent
commit
8817af07fb
6 changed files with 19 additions and 6 deletions
  1. 3 2
      builtin/common/misc_helpers.lua
  2. 1 0
      doc/breakages.md
  3. 5 0
      doc/lua_api.txt
  4. 8 3
      src/metadata.cpp
  5. 1 1
      src/metadata.h
  6. 1 0
      src/util/string.h

+ 3 - 2
builtin/common/misc_helpers.lua

@@ -250,11 +250,12 @@ local formspec_escapes = {
 	["["] = "\\[",
 	["]"] = "\\]",
 	[";"] = "\\;",
-	[","] = "\\,"
+	[","] = "\\,",
+	["$"] = "\\$",
 }
 function core.formspec_escape(text)
 	-- 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
 
 

+ 1 - 0
doc/breakages.md

@@ -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)
 * 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 special handling of `${key}` syntax in metadata values

+ 5 - 0
doc/lua_api.txt

@@ -6861,6 +6861,11 @@ Can be obtained via `item:get_meta()`.
 Base class used by [`StorageRef`], [`NodeMetaRef`], [`ItemStackMetaRef`],
 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
 
 * `contains(key)`: Returns true if key present, otherwise false.

+ 8 - 3
src/metadata.cpp

@@ -51,7 +51,7 @@ const std::string &IMetadata::getString(const std::string &name, std::string *pl
 		return empty_string;
 	}
 
-	return resolveString(*raw, place, recursion);
+	return resolveString(*raw, place, recursion, true);
 }
 
 bool IMetadata::getStringToRef(const std::string &name,
@@ -61,16 +61,21 @@ bool IMetadata::getStringToRef(const std::string &name,
 	if (!raw)
 		return false;
 
-	const std::string &resolved = resolveString(*raw, &str, recursion);
+	const std::string &resolved = resolveString(*raw, &str, recursion, true);
 	if (&resolved != &str)
 		str = resolved;
 	return true;
 }
 
 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 (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.
 		return getString(str.substr(2, str.length() - 3), place, recursion + 1);
 	}

+ 1 - 1
src/metadata.h

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

+ 1 - 0
src/util/string.h

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