gdscript_rpc_callable.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. return class_name + "(" + script->get_path().get_file() + ")::" + String(method) + " (rpc)";
  47. }
  48. CallableCustom::CompareEqualFunc GDScriptRPCCallable::get_compare_equal_func() const {
  49. return compare_equal;
  50. }
  51. CallableCustom::CompareLessFunc GDScriptRPCCallable::get_compare_less_func() const {
  52. return compare_less;
  53. }
  54. ObjectID GDScriptRPCCallable::get_object() const {
  55. return object->get_instance_id();
  56. }
  57. StringName GDScriptRPCCallable::get_method() const {
  58. return method;
  59. }
  60. void GDScriptRPCCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  61. r_return_value = object->callp(method, p_arguments, p_argcount, r_call_error);
  62. }
  63. GDScriptRPCCallable::GDScriptRPCCallable(Object *p_object, const StringName &p_method) {
  64. ERR_FAIL_NULL(p_object);
  65. object = p_object;
  66. method = p_method;
  67. h = method.hash();
  68. h = hash_murmur3_one_64(object->get_instance_id(), h);
  69. node = Object::cast_to<Node>(object);
  70. ERR_FAIL_NULL_MSG(node, "RPC can only be defined on class that extends Node.");
  71. }
  72. Error GDScriptRPCCallable::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  73. if (unlikely(!node)) {
  74. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  75. return ERR_UNCONFIGURED;
  76. }
  77. r_call_error.error = Callable::CallError::CALL_OK;
  78. return node->rpcp(p_peer_id, method, p_arguments, p_argcount);
  79. }