wrapped.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**************************************************************************/
  2. /* wrapped.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include <vector>
  31. #include <godot_cpp/classes/wrapped.hpp>
  32. #include <godot_cpp/variant/builtin_types.hpp>
  33. #include <godot_cpp/classes/object.hpp>
  34. #include <godot_cpp/core/class_db.hpp>
  35. namespace godot {
  36. const StringName *Wrapped::_get_extension_class_name() const {
  37. return nullptr;
  38. }
  39. void Wrapped::_postinitialize() {
  40. const StringName *extension_class = _get_extension_class_name();
  41. if (extension_class) {
  42. godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this);
  43. }
  44. godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
  45. }
  46. Wrapped::Wrapped(const StringName p_godot_class) {
  47. #ifdef HOT_RELOAD_ENABLED
  48. if (unlikely(Wrapped::recreate_instance)) {
  49. RecreateInstance *recreate_data = Wrapped::recreate_instance;
  50. RecreateInstance *previous = nullptr;
  51. while (recreate_data) {
  52. if (recreate_data->wrapper == this) {
  53. _owner = recreate_data->owner;
  54. if (previous) {
  55. previous->next = recreate_data->next;
  56. } else {
  57. Wrapped::recreate_instance = recreate_data->next;
  58. }
  59. return;
  60. }
  61. previous = recreate_data;
  62. recreate_data = recreate_data->next;
  63. }
  64. }
  65. #endif
  66. _owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
  67. }
  68. Wrapped::Wrapped(GodotObject *p_godot_object) {
  69. _owner = p_godot_object;
  70. }
  71. void postinitialize_handler(Wrapped *p_wrapped) {
  72. p_wrapped->_postinitialize();
  73. }
  74. namespace internal {
  75. std::vector<EngineClassRegistrationCallback> &get_engine_class_registration_callbacks() {
  76. static std::vector<EngineClassRegistrationCallback> engine_class_registration_callbacks;
  77. return engine_class_registration_callbacks;
  78. }
  79. GDExtensionPropertyInfo *create_c_property_list(const ::godot::List<::godot::PropertyInfo> &plist_cpp, uint32_t *r_size) {
  80. GDExtensionPropertyInfo *plist = nullptr;
  81. // Linked list size can be expensive to get so we cache it
  82. const uint32_t plist_size = plist_cpp.size();
  83. if (r_size != nullptr) {
  84. *r_size = plist_size;
  85. }
  86. plist = reinterpret_cast<GDExtensionPropertyInfo *>(memalloc(sizeof(GDExtensionPropertyInfo) * plist_size));
  87. unsigned int i = 0;
  88. for (const ::godot::PropertyInfo &E : plist_cpp) {
  89. plist[i].type = static_cast<GDExtensionVariantType>(E.type);
  90. plist[i].name = E.name._native_ptr();
  91. plist[i].hint = E.hint;
  92. plist[i].hint_string = E.hint_string._native_ptr();
  93. plist[i].class_name = E.class_name._native_ptr();
  94. plist[i].usage = E.usage;
  95. ++i;
  96. }
  97. return plist;
  98. }
  99. void free_c_property_list(GDExtensionPropertyInfo *plist) {
  100. memfree(plist);
  101. }
  102. void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback) {
  103. get_engine_class_registration_callbacks().push_back(p_callback);
  104. }
  105. void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  106. ClassDB::_register_engine_class(p_name, p_callbacks);
  107. }
  108. void register_engine_classes() {
  109. std::vector<EngineClassRegistrationCallback> &engine_class_registration_callbacks = get_engine_class_registration_callbacks();
  110. for (EngineClassRegistrationCallback cb : engine_class_registration_callbacks) {
  111. cb();
  112. }
  113. engine_class_registration_callbacks.clear();
  114. }
  115. } // namespace internal
  116. } // namespace godot