From 302de87e32549ef52a6a8f58d7b50e144d8d7520 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 1 Apr 2024 15:06:06 +0200 Subject: [PATCH] Prevent using external resources for derived blackboard plans in `BTPlayer` and `BTState` --- blackboard/blackboard_plan.cpp | 1 + bt/bt_player.cpp | 7 ++++++- bt/bt_state.cpp | 12 +++++++++--- hsm/limbo_state.h | 2 +- util/limbo_compat.h | 1 + 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/blackboard/blackboard_plan.cpp b/blackboard/blackboard_plan.cpp index 4545a6b..d55cd03 100644 --- a/blackboard/blackboard_plan.cpp +++ b/blackboard/blackboard_plan.cpp @@ -122,6 +122,7 @@ bool BlackboardPlan::_property_get_revert(const StringName &p_name, Variant &r_p void BlackboardPlan::set_base_plan(const Ref &p_base) { if (p_base == this) { + WARN_PRINT_ED("BlackboardPlan: Using same resource for derived blackboard plan is not supported."); base.unref(); } else { base = p_base; diff --git a/bt/bt_player.cpp b/bt/bt_player.cpp index 54608b4..7840400 100644 --- a/bt/bt_player.cpp +++ b/bt/bt_player.cpp @@ -86,7 +86,12 @@ void BTPlayer::set_behavior_tree(const Ref &p_tree) { } void BTPlayer::set_blackboard_plan(const Ref &p_plan) { - blackboard_plan = p_plan; + if (p_plan.is_valid() && !RESOURCE_IS_BUILT_IN(p_plan)) { + WARN_PRINT_ED("BTPlayer: Using external resource for derived blackboard plan is not supported. Converted to built-in resource."); + blackboard_plan = p_plan->duplicate(); + } else { + blackboard_plan = p_plan; + } _update_blackboard_plan(); } diff --git a/bt/bt_state.cpp b/bt/bt_state.cpp index 749762f..ec1c64f 100644 --- a/bt/bt_state.cpp +++ b/bt/bt_state.cpp @@ -31,16 +31,22 @@ void BTState::set_behavior_tree(const Ref &p_tree) { if (p_tree.is_valid()) { p_tree->connect(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan)); } + behavior_tree = p_tree; _update_blackboard_plan(); + } else { + behavior_tree = p_tree; } - behavior_tree = p_tree; } void BTState::_update_blackboard_plan() { if (get_blackboard_plan().is_null()) { - set_blackboard_plan(Ref(memnew(BlackboardPlan))); + set_blackboard_plan(memnew(BlackboardPlan)); + } else if (!RESOURCE_IS_BUILT_IN(get_blackboard_plan())) { + WARN_PRINT_ED("BTState: Using external resource for derived blackboard plan is not supported. Converted to built-in resource."); + set_blackboard_plan(get_blackboard_plan()->duplicate()); + } else { + get_blackboard_plan()->set_base_plan(behavior_tree.is_valid() ? behavior_tree->get_blackboard_plan() : nullptr); } - get_blackboard_plan()->set_base_plan(behavior_tree.is_valid() ? behavior_tree->get_blackboard_plan() : nullptr); } void BTState::_setup() { diff --git a/hsm/limbo_state.h b/hsm/limbo_state.h index 706045e..69bc2cf 100644 --- a/hsm/limbo_state.h +++ b/hsm/limbo_state.h @@ -67,7 +67,7 @@ protected: public: void set_blackboard_plan(const Ref &p_plan); - Ref get_blackboard_plan() const { return blackboard_plan; } + _FORCE_INLINE_ Ref get_blackboard_plan() const { return blackboard_plan; } Ref get_blackboard() const { return blackboard; } diff --git a/util/limbo_compat.h b/util/limbo_compat.h index 690682b..321dc40 100644 --- a/util/limbo_compat.h +++ b/util/limbo_compat.h @@ -234,6 +234,7 @@ Variant VARIANT_DEFAULT(Variant::Type p_type); #define PROJECT_CONFIG_FILE() GET_PROJECT_SETTINGS_DIR().path_join("limbo_ai.cfg") #define IS_RESOURCE_FILE(m_path) (m_path.begins_with("res://") && m_path.find("::") == -1) #define RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type) +#define RESOURCE_IS_BUILT_IN(m_res) (m_res->get_path().is_empty() || m_res->get_path().contains("::")) #ifdef TOOLS_ENABLED