glue_header.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*************************************************************************/
  2. /* glue_header.h */
  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 "builtin_types_glue.h"
  31. #include "collections_glue.h"
  32. #include "../csharp_script.h"
  33. #include "../mono_gd/gd_mono_class.h"
  34. #include "../mono_gd/gd_mono_internals.h"
  35. #include "../mono_gd/gd_mono_marshal.h"
  36. #include "../signal_awaiter_utils.h"
  37. #include "bind/core_bind.h"
  38. #include "class_db.h"
  39. #include "engine.h"
  40. #include "io/marshalls.h"
  41. #include "object.h"
  42. #include "os/os.h"
  43. #include "reference.h"
  44. #include "variant_parser.h"
  45. #ifdef TOOLS_ENABLED
  46. #include "editor/editor_node.h"
  47. #endif
  48. #define GODOTSHARP_INSTANCE_OBJECT(m_instance, m_type) \
  49. static ClassDB::ClassInfo *ci = NULL; \
  50. if (!ci) { \
  51. ci = ClassDB::classes.getptr(m_type); \
  52. } \
  53. Object *m_instance = ci->creation_func();
  54. void godot_icall_Object_Dtor(MonoObject *obj, Object *ptr) {
  55. #ifdef DEBUG_ENABLED
  56. CRASH_COND(ptr == NULL);
  57. #endif
  58. _GodotSharp::get_singleton()->queue_dispose(obj, ptr);
  59. }
  60. // -- ClassDB --
  61. MethodBind *godot_icall_ClassDB_get_method(MonoString *p_type, MonoString *p_method) {
  62. StringName type(GDMonoMarshal::mono_string_to_godot(p_type));
  63. StringName method(GDMonoMarshal::mono_string_to_godot(p_method));
  64. return ClassDB::get_method(type, method);
  65. }
  66. // -- SignalAwaiter --
  67. Error godot_icall_Object_connect_signal_awaiter(Object *p_source, MonoString *p_signal, Object *p_target, MonoObject *p_awaiter) {
  68. String signal = GDMonoMarshal::mono_string_to_godot(p_signal);
  69. return SignalAwaiterUtils::connect_signal_awaiter(p_source, signal, p_target, p_awaiter);
  70. }
  71. // -- NodePath --
  72. NodePath *godot_icall_NodePath_Ctor(MonoString *p_path) {
  73. return memnew(NodePath(GDMonoMarshal::mono_string_to_godot(p_path)));
  74. }
  75. void godot_icall_NodePath_Dtor(NodePath *p_ptr) {
  76. ERR_FAIL_NULL(p_ptr);
  77. _GodotSharp::get_singleton()->queue_dispose(p_ptr);
  78. }
  79. MonoString *godot_icall_NodePath_operator_String(NodePath *p_np) {
  80. return GDMonoMarshal::mono_string_from_godot(p_np->operator String());
  81. }
  82. // -- RID --
  83. RID *godot_icall_RID_Ctor(Object *p_from) {
  84. Resource *res_from = Object::cast_to<Resource>(p_from);
  85. if (res_from)
  86. return memnew(RID(res_from->get_rid()));
  87. return memnew(RID);
  88. }
  89. void godot_icall_RID_Dtor(RID *p_ptr) {
  90. ERR_FAIL_NULL(p_ptr);
  91. _GodotSharp::get_singleton()->queue_dispose(p_ptr);
  92. }
  93. // -- String --
  94. MonoArray *godot_icall_String_md5_buffer(MonoString *p_str) {
  95. Vector<uint8_t> ret = GDMonoMarshal::mono_string_to_godot(p_str).md5_buffer();
  96. // TODO Check possible Array/Vector<uint8_t> problem?
  97. return GDMonoMarshal::Array_to_mono_array(Variant(ret));
  98. }
  99. MonoString *godot_icall_String_md5_text(MonoString *p_str) {
  100. String ret = GDMonoMarshal::mono_string_to_godot(p_str).md5_text();
  101. return GDMonoMarshal::mono_string_from_godot(ret);
  102. }
  103. int godot_icall_String_rfind(MonoString *p_str, MonoString *p_what, int p_from) {
  104. String what = GDMonoMarshal::mono_string_to_godot(p_what);
  105. return GDMonoMarshal::mono_string_to_godot(p_str).rfind(what, p_from);
  106. }
  107. int godot_icall_String_rfindn(MonoString *p_str, MonoString *p_what, int p_from) {
  108. String what = GDMonoMarshal::mono_string_to_godot(p_what);
  109. return GDMonoMarshal::mono_string_to_godot(p_str).rfindn(what, p_from);
  110. }
  111. MonoArray *godot_icall_String_sha256_buffer(MonoString *p_str) {
  112. Vector<uint8_t> ret = GDMonoMarshal::mono_string_to_godot(p_str).sha256_buffer();
  113. return GDMonoMarshal::Array_to_mono_array(Variant(ret));
  114. }
  115. MonoString *godot_icall_String_sha256_text(MonoString *p_str) {
  116. String ret = GDMonoMarshal::mono_string_to_godot(p_str).sha256_text();
  117. return GDMonoMarshal::mono_string_from_godot(ret);
  118. }
  119. // -- Global Scope --
  120. MonoObject *godot_icall_Godot_bytes2var(MonoArray *p_bytes) {
  121. Variant ret;
  122. PoolByteArray varr = GDMonoMarshal::mono_array_to_PoolByteArray(p_bytes);
  123. PoolByteArray::Read r = varr.read();
  124. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL);
  125. if (err != OK) {
  126. ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  127. }
  128. return GDMonoMarshal::variant_to_mono_object(ret);
  129. }
  130. MonoObject *godot_icall_Godot_convert(MonoObject *p_what, int p_type) {
  131. Variant what = GDMonoMarshal::mono_object_to_variant(p_what);
  132. const Variant *args[1] = { &what };
  133. Variant::CallError ce;
  134. Variant ret = Variant::construct(Variant::Type(p_type), args, 1, ce);
  135. ERR_FAIL_COND_V(ce.error != Variant::CallError::CALL_OK, NULL);
  136. return GDMonoMarshal::variant_to_mono_object(ret);
  137. }
  138. int godot_icall_Godot_hash(MonoObject *p_var) {
  139. return GDMonoMarshal::mono_object_to_variant(p_var).hash();
  140. }
  141. MonoObject *godot_icall_Godot_instance_from_id(int p_instance_id) {
  142. return GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(p_instance_id));
  143. }
  144. void godot_icall_Godot_print(MonoArray *p_what) {
  145. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  146. String str;
  147. for (int i = 0; i < what.size(); i++)
  148. str += what[i].operator String();
  149. print_line(str);
  150. }
  151. void godot_icall_Godot_printerr(MonoArray *p_what) {
  152. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  153. String str;
  154. for (int i = 0; i < what.size(); i++)
  155. str += what[i].operator String();
  156. OS::get_singleton()->printerr("%s\n", str.utf8().get_data());
  157. }
  158. void godot_icall_Godot_printraw(MonoArray *p_what) {
  159. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  160. String str;
  161. for (int i = 0; i < what.size(); i++)
  162. str += what[i].operator String();
  163. OS::get_singleton()->print("%s", str.utf8().get_data());
  164. }
  165. void godot_icall_Godot_prints(MonoArray *p_what) {
  166. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  167. String str;
  168. for (int i = 0; i < what.size(); i++) {
  169. if (i)
  170. str += " ";
  171. str += what[i].operator String();
  172. }
  173. print_line(str);
  174. }
  175. void godot_icall_Godot_printt(MonoArray *p_what) {
  176. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  177. String str;
  178. for (int i = 0; i < what.size(); i++) {
  179. if (i)
  180. str += "\t";
  181. str += what[i].operator String();
  182. }
  183. print_line(str);
  184. }
  185. void godot_icall_Godot_seed(int p_seed) {
  186. Math::seed(p_seed);
  187. }
  188. MonoString *godot_icall_Godot_str(MonoArray *p_what) {
  189. String str;
  190. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  191. for (int i = 0; i < what.size(); i++) {
  192. String os = what[i].operator String();
  193. if (i == 0)
  194. str = os;
  195. else
  196. str += os;
  197. }
  198. return GDMonoMarshal::mono_string_from_godot(str);
  199. }
  200. MonoObject *godot_icall_Godot_str2var(MonoString *p_str) {
  201. Variant ret;
  202. VariantParser::StreamString ss;
  203. ss.s = GDMonoMarshal::mono_string_to_godot(p_str);
  204. String errs;
  205. int line;
  206. Error err = VariantParser::parse(&ss, ret, errs, line);
  207. if (err != OK) {
  208. String err_str = "Parse error at line " + itos(line) + ": " + errs;
  209. ERR_PRINTS(err_str);
  210. ret = err_str;
  211. }
  212. return GDMonoMarshal::variant_to_mono_object(ret);
  213. }
  214. bool godot_icall_Godot_type_exists(MonoString *p_type) {
  215. return ClassDB::class_exists(GDMonoMarshal::mono_string_to_godot(p_type));
  216. }
  217. MonoArray *godot_icall_Godot_var2bytes(MonoObject *p_var) {
  218. Variant var = GDMonoMarshal::mono_object_to_variant(p_var);
  219. PoolByteArray barr;
  220. int len;
  221. Error err = encode_variant(var, NULL, len);
  222. ERR_EXPLAIN("Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).");
  223. ERR_FAIL_COND_V(err != OK, NULL);
  224. barr.resize(len);
  225. {
  226. PoolByteArray::Write w = barr.write();
  227. encode_variant(var, w.ptr(), len);
  228. }
  229. return GDMonoMarshal::PoolByteArray_to_mono_array(barr);
  230. }
  231. MonoString *godot_icall_Godot_var2str(MonoObject *p_var) {
  232. String vars;
  233. VariantWriter::write_to_string(GDMonoMarshal::mono_object_to_variant(p_var), vars);
  234. return GDMonoMarshal::mono_string_from_godot(vars);
  235. }
  236. MonoObject *godot_icall_Godot_weakref(Object *p_obj) {
  237. if (!p_obj)
  238. return NULL;
  239. Ref<WeakRef> wref;
  240. Reference *ref = Object::cast_to<Reference>(p_obj);
  241. if (ref) {
  242. REF r = ref;
  243. if (!r.is_valid())
  244. return NULL;
  245. wref.instance();
  246. wref->set_ref(r);
  247. } else {
  248. wref.instance();
  249. wref->set_obj(p_obj);
  250. }
  251. return GDMonoUtils::create_managed_for_godot_object(CACHED_CLASS(WeakRef), Reference::get_class_static(), Object::cast_to<Object>(wref.ptr()));
  252. }
  253. void godot_register_header_icalls() {
  254. godot_register_builtin_type_icalls();
  255. godot_register_collections_icalls();
  256. }