You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
JSONRPC: Require manual method registration
This commit is contained in:
41
modules/jsonrpc/jsonrpc.compat.inc
Normal file
41
modules/jsonrpc/jsonrpc.compat.inc
Normal file
@@ -0,0 +1,41 @@
|
||||
/**************************************************************************/
|
||||
/* jsonrpc.compat.inc */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
|
||||
void JSONRPC::_set_scope_bind_compat_104890(const String &p_scope, Object *p_obj) {
|
||||
ERR_PRINT("JSONRPC::set_scope is not supported anymore. Upgrade to JSONRPC::set_method.");
|
||||
}
|
||||
|
||||
void JSONRPC::_bind_compatibility_methods() {
|
||||
ClassDB::bind_compatibility_method(D_METHOD("set_scope", "scope", "target"), &JSONRPC::_set_scope_bind_compat_104890);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -29,6 +29,7 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "jsonrpc.h"
|
||||
#include "jsonrpc.compat.inc"
|
||||
|
||||
#include "core/io/json.h"
|
||||
|
||||
@@ -39,7 +40,7 @@ JSONRPC::~JSONRPC() {
|
||||
}
|
||||
|
||||
void JSONRPC::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("set_scope", "scope", "target"), &JSONRPC::set_scope);
|
||||
ClassDB::bind_method(D_METHOD("set_method", "name", "callback"), &JSONRPC::set_method);
|
||||
ClassDB::bind_method(D_METHOD("process_action", "action", "recurse"), &JSONRPC::process_action, DEFVAL(false));
|
||||
ClassDB::bind_method(D_METHOD("process_string", "action"), &JSONRPC::process_string);
|
||||
|
||||
@@ -113,12 +114,6 @@ Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elem
|
||||
}
|
||||
}
|
||||
|
||||
Object *object = this;
|
||||
if (method_scopes.has(method.get_base_dir())) {
|
||||
object = method_scopes[method.get_base_dir()];
|
||||
method = method.get_file();
|
||||
}
|
||||
|
||||
Variant id;
|
||||
if (dict.has("id")) {
|
||||
id = dict["id"];
|
||||
@@ -129,13 +124,13 @@ Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elem
|
||||
}
|
||||
}
|
||||
|
||||
if (object == nullptr || !object->has_method(method)) {
|
||||
ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
|
||||
} else {
|
||||
Variant call_ret = object->callv(method, args);
|
||||
if (methods.has(method)) {
|
||||
Variant call_ret = methods[method].callv(args);
|
||||
if (id.get_type() != Variant::NIL) {
|
||||
ret = make_response(call_ret, id);
|
||||
}
|
||||
} else {
|
||||
ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
|
||||
}
|
||||
} else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) {
|
||||
Array arr = p_action;
|
||||
@@ -175,6 +170,6 @@ String JSONRPC::process_string(const String &p_input) {
|
||||
return ret.to_json_string();
|
||||
}
|
||||
|
||||
void JSONRPC::set_scope(const String &p_scope, Object *p_obj) {
|
||||
method_scopes[p_scope] = p_obj;
|
||||
void JSONRPC::set_method(const String &p_name, const Callable &p_callback) {
|
||||
methods[p_name] = p_callback;
|
||||
}
|
||||
|
||||
@@ -36,11 +36,16 @@
|
||||
class JSONRPC : public Object {
|
||||
GDCLASS(JSONRPC, Object)
|
||||
|
||||
HashMap<String, Object *> method_scopes;
|
||||
HashMap<String, Callable> methods;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
void _set_scope_bind_compat_104890(const String &p_scope, Object *p_obj);
|
||||
static void _bind_compatibility_methods();
|
||||
#endif
|
||||
|
||||
public:
|
||||
JSONRPC();
|
||||
~JSONRPC();
|
||||
@@ -61,7 +66,7 @@ public:
|
||||
Variant process_action(const Variant &p_action, bool p_process_arr_elements = false);
|
||||
String process_string(const String &p_input);
|
||||
|
||||
void set_scope(const String &p_scope, Object *p_obj);
|
||||
void set_method(const String &p_name, const Callable &p_callback);
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(JSONRPC::ErrorCode);
|
||||
|
||||
@@ -57,10 +57,6 @@ String TestClassJSONRPC::something(const String &p_in) {
|
||||
return p_in + ", please";
|
||||
}
|
||||
|
||||
void TestClassJSONRPC::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("something", "in"), &TestClassJSONRPC::something);
|
||||
}
|
||||
|
||||
void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) {
|
||||
TestClassJSONRPC json_rpc = TestClassJSONRPC();
|
||||
const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements);
|
||||
|
||||
@@ -60,20 +60,17 @@ TEST_CASE("[JSONRPC] process_string invalid") {
|
||||
}
|
||||
|
||||
class TestClassJSONRPC : public JSONRPC {
|
||||
GDCLASS(TestClassJSONRPC, JSONRPC)
|
||||
|
||||
public:
|
||||
String something(const String &p_in);
|
||||
TestClassJSONRPC() {
|
||||
set_method("something", callable_mp(this, &TestClassJSONRPC::something));
|
||||
}
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
String something(const String &p_in);
|
||||
};
|
||||
|
||||
void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
|
||||
|
||||
TEST_CASE("[JSONRPC] process_action Dictionary") {
|
||||
ClassDB::register_class<TestClassJSONRPC>();
|
||||
|
||||
Dictionary in_dict = Dictionary();
|
||||
in_dict["method"] = "something";
|
||||
in_dict["id"] = "ID";
|
||||
|
||||
Reference in New Issue
Block a user