1
0
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:
HolonProduction
2025-04-01 20:19:49 +02:00
parent d52b84e472
commit e2c3731cdd
11 changed files with 124 additions and 43 deletions

View File

@@ -69,11 +69,14 @@
<description>
</description>
</method>
<method name="set_scope">
<method name="set_method">
<return type="void" />
<param index="0" name="scope" type="String" />
<param index="1" name="target" type="Object" />
<param index="0" name="name" type="String" />
<param index="1" name="callback" type="Callable" />
<description>
Registers a callback for the given method name.
- [param name] The name that clients can use to access the callback.
- [param callback] The callback which will handle the specific method.
</description>
</method>
</methods>

View File

@@ -24,3 +24,10 @@ Validate extension JSON: Error: Field 'classes/OpenXRAPIExtension/methods/regist
Validate extension JSON: Error: Field 'classes/OpenXRAPIExtension/methods/unregister_projection_views_extension/arguments/0': type changed value in new API, from "OpenXRExtensionWrapperExtension" to "OpenXRExtensionWrapper".
Switched from `OpenXRExtensionWrapperExtension` to parent `OpenXRExtensionWrapper`. Compatibility methods registered.
GH-104890
---------
Validate extension JSON: API was removed: classes/JSONRPC/methods/set_scope
Replaced `set_scope` with `set_method`. Compatibility method registered for binary compatibility. Manual upgrade required by users to retain functionality.

View File

@@ -334,13 +334,50 @@ bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const {
return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor"));
}
// clang-format off
#define SET_DOCUMENT_METHOD(m_method) set_method(_STR(textDocument/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
#define SET_COMPLETION_METHOD(m_method) set_method(_STR(completionItem/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
#define SET_WORKSPACE_METHOD(m_method) set_method(_STR(workspace/m_method), callable_mp(workspace.ptr(), &GDScriptWorkspace::m_method))
// clang-format on
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
server.instantiate();
singleton = this;
workspace.instantiate();
text_document.instantiate();
set_scope("textDocument", text_document.ptr());
set_scope("completionItem", text_document.ptr());
set_scope("workspace", workspace.ptr());
SET_DOCUMENT_METHOD(didOpen);
SET_DOCUMENT_METHOD(didClose);
SET_DOCUMENT_METHOD(didChange);
SET_DOCUMENT_METHOD(willSaveWaitUntil);
SET_DOCUMENT_METHOD(didSave);
SET_DOCUMENT_METHOD(documentSymbol);
SET_DOCUMENT_METHOD(completion);
SET_DOCUMENT_METHOD(rename);
SET_DOCUMENT_METHOD(prepareRename);
SET_DOCUMENT_METHOD(references);
SET_DOCUMENT_METHOD(foldingRange);
SET_DOCUMENT_METHOD(codeLens);
SET_DOCUMENT_METHOD(documentLink);
SET_DOCUMENT_METHOD(colorPresentation);
SET_DOCUMENT_METHOD(hover);
SET_DOCUMENT_METHOD(definition);
SET_DOCUMENT_METHOD(declaration);
SET_DOCUMENT_METHOD(signatureHelp);
SET_DOCUMENT_METHOD(nativeSymbol); // Custom method.
SET_COMPLETION_METHOD(resolve);
SET_WORKSPACE_METHOD(didDeleteFiles);
set_method("initialize", callable_mp(this, &GDScriptLanguageProtocol::initialize));
set_method("initialized", callable_mp(this, &GDScriptLanguageProtocol::initialized));
workspace->root = ProjectSettings::get_singleton()->get_resource_path();
}
#undef SET_DOCUMENT_METHOD
#undef SET_COMPLETION_METHOD
#undef SET_WORKSPACE_METHOD

View File

@@ -44,6 +44,14 @@ protected:
Ref<FileAccess> file_checker;
Array native_member_completions;
private:
Array find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list);
LSP::TextDocumentItem load_document_item(const Variant &p_param);
void notify_client_show_symbol(const LSP::DocumentSymbol *symbol);
public:
void didOpen(const Variant &p_param);
void didClose(const Variant &p_param);
void didChange(const Variant &p_param);
@@ -54,14 +62,6 @@ protected:
void sync_script_content(const String &p_path, const String &p_content);
void show_native_symbol_in_editor(const String &p_symbol_id);
Array native_member_completions;
private:
Array find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list);
LSP::TextDocumentItem load_document_item(const Variant &p_param);
void notify_client_show_symbol(const LSP::DocumentSymbol *symbol);
public:
Variant nativeSymbol(const Dictionary &p_params);
Array documentSymbol(const Dictionary &p_params);
Array completion(const Dictionary &p_params);

View File

@@ -45,7 +45,7 @@
void GDScriptWorkspace::_bind_methods() {
ClassDB::bind_method(D_METHOD("apply_new_signal"), &GDScriptWorkspace::apply_new_signal);
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::did_delete_files);
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::didDeleteFiles);
ClassDB::bind_method(D_METHOD("parse_script", "path", "content"), &GDScriptWorkspace::parse_script);
ClassDB::bind_method(D_METHOD("parse_local_script", "path"), &GDScriptWorkspace::parse_local_script);
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
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
}
void GDScriptWorkspace::did_delete_files(const Dictionary &p_params) {
void GDScriptWorkspace::didDeleteFiles(const Dictionary &p_params) {
Array files = p_params["files"];
for (int i = 0; i < files.size(); ++i) {
Dictionary file = files[i];

View File

@@ -90,7 +90,7 @@ public:
void resolve_document_links(const String &p_uri, List<LSP::DocumentLink> &r_list);
Dictionary generate_script_api(const String &p_path);
Error resolve_signature(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::SignatureHelp &r_signature);
void did_delete_files(const Dictionary &p_params);
void didDeleteFiles(const Dictionary &p_params);
Dictionary rename(const LSP::TextDocumentPositionParams &p_doc_pos, const String &new_name);
bool can_rename(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::DocumentSymbol &r_symbol, LSP::Range &r_range);
Vector<LSP::Location> find_usages_in_file(const LSP::DocumentSymbol &p_symbol, const String &p_file_path);

View 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

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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";