gd_glue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*************************************************************************/
  2. /* gd_glue.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. #ifdef MONO_GLUE_ENABLED
  31. #include "core/io/marshalls.h"
  32. #include "core/os/os.h"
  33. #include "core/string/ustring.h"
  34. #include "core/variant/array.h"
  35. #include "core/variant/variant.h"
  36. #include "core/variant/variant_parser.h"
  37. #include "../mono_gd/gd_mono_cache.h"
  38. #include "../mono_gd/gd_mono_marshal.h"
  39. #include "../mono_gd/gd_mono_utils.h"
  40. MonoObject *godot_icall_GD_bytes2var(MonoArray *p_bytes, MonoBoolean p_allow_objects) {
  41. Variant ret;
  42. PackedByteArray varr = GDMonoMarshal::mono_array_to_PackedByteArray(p_bytes);
  43. Error err = decode_variant(ret, varr.ptr(), varr.size(), nullptr, p_allow_objects);
  44. if (err != OK) {
  45. ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  46. }
  47. return GDMonoMarshal::variant_to_mono_object(ret);
  48. }
  49. MonoObject *godot_icall_GD_convert(MonoObject *p_what, int32_t p_type) {
  50. Variant what = GDMonoMarshal::mono_object_to_variant(p_what);
  51. const Variant *args[1] = { &what };
  52. Callable::CallError ce;
  53. Variant ret;
  54. Variant::construct(Variant::Type(p_type), ret, args, 1, ce);
  55. ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
  56. return GDMonoMarshal::variant_to_mono_object(ret);
  57. }
  58. int godot_icall_GD_hash(MonoObject *p_var) {
  59. return GDMonoMarshal::mono_object_to_variant(p_var).hash();
  60. }
  61. MonoObject *godot_icall_GD_instance_from_id(uint64_t p_instance_id) {
  62. return GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(ObjectID(p_instance_id)));
  63. }
  64. void godot_icall_GD_print(MonoArray *p_what) {
  65. String str;
  66. int length = mono_array_length(p_what);
  67. for (int i = 0; i < length; i++) {
  68. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  69. MonoException *exc = nullptr;
  70. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  71. if (exc) {
  72. GDMonoUtils::set_pending_exception(exc);
  73. return;
  74. }
  75. str += elem_str;
  76. }
  77. print_line(str);
  78. }
  79. void godot_icall_GD_printerr(MonoArray *p_what) {
  80. String str;
  81. int length = mono_array_length(p_what);
  82. for (int i = 0; i < length; i++) {
  83. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  84. MonoException *exc = nullptr;
  85. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  86. if (exc) {
  87. GDMonoUtils::set_pending_exception(exc);
  88. return;
  89. }
  90. str += elem_str;
  91. }
  92. print_error(str);
  93. }
  94. void godot_icall_GD_printraw(MonoArray *p_what) {
  95. String str;
  96. int length = mono_array_length(p_what);
  97. for (int i = 0; i < length; i++) {
  98. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  99. MonoException *exc = nullptr;
  100. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  101. if (exc) {
  102. GDMonoUtils::set_pending_exception(exc);
  103. return;
  104. }
  105. str += elem_str;
  106. }
  107. OS::get_singleton()->print("%s", str.utf8().get_data());
  108. }
  109. void godot_icall_GD_prints(MonoArray *p_what) {
  110. String str;
  111. int length = mono_array_length(p_what);
  112. for (int i = 0; i < length; i++) {
  113. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  114. MonoException *exc = nullptr;
  115. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  116. if (exc) {
  117. GDMonoUtils::set_pending_exception(exc);
  118. return;
  119. }
  120. if (i) {
  121. str += " ";
  122. }
  123. str += elem_str;
  124. }
  125. print_line(str);
  126. }
  127. void godot_icall_GD_printt(MonoArray *p_what) {
  128. String str;
  129. int length = mono_array_length(p_what);
  130. for (int i = 0; i < length; i++) {
  131. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  132. MonoException *exc = nullptr;
  133. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  134. if (exc) {
  135. GDMonoUtils::set_pending_exception(exc);
  136. return;
  137. }
  138. if (i) {
  139. str += "\t";
  140. }
  141. str += elem_str;
  142. }
  143. print_line(str);
  144. }
  145. float godot_icall_GD_randf() {
  146. return Math::randf();
  147. }
  148. uint32_t godot_icall_GD_randi() {
  149. return Math::rand();
  150. }
  151. void godot_icall_GD_randomize() {
  152. Math::randomize();
  153. }
  154. double godot_icall_GD_randf_range(double from, double to) {
  155. return Math::random(from, to);
  156. }
  157. int32_t godot_icall_GD_randi_range(int32_t from, int32_t to) {
  158. return Math::random(from, to);
  159. }
  160. uint32_t godot_icall_GD_rand_seed(uint64_t seed, uint64_t *newSeed) {
  161. uint32_t ret = Math::rand_from_seed(&seed);
  162. *newSeed = seed;
  163. return ret;
  164. }
  165. void godot_icall_GD_seed(uint64_t p_seed) {
  166. Math::seed(p_seed);
  167. }
  168. MonoString *godot_icall_GD_str(MonoArray *p_what) {
  169. String str;
  170. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  171. for (int i = 0; i < what.size(); i++) {
  172. String os = what[i].operator String();
  173. if (i == 0) {
  174. str = os;
  175. } else {
  176. str += os;
  177. }
  178. }
  179. return GDMonoMarshal::mono_string_from_godot(str);
  180. }
  181. MonoObject *godot_icall_GD_str2var(MonoString *p_str) {
  182. Variant ret;
  183. VariantParser::StreamString ss;
  184. ss.s = GDMonoMarshal::mono_string_to_godot(p_str);
  185. String errs;
  186. int line;
  187. Error err = VariantParser::parse(&ss, ret, errs, line);
  188. if (err != OK) {
  189. String err_str = "Parse error at line " + itos(line) + ": " + errs + ".";
  190. ERR_PRINT(err_str);
  191. ret = err_str;
  192. }
  193. return GDMonoMarshal::variant_to_mono_object(ret);
  194. }
  195. MonoBoolean godot_icall_GD_type_exists(StringName *p_type) {
  196. StringName type = p_type ? *p_type : StringName();
  197. return ClassDB::class_exists(type);
  198. }
  199. void godot_icall_GD_pusherror(MonoString *p_str) {
  200. ERR_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
  201. }
  202. void godot_icall_GD_pushwarning(MonoString *p_str) {
  203. WARN_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
  204. }
  205. MonoArray *godot_icall_GD_var2bytes(MonoObject *p_var, MonoBoolean p_full_objects) {
  206. Variant var = GDMonoMarshal::mono_object_to_variant(p_var);
  207. PackedByteArray barr;
  208. int len;
  209. Error err = encode_variant(var, nullptr, len, p_full_objects);
  210. ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).");
  211. barr.resize(len);
  212. encode_variant(var, barr.ptrw(), len, p_full_objects);
  213. return GDMonoMarshal::PackedByteArray_to_mono_array(barr);
  214. }
  215. MonoString *godot_icall_GD_var2str(MonoObject *p_var) {
  216. String vars;
  217. VariantWriter::write_to_string(GDMonoMarshal::mono_object_to_variant(p_var), vars);
  218. return GDMonoMarshal::mono_string_from_godot(vars);
  219. }
  220. uint32_t godot_icall_TypeToVariantType(MonoReflectionType *p_refl_type) {
  221. return (uint32_t)GDMonoMarshal::managed_to_variant_type(ManagedType::from_reftype(p_refl_type));
  222. }
  223. MonoObject *godot_icall_DefaultGodotTaskScheduler() {
  224. return GDMonoCache::cached_data.task_scheduler_handle->get_target();
  225. }
  226. void godot_register_gd_icalls() {
  227. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_bytes2var", godot_icall_GD_bytes2var);
  228. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_convert", godot_icall_GD_convert);
  229. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_hash", godot_icall_GD_hash);
  230. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_instance_from_id", godot_icall_GD_instance_from_id);
  231. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pusherror", godot_icall_GD_pusherror);
  232. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pushwarning", godot_icall_GD_pushwarning);
  233. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print", godot_icall_GD_print);
  234. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printerr", godot_icall_GD_printerr);
  235. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printraw", godot_icall_GD_printraw);
  236. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_prints", godot_icall_GD_prints);
  237. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printt", godot_icall_GD_printt);
  238. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf", godot_icall_GD_randf);
  239. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randi", godot_icall_GD_randi);
  240. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randomize", godot_icall_GD_randomize);
  241. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf_range", godot_icall_GD_randf_range);
  242. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randi_range", godot_icall_GD_randi_range);
  243. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_rand_seed", godot_icall_GD_rand_seed);
  244. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_seed", godot_icall_GD_seed);
  245. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str", godot_icall_GD_str);
  246. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str2var", godot_icall_GD_str2var);
  247. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_type_exists", godot_icall_GD_type_exists);
  248. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2bytes", godot_icall_GD_var2bytes);
  249. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2str", godot_icall_GD_var2str);
  250. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_TypeToVariantType", godot_icall_TypeToVariantType);
  251. // Dispatcher
  252. GDMonoUtils::add_internal_call("Godot.Dispatcher::godot_icall_DefaultGodotTaskScheduler", godot_icall_DefaultGodotTaskScheduler);
  253. }
  254. #endif // MONO_GLUE_ENABLED