gdscript_utility_callable.cpp 4.9 KB

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