2
0

gd_mono_property.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*************************************************************************/
  2. /* gd_mono_property.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gd_mono_property.h"
  31. #include "gd_mono_cache.h"
  32. #include "gd_mono_class.h"
  33. #include "gd_mono_marshal.h"
  34. #include "gd_mono_utils.h"
  35. #include <mono/metadata/attrdefs.h>
  36. GDMonoProperty::GDMonoProperty(MonoProperty *p_mono_property, GDMonoClass *p_owner) {
  37. owner = p_owner;
  38. mono_property = p_mono_property;
  39. name = String::utf8(mono_property_get_name(mono_property));
  40. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  41. if (prop_method) {
  42. MonoMethodSignature *getter_sig = mono_method_signature(prop_method);
  43. MonoType *ret_type = mono_signature_get_return_type(getter_sig);
  44. type.type_encoding = mono_type_get_type(ret_type);
  45. MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
  46. type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
  47. } else {
  48. prop_method = mono_property_get_set_method(mono_property);
  49. MonoMethodSignature *setter_sig = mono_method_signature(prop_method);
  50. void *iter = nullptr;
  51. MonoType *param_raw_type = mono_signature_get_params(setter_sig, &iter);
  52. type.type_encoding = mono_type_get_type(param_raw_type);
  53. MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
  54. type.type_class = GDMono::get_singleton()->get_class(param_type_class);
  55. }
  56. param_buffer_size = GDMonoMarshal::variant_get_managed_unboxed_size(type);
  57. attrs_fetched = false;
  58. attributes = nullptr;
  59. }
  60. GDMonoProperty::~GDMonoProperty() {
  61. if (attributes) {
  62. mono_custom_attrs_free(attributes);
  63. }
  64. }
  65. bool GDMonoProperty::is_static() {
  66. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  67. if (prop_method == nullptr) {
  68. prop_method = mono_property_get_set_method(mono_property);
  69. }
  70. return mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_STATIC;
  71. }
  72. IMonoClassMember::Visibility GDMonoProperty::get_visibility() {
  73. MonoMethod *prop_method = mono_property_get_get_method(mono_property);
  74. if (prop_method == nullptr) {
  75. prop_method = mono_property_get_set_method(mono_property);
  76. }
  77. switch (mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_ACCESS_MASK) {
  78. case MONO_METHOD_ATTR_PRIVATE:
  79. return IMonoClassMember::PRIVATE;
  80. case MONO_METHOD_ATTR_FAM_AND_ASSEM:
  81. return IMonoClassMember::PROTECTED_AND_INTERNAL;
  82. case MONO_METHOD_ATTR_ASSEM:
  83. return IMonoClassMember::INTERNAL;
  84. case MONO_METHOD_ATTR_FAMILY:
  85. return IMonoClassMember::PROTECTED;
  86. case MONO_METHOD_ATTR_PUBLIC:
  87. return IMonoClassMember::PUBLIC;
  88. default:
  89. ERR_FAIL_V(IMonoClassMember::PRIVATE);
  90. }
  91. }
  92. bool GDMonoProperty::has_attribute(GDMonoClass *p_attr_class) {
  93. ERR_FAIL_NULL_V(p_attr_class, false);
  94. if (!attrs_fetched) {
  95. fetch_attributes();
  96. }
  97. if (!attributes) {
  98. return false;
  99. }
  100. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  101. }
  102. MonoObject *GDMonoProperty::get_attribute(GDMonoClass *p_attr_class) {
  103. ERR_FAIL_NULL_V(p_attr_class, nullptr);
  104. if (!attrs_fetched) {
  105. fetch_attributes();
  106. }
  107. if (!attributes) {
  108. return nullptr;
  109. }
  110. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  111. }
  112. void GDMonoProperty::fetch_attributes() {
  113. ERR_FAIL_COND(attributes != nullptr);
  114. attributes = mono_custom_attrs_from_property(owner->get_mono_ptr(), mono_property);
  115. attrs_fetched = true;
  116. }
  117. bool GDMonoProperty::has_getter() {
  118. return mono_property_get_get_method(mono_property) != nullptr;
  119. }
  120. bool GDMonoProperty::has_setter() {
  121. return mono_property_get_set_method(mono_property) != nullptr;
  122. }
  123. void GDMonoProperty::set_value_from_variant(MonoObject *p_object, const Variant &p_value, MonoException **r_exc) {
  124. uint8_t *buffer = (uint8_t *)alloca(param_buffer_size);
  125. unsigned int offset = 0;
  126. void *params[1] = {
  127. GDMonoMarshal::variant_to_managed_unboxed(p_value, type, buffer, offset)
  128. };
  129. #ifdef DEBUG_ENABLED
  130. CRASH_COND(offset != param_buffer_size);
  131. #endif
  132. MonoException *exc = nullptr;
  133. GDMonoUtils::property_set_value(mono_property, p_object, params, &exc);
  134. if (exc) {
  135. if (r_exc) {
  136. *r_exc = exc;
  137. } else {
  138. GDMonoUtils::set_pending_exception(exc);
  139. }
  140. }
  141. }
  142. MonoObject *GDMonoProperty::get_value(MonoObject *p_object, MonoException **r_exc) {
  143. MonoException *exc = nullptr;
  144. MonoObject *ret = GDMonoUtils::property_get_value(mono_property, p_object, nullptr, &exc);
  145. if (exc) {
  146. ret = nullptr;
  147. if (r_exc) {
  148. *r_exc = exc;
  149. } else {
  150. GDMonoUtils::set_pending_exception(exc);
  151. }
  152. }
  153. return ret;
  154. }
  155. bool GDMonoProperty::get_bool_value(MonoObject *p_object) {
  156. return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
  157. }
  158. int GDMonoProperty::get_int_value(MonoObject *p_object) {
  159. return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
  160. }
  161. String GDMonoProperty::get_string_value(MonoObject *p_object) {
  162. MonoObject *val = get_value(p_object);
  163. return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
  164. }