| 1 | /**************************************************************************/ |
| 2 | /* gdscript_utility_functions.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 | |
| 31 | #include "gdscript_utility_functions.h" |
| 32 | |
| 33 | #include "gdscript.h" |
| 34 | |
| 35 | #include "core/io/resource_loader.h" |
| 36 | #include "core/object/class_db.h" |
| 37 | #include "core/object/object.h" |
| 38 | #include "core/templates/a_hash_map.h" |
| 39 | #include "core/templates/vector.h" |
| 40 | #include "core/variant/typed_array.h" |
| 41 | |
| 42 | #ifdef DEBUG_ENABLED |
| 43 | |
| 44 | #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count) \ |
| 45 | if (unlikely(p_arg_count < m_min_count)) { \ |
| 46 | *r_ret = Variant(); \ |
| 47 | r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \ |
| 48 | r_error.expected = m_min_count; \ |
| 49 | return; \ |
| 50 | } \ |
| 51 | if (unlikely(p_arg_count > m_max_count)) { \ |
| 52 | *r_ret = Variant(); \ |
| 53 | r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \ |
| 54 | r_error.expected = m_max_count; \ |
| 55 | return; \ |
| 56 | } |
| 57 | |
| 58 | #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type) \ |
| 59 | if (unlikely(!Variant::can_convert_strict(p_args[m_arg]->get_type(), m_type))) { \ |
| 60 | *r_ret = Variant(); \ |
| 61 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ |
| 62 | r_error.argument = m_arg; \ |
| 63 | r_error.expected = m_type; \ |
| 64 | return; \ |
| 65 | } |
| 66 | |
| 67 | #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \ |
| 68 | if (unlikely(m_cond)) { \ |
| 69 | *r_ret = m_msg; \ |
| 70 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ |
| 71 | r_error.argument = m_arg; \ |
| 72 | r_error.expected = m_type; \ |
| 73 | return; \ |
| 74 | } |
| 75 | |
| 76 | #else // !DEBUG_ENABLED |
| 77 | |
| 78 | #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count) |
| 79 | #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type) |
| 80 | #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) |
| 81 | |
| 82 | #endif // DEBUG_ENABLED |
| 83 | |
| 84 | #define VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \ |
| 85 | if (unlikely(m_cond)) { \ |
| 86 | *r_ret = m_msg; \ |
| 87 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ |
| 88 | r_error.argument = m_arg; \ |
| 89 | r_error.expected = m_type; \ |
| 90 | return; \ |
| 91 | } |
| 92 | |
| 93 | #define GDFUNC_FAIL_COND_MSG(m_cond, m_msg) \ |
| 94 | if (unlikely(m_cond)) { \ |
| 95 | *r_ret = m_msg; \ |
| 96 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; \ |
| 97 | return; \ |
| 98 | } |
| 99 | |
| 100 | struct GDScriptUtilityFunctionsDefinitions { |
| 101 | #ifndef DISABLE_DEPRECATED |
| 102 | static inline void convert(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 103 | DEBUG_VALIDATE_ARG_COUNT(2, 2); |
| 104 | DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT); |
| 105 | |
| 106 | int type = *p_args[1]; |
| 107 | DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::INT, type < 0 || type >= Variant::VARIANT_MAX, |
| 108 | RTR("Invalid type argument to convert(), use TYPE_* constants.")); |
| 109 | |
| 110 | Variant::construct(Variant::Type(type), *r_ret, p_args, 1, r_error); |
| 111 | } |
| 112 | #endif // DISABLE_DEPRECATED |
| 113 | |
| 114 | static inline void type_exists(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 115 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 116 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING_NAME); |
| 117 | *r_ret = ClassDB::class_exists(*p_args[0]); |
| 118 | } |
| 119 | |
| 120 | static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 121 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 122 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT); |
| 123 | const int64_t code = *p_args[0]; |
| 124 | VALIDATE_ARG_CUSTOM(0, Variant::INT, code < 0 || code > UINT32_MAX, RTR("Expected an integer between 0 and 2^32 - 1.")); |
| 125 | *r_ret = String::chr(code); |
| 126 | } |
| 127 | |
| 128 | static inline void ord(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 129 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 130 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING); |
| 131 | const String string = *p_args[0]; |
| 132 | VALIDATE_ARG_CUSTOM(0, Variant::STRING, string.length() != 1, RTR("Expected a string of length 1 (a character).")); |
| 133 | *r_ret = string.get(0); |
| 134 | } |
| 135 | |
| 136 | static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 137 | DEBUG_VALIDATE_ARG_COUNT(1, 3); |
| 138 | switch (p_arg_count) { |
| 139 | case 1: { |
| 140 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT); |
| 141 | |
| 142 | int64_t count = *p_args[0]; |
| 143 | |
| 144 | Array arr; |
| 145 | if (count <= 0) { |
| 146 | *r_ret = arr; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | GDFUNC_FAIL_COND_MSG(count > INT32_MAX, RTR("Range too big.")); |
| 151 | Error err = arr.resize(count); |
| 152 | GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array.")); |
| 153 | |
| 154 | for (int64_t i = 0; i < count; i++) { |
| 155 | arr[i] = i; |
| 156 | } |
| 157 | |
| 158 | *r_ret = arr; |
| 159 | } break; |
| 160 | case 2: { |
| 161 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT); |
| 162 | DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT); |
| 163 | |
| 164 | int64_t from = *p_args[0]; |
| 165 | int64_t to = *p_args[1]; |
| 166 | |
| 167 | Array arr; |
| 168 | if (from >= to) { |
| 169 | *r_ret = arr; |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | GDFUNC_FAIL_COND_MSG(to - from > INT32_MAX, RTR("Range too big.")); |
| 174 | Error err = arr.resize(to - from); |
| 175 | GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array.")); |
| 176 | |
| 177 | for (int64_t i = from; i < to; i++) { |
| 178 | arr[i - from] = i; |
| 179 | } |
| 180 | |
| 181 | *r_ret = arr; |
| 182 | } break; |
| 183 | case 3: { |
| 184 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT); |
| 185 | DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT); |
| 186 | DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT); |
| 187 | |
| 188 | int64_t from = *p_args[0]; |
| 189 | int64_t to = *p_args[1]; |
| 190 | int64_t incr = *p_args[2]; |
| 191 | |
| 192 | VALIDATE_ARG_CUSTOM(2, Variant::INT, incr == 0, RTR("Step argument is zero!")); |
| 193 | |
| 194 | Array arr; |
| 195 | if (from >= to && incr > 0) { |
| 196 | *r_ret = arr; |
| 197 | return; |
| 198 | } |
| 199 | if (from <= to && incr < 0) { |
| 200 | *r_ret = arr; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | // Calculate how many. |
| 205 | int64_t count = 0; |
| 206 | if (incr > 0) { |
| 207 | count = Math::division_round_up(to - from, incr); |
| 208 | } else { |
| 209 | count = Math::division_round_up(from - to, -incr); |
| 210 | } |
| 211 | |
| 212 | GDFUNC_FAIL_COND_MSG(count > INT32_MAX, RTR("Range too big.")); |
| 213 | Error err = arr.resize(count); |
| 214 | GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array.")); |
| 215 | |
| 216 | if (incr > 0) { |
| 217 | int64_t idx = 0; |
| 218 | for (int64_t i = from; i < to; i += incr) { |
| 219 | arr[idx++] = i; |
| 220 | } |
| 221 | } else { |
| 222 | int64_t idx = 0; |
| 223 | for (int64_t i = from; i > to; i += incr) { |
| 224 | arr[idx++] = i; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | *r_ret = arr; |
| 229 | } break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 234 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 235 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING); |
| 236 | *r_ret = ResourceLoader::load(*p_args[0]); |
| 237 | } |
| 238 | |
| 239 | #ifndef DISABLE_DEPRECATED |
| 240 | |
| 241 | static inline void inst_to_dict(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 242 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 243 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::OBJECT); |
| 244 | |
| 245 | if (p_args[0]->get_type() == Variant::NIL) { |
| 246 | *r_ret = Variant(); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | Object *obj = *p_args[0]; |
| 251 | if (!obj) { |
| 252 | *r_ret = Variant(); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, |
| 257 | !obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton(), |
| 258 | RTR("Not a script with an instance.")); |
| 259 | |
| 260 | GDScriptInstance *inst = static_cast<GDScriptInstance *>(obj->get_script_instance()); |
| 261 | |
| 262 | Ref<GDScript> base = inst->get_script(); |
| 263 | VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, base.is_null(), RTR("Not based on a script.")); |
| 264 | |
| 265 | GDScript *p = base.ptr(); |
| 266 | String path = p->get_script_path(); |
| 267 | Vector<StringName> sname; |
| 268 | |
| 269 | while (p->_owner) { |
| 270 | sname.push_back(p->local_name); |
| 271 | p = p->_owner; |
| 272 | } |
| 273 | sname.reverse(); |
| 274 | |
| 275 | VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, !path.is_resource_file(), RTR("Not based on a resource file.")); |
| 276 | |
| 277 | NodePath cp(sname, Vector<StringName>(), false); |
| 278 | |
| 279 | Dictionary d; |
| 280 | d["@subpath"] = cp; |
| 281 | d["@path"] = path; |
| 282 | |
| 283 | for (const KeyValue<StringName, GDScript::MemberInfo> &E : base->member_indices) { |
| 284 | if (!d.has(E.key)) { |
| 285 | d[E.key] = inst->members[E.value.index]; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | *r_ret = d; |
| 290 | } |
| 291 | |
| 292 | static inline void dict_to_inst(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 293 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 294 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::DICTIONARY); |
| 295 | |
| 296 | Dictionary d = *p_args[0]; |
| 297 | |
| 298 | VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !d.has("@path"), RTR("Invalid instance dictionary format (missing @path).")); |
| 299 | |
| 300 | Ref<Script> scr = ResourceLoader::load(d["@path"]); |
| 301 | VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, scr.is_null(), RTR("Invalid instance dictionary format (can't load script at @path).")); |
| 302 | |
| 303 | Ref<GDScript> gdscr = scr; |
| 304 | VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, gdscr.is_null(), RTR("Invalid instance dictionary format (invalid script at @path).")); |
| 305 | |
| 306 | NodePath sub; |
| 307 | if (d.has("@subpath")) { |
| 308 | sub = d["@subpath"]; |
| 309 | } |
| 310 | |
| 311 | for (int i = 0; i < sub.get_name_count(); i++) { |
| 312 | gdscr = gdscr->subclasses[sub.get_name(i)]; |
| 313 | VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, gdscr.is_null(), RTR("Invalid instance dictionary (invalid subclasses).")); |
| 314 | } |
| 315 | |
| 316 | *r_ret = gdscr->_new(nullptr, -1 /* skip initializer */, r_error); |
| 317 | if (r_error.error != Callable::CallError::CALL_OK) { |
| 318 | *r_ret = RTR("Cannot instantiate GDScript class."); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | GDScriptInstance *inst = static_cast<GDScriptInstance *>(static_cast<Object *>(*r_ret)->get_script_instance()); |
| 323 | Ref<GDScript> gd_ref = inst->get_script(); |
| 324 | |
| 325 | for (KeyValue<StringName, GDScript::MemberInfo> &E : gd_ref->member_indices) { |
| 326 | if (d.has(E.key)) { |
| 327 | inst->members.write[E.value.index] = d[E.key]; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static inline void Color8(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 333 | DEBUG_VALIDATE_ARG_COUNT(3, 4); |
| 334 | DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT); |
| 335 | DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT); |
| 336 | DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT); |
| 337 | |
| 338 | int64_t alpha = 255; |
| 339 | if (p_arg_count == 4) { |
| 340 | DEBUG_VALIDATE_ARG_TYPE(3, Variant::INT); |
| 341 | alpha = *p_args[3]; |
| 342 | } |
| 343 | |
| 344 | *r_ret = Color::from_rgba8(*p_args[0], *p_args[1], *p_args[2], alpha); |
| 345 | } |
| 346 | |
| 347 | #endif // DISABLE_DEPRECATED |
| 348 | |
| 349 | static inline void print_debug(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 350 | String s; |
| 351 | for (int i = 0; i < p_arg_count; i++) { |
| 352 | s += p_args[i]->operator String(); |
| 353 | } |
| 354 | |
| 355 | ScriptLanguage *script = GDScriptLanguage::get_singleton(); |
| 356 | if (script->debug_get_stack_level_count() > 0) { |
| 357 | s += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()"; |
| 358 | } |
| 359 | |
| 360 | print_line(s); |
| 361 | *r_ret = Variant(); |
| 362 | } |
| 363 | |
| 364 | static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 365 | DEBUG_VALIDATE_ARG_COUNT(0, 0); |
| 366 | |
| 367 | ScriptLanguage *script = GDScriptLanguage::get_singleton(); |
| 368 | for (int i = 0; i < script->debug_get_stack_level_count(); i++) { |
| 369 | print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'"); |
| 370 | }; |
| 371 | *r_ret = Variant(); |
| 372 | } |
| 373 | |
| 374 | static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 375 | DEBUG_VALIDATE_ARG_COUNT(0, 0); |
| 376 | |
| 377 | ScriptLanguage *script = GDScriptLanguage::get_singleton(); |
| 378 | TypedArray<Dictionary> ret; |
| 379 | for (int i = 0; i < script->debug_get_stack_level_count(); i++) { |
| 380 | Dictionary frame; |
| 381 | frame["source"] = script->debug_get_stack_level_source(i); |
| 382 | frame["function"] = script->debug_get_stack_level_function(i); |
| 383 | frame["line"] = script->debug_get_stack_level_line(i); |
| 384 | ret.push_back(frame); |
| 385 | }; |
| 386 | *r_ret = ret; |
| 387 | } |
| 388 | |
| 389 | static inline void len(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 390 | DEBUG_VALIDATE_ARG_COUNT(1, 1); |
| 391 | switch (p_args[0]->get_type()) { |
| 392 | case Variant::STRING: |
| 393 | case Variant::STRING_NAME: { |
| 394 | String d = *p_args[0]; |
| 395 | *r_ret = d.length(); |
| 396 | } break; |
| 397 | case Variant::DICTIONARY: { |
| 398 | Dictionary d = *p_args[0]; |
| 399 | *r_ret = d.size(); |
| 400 | } break; |
| 401 | case Variant::ARRAY: { |
| 402 | Array d = *p_args[0]; |
| 403 | *r_ret = d.size(); |
| 404 | } break; |
| 405 | case Variant::PACKED_BYTE_ARRAY: { |
| 406 | Vector<uint8_t> d = *p_args[0]; |
| 407 | *r_ret = d.size(); |
| 408 | } break; |
| 409 | case Variant::PACKED_INT32_ARRAY: { |
| 410 | Vector<int32_t> d = *p_args[0]; |
| 411 | *r_ret = d.size(); |
| 412 | } break; |
| 413 | case Variant::PACKED_INT64_ARRAY: { |
| 414 | Vector<int64_t> d = *p_args[0]; |
| 415 | *r_ret = d.size(); |
| 416 | } break; |
| 417 | case Variant::PACKED_FLOAT32_ARRAY: { |
| 418 | Vector<float> d = *p_args[0]; |
| 419 | *r_ret = d.size(); |
| 420 | } break; |
| 421 | case Variant::PACKED_FLOAT64_ARRAY: { |
| 422 | Vector<double> d = *p_args[0]; |
| 423 | *r_ret = d.size(); |
| 424 | } break; |
| 425 | case Variant::PACKED_STRING_ARRAY: { |
| 426 | Vector<String> d = *p_args[0]; |
| 427 | *r_ret = d.size(); |
| 428 | } break; |
| 429 | case Variant::PACKED_VECTOR2_ARRAY: { |
| 430 | Vector<Vector2> d = *p_args[0]; |
| 431 | *r_ret = d.size(); |
| 432 | } break; |
| 433 | case Variant::PACKED_VECTOR3_ARRAY: { |
| 434 | Vector<Vector3> d = *p_args[0]; |
| 435 | *r_ret = d.size(); |
| 436 | } break; |
| 437 | case Variant::PACKED_COLOR_ARRAY: { |
| 438 | Vector<Color> d = *p_args[0]; |
| 439 | *r_ret = d.size(); |
| 440 | } break; |
| 441 | case Variant::PACKED_VECTOR4_ARRAY: { |
| 442 | Vector<Vector4> d = *p_args[0]; |
| 443 | *r_ret = d.size(); |
| 444 | } break; |
| 445 | default: { |
| 446 | *r_ret = vformat(RTR("Value of type '%s' can't provide a length."), Variant::get_type_name(p_args[0]->get_type())); |
| 447 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; |
| 448 | r_error.argument = 0; |
| 449 | r_error.expected = Variant::NIL; |
| 450 | } break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static inline void is_instance_of(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { |
| 455 | DEBUG_VALIDATE_ARG_COUNT(2, 2); |
| 456 | |
| 457 | if (p_args[1]->get_type() == Variant::INT) { |
| 458 | int builtin_type = *p_args[1]; |
| 459 | DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::NIL, builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX, |
| 460 | RTR("Invalid type argument for is_instance_of(), use TYPE_* constants for built-in types.")); |
| 461 | *r_ret = p_args[0]->get_type() == builtin_type; |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | bool was_type_freed = false; |
| 466 | Object *type_object = p_args[1]->get_validated_object_with_check(was_type_freed); |
| 467 | VALIDATE_ARG_CUSTOM(1, Variant::NIL, was_type_freed, RTR("Type argument is a previously freed instance.")); |
| 468 | VALIDATE_ARG_CUSTOM(1, Variant::NIL, !type_object, |
| 469 | RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script.")); |
| 470 | |
| 471 | bool was_value_freed = false; |
| 472 | Object *value_object = p_args[0]->get_validated_object_with_check(was_value_freed); |
| 473 | VALIDATE_ARG_CUSTOM(0, Variant::NIL, was_value_freed, RTR("Value argument is a previously freed instance.")); |
| 474 | if (!value_object) { |
| 475 | *r_ret = false; |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | GDScriptNativeClass *native_type = Object::cast_to<GDScriptNativeClass>(type_object); |
| 480 | if (native_type) { |
| 481 | *r_ret = ClassDB::is_parent_class(value_object->get_class_name(), native_type->get_name()); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | Script *script_type = Object::cast_to<Script>(type_object); |
| 486 | if (script_type) { |
| 487 | bool result = false; |
| 488 | if (value_object->get_script_instance()) { |
| 489 | Script *script_ptr = value_object->get_script_instance()->get_script().ptr(); |
| 490 | while (script_ptr) { |
| 491 | if (script_ptr == script_type) { |
| 492 | result = true; |
| 493 | break; |
| 494 | } |
| 495 | script_ptr = script_ptr->get_base_script().ptr(); |
| 496 | } |
| 497 | } |
| 498 | *r_ret = result; |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | *r_ret = RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script."); |
| 503 | r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; |
| 504 | r_error.argument = 1; |
| 505 | r_error.expected = Variant::NIL; |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | struct GDScriptUtilityFunctionInfo { |
| 510 | GDScriptUtilityFunctions::FunctionPtr function = nullptr; |
| 511 | MethodInfo info; |
| 512 | bool is_constant = false; |
| 513 | }; |
| 514 | |
| 515 | static AHashMap<StringName, GDScriptUtilityFunctionInfo> utility_function_table; |
| 516 | static List<StringName> utility_function_name_table; |
| 517 | |
| 518 | static void _register_function(const StringName &p_name, const MethodInfo &p_method_info, GDScriptUtilityFunctions::FunctionPtr p_function, bool p_is_const) { |
| 519 | ERR_FAIL_COND(utility_function_table.has(p_name)); |
| 520 | |
| 521 | GDScriptUtilityFunctionInfo function; |
| 522 | function.function = p_function; |
| 523 | function.info = p_method_info; |
| 524 | function.is_constant = p_is_const; |
| 525 | |
| 526 | utility_function_table.insert(p_name, function); |
| 527 | utility_function_name_table.push_back(p_name); |
| 528 | } |
| 529 | |
| 530 | #define REGISTER_FUNC(m_func, m_is_const, m_return, m_args, m_is_vararg, m_default_args) \ |
| 531 | { \ |
| 532 | String name(#m_func); \ |
| 533 | if (name.begins_with("_")) { \ |
| 534 | name = name.substr(1); \ |
| 535 | } \ |
| 536 | MethodInfo info = m_args; \ |
| 537 | info.name = name; \ |
| 538 | info.return_val = m_return; \ |
| 539 | info.default_arguments = m_default_args; \ |
| 540 | if (m_is_vararg) { \ |
| 541 | info.flags |= METHOD_FLAG_VARARG; \ |
| 542 | } \ |
| 543 | _register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \ |
| 544 | } |
| 545 | |
| 546 | #define RET(m_type) \ |
| 547 | PropertyInfo(Variant::m_type, "") |
| 548 | |
| 549 | #define RETVAR \ |
| 550 | PropertyInfo(Variant::NIL, "", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT) |
| 551 | |
| 552 | #define RETCLS(m_class) \ |
| 553 | PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, m_class) |
| 554 | |
| 555 | #define NOARGS \ |
| 556 | MethodInfo() |
| 557 | |
| 558 | #define ARGS(...) \ |
| 559 | MethodInfo("", __VA_ARGS__) |
| 560 | |
| 561 | #define ARG(m_name, m_type) \ |
| 562 | PropertyInfo(Variant::m_type, m_name) |
| 563 | |
| 564 | #define ARGVAR(m_name) \ |
| 565 | PropertyInfo(Variant::NIL, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT) |
| 566 | |
| 567 | #define ARGTYPE(m_name) \ |
| 568 | PropertyInfo(Variant::INT, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "Variant.Type") |
| 569 | |
| 570 | void GDScriptUtilityFunctions::register_functions() { |
| 571 | /* clang-format off */ |
| 572 | #ifndef DISABLE_DEPRECATED |
| 573 | REGISTER_FUNC( convert, true, RETVAR, ARGS( ARGVAR("what"), ARGTYPE("type") ), false, varray( )); |
| 574 | #endif // DISABLE_DEPRECATED |
| 575 | REGISTER_FUNC( type_exists, true, RET(BOOL), ARGS( ARG("type", STRING_NAME) ), false, varray( )); |
| 576 | REGISTER_FUNC( _char, true, RET(STRING), ARGS( ARG("code", INT) ), false, varray( )); |
| 577 | REGISTER_FUNC( ord, true, RET(INT), ARGS( ARG("char", STRING) ), false, varray( )); |
| 578 | REGISTER_FUNC( range, false, RET(ARRAY), NOARGS, true, varray( )); |
| 579 | REGISTER_FUNC( load, false, RETCLS("Resource"), ARGS( ARG("path", STRING) ), false, varray( )); |
| 580 | #ifndef DISABLE_DEPRECATED |
| 581 | REGISTER_FUNC( inst_to_dict, false, RET(DICTIONARY), ARGS( ARG("instance", OBJECT) ), false, varray( )); |
| 582 | REGISTER_FUNC( dict_to_inst, false, RET(OBJECT), ARGS( ARG("dictionary", DICTIONARY) ), false, varray( )); |
| 583 | REGISTER_FUNC( Color8, true, RET(COLOR), ARGS( ARG("r8", INT), ARG("g8", INT), |
| 584 | ARG("b8", INT), ARG("a8", INT) ), false, varray( 255 )); |
| 585 | #endif // DISABLE_DEPRECATED |
| 586 | REGISTER_FUNC( print_debug, false, RET(NIL), NOARGS, true, varray( )); |
| 587 | REGISTER_FUNC( print_stack, false, RET(NIL), NOARGS, false, varray( )); |
| 588 | REGISTER_FUNC( get_stack, false, RET(ARRAY), NOARGS, false, varray( )); |
| 589 | REGISTER_FUNC( len, true, RET(INT), ARGS( ARGVAR("var") ), false, varray( )); |
| 590 | REGISTER_FUNC( is_instance_of, true, RET(BOOL), ARGS( ARGVAR("value"), ARGVAR("type") ), false, varray( )); |
| 591 | /* clang-format on */ |
| 592 | } |
| 593 | |
| 594 | void GDScriptUtilityFunctions::unregister_functions() { |
| 595 | utility_function_name_table.clear(); |
| 596 | utility_function_table.clear(); |
| 597 | } |
| 598 | |
| 599 | GDScriptUtilityFunctions::FunctionPtr GDScriptUtilityFunctions::get_function(const StringName &p_function) { |
| 600 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 601 | ERR_FAIL_NULL_V(info, nullptr); |
| 602 | return info->function; |
| 603 | } |
| 604 | |
| 605 | bool GDScriptUtilityFunctions::has_function_return_value(const StringName &p_function) { |
| 606 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 607 | ERR_FAIL_NULL_V(info, false); |
| 608 | return info->info.return_val.type != Variant::NIL || bool(info->info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT); |
| 609 | } |
| 610 | |
| 611 | Variant::Type GDScriptUtilityFunctions::get_function_return_type(const StringName &p_function) { |
| 612 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 613 | ERR_FAIL_NULL_V(info, Variant::NIL); |
| 614 | return info->info.return_val.type; |
| 615 | } |
| 616 | |
| 617 | StringName GDScriptUtilityFunctions::get_function_return_class(const StringName &p_function) { |
| 618 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 619 | ERR_FAIL_NULL_V(info, StringName()); |
| 620 | return info->info.return_val.class_name; |
| 621 | } |
| 622 | |
| 623 | Variant::Type GDScriptUtilityFunctions::get_function_argument_type(const StringName &p_function, int p_arg) { |
| 624 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 625 | ERR_FAIL_NULL_V(info, Variant::NIL); |
| 626 | ERR_FAIL_INDEX_V(p_arg, info->info.arguments.size(), Variant::NIL); |
| 627 | return info->info.arguments[p_arg].type; |
| 628 | } |
| 629 | |
| 630 | int GDScriptUtilityFunctions::get_function_argument_count(const StringName &p_function) { |
| 631 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 632 | ERR_FAIL_NULL_V(info, 0); |
| 633 | return info->info.arguments.size(); |
| 634 | } |
| 635 | |
| 636 | bool GDScriptUtilityFunctions::is_function_vararg(const StringName &p_function) { |
| 637 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 638 | ERR_FAIL_NULL_V(info, false); |
| 639 | return (bool)(info->info.flags & METHOD_FLAG_VARARG); |
| 640 | } |
| 641 | |
| 642 | bool GDScriptUtilityFunctions::is_function_constant(const StringName &p_function) { |
| 643 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 644 | ERR_FAIL_NULL_V(info, false); |
| 645 | return info->is_constant; |
| 646 | } |
| 647 | |
| 648 | bool GDScriptUtilityFunctions::function_exists(const StringName &p_function) { |
| 649 | return utility_function_table.has(p_function); |
| 650 | } |
| 651 | |
| 652 | void GDScriptUtilityFunctions::get_function_list(List<StringName> *r_functions) { |
| 653 | for (const StringName &E : utility_function_name_table) { |
| 654 | r_functions->push_back(E); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | MethodInfo GDScriptUtilityFunctions::get_function_info(const StringName &p_function) { |
| 659 | GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function); |
| 660 | ERR_FAIL_NULL_V(info, MethodInfo()); |
| 661 | return info->info; |
| 662 | } |