Allow custom monitors to select desired type

This commit is contained in:
devloglogan
2025-09-09 21:34:22 -05:00
parent 9cd297b6f2
commit 1a8306bbc1
9 changed files with 151 additions and 28 deletions

View File

@ -29,6 +29,7 @@
/**************************************************************************/
#include "performance.h"
#include "performance.compat.inc"
#include "core/os/os.h"
#include "core/variant/typed_array.h"
@ -57,12 +58,13 @@ Performance *Performance::singleton = nullptr;
void Performance::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
BIND_ENUM_CONSTANT(TIME_FPS);
BIND_ENUM_CONSTANT(TIME_PROCESS);
@ -132,6 +134,11 @@ void Performance::_bind_methods() {
BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
#endif // NAVIGATION_3D_DISABLED
BIND_ENUM_CONSTANT(MONITOR_MAX);
BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
}
int Performance::_get_node_count() const {
@ -537,9 +544,9 @@ void Performance::set_navigation_process_time(double p_pt) {
_navigation_process_time = p_pt;
}
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type) {
ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
_monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
_monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
_monitor_modification_time = OS::get_singleton()->get_ticks_usec();
}
@ -576,6 +583,20 @@ TypedArray<StringName> Performance::get_custom_monitor_names() {
return return_array;
}
Vector<int> Performance::get_custom_monitor_types() {
if (_monitor_map.is_empty()) {
return Vector<int>();
}
Vector<int> ret;
ret.resize(_monitor_map.size());
int index = 0;
for (const KeyValue<StringName, MonitorCall> &i : _monitor_map) {
ret.set(index, (int)i.value.get_monitor_type());
index++;
}
return ret;
}
uint64_t Performance::get_monitor_modification_time() {
return _monitor_modification_time;
}
@ -588,7 +609,8 @@ Performance::Performance() {
singleton = this;
}
Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, const Callable &p_callable, const Vector<Variant> &p_arguments) {
_type = p_type;
_callable = p_callable;
_arguments = p_arguments;
}