From cf8c679a23b21d6c6f29cba6a54eaa2eed88bf92 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 12 Feb 2020 14:24:06 -0300 Subject: ObjectID converted to a structure, fixes many bugs where used incorrectly as 32 bits. --- core/func_ref.cpp | 9 +++--- core/hashfuncs.h | 3 +- core/io/marshalls.cpp | 9 +++--- core/io/multiplayer_api.cpp | 1 - core/method_ptrcall.h | 16 +++++++++++ core/object.cpp | 9 +++--- core/object.h | 7 +++-- core/object_id.h | 32 ++++++++++++++++++++++ core/reference.cpp | 9 +++--- core/type_info.h | 10 +++++++ core/variant.cpp | 13 +++++++++ core/variant.h | 4 +++ editor/dictionary_property_edit.cpp | 1 - editor/editor_data.cpp | 14 +++++----- editor/editor_node.cpp | 8 +++--- editor/editor_properties.cpp | 10 +++---- editor/editor_sectioned_inspector.cpp | 3 +- editor/inspector_dock.cpp | 12 ++++---- editor/plugins/animation_tree_editor_plugin.cpp | 7 ++--- editor/plugins/canvas_item_editor_plugin.cpp | 2 +- editor/plugins/spatial_editor_plugin.cpp | 30 ++++++++++---------- editor/property_editor.cpp | 8 +++--- editor/property_selector.cpp | 12 ++++---- editor/script_editor_debugger.cpp | 13 ++++----- modules/bullet/area_bullet.cpp | 6 ++-- modules/bullet/area_bullet.h | 3 +- modules/bullet/bullet_physics_server.cpp | 14 +++++----- modules/bullet/bullet_physics_server.h | 4 +-- modules/bullet/collision_object_bullet.cpp | 2 +- modules/bullet/godot_result_callbacks.cpp | 4 +-- modules/bullet/rigid_body_bullet.cpp | 4 +-- modules/bullet/space_bullet.cpp | 2 +- modules/gdnative/gdnative/gdnative.cpp | 2 +- modules/gdnative/gdnative/string.cpp | 7 ++++- modules/gdnative/include/gdnative/gdnative.h | 2 +- modules/gdnavigation/gd_navigation_server.cpp | 2 +- modules/gdnavigation/rvo_agent.cpp | 8 +++--- modules/gdscript/gdscript_function.cpp | 6 ++-- modules/gdscript/gdscript_functions.cpp | 2 +- modules/visual_script/visual_script.cpp | 8 +++--- .../visual_script_property_selector.cpp | 6 ---- scene/2d/area_2d.cpp | 4 +-- scene/2d/area_2d.h | 4 +-- scene/2d/camera_2d.cpp | 4 +-- scene/2d/canvas_item.cpp | 2 +- scene/2d/collision_object_2d.cpp | 4 +-- scene/2d/physics_body_2d.cpp | 4 +-- scene/2d/polygon_2d.cpp | 4 +-- scene/2d/ray_cast_2d.cpp | 6 ++-- scene/2d/remote_transform_2d.cpp | 7 ++--- scene/3d/area.cpp | 4 +-- scene/3d/area.h | 4 +-- scene/3d/physics_body.cpp | 4 +-- scene/3d/ray_cast.cpp | 6 ++-- scene/3d/remote_transform.cpp | 7 ++--- scene/3d/skeleton.cpp | 12 ++++---- scene/3d/skeleton.h | 2 +- scene/animation/animation_player.cpp | 2 +- scene/animation/animation_player.h | 10 +++---- scene/animation/animation_tree.cpp | 9 +++--- scene/animation/animation_tree.h | 1 - scene/gui/control.cpp | 13 ++++----- scene/gui/tree.cpp | 2 +- scene/gui/tree.h | 2 +- scene/main/canvas_layer.cpp | 4 +-- scene/main/viewport.cpp | 32 ++++++++++------------ servers/physics/area_sw.cpp | 10 +++---- servers/physics/area_sw.h | 4 +-- servers/physics/body_sw.cpp | 4 +-- servers/physics/body_sw.h | 2 +- servers/physics/collision_object_sw.cpp | 2 +- servers/physics/physics_server_sw.cpp | 14 +++++----- servers/physics/physics_server_sw.h | 4 +-- servers/physics/space_sw.cpp | 8 +++--- servers/physics_2d/area_2d_sw.cpp | 10 +++---- servers/physics_2d/area_2d_sw.h | 4 +-- servers/physics_2d/body_2d_sw.cpp | 4 +-- servers/physics_2d/body_2d_sw.h | 2 +- servers/physics_2d/collision_object_2d_sw.cpp | 2 -- servers/physics_2d/physics_2d_server_sw.cpp | 22 +++++++-------- servers/physics_2d/physics_2d_server_sw.h | 8 +++--- servers/physics_2d/physics_2d_server_wrap_mt.h | 8 +++--- servers/physics_2d/space_2d_sw.cpp | 8 +++--- servers/physics_2d/space_2d_sw.h | 2 +- servers/physics_2d_server.cpp | 2 +- servers/physics_2d_server.h | 12 ++++---- servers/physics_server.h | 6 ++-- servers/visual/visual_server_scene.cpp | 6 ++-- servers/visual/visual_server_scene.h | 3 +- 89 files changed, 337 insertions(+), 287 deletions(-) create mode 100644 core/object_id.h diff --git a/core/func_ref.cpp b/core/func_ref.cpp index 2dffb30bab..e20188c813 100644 --- a/core/func_ref.cpp +++ b/core/func_ref.cpp @@ -32,7 +32,7 @@ Variant FuncRef::call_func(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { - if (id == 0) { + if (id.is_null()) { r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL; return Variant(); } @@ -48,7 +48,7 @@ Variant FuncRef::call_func(const Variant **p_args, int p_argcount, Variant::Call Variant FuncRef::call_funcv(const Array &p_args) { - ERR_FAIL_COND_V(id == 0, Variant()); + ERR_FAIL_COND_V(id.is_null(), Variant()); Object *obj = ObjectDB::get_instance(id); @@ -69,7 +69,7 @@ void FuncRef::set_function(const StringName &p_func) { } bool FuncRef::is_valid() const { - if (id == 0) + if (id.is_null()) return false; Object *obj = ObjectDB::get_instance(id); @@ -95,6 +95,5 @@ void FuncRef::_bind_methods() { ClassDB::bind_method(D_METHOD("is_valid"), &FuncRef::is_valid); } -FuncRef::FuncRef() : - id(0) { +FuncRef::FuncRef() { } diff --git a/core/hashfuncs.h b/core/hashfuncs.h index 647e8a40b2..d6cf04e560 100644 --- a/core/hashfuncs.h +++ b/core/hashfuncs.h @@ -34,10 +34,10 @@ #include "core/math/math_defs.h" #include "core/math/math_funcs.h" #include "core/node_path.h" +#include "core/object_id.h" #include "core/string_name.h" #include "core/typedefs.h" #include "core/ustring.h" - /** * Hashing functions */ @@ -137,6 +137,7 @@ struct HashMapHasherDefault { static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } + static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); } static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); } static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); } diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index e847a9cf0c..fdce9db2a0 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -53,8 +53,7 @@ ObjectID EncodedObjectAsID::get_object_id() const { return id; } -EncodedObjectAsID::EncodedObjectAsID() : - id(0) { +EncodedObjectAsID::EncodedObjectAsID() { } #define _S(a) ((int32_t)a) @@ -386,11 +385,11 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int if (type & ENCODE_FLAG_OBJECT_AS_ID) { //this _is_ allowed ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA); - ObjectID val = decode_uint64(buf); + ObjectID val = ObjectID(decode_uint64(buf)); if (r_len) (*r_len) += 8; - if (val == 0) { + if (val.is_null()) { r_variant = (Object *)NULL; } else { Ref obj_as_id; @@ -1129,7 +1128,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo if (buf) { Object *obj = p_variant; - ObjectID id = 0; + ObjectID id; if (obj && ObjectDB::instance_validate(obj)) { id = obj->get_instance_id(); } diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index 27c122fac7..ce641dd0f5 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -446,7 +446,6 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, PathGetCache::NodeInfo ni; ni.path = path; - ni.instance = 0; path_get_cache[p_from].nodes[id] = ni; diff --git a/core/method_ptrcall.h b/core/method_ptrcall.h index 0f2458d982..118970de80 100644 --- a/core/method_ptrcall.h +++ b/core/method_ptrcall.h @@ -32,6 +32,7 @@ #define METHOD_PTRCALL_H #include "core/math/transform_2d.h" +#include "core/object_id.h" #include "core/typedefs.h" #include "core/variant.h" @@ -167,6 +168,21 @@ struct PtrToArg { } }; +//this is for ObjectID + +template <> +struct PtrToArg { + _FORCE_INLINE_ static const ObjectID convert(const void *p_ptr) { + + return ObjectID(*reinterpret_cast(p_ptr)); + } + + _FORCE_INLINE_ static void encode(const ObjectID &p_val, void *p_ptr) { + + *((uint64_t *)p_ptr) = p_val; + } +}; + //this is for the special cases used by Variant #define MAKE_VECARG(m_type) \ diff --git a/core/object.cpp b/core/object.cpp index 937b1ae8d4..dc1dc2c41f 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1916,7 +1916,6 @@ Object::Object() { _class_ptr = NULL; _block_signals = false; _predelete_ok = 0; - _instance_id = 0; _instance_id = ObjectDB::add_instance(this); _can_translate = true; _is_queued_for_deletion = false; @@ -1972,7 +1971,7 @@ Object::~Object() { } ObjectDB::remove_instance(this); - _instance_id = 0; + _instance_id = ObjectID(); _predelete_ok = 2; if (!ScriptServer::are_languages_finished()) { @@ -1995,14 +1994,14 @@ void postinitialize_handler(Object *p_object) { } HashMap ObjectDB::instances; -ObjectID ObjectDB::instance_counter = 1; +uint64_t ObjectDB::instance_counter = 1; HashMap ObjectDB::instance_checks; ObjectID ObjectDB::add_instance(Object *p_object) { - ERR_FAIL_COND_V(p_object->get_instance_id() != 0, 0); + ERR_FAIL_COND_V(p_object->get_instance_id().is_valid(), ObjectID()); rw_lock->write_lock(); - ObjectID instance_id = ++instance_counter; + ObjectID instance_id = ObjectID(++instance_counter); instances[instance_id] = p_object; instance_checks[p_object] = instance_id; diff --git a/core/object.h b/core/object.h index 865c155764..312fe07a17 100644 --- a/core/object.h +++ b/core/object.h @@ -34,6 +34,7 @@ #include "core/hash_map.h" #include "core/list.h" #include "core/map.h" +#include "core/object_id.h" #include "core/os/rw_lock.h" #include "core/set.h" #include "core/variant.h" @@ -89,6 +90,7 @@ enum PropertyHint { PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send PROPERTY_HINT_NODE_PATH_VALID_TYPES, PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog + PROPERTY_HINT_INT_IS_OBJECTID, PROPERTY_HINT_MAX, // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit }; @@ -397,7 +399,6 @@ public: \ private: class ScriptInstance; -typedef uint64_t ObjectID; class Object { public: @@ -452,7 +453,7 @@ private: _id(p_id), method(p_method) { } - Target() { _id = 0; } + Target() { _id = ObjectID(); } }; struct Slot { @@ -775,7 +776,7 @@ class ObjectDB { static HashMap instances; static HashMap instance_checks; - static ObjectID instance_counter; + static uint64_t instance_counter; friend class Object; friend void unregister_core_types(); diff --git a/core/object_id.h b/core/object_id.h new file mode 100644 index 0000000000..f0ff2a24f5 --- /dev/null +++ b/core/object_id.h @@ -0,0 +1,32 @@ +#ifndef OBJECT_ID_H +#define OBJECT_ID_H + +#include "core/typedefs.h" + +// Class to store an object ID (int64) +// needs to be compatile with int64 because this is what Variant uses +// Also, need to be explicitly only castable to 64 bits integer types +// to avoid bugs due to loss of precision + +class ObjectID { + uint64_t id = 0; + +public: + _ALWAYS_INLINE_ bool is_valid() const { return id != 0; } + _ALWAYS_INLINE_ bool is_null() const { return id == 0; } + _ALWAYS_INLINE_ operator uint64_t() const { return id; } + _ALWAYS_INLINE_ operator int64_t() const { return id; } + + _ALWAYS_INLINE_ bool operator==(const ObjectID &p_id) const { return id == p_id.id; } + _ALWAYS_INLINE_ bool operator!=(const ObjectID &p_id) const { return id != p_id.id; } + _ALWAYS_INLINE_ bool operator<(const ObjectID &p_id) const { return id < p_id.id; } + + _ALWAYS_INLINE_ void operator=(int64_t p_int64) { id = p_int64; } + _ALWAYS_INLINE_ void operator=(uint64_t p_uint64) { id = p_uint64; } + + _ALWAYS_INLINE_ ObjectID() {} + _ALWAYS_INLINE_ explicit ObjectID(const uint64_t p_id) { id = p_id; } + _ALWAYS_INLINE_ explicit ObjectID(const int64_t p_id) { id = p_id; } +}; + +#endif // OBJECT_ID_H diff --git a/core/reference.cpp b/core/reference.cpp index b24b2a3ec0..5c211fe301 100644 --- a/core/reference.cpp +++ b/core/reference.cpp @@ -113,7 +113,7 @@ Reference::~Reference() { Variant WeakRef::get_ref() const { - if (ref == 0) + if (ref.is_null()) return Variant(); Object *obj = ObjectDB::get_instance(ref); @@ -129,16 +129,15 @@ Variant WeakRef::get_ref() const { } void WeakRef::set_obj(Object *p_object) { - ref = p_object ? p_object->get_instance_id() : 0; + ref = p_object ? p_object->get_instance_id() : ObjectID(); } void WeakRef::set_ref(const REF &p_ref) { - ref = p_ref.is_valid() ? p_ref->get_instance_id() : 0; + ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID(); } -WeakRef::WeakRef() : - ref(0) { +WeakRef::WeakRef() { } void WeakRef::_bind_methods() { diff --git a/core/type_info.h b/core/type_info.h index 68bc1cc554..b9e5b61a6a 100644 --- a/core/type_info.h +++ b/core/type_info.h @@ -187,6 +187,16 @@ struct GetTypeInfo { } }; +//objectID +template <> +struct GetTypeInfo { + static const Variant::Type VARIANT_TYPE = Variant::INT; + static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE; + static inline PropertyInfo get_class_info() { + return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_INT_IS_OBJECTID); + } +}; + //for variant template <> struct GetTypeInfo { diff --git a/core/variant.cpp b/core/variant.cpp index f4e4cd5341..0f04710d13 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -1248,6 +1248,14 @@ Variant::operator uint64_t() const { } } +Variant::operator ObjectID() const { + if (type == INT) { + return ObjectID(_data._int); + } else { + return ObjectID(); + } +} + #ifdef NEED_LONG_INT Variant::operator signed long() const { @@ -2193,6 +2201,11 @@ Variant::Variant(double p_double) { _data._real = p_double; } +Variant::Variant(const ObjectID &p_id) { + type = INT; + _data._int = p_id; +} + Variant::Variant(const StringName &p_string) { type = STRING; diff --git a/core/variant.h b/core/variant.h index 06be914408..d8007f9e12 100644 --- a/core/variant.h +++ b/core/variant.h @@ -44,6 +44,7 @@ #include "core/math/transform_2d.h" #include "core/math/vector3.h" #include "core/node_path.h" +#include "core/object_id.h" #include "core/pool_vector.h" #include "core/ref_ptr.h" #include "core/rid.h" @@ -177,6 +178,8 @@ public: operator unsigned long() const; #endif + operator ObjectID() const; + operator CharType() const; operator float() const; operator double() const; @@ -248,6 +251,7 @@ public: Variant(uint64_t p_int); Variant(float p_float); Variant(double p_double); + Variant(const ObjectID &p_id); Variant(const String &p_string); Variant(const StringName &p_string); Variant(const char *const p_cstring); diff --git a/editor/dictionary_property_edit.cpp b/editor/dictionary_property_edit.cpp index bb01fadb72..82db639379 100644 --- a/editor/dictionary_property_edit.cpp +++ b/editor/dictionary_property_edit.cpp @@ -190,5 +190,4 @@ bool DictionaryPropertyEdit::_get(const StringName &p_name, Variant &r_ret) cons } DictionaryPropertyEdit::DictionaryPropertyEdit() { - obj = 0; } diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index d7c610f109..5cb7720170 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -156,8 +156,8 @@ bool EditorHistory::is_history_obj_inspector_only(int p_obj) const { } ObjectID EditorHistory::get_history_obj(int p_obj) const { - ERR_FAIL_INDEX_V(p_obj, history.size(), 0); - ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), 0); + ERR_FAIL_INDEX_V(p_obj, history.size(), ObjectID()); + ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), ObjectID()); return history[p_obj].path[history[p_obj].level].object; } @@ -204,12 +204,12 @@ bool EditorHistory::is_current_inspector_only() const { ObjectID EditorHistory::get_current() { if (current < 0 || current >= history.size()) - return 0; + return ObjectID(); History &h = history.write[current]; Object *obj = ObjectDB::get_instance(h.path[h.level].object); if (!obj) - return 0; + return ObjectID(); return obj->get_instance_id(); } @@ -226,15 +226,15 @@ int EditorHistory::get_path_size() const { ObjectID EditorHistory::get_path_object(int p_index) const { if (current < 0 || current >= history.size()) - return 0; + return ObjectID(); const History &h = history[current]; - ERR_FAIL_INDEX_V(p_index, h.path.size(), 0); + ERR_FAIL_INDEX_V(p_index, h.path.size(), ObjectID()); Object *obj = ObjectDB::get_instance(h.path[p_index].object); if (!obj) - return 0; + return ObjectID(); return obj->get_instance_id(); } diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 4440a747be..a6172faeaa 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -1558,7 +1558,7 @@ void EditorNode::_dialog_action(String p_file) { save_resource_in_path(saving_resource, p_file); saving_resource = Ref(); ObjectID current = editor_history.get_current(); - Object *current_obj = current > 0 ? ObjectDB::get_instance(current) : NULL; + Object *current_obj = current.is_valid() ? ObjectDB::get_instance(current) : NULL; ERR_FAIL_COND(!current_obj); current_obj->_change_notify(); } break; @@ -1711,7 +1711,7 @@ void EditorNode::push_item(Object *p_object, const String &p_property, bool p_in return; } - uint32_t id = p_object->get_instance_id(); + ObjectID id = p_object->get_instance_id(); if (id != editor_history.get_current()) { if (p_inspector_only) { @@ -1767,8 +1767,8 @@ static bool overrides_external_editor(Object *p_object) { void EditorNode::_edit_current() { - uint32_t current = editor_history.get_current(); - Object *current_obj = current > 0 ? ObjectDB::get_instance(current) : NULL; + ObjectID current = editor_history.get_current(); + Object *current_obj = current.is_valid() ? ObjectDB::get_instance(current) : NULL; bool inspector_only = editor_history.is_current_inspector_only(); this->current = current_obj; diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 0794940cb1..905e928c5a 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -388,13 +388,13 @@ void EditorPropertyMember::_property_select() { } else if (hint == MEMBER_METHOD_OF_INSTANCE) { - Object *instance = ObjectDB::get_instance(hint_text.to_int64()); + Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); if (instance) selector->select_method_from_instance(instance, current); } else if (hint == MEMBER_METHOD_OF_SCRIPT) { - Object *obj = ObjectDB::get_instance(hint_text.to_int64()); + Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int64())); if (Object::cast_to