Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_rpc_callable.cpp
10277 views
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
if (script.is_valid()) {
53
if (!script->get_global_name().is_empty()) {
54
class_name += "(" + script->get_global_name() + ")";
55
} else if (script->get_path().is_resource_file()) {
56
class_name += "(" + script->get_path().get_file() + ")";
57
}
58
}
59
return class_name + "::" + String(method) + " (rpc)";
60
}
61
62
CallableCustom::CompareEqualFunc GDScriptRPCCallable::get_compare_equal_func() const {
63
return compare_equal;
64
}
65
66
CallableCustom::CompareLessFunc GDScriptRPCCallable::get_compare_less_func() const {
67
return compare_less;
68
}
69
70
ObjectID GDScriptRPCCallable::get_object() const {
71
return object->get_instance_id();
72
}
73
74
StringName GDScriptRPCCallable::get_method() const {
75
return method;
76
}
77
78
int GDScriptRPCCallable::get_argument_count(bool &r_is_valid) const {
79
return object->get_method_argument_count(method, &r_is_valid);
80
}
81
82
void GDScriptRPCCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
83
r_return_value = object->callp(method, p_arguments, p_argcount, r_call_error);
84
}
85
86
GDScriptRPCCallable::GDScriptRPCCallable(Object *p_object, const StringName &p_method) {
87
ERR_FAIL_NULL(p_object);
88
object = p_object;
89
method = p_method;
90
h = method.hash();
91
h = hash_murmur3_one_64(object->get_instance_id(), h);
92
node = Object::cast_to<Node>(object);
93
ERR_FAIL_NULL_MSG(node, "RPC can only be defined on class that extends Node.");
94
}
95
96
Error GDScriptRPCCallable::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
97
if (unlikely(!node)) {
98
r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
99
return ERR_UNCONFIGURED;
100
}
101
r_call_error.error = Callable::CallError::CALL_OK;
102
return node->rpcp(p_peer_id, method, p_arguments, p_argcount);
103
}
104
105