gd_glue.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**************************************************************************/
  2. /* gd_glue.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 "gd_glue.h"
  31. #ifdef MONO_GLUE_ENABLED
  32. #include "core/array.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/os/os.h"
  35. #include "core/ustring.h"
  36. #include "core/variant.h"
  37. #include "core/variant_parser.h"
  38. #include "../mono_gd/gd_mono_cache.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. PoolByteArray varr = GDMonoMarshal::mono_array_to_PoolByteArray(p_bytes);
  43. PoolByteArray::Read r = varr.read();
  44. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL, p_allow_objects);
  45. if (err != OK) {
  46. ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  47. }
  48. return GDMonoMarshal::variant_to_mono_object(ret);
  49. }
  50. MonoObject *godot_icall_GD_convert(MonoObject *p_what, int32_t p_type) {
  51. Variant what = GDMonoMarshal::mono_object_to_variant(p_what);
  52. const Variant *args[1] = { &what };
  53. Variant::CallError ce;
  54. Variant ret = Variant::construct(Variant::Type(p_type), args, 1, ce);
  55. ERR_FAIL_COND_V(ce.error != Variant::CallError::CALL_OK, NULL);
  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(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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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. str += elem_str;
  123. }
  124. print_line(str);
  125. }
  126. void godot_icall_GD_printt(MonoArray *p_what) {
  127. String str;
  128. int length = mono_array_length(p_what);
  129. for (int i = 0; i < length; i++) {
  130. MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
  131. MonoException *exc = NULL;
  132. String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
  133. if (exc) {
  134. GDMonoUtils::set_pending_exception(exc);
  135. return;
  136. }
  137. if (i)
  138. str += "\t";
  139. str += elem_str;
  140. }
  141. print_line(str);
  142. }
  143. float godot_icall_GD_randf() {
  144. return Math::randf();
  145. }
  146. uint32_t godot_icall_GD_randi() {
  147. return Math::rand();
  148. }
  149. void godot_icall_GD_randomize() {
  150. Math::randomize();
  151. }
  152. double godot_icall_GD_rand_range(double from, double to) {
  153. return Math::random(from, to);
  154. }
  155. uint32_t godot_icall_GD_rand_seed(uint64_t seed, uint64_t *newSeed) {
  156. int ret = Math::rand_from_seed(&seed);
  157. *newSeed = seed;
  158. return ret;
  159. }
  160. void godot_icall_GD_seed(uint64_t p_seed) {
  161. Math::seed(p_seed);
  162. }
  163. MonoString *godot_icall_GD_str(MonoArray *p_what) {
  164. String str;
  165. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  166. for (int i = 0; i < what.size(); i++) {
  167. String os = what[i].operator String();
  168. if (i == 0)
  169. str = os;
  170. else
  171. str += os;
  172. }
  173. return GDMonoMarshal::mono_string_from_godot(str);
  174. }
  175. MonoObject *godot_icall_GD_str2var(MonoString *p_str) {
  176. Variant ret;
  177. VariantParser::StreamString ss;
  178. ss.s = GDMonoMarshal::mono_string_to_godot(p_str);
  179. String errs;
  180. int line;
  181. Error err = VariantParser::parse(&ss, ret, errs, line);
  182. if (err != OK) {
  183. String err_str = "Parse error at line " + itos(line) + ": " + errs + ".";
  184. ERR_PRINT(err_str);
  185. ret = err_str;
  186. }
  187. return GDMonoMarshal::variant_to_mono_object(ret);
  188. }
  189. MonoBoolean godot_icall_GD_type_exists(MonoString *p_type) {
  190. return ClassDB::class_exists(GDMonoMarshal::mono_string_to_godot(p_type));
  191. }
  192. void godot_icall_GD_pusherror(MonoString *p_str) {
  193. ERR_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
  194. }
  195. void godot_icall_GD_pushwarning(MonoString *p_str) {
  196. WARN_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
  197. }
  198. MonoArray *godot_icall_GD_var2bytes(MonoObject *p_var, MonoBoolean p_full_objects) {
  199. Variant var = GDMonoMarshal::mono_object_to_variant(p_var);
  200. PoolByteArray barr;
  201. int len;
  202. Error err = encode_variant(var, NULL, len, p_full_objects);
  203. ERR_FAIL_COND_V_MSG(err != OK, NULL, "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).");
  204. barr.resize(len);
  205. {
  206. PoolByteArray::Write w = barr.write();
  207. encode_variant(var, w.ptr(), len, p_full_objects);
  208. }
  209. return GDMonoMarshal::PoolByteArray_to_mono_array(barr);
  210. }
  211. MonoString *godot_icall_GD_var2str(MonoObject *p_var) {
  212. String vars;
  213. VariantWriter::write_to_string(GDMonoMarshal::mono_object_to_variant(p_var), vars);
  214. return GDMonoMarshal::mono_string_from_godot(vars);
  215. }
  216. MonoObject *godot_icall_DefaultGodotTaskScheduler() {
  217. return GDMonoCache::cached_data.task_scheduler_handle->get_target();
  218. }
  219. void godot_register_gd_icalls() {
  220. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_bytes2var", godot_icall_GD_bytes2var);
  221. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_convert", godot_icall_GD_convert);
  222. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_hash", godot_icall_GD_hash);
  223. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_instance_from_id", godot_icall_GD_instance_from_id);
  224. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pusherror", godot_icall_GD_pusherror);
  225. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pushwarning", godot_icall_GD_pushwarning);
  226. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print", godot_icall_GD_print);
  227. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printerr", godot_icall_GD_printerr);
  228. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printraw", godot_icall_GD_printraw);
  229. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_prints", godot_icall_GD_prints);
  230. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printt", godot_icall_GD_printt);
  231. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf", godot_icall_GD_randf);
  232. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randi", godot_icall_GD_randi);
  233. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randomize", godot_icall_GD_randomize);
  234. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_rand_range", godot_icall_GD_rand_range);
  235. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_rand_seed", godot_icall_GD_rand_seed);
  236. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_seed", godot_icall_GD_seed);
  237. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str", godot_icall_GD_str);
  238. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str2var", godot_icall_GD_str2var);
  239. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_type_exists", godot_icall_GD_type_exists);
  240. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2bytes", godot_icall_GD_var2bytes);
  241. GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2str", godot_icall_GD_var2str);
  242. // Dispatcher
  243. GDMonoUtils::add_internal_call("Godot.Dispatcher::godot_icall_DefaultGodotTaskScheduler", godot_icall_DefaultGodotTaskScheduler);
  244. }
  245. #endif // MONO_GLUE_ENABLED