| 1 | /**************************************************************************/ |
| 2 | /* gdscript_utility_callable.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_callable.h" |
| 32 | |
| 33 | bool GDScriptUtilityCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { |
| 34 | return p_a->hash() == p_b->hash(); |
| 35 | } |
| 36 | |
| 37 | bool GDScriptUtilityCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) { |
| 38 | return p_a->hash() < p_b->hash(); |
| 39 | } |
| 40 | |
| 41 | uint32_t GDScriptUtilityCallable::hash() const { |
| 42 | return h; |
| 43 | } |
| 44 | |
| 45 | String GDScriptUtilityCallable::get_as_text() const { |
| 46 | String scope; |
| 47 | switch (type) { |
| 48 | case TYPE_INVALID: |
| 49 | scope = "<invalid scope>"; |
| 50 | break; |
| 51 | case TYPE_GLOBAL: |
| 52 | scope = "@GlobalScope"; |
| 53 | break; |
| 54 | case TYPE_GDSCRIPT: |
| 55 | scope = "@GDScript"; |
| 56 | break; |
| 57 | } |
| 58 | return vformat("%s::%s", scope, function_name); |
| 59 | } |
| 60 | |
| 61 | CallableCustom::CompareEqualFunc GDScriptUtilityCallable::get_compare_equal_func() const { |
| 62 | return compare_equal; |
| 63 | } |
| 64 | |
| 65 | CallableCustom::CompareLessFunc GDScriptUtilityCallable::get_compare_less_func() const { |
| 66 | return compare_less; |
| 67 | } |
| 68 | |
| 69 | bool GDScriptUtilityCallable::is_valid() const { |
| 70 | return type != TYPE_INVALID; |
| 71 | } |
| 72 | |
| 73 | StringName GDScriptUtilityCallable::get_method() const { |
| 74 | return function_name; |
| 75 | } |
| 76 | |
| 77 | ObjectID GDScriptUtilityCallable::get_object() const { |
| 78 | return ObjectID(); |
| 79 | } |
| 80 | |
| 81 | int GDScriptUtilityCallable::get_argument_count(bool &r_is_valid) const { |
| 82 | switch (type) { |
| 83 | case TYPE_INVALID: |
| 84 | r_is_valid = false; |
| 85 | return 0; |
| 86 | case TYPE_GLOBAL: |
| 87 | r_is_valid = true; |
| 88 | return Variant::get_utility_function_argument_count(function_name); |
| 89 | case TYPE_GDSCRIPT: |
| 90 | r_is_valid = true; |
| 91 | return GDScriptUtilityFunctions::get_function_argument_count(function_name); |
| 92 | } |
| 93 | ERR_FAIL_V_MSG(0, "Invalid type."); |
| 94 | } |
| 95 | |
| 96 | void GDScriptUtilityCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { |
| 97 | switch (type) { |
| 98 | case TYPE_INVALID: |
| 99 | r_return_value = vformat(R"(Trying to call invalid utility function "%s".)", function_name); |
| 100 | r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; |
| 101 | r_call_error.argument = 0; |
| 102 | r_call_error.expected = 0; |
| 103 | break; |
| 104 | case TYPE_GLOBAL: |
| 105 | Variant::call_utility_function(function_name, &r_return_value, p_arguments, p_argcount, r_call_error); |
| 106 | break; |
| 107 | case TYPE_GDSCRIPT: |
| 108 | gdscript_function(&r_return_value, p_arguments, p_argcount, r_call_error); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | GDScriptUtilityCallable::GDScriptUtilityCallable(const StringName &p_function_name) { |
| 114 | function_name = p_function_name; |
| 115 | if (GDScriptUtilityFunctions::function_exists(p_function_name)) { |
| 116 | type = TYPE_GDSCRIPT; |
| 117 | gdscript_function = GDScriptUtilityFunctions::get_function(p_function_name); |
| 118 | } else if (Variant::has_utility_function(p_function_name)) { |
| 119 | type = TYPE_GLOBAL; |
| 120 | } else { |
| 121 | ERR_PRINT(vformat(R"(Unknown utility function "%s".)", p_function_name)); |
| 122 | } |
| 123 | h = p_function_name.hash(); |
| 124 | } |