Skip to content

Commit 8bd9cde

Browse files
committed
Merge pull request #104890 from HolonProduction/json-rpc-manual-bind
JSONRPC: Require manual method registration
2 parents 21db848 + e2c3731 commit 8bd9cde

File tree

11 files changed

+124
-43
lines changed

11 files changed

+124
-43
lines changed

doc/classes/JSONRPC.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@
6969
<description>
7070
</description>
7171
</method>
72-
<method name="set_scope">
72+
<method name="set_method">
7373
<return type="void" />
74-
<param index="0" name="scope" type="String" />
75-
<param index="1" name="target" type="Object" />
74+
<param index="0" name="name" type="String" />
75+
<param index="1" name="callback" type="Callable" />
7676
<description>
77+
Registers a callback for the given method name.
78+
- [param name] The name that clients can use to access the callback.
79+
- [param callback] The callback which will handle the specific method.
7780
</description>
7881
</method>
7982
</methods>

misc/extension_api_validation/4.4-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ Validate extension JSON: Error: Field 'classes/OpenXRAPIExtension/methods/regist
2424
Validate extension JSON: Error: Field 'classes/OpenXRAPIExtension/methods/unregister_projection_views_extension/arguments/0': type changed value in new API, from "OpenXRExtensionWrapperExtension" to "OpenXRExtensionWrapper".
2525

2626
Switched from `OpenXRExtensionWrapperExtension` to parent `OpenXRExtensionWrapper`. Compatibility methods registered.
27+
28+
29+
GH-104890
30+
---------
31+
Validate extension JSON: API was removed: classes/JSONRPC/methods/set_scope
32+
33+
Replaced `set_scope` with `set_method`. Compatibility method registered for binary compatibility. Manual upgrade required by users to retain functionality.

modules/gdscript/language_server/gdscript_language_protocol.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,50 @@ bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const {
334334
return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor"));
335335
}
336336

337+
// clang-format off
338+
#define SET_DOCUMENT_METHOD(m_method) set_method(_STR(textDocument/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
339+
#define SET_COMPLETION_METHOD(m_method) set_method(_STR(completionItem/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
340+
#define SET_WORKSPACE_METHOD(m_method) set_method(_STR(workspace/m_method), callable_mp(workspace.ptr(), &GDScriptWorkspace::m_method))
341+
// clang-format on
342+
337343
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
338344
server.instantiate();
339345
singleton = this;
340346
workspace.instantiate();
341347
text_document.instantiate();
342-
set_scope("textDocument", text_document.ptr());
343-
set_scope("completionItem", text_document.ptr());
344-
set_scope("workspace", workspace.ptr());
348+
349+
SET_DOCUMENT_METHOD(didOpen);
350+
SET_DOCUMENT_METHOD(didClose);
351+
SET_DOCUMENT_METHOD(didChange);
352+
SET_DOCUMENT_METHOD(willSaveWaitUntil);
353+
SET_DOCUMENT_METHOD(didSave);
354+
355+
SET_DOCUMENT_METHOD(documentSymbol);
356+
SET_DOCUMENT_METHOD(completion);
357+
SET_DOCUMENT_METHOD(rename);
358+
SET_DOCUMENT_METHOD(prepareRename);
359+
SET_DOCUMENT_METHOD(references);
360+
SET_DOCUMENT_METHOD(foldingRange);
361+
SET_DOCUMENT_METHOD(codeLens);
362+
SET_DOCUMENT_METHOD(documentLink);
363+
SET_DOCUMENT_METHOD(colorPresentation);
364+
SET_DOCUMENT_METHOD(hover);
365+
SET_DOCUMENT_METHOD(definition);
366+
SET_DOCUMENT_METHOD(declaration);
367+
SET_DOCUMENT_METHOD(signatureHelp);
368+
369+
SET_DOCUMENT_METHOD(nativeSymbol); // Custom method.
370+
371+
SET_COMPLETION_METHOD(resolve);
372+
373+
SET_WORKSPACE_METHOD(didDeleteFiles);
374+
375+
set_method("initialize", callable_mp(this, &GDScriptLanguageProtocol::initialize));
376+
set_method("initialized", callable_mp(this, &GDScriptLanguageProtocol::initialized));
377+
345378
workspace->root = ProjectSettings::get_singleton()->get_resource_path();
346379
}
380+
381+
#undef SET_DOCUMENT_METHOD
382+
#undef SET_COMPLETION_METHOD
383+
#undef SET_WORKSPACE_METHOD

modules/gdscript/language_server/gdscript_text_document.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class GDScriptTextDocument : public RefCounted {
4444

4545
Ref<FileAccess> file_checker;
4646

47+
Array native_member_completions;
48+
49+
private:
50+
Array find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list);
51+
LSP::TextDocumentItem load_document_item(const Variant &p_param);
52+
void notify_client_show_symbol(const LSP::DocumentSymbol *symbol);
53+
54+
public:
4755
void didOpen(const Variant &p_param);
4856
void didClose(const Variant &p_param);
4957
void didChange(const Variant &p_param);
@@ -54,14 +62,6 @@ class GDScriptTextDocument : public RefCounted {
5462
void sync_script_content(const String &p_path, const String &p_content);
5563
void show_native_symbol_in_editor(const String &p_symbol_id);
5664

57-
Array native_member_completions;
58-
59-
private:
60-
Array find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list);
61-
LSP::TextDocumentItem load_document_item(const Variant &p_param);
62-
void notify_client_show_symbol(const LSP::DocumentSymbol *symbol);
63-
64-
public:
6565
Variant nativeSymbol(const Dictionary &p_params);
6666
Array documentSymbol(const Dictionary &p_params);
6767
Array completion(const Dictionary &p_params);

modules/gdscript/language_server/gdscript_workspace.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
void GDScriptWorkspace::_bind_methods() {
4747
ClassDB::bind_method(D_METHOD("apply_new_signal"), &GDScriptWorkspace::apply_new_signal);
48-
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::did_delete_files);
48+
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::didDeleteFiles);
4949
ClassDB::bind_method(D_METHOD("parse_script", "path", "content"), &GDScriptWorkspace::parse_script);
5050
ClassDB::bind_method(D_METHOD("parse_local_script", "path"), &GDScriptWorkspace::parse_local_script);
5151
ClassDB::bind_method(D_METHOD("get_file_path", "uri"), &GDScriptWorkspace::get_file_path);
@@ -106,7 +106,7 @@ void GDScriptWorkspace::apply_new_signal(Object *obj, String function, PackedStr
106106
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
107107
}
108108

109-
void GDScriptWorkspace::did_delete_files(const Dictionary &p_params) {
109+
void GDScriptWorkspace::didDeleteFiles(const Dictionary &p_params) {
110110
Array files = p_params["files"];
111111
for (int i = 0; i < files.size(); ++i) {
112112
Dictionary file = files[i];

modules/gdscript/language_server/gdscript_workspace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class GDScriptWorkspace : public RefCounted {
9090
void resolve_document_links(const String &p_uri, List<LSP::DocumentLink> &r_list);
9191
Dictionary generate_script_api(const String &p_path);
9292
Error resolve_signature(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::SignatureHelp &r_signature);
93-
void did_delete_files(const Dictionary &p_params);
93+
void didDeleteFiles(const Dictionary &p_params);
9494
Dictionary rename(const LSP::TextDocumentPositionParams &p_doc_pos, const String &new_name);
9595
bool can_rename(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::DocumentSymbol &r_symbol, LSP::Range &r_range);
9696
Vector<LSP::Location> find_usages_in_file(const LSP::DocumentSymbol &p_symbol, const String &p_file_path);

modules/jsonrpc/jsonrpc.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* jsonrpc.compat.inc */
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+
#ifndef DISABLE_DEPRECATED
32+
33+
void JSONRPC::_set_scope_bind_compat_104890(const String &p_scope, Object *p_obj) {
34+
ERR_PRINT("JSONRPC::set_scope is not supported anymore. Upgrade to JSONRPC::set_method.");
35+
}
36+
37+
void JSONRPC::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("set_scope", "scope", "target"), &JSONRPC::_set_scope_bind_compat_104890);
39+
}
40+
41+
#endif

modules/jsonrpc/jsonrpc.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "jsonrpc.h"
32+
#include "jsonrpc.compat.inc"
3233

3334
#include "core/io/json.h"
3435

@@ -39,7 +40,7 @@ JSONRPC::~JSONRPC() {
3940
}
4041

4142
void JSONRPC::_bind_methods() {
42-
ClassDB::bind_method(D_METHOD("set_scope", "scope", "target"), &JSONRPC::set_scope);
43+
ClassDB::bind_method(D_METHOD("set_method", "name", "callback"), &JSONRPC::set_method);
4344
ClassDB::bind_method(D_METHOD("process_action", "action", "recurse"), &JSONRPC::process_action, DEFVAL(false));
4445
ClassDB::bind_method(D_METHOD("process_string", "action"), &JSONRPC::process_string);
4546

@@ -113,12 +114,6 @@ Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elem
113114
}
114115
}
115116

116-
Object *object = this;
117-
if (method_scopes.has(method.get_base_dir())) {
118-
object = method_scopes[method.get_base_dir()];
119-
method = method.get_file();
120-
}
121-
122117
Variant id;
123118
if (dict.has("id")) {
124119
id = dict["id"];
@@ -129,13 +124,13 @@ Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elem
129124
}
130125
}
131126

132-
if (object == nullptr || !object->has_method(method)) {
133-
ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
134-
} else {
135-
Variant call_ret = object->callv(method, args);
127+
if (methods.has(method)) {
128+
Variant call_ret = methods[method].callv(args);
136129
if (id.get_type() != Variant::NIL) {
137130
ret = make_response(call_ret, id);
138131
}
132+
} else {
133+
ret = make_response_error(JSONRPC::METHOD_NOT_FOUND, "Method not found: " + method, id);
139134
}
140135
} else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) {
141136
Array arr = p_action;
@@ -175,6 +170,6 @@ String JSONRPC::process_string(const String &p_input) {
175170
return ret.to_json_string();
176171
}
177172

178-
void JSONRPC::set_scope(const String &p_scope, Object *p_obj) {
179-
method_scopes[p_scope] = p_obj;
173+
void JSONRPC::set_method(const String &p_name, const Callable &p_callback) {
174+
methods[p_name] = p_callback;
180175
}

modules/jsonrpc/jsonrpc.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@
3636
class JSONRPC : public Object {
3737
GDCLASS(JSONRPC, Object)
3838

39-
HashMap<String, Object *> method_scopes;
39+
HashMap<String, Callable> methods;
4040

4141
protected:
4242
static void _bind_methods();
4343

44+
#ifndef DISABLE_DEPRECATED
45+
void _set_scope_bind_compat_104890(const String &p_scope, Object *p_obj);
46+
static void _bind_compatibility_methods();
47+
#endif
48+
4449
public:
4550
JSONRPC();
4651
~JSONRPC();
@@ -61,7 +66,7 @@ class JSONRPC : public Object {
6166
Variant process_action(const Variant &p_action, bool p_process_arr_elements = false);
6267
String process_string(const String &p_input);
6368

64-
void set_scope(const String &p_scope, Object *p_obj);
69+
void set_method(const String &p_name, const Callable &p_callback);
6570
};
6671

6772
VARIANT_ENUM_CAST(JSONRPC::ErrorCode);

modules/jsonrpc/tests/test_jsonrpc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ String TestClassJSONRPC::something(const String &p_in) {
5757
return p_in + ", please";
5858
}
5959

60-
void TestClassJSONRPC::_bind_methods() {
61-
ClassDB::bind_method(D_METHOD("something", "in"), &TestClassJSONRPC::something);
62-
}
63-
6460
void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) {
6561
TestClassJSONRPC json_rpc = TestClassJSONRPC();
6662
const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements);

modules/jsonrpc/tests/test_jsonrpc.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,17 @@ TEST_CASE("[JSONRPC] process_string invalid") {
6060
}
6161

6262
class TestClassJSONRPC : public JSONRPC {
63-
GDCLASS(TestClassJSONRPC, JSONRPC)
64-
6563
public:
66-
String something(const String &p_in);
64+
TestClassJSONRPC() {
65+
set_method("something", callable_mp(this, &TestClassJSONRPC::something));
66+
}
6767

68-
protected:
69-
static void _bind_methods();
68+
String something(const String &p_in);
7069
};
7170

7271
void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
7372

7473
TEST_CASE("[JSONRPC] process_action Dictionary") {
75-
ClassDB::register_class<TestClassJSONRPC>();
76-
7774
Dictionary in_dict = Dictionary();
7875
in_dict["method"] = "something";
7976
in_dict["id"] = "ID";

0 commit comments

Comments
 (0)