gdscript_rpc_callable.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**************************************************************************/
  2. /* gdscript_rpc_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_rpc_callable.h"
  31. #include "core/object/script_language.h"
  32. #include "core/templates/hashfuncs.h"
  33. #include "scene/main/node.h"
  34. bool GDScriptRPCCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  35. return p_a->hash() == p_b->hash();
  36. }
  37. bool GDScriptRPCCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  38. return p_a->hash() < p_b->hash();
  39. }
  40. uint32_t GDScriptRPCCallable::hash() const {
  41. return h;
  42. }
  43. String GDScriptRPCCallable::get_as_text() const {
  44. String class_name = object->get_class();
  45. Ref<Script> script = object->get_script();
  46. if (script.is_valid()) {
  47. if (!script->get_global_name().is_empty()) {
  48. class_name += "(" + script->get_global_name() + ")";
  49. } else if (script->get_path().is_resource_file()) {
  50. class_name += "(" + script->get_path().get_file() + ")";
  51. }
  52. }
  53. return class_name + "::" + String(method) + " (rpc)";
  54. }
  55. CallableCustom::CompareEqualFunc GDScriptRPCCallable::get_compare_equal_func() const {
  56. return compare_equal;
  57. }
  58. CallableCustom::CompareLessFunc GDScriptRPCCallable::get_compare_less_func() const {
  59. return compare_less;
  60. }
  61. ObjectID GDScriptRPCCallable::get_object() const {
  62. return object->get_instance_id();
  63. }
  64. StringName GDScriptRPCCallable::get_method() const {
  65. return method;
  66. }
  67. int GDScriptRPCCallable::get_argument_count(bool &r_is_valid) const {
  68. return object->get_method_argument_count(method, &r_is_valid);
  69. }
  70. void GDScriptRPCCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  71. r_return_value = object->callp(method, p_arguments, p_argcount, r_call_error);
  72. }
  73. GDScriptRPCCallable::GDScriptRPCCallable(Object *p_object, const StringName &p_method) {
  74. ERR_FAIL_NULL(p_object);
  75. object = p_object;
  76. method = p_method;
  77. h = method.hash();
  78. h = hash_murmur3_one_64(object->get_instance_id(), h);
  79. node = Object::cast_to<Node>(object);
  80. ERR_FAIL_NULL_MSG(node, "RPC can only be defined on class that extends Node.");
  81. }
  82. Error GDScriptRPCCallable::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  83. if (unlikely(!node)) {
  84. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  85. return ERR_UNCONFIGURED;
  86. }
  87. r_call_error.error = Callable::CallError::CALL_OK;
  88. return node->rpcp(p_peer_id, method, p_arguments, p_argcount);
  89. }