2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* limbo_debugger.cpp
|
|
|
|
* =============================================================================
|
2024-03-21 20:38:57 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2023-04-13 07:29:45 +00:00
|
|
|
|
|
|
|
#include "limbo_debugger.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
#include "../../bt/bt_instance.h"
|
2024-01-07 01:40:51 +00:00
|
|
|
#include "../../bt/tasks/bt_task.h"
|
2024-01-10 21:45:42 +00:00
|
|
|
#include "../../util/limbo_compat.h"
|
2023-04-13 07:29:45 +00:00
|
|
|
#include "behavior_tree_data.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-07 01:40:51 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2023-04-13 07:29:45 +00:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2023-12-12 22:37:28 +00:00
|
|
|
#include "core/io/resource.h"
|
2023-04-13 07:29:45 +00:00
|
|
|
#include "core/string/node_path.h"
|
|
|
|
#include "scene/main/scene_tree.h"
|
|
|
|
#include "scene/main/window.h"
|
2024-01-07 01:40:51 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/classes/engine_debugger.hpp>
|
|
|
|
#include <godot_cpp/classes/scene_tree.hpp>
|
|
|
|
#include <godot_cpp/classes/window.hpp>
|
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-01-13 16:10:42 +00:00
|
|
|
//**** LimboDebugger
|
2023-04-13 07:29:45 +00:00
|
|
|
|
|
|
|
LimboDebugger *LimboDebugger::singleton = nullptr;
|
|
|
|
|
|
|
|
LimboDebugger::LimboDebugger() {
|
|
|
|
singleton = this;
|
2024-01-07 01:40:51 +00:00
|
|
|
#if defined(DEBUG_ENABLED) && defined(LIMBOAI_MODULE)
|
2023-04-13 07:29:45 +00:00
|
|
|
EngineDebugger::register_message_capture("limboai", EngineDebugger::Capture(nullptr, LimboDebugger::parse_message));
|
2024-01-13 16:10:42 +00:00
|
|
|
#elif defined(DEBUG_ENABLED) && defined(LIMBOAI_GDEXTENSION)
|
2024-01-09 20:47:22 +00:00
|
|
|
EngineDebugger::get_singleton()->register_message_capture("limboai", callable_mp(this, &LimboDebugger::parse_message_gdext));
|
2024-01-07 01:40:51 +00:00
|
|
|
#endif
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LimboDebugger::~LimboDebugger() {
|
|
|
|
singleton = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboDebugger::initialize() {
|
2024-01-07 01:40:51 +00:00
|
|
|
if (IS_DEBUGGER_ACTIVE()) {
|
2023-04-13 07:29:45 +00:00
|
|
|
memnew(LimboDebugger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboDebugger::deinitialize() {
|
|
|
|
if (singleton) {
|
|
|
|
memdelete(singleton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 01:40:51 +00:00
|
|
|
void LimboDebugger::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:29:45 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
Error LimboDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
|
|
|
|
r_captured = true;
|
2023-04-15 07:01:37 +00:00
|
|
|
if (p_msg == "track_bt_player") {
|
2023-04-13 07:29:45 +00:00
|
|
|
singleton->_track_tree(p_args[0]);
|
2023-04-15 07:01:37 +00:00
|
|
|
} else if (p_msg == "untrack_bt_player") {
|
|
|
|
singleton->_untrack_tree();
|
2023-04-13 07:29:45 +00:00
|
|
|
} else if (p_msg == "start_session") {
|
|
|
|
singleton->session_active = true;
|
2023-04-15 07:01:37 +00:00
|
|
|
singleton->_send_active_bt_players();
|
2023-04-13 07:29:45 +00:00
|
|
|
} else if (p_msg == "stop_session") {
|
|
|
|
singleton->session_active = false;
|
|
|
|
} else {
|
|
|
|
r_captured = false;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2024-01-10 21:45:42 +00:00
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
2024-01-07 01:40:51 +00:00
|
|
|
bool LimboDebugger::parse_message_gdext(const String &p_msg, const Array &p_args) {
|
2024-01-09 20:47:22 +00:00
|
|
|
bool r_captured;
|
|
|
|
LimboDebugger::parse_message(nullptr, p_msg, p_args, r_captured);
|
|
|
|
return r_captured;
|
2024-01-07 01:40:51 +00:00
|
|
|
}
|
2024-01-13 16:10:42 +00:00
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
2024-01-07 01:40:51 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
void LimboDebugger::register_bt_instance(uint64_t p_instance_id) {
|
|
|
|
ERR_FAIL_COND(p_instance_id == 0);
|
2024-08-03 09:39:23 +00:00
|
|
|
if (!IS_DEBUGGER_ACTIVE()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(p_instance_id));
|
|
|
|
ERR_FAIL_NULL(inst);
|
|
|
|
ERR_FAIL_COND(!inst->is_instance_valid());
|
|
|
|
|
|
|
|
if (active_bt_instances.has(p_instance_id)) {
|
2023-09-09 11:37:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
if (!inst->is_connected(LW_NAME(freed), callable_mp(this, &LimboDebugger::unregister_bt_instance).bind(p_instance_id))) {
|
|
|
|
inst->connect(LW_NAME(freed), callable_mp(this, &LimboDebugger::unregister_bt_instance).bind(p_instance_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
active_bt_instances.insert(p_instance_id);
|
2023-04-13 07:29:45 +00:00
|
|
|
if (session_active) {
|
2023-04-15 07:01:37 +00:00
|
|
|
_send_active_bt_players();
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
void LimboDebugger::unregister_bt_instance(uint64_t p_instance_id) {
|
|
|
|
if (!active_bt_instances.has(p_instance_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
if (tracked_instance_id == p_instance_id) {
|
2023-04-13 07:29:45 +00:00
|
|
|
_untrack_tree();
|
|
|
|
}
|
2024-08-03 09:07:06 +00:00
|
|
|
active_bt_instances.erase(p_instance_id);
|
2023-04-13 07:29:45 +00:00
|
|
|
|
|
|
|
if (session_active) {
|
2023-04-15 07:01:37 +00:00
|
|
|
_send_active_bt_players();
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:39:23 +00:00
|
|
|
bool LimboDebugger::is_active() const {
|
|
|
|
return IS_DEBUGGER_ACTIVE();
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
void LimboDebugger::_track_tree(uint64_t p_instance_id) {
|
|
|
|
ERR_FAIL_COND(!active_bt_instances.has(p_instance_id));
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
_untrack_tree();
|
2023-04-13 07:29:45 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
tracked_instance_id = p_instance_id;
|
2023-12-12 22:37:28 +00:00
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(p_instance_id));
|
|
|
|
ERR_FAIL_NULL(inst);
|
|
|
|
inst->connect(LW_NAME(updated), callable_mp(this, &LimboDebugger::_on_bt_instance_updated).bind(p_instance_id));
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboDebugger::_untrack_tree() {
|
2024-08-03 09:07:06 +00:00
|
|
|
if (tracked_instance_id == 0) {
|
2023-04-13 07:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(tracked_instance_id));
|
|
|
|
if (inst) {
|
|
|
|
inst->disconnect(LW_NAME(updated), callable_mp(this, &LimboDebugger::_on_bt_instance_updated));
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
2024-08-03 09:07:06 +00:00
|
|
|
tracked_instance_id = 0;
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 07:01:37 +00:00
|
|
|
void LimboDebugger::_send_active_bt_players() {
|
2023-04-13 07:29:45 +00:00
|
|
|
Array arr;
|
2024-08-03 09:07:06 +00:00
|
|
|
for (uint64_t instance_id : active_bt_instances) {
|
|
|
|
arr.append(instance_id);
|
|
|
|
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(instance_id));
|
|
|
|
if (inst == nullptr) {
|
|
|
|
ERR_PRINT("LimboDebugger::_send_active_bt_players: Registered BTInstance not found (no longer exists?).");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Node *owner_node = inst->get_owner_node();
|
|
|
|
arr.append(owner_node ? owner_node->get_path() : NodePath());
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
2023-04-15 07:01:37 +00:00
|
|
|
EngineDebugger::get_singleton()->send_message("limboai:active_bt_players", arr);
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 09:07:06 +00:00
|
|
|
void LimboDebugger::_on_bt_instance_updated(int _status, uint64_t p_instance_id) {
|
|
|
|
if (p_instance_id != tracked_instance_id) {
|
2023-04-13 07:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-08-03 09:07:06 +00:00
|
|
|
BTInstance *inst = Object::cast_to<BTInstance>(OBJECT_DB_GET_INSTANCE(p_instance_id));
|
|
|
|
ERR_FAIL_NULL(inst);
|
|
|
|
Array arr = BehaviorTreeData::serialize(inst);
|
2023-04-13 07:29:45 +00:00
|
|
|
EngineDebugger::get_singleton()->send_message("limboai:bt_update", arr);
|
|
|
|
}
|
|
|
|
|
2024-01-11 10:22:02 +00:00
|
|
|
#endif // ! DEBUG_ENABLED
|