/**************************************************************************/ /* gdscript_utility_functions.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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. */ /**************************************************************************/ #include "gdscript_utility_functions.h" #include "gdscript.h" #include "core/io/resource_loader.h" #include "core/object/class_db.h" #include "core/object/method_bind.h" #include "core/object/object.h" #include "core/templates/oa_hash_map.h" #include "core/templates/vector.h" #include "core/variant/typed_array.h" #ifdef DEBUG_ENABLED #define VALIDATE_ARG_COUNT(m_count) \ if (p_arg_count < m_count) { \ r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \ r_error.argument = m_count; \ r_error.expected = m_count; \ *r_ret = Variant(); \ return; \ } \ if (p_arg_count > m_count) { \ r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \ r_error.argument = m_count; \ r_error.expected = m_count; \ *r_ret = Variant(); \ return; \ } #define VALIDATE_ARG_INT(m_arg) \ if (p_args[m_arg]->get_type() != Variant::INT) { \ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ r_error.argument = m_arg; \ r_error.expected = Variant::INT; \ *r_ret = Variant(); \ return; \ } #define VALIDATE_ARG_NUM(m_arg) \ if (!p_args[m_arg]->is_num()) { \ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ r_error.argument = m_arg; \ r_error.expected = Variant::FLOAT; \ *r_ret = Variant(); \ return; \ } #else #define VALIDATE_ARG_COUNT(m_count) #define VALIDATE_ARG_INT(m_arg) #define VALIDATE_ARG_NUM(m_arg) #endif struct GDScriptUtilityFunctionsDefinitions { #ifndef DISABLE_DEPRECATED static inline void convert(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(2); VALIDATE_ARG_INT(1); int type = *p_args[1]; if (type < 0 || type >= Variant::VARIANT_MAX) { *r_ret = RTR("Invalid type argument to convert(), use TYPE_* constants."); r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::INT; return; } else { Variant::construct(Variant::Type(type), *r_ret, p_args, 1, r_error); } } #endif // DISABLE_DEPRECATED static inline void type_exists(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(1); *r_ret = ClassDB::class_exists(*p_args[0]); } static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(1); VALIDATE_ARG_INT(0); char32_t result[2] = { *p_args[0], 0 }; *r_ret = String(result); } static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { switch (p_arg_count) { case 0: { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 1; r_error.expected = 1; *r_ret = Variant(); } break; case 1: { VALIDATE_ARG_NUM(0); int count = *p_args[0]; Array arr; if (count <= 0) { *r_ret = arr; return; } Error err = arr.resize(count); if (err != OK) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; *r_ret = Variant(); return; } for (int i = 0; i < count; i++) { arr[i] = i; } *r_ret = arr; } break; case 2: { VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); int from = *p_args[0]; int to = *p_args[1]; Array arr; if (from >= to) { *r_ret = arr; return; } Error err = arr.resize(to - from); if (err != OK) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; *r_ret = Variant(); return; } for (int i = from; i < to; i++) { arr[i - from] = i; } *r_ret = arr; } break; case 3: { VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); int from = *p_args[0]; int to = *p_args[1]; int incr = *p_args[2]; if (incr == 0) { *r_ret = RTR("Step argument is zero!"); r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return; } Array arr; if (from >= to && incr > 0) { *r_ret = arr; return; } if (from <= to && incr < 0) { *r_ret = arr; return; } // Calculate how many. int count = 0; if (incr > 0) { count = ((to - from - 1) / incr) + 1; } else { count = ((from - to - 1) / -incr) + 1; } Error err = arr.resize(count); if (err != OK) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; *r_ret = Variant(); return; } if (incr > 0) { int idx = 0; for (int i = from; i < to; i += incr) { arr[idx++] = i; } } else { int idx = 0; for (int i = from; i > to; i += incr) { arr[idx++] = i; } } *r_ret = arr; } break; default: { r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; r_error.argument = 3; r_error.expected = 3; *r_ret = Variant(); } break; } } static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(1); if (p_args[0]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING; *r_ret = Variant(); } else { *r_ret = ResourceLoader::load(*p_args[0]); } } static inline void inst_to_dict(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(1); if (p_args[0]->get_type() == Variant::NIL) { *r_ret = Variant(); } else if (p_args[0]->get_type() != Variant::OBJECT) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; *r_ret = Variant(); } else { Object *obj = *p_args[0]; if (!obj) { *r_ret = Variant(); } else if (!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::DICTIONARY; *r_ret = RTR("Not a script with an instance"); return; } else { GDScriptInstance *ins = static_cast(obj->get_script_instance()); Ref base = ins->get_script(); if (base.is_null()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::DICTIONARY; *r_ret = RTR("Not based on a script"); return; } GDScript *p = base.ptr(); String path = p->get_script_path(); Vector sname; while (p->_owner) { sname.push_back(p->local_name); p = p->_owner; } sname.reverse(); if (!path.is_resource_file()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::DICTIONARY; *r_ret = Variant(); *r_ret = RTR("Not based on a resource file"); return; } NodePath cp(sname, Vector(), false); Dictionary d; d["@subpath"] = cp; d["@path"] = path; for (const KeyValue &E : base->member_indices) { if (!d.has(E.key)) { d[E.key] = ins->members[E.value.index]; } } *r_ret = d; } } } static inline void dict_to_inst(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { VALIDATE_ARG_COUNT(1); if (p_args[0]->get_type() != Variant::DICTIONARY) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::DICTIONARY; *r_ret = Variant(); return; } Dictionary d = *p_args[0]; if (!d.has("@path")) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::OBJECT; *r_ret = RTR("Invalid instance dictionary format (missing @path)"); return; } Ref