Fix global AnimationLibrary name validation

This commit is contained in:
Haoyu Qiu 2022-05-05 11:19:54 +08:00
parent 0f8ee1d256
commit b3162e270e
4 changed files with 14 additions and 13 deletions

View File

@ -55,7 +55,7 @@ void AnimationLibraryEditor::_add_library_validate(const String &p_name) {
ERR_FAIL_COND(al.is_null()); ERR_FAIL_COND(al.is_null());
if (p_name == "") { if (p_name == "") {
error = TTR("Animation name can't be empty."); error = TTR("Animation name can't be empty.");
} else if (!AnimationLibrary::is_valid_name(p_name)) { } else if (!AnimationLibrary::is_valid_animation_name(p_name)) {
error = TTR("Animation name contains invalid characters: '/', ':', ',' or '['."); error = TTR("Animation name contains invalid characters: '/', ':', ',' or '['.");
} else if (al->has_animation(p_name)) { } else if (al->has_animation(p_name)) {
error = TTR("Animation with the same name already exists."); error = TTR("Animation with the same name already exists.");
@ -63,7 +63,7 @@ void AnimationLibraryEditor::_add_library_validate(const String &p_name) {
} else { } else {
if (p_name == "" && bool(player->call("has_animation_library", ""))) { if (p_name == "" && bool(player->call("has_animation_library", ""))) {
error = TTR("Enter a library name."); error = TTR("Enter a library name.");
} else if (!AnimationLibrary::is_valid_name(p_name)) { } else if (!AnimationLibrary::is_valid_library_name(p_name)) {
error = TTR("Library name contains invalid characters: '/', ':', ',' or '['."); error = TTR("Library name contains invalid characters: '/', ':', ',' or '['.");
} else if (bool(player->call("has_animation_library", p_name))) { } else if (bool(player->call("has_animation_library", p_name))) {
error = TTR("Library with the same name already exists."); error = TTR("Library with the same name already exists.");
@ -256,7 +256,7 @@ void AnimationLibraryEditor::_load_file(String p_path) {
} }
} }
String name = AnimationLibrary::validate_name(p_path.get_file().get_basename()); String name = AnimationLibrary::validate_library_name(p_path.get_file().get_basename());
int attempt = 1; int attempt = 1;

View File

@ -427,7 +427,7 @@ void AnimationPlayerEditor::_animation_name_edited() {
player->stop(); player->stop();
String new_name = name->get_text(); String new_name = name->get_text();
if (!AnimationLibrary::is_valid_name(new_name)) { if (!AnimationLibrary::is_valid_animation_name(new_name)) {
error_dialog->set_text(TTR("Invalid animation name!")); error_dialog->set_text(TTR("Invalid animation name!"));
error_dialog->popup_centered(); error_dialog->popup_centered();
return; return;

View File

@ -30,15 +30,15 @@
#include "animation_library.h" #include "animation_library.h"
bool AnimationLibrary::is_valid_name(const String &p_name) { bool AnimationLibrary::is_valid_animation_name(const String &p_name) {
return !(p_name.is_empty() || p_name.contains("/") || p_name.contains(":") || p_name.contains(",") || p_name.contains("[")); 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) { bool AnimationLibrary::is_valid_library_name(const String &p_name) {
if (p_name.is_empty()) { return !(p_name.contains("/") || p_name.contains(":") || p_name.contains(",") || p_name.contains("["));
return "_"; // Should always return a valid name.
} }
String AnimationLibrary::validate_library_name(const String &p_name) {
String name = p_name; String name = p_name;
const char *characters = "/:,["; const char *characters = "/:,[";
for (const char *p = characters; *p; p++) { for (const char *p = characters; *p; p++) {
@ -48,7 +48,7 @@ String AnimationLibrary::validate_name(const String &p_name) {
} }
Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) { Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) {
ERR_FAIL_COND_V_MSG(!is_valid_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'."); ERR_FAIL_COND_V_MSG(!is_valid_animation_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'.");
ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER);
if (animations.has(p_name)) { if (animations.has(p_name)) {
@ -72,7 +72,7 @@ void AnimationLibrary::remove_animation(const StringName &p_name) {
void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) { void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) {
ERR_FAIL_COND(!animations.has(p_name)); ERR_FAIL_COND(!animations.has(p_name));
ERR_FAIL_COND_MSG(!is_valid_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'."); ERR_FAIL_COND_MSG(!is_valid_animation_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'.");
ERR_FAIL_COND(animations.has(p_new_name)); ERR_FAIL_COND(animations.has(p_new_name));
animations.insert(p_new_name, animations[p_name]); animations.insert(p_new_name, animations[p_name]);

View File

@ -49,8 +49,9 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
static bool is_valid_name(const String &p_name); static bool is_valid_animation_name(const String &p_name);
static String validate_name(const String &p_name); static bool is_valid_library_name(const String &p_name);
static String validate_library_name(const String &p_name);
Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation); Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation);
void remove_animation(const StringName &p_name); void remove_animation(const StringName &p_name);