Path: blob/master/modules/gdscript/gdscript_utility_callable.cpp
10277 views
/**************************************************************************/1/* gdscript_utility_callable.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gdscript_utility_callable.h"3132bool GDScriptUtilityCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {33return p_a->hash() == p_b->hash();34}3536bool GDScriptUtilityCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {37return p_a->hash() < p_b->hash();38}3940uint32_t GDScriptUtilityCallable::hash() const {41return h;42}4344String GDScriptUtilityCallable::get_as_text() const {45String scope;46switch (type) {47case TYPE_INVALID:48scope = "<invalid scope>";49break;50case TYPE_GLOBAL:51scope = "@GlobalScope";52break;53case TYPE_GDSCRIPT:54scope = "@GDScript";55break;56}57return vformat("%s::%s", scope, function_name);58}5960CallableCustom::CompareEqualFunc GDScriptUtilityCallable::get_compare_equal_func() const {61return compare_equal;62}6364CallableCustom::CompareLessFunc GDScriptUtilityCallable::get_compare_less_func() const {65return compare_less;66}6768bool GDScriptUtilityCallable::is_valid() const {69return type != TYPE_INVALID;70}7172StringName GDScriptUtilityCallable::get_method() const {73return function_name;74}7576ObjectID GDScriptUtilityCallable::get_object() const {77return ObjectID();78}7980int GDScriptUtilityCallable::get_argument_count(bool &r_is_valid) const {81switch (type) {82case TYPE_INVALID:83r_is_valid = false;84return 0;85case TYPE_GLOBAL:86r_is_valid = true;87return Variant::get_utility_function_argument_count(function_name);88case TYPE_GDSCRIPT:89r_is_valid = true;90return GDScriptUtilityFunctions::get_function_argument_count(function_name);91}92ERR_FAIL_V_MSG(0, "Invalid type.");93}9495void GDScriptUtilityCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {96switch (type) {97case TYPE_INVALID:98r_return_value = vformat(R"(Trying to call invalid utility function "%s".)", function_name);99r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;100r_call_error.argument = 0;101r_call_error.expected = 0;102break;103case TYPE_GLOBAL:104Variant::call_utility_function(function_name, &r_return_value, p_arguments, p_argcount, r_call_error);105break;106case TYPE_GDSCRIPT:107gdscript_function(&r_return_value, p_arguments, p_argcount, r_call_error);108break;109}110}111112GDScriptUtilityCallable::GDScriptUtilityCallable(const StringName &p_function_name) {113function_name = p_function_name;114if (GDScriptUtilityFunctions::function_exists(p_function_name)) {115type = TYPE_GDSCRIPT;116gdscript_function = GDScriptUtilityFunctions::get_function(p_function_name);117} else if (Variant::has_utility_function(p_function_name)) {118type = TYPE_GLOBAL;119} else {120ERR_PRINT(vformat(R"(Unknown utility function "%s".)", p_function_name));121}122h = p_function_name.hash();123}124125126