2018-09-04 05:40:41 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_glue.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-09-04 05:40:41 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#ifdef MONO_GLUE_ENABLED
|
|
|
|
|
|
|
|
#include "core/io/marshalls.h"
|
|
|
|
#include "core/os/os.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/ustring.h"
|
|
|
|
#include "core/variant/array.h"
|
|
|
|
#include "core/variant/variant.h"
|
|
|
|
#include "core/variant/variant_parser.h"
|
2018-09-04 05:40:41 +02:00
|
|
|
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "../mono_gd/gd_mono_cache.h"
|
2020-07-05 19:19:36 +02:00
|
|
|
#include "../mono_gd/gd_mono_marshal.h"
|
2018-09-04 05:40:41 +02:00
|
|
|
#include "../mono_gd/gd_mono_utils.h"
|
|
|
|
|
2019-03-26 16:52:42 +01:00
|
|
|
MonoObject *godot_icall_GD_bytes2var(MonoArray *p_bytes, MonoBoolean p_allow_objects) {
|
2018-09-04 05:40:41 +02:00
|
|
|
Variant ret;
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray varr = GDMonoMarshal::mono_array_to_PackedByteArray(p_bytes);
|
2020-04-02 01:20:12 +02:00
|
|
|
Error err = decode_variant(ret, varr.ptr(), varr.size(), nullptr, p_allow_objects);
|
2018-09-04 05:40:41 +02:00
|
|
|
if (err != OK) {
|
|
|
|
ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
|
|
|
|
}
|
|
|
|
return GDMonoMarshal::variant_to_mono_object(ret);
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:35:19 +01:00
|
|
|
MonoObject *godot_icall_GD_convert(MonoObject *p_what, int32_t p_type) {
|
2018-09-04 05:40:41 +02:00
|
|
|
Variant what = GDMonoMarshal::mono_object_to_variant(p_what);
|
|
|
|
const Variant *args[1] = { &what };
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::CallError ce;
|
2020-11-10 11:13:54 +01:00
|
|
|
Variant ret;
|
|
|
|
Variant::construct(Variant::Type(p_type), ret, args, 1, ce);
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
|
2018-09-04 05:40:41 +02:00
|
|
|
return GDMonoMarshal::variant_to_mono_object(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int godot_icall_GD_hash(MonoObject *p_var) {
|
|
|
|
return GDMonoMarshal::mono_object_to_variant(p_var).hash();
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:35:19 +01:00
|
|
|
MonoObject *godot_icall_GD_instance_from_id(uint64_t p_instance_id) {
|
2020-02-18 11:02:46 +01:00
|
|
|
return GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(ObjectID(p_instance_id)));
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void godot_icall_GD_print(MonoArray *p_what) {
|
|
|
|
String str;
|
2020-01-02 13:54:16 +01:00
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-01-02 13:54:16 +01:00
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += elem_str;
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
print_line(str);
|
|
|
|
}
|
|
|
|
|
2022-05-03 01:29:38 +02:00
|
|
|
void godot_icall_GD_print_rich(MonoArray *p_what) {
|
|
|
|
String str;
|
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
|
|
|
MonoException *exc = nullptr;
|
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += elem_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_line_rich(str);
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
void godot_icall_GD_printerr(MonoArray *p_what) {
|
|
|
|
String str;
|
2020-01-02 13:54:16 +01:00
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-01-02 13:54:16 +01:00
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += elem_str;
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:13:22 +01:00
|
|
|
print_error(str);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void godot_icall_GD_printraw(MonoArray *p_what) {
|
|
|
|
String str;
|
2020-01-02 13:54:16 +01:00
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-01-02 13:54:16 +01:00
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += elem_str;
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
OS::get_singleton()->print("%s", str.utf8().get_data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void godot_icall_GD_prints(MonoArray *p_what) {
|
|
|
|
String str;
|
2020-01-02 13:54:16 +01:00
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-01-02 13:54:16 +01:00
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (i) {
|
2018-09-04 05:40:41 +02:00
|
|
|
str += " ";
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-01-02 13:54:16 +01:00
|
|
|
|
|
|
|
str += elem_str;
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
2020-01-02 13:54:16 +01:00
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
print_line(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void godot_icall_GD_printt(MonoArray *p_what) {
|
|
|
|
String str;
|
2020-01-02 13:54:16 +01:00
|
|
|
int length = mono_array_length(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2020-01-02 13:54:16 +01:00
|
|
|
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (i) {
|
2018-09-04 05:40:41 +02:00
|
|
|
str += "\t";
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-01-02 13:54:16 +01:00
|
|
|
|
|
|
|
str += elem_str;
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
2020-01-02 13:54:16 +01:00
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
print_line(str);
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:58:31 +02:00
|
|
|
void godot_icall_GD_randomize() {
|
|
|
|
Math::randomize();
|
2019-02-19 00:35:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t godot_icall_GD_randi() {
|
|
|
|
return Math::rand();
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:58:31 +02:00
|
|
|
float godot_icall_GD_randf() {
|
|
|
|
return Math::randf();
|
2019-02-19 00:35:19 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:58:31 +02:00
|
|
|
int32_t godot_icall_GD_randi_range(int32_t from, int32_t to) {
|
2020-07-26 12:52:24 +02:00
|
|
|
return Math::random(from, to);
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:58:31 +02:00
|
|
|
double godot_icall_GD_randf_range(double from, double to) {
|
2019-02-19 00:35:19 +01:00
|
|
|
return Math::random(from, to);
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:58:31 +02:00
|
|
|
double godot_icall_GD_randfn(double mean, double deviation) {
|
|
|
|
return Math::randfn(mean, deviation);
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:35:19 +01:00
|
|
|
uint32_t godot_icall_GD_rand_seed(uint64_t seed, uint64_t *newSeed) {
|
2020-07-05 19:19:36 +02:00
|
|
|
uint32_t ret = Math::rand_from_seed(&seed);
|
2019-02-19 00:35:19 +01:00
|
|
|
*newSeed = seed;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void godot_icall_GD_seed(uint64_t p_seed) {
|
2018-09-04 05:40:41 +02:00
|
|
|
Math::seed(p_seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoString *godot_icall_GD_str(MonoArray *p_what) {
|
|
|
|
String str;
|
|
|
|
Array what = GDMonoMarshal::mono_array_to_Array(p_what);
|
|
|
|
|
|
|
|
for (int i = 0; i < what.size(); i++) {
|
|
|
|
String os = what[i].operator String();
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (i == 0) {
|
2018-09-04 05:40:41 +02:00
|
|
|
str = os;
|
2020-07-05 19:19:36 +02:00
|
|
|
} else {
|
2018-09-04 05:40:41 +02:00
|
|
|
str += os;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return GDMonoMarshal::mono_string_from_godot(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *godot_icall_GD_str2var(MonoString *p_str) {
|
|
|
|
Variant ret;
|
|
|
|
|
|
|
|
VariantParser::StreamString ss;
|
|
|
|
ss.s = GDMonoMarshal::mono_string_to_godot(p_str);
|
|
|
|
|
|
|
|
String errs;
|
|
|
|
int line;
|
|
|
|
Error err = VariantParser::parse(&ss, ret, errs, line);
|
|
|
|
if (err != OK) {
|
2019-08-09 03:39:45 +02:00
|
|
|
String err_str = "Parse error at line " + itos(line) + ": " + errs + ".";
|
2019-11-06 17:03:04 +01:00
|
|
|
ERR_PRINT(err_str);
|
2018-09-04 05:40:41 +02:00
|
|
|
ret = err_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GDMonoMarshal::variant_to_mono_object(ret);
|
|
|
|
}
|
|
|
|
|
2020-03-14 19:20:17 +01:00
|
|
|
MonoBoolean godot_icall_GD_type_exists(StringName *p_type) {
|
|
|
|
StringName type = p_type ? *p_type : StringName();
|
|
|
|
return ClassDB::class_exists(type);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 19:30:01 +01:00
|
|
|
void godot_icall_GD_pusherror(MonoString *p_str) {
|
2019-11-06 17:03:04 +01:00
|
|
|
ERR_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
|
2018-10-26 05:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 19:30:01 +01:00
|
|
|
void godot_icall_GD_pushwarning(MonoString *p_str) {
|
2019-11-07 09:44:15 +01:00
|
|
|
WARN_PRINT(GDMonoMarshal::mono_string_to_godot(p_str));
|
2018-10-26 05:32:50 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 16:52:42 +01:00
|
|
|
MonoArray *godot_icall_GD_var2bytes(MonoObject *p_var, MonoBoolean p_full_objects) {
|
2018-09-04 05:40:41 +02:00
|
|
|
Variant var = GDMonoMarshal::mono_object_to_variant(p_var);
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray barr;
|
2018-09-04 05:40:41 +02:00
|
|
|
int len;
|
2020-04-02 01:20:12 +02:00
|
|
|
Error err = encode_variant(var, nullptr, len, p_full_objects);
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).");
|
2018-09-04 05:40:41 +02:00
|
|
|
|
|
|
|
barr.resize(len);
|
2020-02-18 11:02:46 +01:00
|
|
|
encode_variant(var, barr.ptrw(), len, p_full_objects);
|
2018-09-04 05:40:41 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
return GDMonoMarshal::PackedByteArray_to_mono_array(barr);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MonoString *godot_icall_GD_var2str(MonoObject *p_var) {
|
|
|
|
String vars;
|
|
|
|
VariantWriter::write_to_string(GDMonoMarshal::mono_object_to_variant(p_var), vars);
|
|
|
|
return GDMonoMarshal::mono_string_from_godot(vars);
|
|
|
|
}
|
|
|
|
|
2020-03-14 19:20:17 +01:00
|
|
|
uint32_t godot_icall_TypeToVariantType(MonoReflectionType *p_refl_type) {
|
|
|
|
return (uint32_t)GDMonoMarshal::managed_to_variant_type(ManagedType::from_reftype(p_refl_type));
|
|
|
|
}
|
|
|
|
|
2019-07-18 04:08:24 +02:00
|
|
|
MonoObject *godot_icall_DefaultGodotTaskScheduler() {
|
2019-11-10 17:10:38 +01:00
|
|
|
return GDMonoCache::cached_data.task_scheduler_handle->get_target();
|
2019-07-18 04:08:24 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
void godot_register_gd_icalls() {
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_bytes2var", godot_icall_GD_bytes2var);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_convert", godot_icall_GD_convert);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_hash", godot_icall_GD_hash);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_instance_from_id", godot_icall_GD_instance_from_id);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pusherror", godot_icall_GD_pusherror);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_pushwarning", godot_icall_GD_pushwarning);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print", godot_icall_GD_print);
|
2022-05-03 01:29:38 +02:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print_rich", godot_icall_GD_print_rich);
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printerr", godot_icall_GD_printerr);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printraw", godot_icall_GD_printraw);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_prints", godot_icall_GD_prints);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printt", godot_icall_GD_printt);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randomize", godot_icall_GD_randomize);
|
2021-06-21 11:58:31 +02:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randi", godot_icall_GD_randi);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf", godot_icall_GD_randf);
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randi_range", godot_icall_GD_randi_range);
|
2021-06-21 11:58:31 +02:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf_range", godot_icall_GD_randf_range);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randfn", godot_icall_GD_randfn);
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_rand_seed", godot_icall_GD_rand_seed);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_seed", godot_icall_GD_seed);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str", godot_icall_GD_str);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_str2var", godot_icall_GD_str2var);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_type_exists", godot_icall_GD_type_exists);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2bytes", godot_icall_GD_var2bytes);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_var2str", godot_icall_GD_var2str);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_TypeToVariantType", godot_icall_TypeToVariantType);
|
2019-07-18 04:08:24 +02:00
|
|
|
|
|
|
|
// Dispatcher
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Dispatcher::godot_icall_DefaultGodotTaskScheduler", godot_icall_DefaultGodotTaskScheduler);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MONO_GLUE_ENABLED
|