gdscript_utility_callable.cpp 4.9 KB

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