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