gdscript_rpc_callable.cpp 4.1 KB

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