diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-04-26 08:17:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-26 08:17:41 +0200 |
commit | e77a7c297681381d8f6b2008574a32f45bda834f (patch) | |
tree | e1d135cd3cfb3dea5eb7232233082e2b68f0b86d /scene | |
parent | 2b50d4af5f3eaf18091821ced901627d294e5005 (diff) | |
parent | d8fed8fb695b585533edf81ab941d62c4e5caade (diff) |
Merge pull request #60369 from timothyqiu/al-validate
Fix AnimationLibrary name validation
Diffstat (limited to 'scene')
-rw-r--r-- | scene/resources/animation_library.cpp | 21 | ||||
-rw-r--r-- | scene/resources/animation_library.h | 3 |
2 files changed, 22 insertions, 2 deletions
diff --git a/scene/resources/animation_library.cpp b/scene/resources/animation_library.cpp index 229d9ab218..5d92c3b0c6 100644 --- a/scene/resources/animation_library.cpp +++ b/scene/resources/animation_library.cpp @@ -30,8 +30,25 @@ #include "animation_library.h" +bool AnimationLibrary::is_valid_name(const String &p_name) { + return !(p_name.is_empty() || p_name.contains("/") || p_name.contains(":") || p_name.contains(",") || p_name.contains("[")); +} + +String AnimationLibrary::validate_name(const String &p_name) { + if (p_name.is_empty()) { + return "_"; // Should always return a valid name. + } + + String name = p_name; + const char *characters = "/:,["; + for (const char *p = characters; *p; p++) { + name = name.replace(String::chr(*p), "_"); + } + return name; +} + Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) { - ERR_FAIL_COND_V_MSG(String(p_name).contains("/") || String(p_name).contains(":") || String(p_name).contains(",") || String(p_name).contains("["), ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + "."); + ERR_FAIL_COND_V_MSG(!is_valid_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'."); ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); if (animations.has(p_name)) { @@ -55,7 +72,7 @@ void AnimationLibrary::remove_animation(const StringName &p_name) { void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) { ERR_FAIL_COND(!animations.has(p_name)); - ERR_FAIL_COND_MSG(String(p_new_name).contains("/") || String(p_new_name).contains(":") || String(p_new_name).contains(",") || String(p_new_name).contains("["), "Invalid animation name: " + String(p_new_name) + "."); + ERR_FAIL_COND_MSG(!is_valid_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'."); ERR_FAIL_COND(animations.has(p_new_name)); animations.insert(p_new_name, animations[p_name]); diff --git a/scene/resources/animation_library.h b/scene/resources/animation_library.h index 69ac5a97d2..0f327fb072 100644 --- a/scene/resources/animation_library.h +++ b/scene/resources/animation_library.h @@ -49,6 +49,9 @@ protected: static void _bind_methods(); public: + static bool is_valid_name(const String &p_name); + static String validate_name(const String &p_name); + Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation); void remove_animation(const StringName &p_name); void rename_animation(const StringName &p_name, const StringName &p_new_name); |