gd_glue.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* gd_glue.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_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_utils.h"
  39. MonoObject *godot_icall_GD_bytes2var(MonoArray *p_bytes) {
  40. Variant ret;
  41. PoolByteArray varr = GDMonoMarshal::mono_array_to_PoolByteArray(p_bytes);
  42. PoolByteArray::Read r = varr.read();
  43. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL);
  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, int p_type) {
  50. Variant what = GDMonoMarshal::mono_object_to_variant(p_what);
  51. const Variant *args[1] = { &what };
  52. Variant::CallError ce;
  53. Variant ret = Variant::construct(Variant::Type(p_type), args, 1, ce);
  54. ERR_FAIL_COND_V(ce.error != Variant::CallError::CALL_OK, NULL);
  55. return GDMonoMarshal::variant_to_mono_object(ret);
  56. }
  57. int godot_icall_GD_hash(MonoObject *p_var) {
  58. return GDMonoMarshal::mono_object_to_variant(p_var).hash();
  59. }
  60. MonoObject *godot_icall_GD_instance_from_id(int p_instance_id) {
  61. return GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(p_instance_id));
  62. }
  63. void godot_icall_GD_print(MonoArray *p_what) {
  64. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  65. String str;
  66. for (int i = 0; i < what.size(); i++)
  67. str += what[i].operator String();
  68. print_line(str);
  69. }
  70. void godot_icall_GD_printerr(MonoArray *p_what) {
  71. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  72. String str;
  73. for (int i = 0; i < what.size(); i++)
  74. str += what[i].operator String();
  75. OS::get_singleton()->printerr("%s\n", str.utf8().get_data());
  76. }
  77. void godot_icall_GD_printraw(MonoArray *p_what) {
  78. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  79. String str;
  80. for (int i = 0; i < what.size(); i++)
  81. str += what[i].operator String();
  82. OS::get_singleton()->print("%s", str.utf8().get_data());
  83. }
  84. void godot_icall_GD_prints(MonoArray *p_what) {
  85. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  86. String str;
  87. for (int i = 0; i < what.size(); i++) {
  88. if (i)
  89. str += " ";
  90. str += what[i].operator String();
  91. }
  92. print_line(str);
  93. }
  94. void godot_icall_GD_printt(MonoArray *p_what) {
  95. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  96. String str;
  97. for (int i = 0; i < what.size(); i++) {
  98. if (i)
  99. str += "\t";
  100. str += what[i].operator String();
  101. }
  102. print_line(str);
  103. }
  104. void godot_icall_GD_seed(int p_seed) {
  105. Math::seed(p_seed);
  106. }
  107. MonoString *godot_icall_GD_str(MonoArray *p_what) {
  108. String str;
  109. Array what = GDMonoMarshal::mono_array_to_Array(p_what);
  110. for (int i = 0; i < what.size(); i++) {
  111. String os = what[i].operator String();
  112. if (i == 0)
  113. str = os;
  114. else
  115. str += os;
  116. }
  117. return GDMonoMarshal::mono_string_from_godot(str);
  118. }
  119. MonoObject *godot_icall_GD_str2var(MonoString *p_str) {
  120. Variant ret;
  121. VariantParser::StreamString ss;
  122. ss.s = GDMonoMarshal::mono_string_to_godot(p_str);
  123. String errs;
  124. int line;
  125. Error err = VariantParser::parse(&ss, ret, errs, line);
  126. if (err != OK) {
  127. String err_str = "Parse error at line " + itos(line) + ": " + errs;
  128. ERR_PRINTS(err_str);
  129. ret = err_str;
  130. }
  131. return GDMonoMarshal::variant_to_mono_object(ret);
  132. }
  133. bool godot_icall_GD_type_exists(MonoString *p_type) {
  134. return ClassDB::class_exists(GDMonoMarshal::mono_string_to_godot(p_type));
  135. }
  136. void godot_icall_GD_pusherror(MonoString *p_str) {
  137. ERR_PRINTS(GDMonoMarshal::mono_string_to_godot(p_str));
  138. }
  139. void godot_icall_GD_pushwarning(MonoString *p_str) {
  140. WARN_PRINTS(GDMonoMarshal::mono_string_to_godot(p_str));
  141. }
  142. MonoArray *godot_icall_GD_var2bytes(MonoObject *p_var) {
  143. Variant var = GDMonoMarshal::mono_object_to_variant(p_var);
  144. PoolByteArray barr;
  145. int len;
  146. Error err = encode_variant(var, NULL, len);
  147. ERR_EXPLAIN("Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).");
  148. ERR_FAIL_COND_V(err != OK, NULL);
  149. barr.resize(len);
  150. {
  151. PoolByteArray::Write w = barr.write();
  152. encode_variant(var, w.ptr(), len);
  153. }
  154. return GDMonoMarshal::PoolByteArray_to_mono_array(barr);
  155. }
  156. MonoString *godot_icall_GD_var2str(MonoObject *p_var) {
  157. String vars;
  158. VariantWriter::write_to_string(GDMonoMarshal::mono_object_to_variant(p_var), vars);
  159. return GDMonoMarshal::mono_string_from_godot(vars);
  160. }
  161. void godot_register_gd_icalls() {
  162. mono_add_internal_call("Godot.GD::godot_icall_GD_bytes2var", (void *)godot_icall_GD_bytes2var);
  163. mono_add_internal_call("Godot.GD::godot_icall_GD_convert", (void *)godot_icall_GD_convert);
  164. mono_add_internal_call("Godot.GD::godot_icall_GD_hash", (void *)godot_icall_GD_hash);
  165. mono_add_internal_call("Godot.GD::godot_icall_GD_instance_from_id", (void *)godot_icall_GD_instance_from_id);
  166. mono_add_internal_call("Godot.GD::godot_icall_GD_pusherror", (void *)godot_icall_GD_pusherror);
  167. mono_add_internal_call("Godot.GD::godot_icall_GD_pushwarning", (void *)godot_icall_GD_pushwarning);
  168. mono_add_internal_call("Godot.GD::godot_icall_GD_print", (void *)godot_icall_GD_print);
  169. mono_add_internal_call("Godot.GD::godot_icall_GD_printerr", (void *)godot_icall_GD_printerr);
  170. mono_add_internal_call("Godot.GD::godot_icall_GD_printraw", (void *)godot_icall_GD_printraw);
  171. mono_add_internal_call("Godot.GD::godot_icall_GD_prints", (void *)godot_icall_GD_prints);
  172. mono_add_internal_call("Godot.GD::godot_icall_GD_printt", (void *)godot_icall_GD_printt);
  173. mono_add_internal_call("Godot.GD::godot_icall_GD_seed", (void *)godot_icall_GD_seed);
  174. mono_add_internal_call("Godot.GD::godot_icall_GD_str", (void *)godot_icall_GD_str);
  175. mono_add_internal_call("Godot.GD::godot_icall_GD_str2var", (void *)godot_icall_GD_str2var);
  176. mono_add_internal_call("Godot.GD::godot_icall_GD_type_exists", (void *)godot_icall_GD_type_exists);
  177. mono_add_internal_call("Godot.GD::godot_icall_GD_var2bytes", (void *)godot_icall_GD_var2bytes);
  178. mono_add_internal_call("Godot.GD::godot_icall_GD_var2str", (void *)godot_icall_GD_var2str);
  179. }
  180. #endif // MONO_GLUE_ENABLED