gd_mono_property.cpp 6.9 KB

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