Skip to content

Commit 07d290e

Browse files
committed
Merge pull request #83747 from Riteo/gdext-doc
GDExtension: Add an interface for loading extra documentation
2 parents e2c5d2f + f468e59 commit 07d290e

9 files changed

+134
-0
lines changed

core/extension/gdextension.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_libra
653653
if (!ext->is_reloading) {
654654
self->extension_classes.erase(class_name);
655655
}
656+
657+
GDExtensionEditorHelp::remove_class(class_name);
656658
#else
657659
self->extension_classes.erase(class_name);
658660
#endif
@@ -1196,4 +1198,17 @@ void GDExtensionEditorPlugins::remove_extension_class(const StringName &p_class_
11961198
extension_classes.erase(p_class_name);
11971199
}
11981200
}
1201+
1202+
GDExtensionEditorHelp::EditorHelpLoadXmlBufferFunc GDExtensionEditorHelp::editor_help_load_xml_buffer = nullptr;
1203+
GDExtensionEditorHelp::EditorHelpRemoveClassFunc GDExtensionEditorHelp::editor_help_remove_class = nullptr;
1204+
1205+
void GDExtensionEditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) {
1206+
ERR_FAIL_NULL(editor_help_load_xml_buffer);
1207+
editor_help_load_xml_buffer(p_buffer, p_size);
1208+
}
1209+
1210+
void GDExtensionEditorHelp::remove_class(const String &p_class) {
1211+
ERR_FAIL_NULL(editor_help_remove_class);
1212+
editor_help_remove_class(p_class);
1213+
}
11991214
#endif // TOOLS_ENABLED

core/extension/gdextension.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ class GDExtensionEditorPlugins {
197197
return extension_classes;
198198
}
199199
};
200+
201+
class GDExtensionEditorHelp {
202+
protected:
203+
friend class EditorHelp;
204+
205+
// Similarly to EditorNode above, we need to be able to ask EditorHelp to parse
206+
// new documentation data. Note though that, differently from EditorHelp, this
207+
// is initialized even _before_ it gets instantiated, as we need to rely on
208+
// this method while initializing the engine.
209+
typedef void (*EditorHelpLoadXmlBufferFunc)(const uint8_t *p_buffer, int p_size);
210+
static EditorHelpLoadXmlBufferFunc editor_help_load_xml_buffer;
211+
212+
typedef void (*EditorHelpRemoveClassFunc)(const String &p_class);
213+
static EditorHelpRemoveClassFunc editor_help_remove_class;
214+
215+
public:
216+
static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
217+
static void remove_class(const String &p_class);
218+
};
219+
200220
#endif // TOOLS_ENABLED
201221

202222
#endif // GDEXTENSION_H

core/extension/gdextension_interface.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "core/variant/variant.h"
4343
#include "core/version.h"
4444

45+
#include <string.h>
46+
4547
class CallableCustomExtension : public CallableCustom {
4648
void *userdata;
4749
void *token;
@@ -1373,6 +1375,19 @@ static void gdextension_editor_remove_plugin(GDExtensionConstStringNamePtr p_cla
13731375
#endif
13741376
}
13751377

1378+
static void gdextension_editor_help_load_xml_from_utf8_chars_and_len(const char *p_data, GDExtensionInt p_size) {
1379+
#ifdef TOOLS_ENABLED
1380+
GDExtensionEditorHelp::load_xml_buffer((const uint8_t *)p_data, p_size);
1381+
#endif
1382+
}
1383+
1384+
static void gdextension_editor_help_load_xml_from_utf8_chars(const char *p_data) {
1385+
#ifdef TOOLS_ENABLED
1386+
size_t len = strlen(p_data);
1387+
gdextension_editor_help_load_xml_from_utf8_chars_and_len(p_data, len);
1388+
#endif
1389+
}
1390+
13761391
#define REGISTER_INTERFACE_FUNC(m_name) GDExtension::register_interface_function(#m_name, (GDExtensionInterfaceFunctionPtr)&gdextension_##m_name)
13771392

13781393
void gdextension_setup_interface() {
@@ -1516,6 +1531,8 @@ void gdextension_setup_interface() {
15161531
REGISTER_INTERFACE_FUNC(classdb_get_class_tag);
15171532
REGISTER_INTERFACE_FUNC(editor_add_plugin);
15181533
REGISTER_INTERFACE_FUNC(editor_remove_plugin);
1534+
REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars);
1535+
REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars_and_len);
15191536
}
15201537

15211538
#undef REGISTER_INTERFACE_FUNCTION

core/extension/gdextension_interface.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,31 @@ typedef void (*GDExtensionInterfaceEditorAddPlugin)(GDExtensionConstStringNamePt
26172617
*/
26182618
typedef void (*GDExtensionInterfaceEditorRemovePlugin)(GDExtensionConstStringNamePtr p_class_name);
26192619

2620+
/**
2621+
* @name editor_help_load_xml_from_utf8_chars
2622+
* @since 4.3
2623+
*
2624+
* Loads new XML-formatted documentation data in the editor.
2625+
*
2626+
* The provided pointer can be immediately freed once the function returns.
2627+
*
2628+
* @param p_data A pointer to an UTF-8 encoded C string (null terminated).
2629+
*/
2630+
typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars)(const char *p_data);
2631+
2632+
/**
2633+
* @name editor_help_load_xml_from_utf8_chars_and_len
2634+
* @since 4.3
2635+
*
2636+
* Loads new XML-formatted documentation data in the editor.
2637+
*
2638+
* The provided pointer can be immediately freed once the function returns.
2639+
*
2640+
* @param p_data A pointer to an UTF-8 encoded C string.
2641+
* @param p_size The number of bytes (not code units).
2642+
*/
2643+
typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen)(const char *p_data, GDExtensionInt p_size);
2644+
26202645
#ifdef __cplusplus
26212646
}
26222647
#endif

editor/doc_tools.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,3 +1652,15 @@ Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, in
16521652

16531653
return OK;
16541654
}
1655+
1656+
Error DocTools::load_xml(const uint8_t *p_data, int p_size) {
1657+
Ref<XMLParser> parser = memnew(XMLParser);
1658+
Error err = parser->_open_buffer(p_data, p_size);
1659+
if (err) {
1660+
return err;
1661+
}
1662+
1663+
_load(parser);
1664+
1665+
return OK;
1666+
}

editor/doc_tools.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class DocTools {
5656

5757
Error _load(Ref<XMLParser> parser);
5858
Error load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size);
59+
Error load_xml(const uint8_t *p_data, int p_size);
5960
};
6061

6162
#endif // DOC_TOOLS_H

editor/editor_help.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "editor_help.h"
3232

3333
#include "core/core_constants.h"
34+
#include "core/extension/gdextension.h"
3435
#include "core/input/input.h"
3536
#include "core/object/script_language.h"
3637
#include "core/os/keyboard.h"
@@ -83,6 +84,7 @@ const Vector<String> classes_with_csharp_differences = {
8384
// TODO: this is sometimes used directly as doc->something, other times as EditorHelp::get_doc_data(), which is thread-safe.
8485
// Might this be a problem?
8586
DocTools *EditorHelp::doc = nullptr;
87+
DocTools *EditorHelp::ext_doc = nullptr;
8688

8789
static bool _attempt_doc_load(const String &p_class) {
8890
// Docgen always happens in the outer-most class: it also generates docs for inner classes.
@@ -2369,6 +2371,28 @@ String EditorHelp::get_cache_full_path() {
23692371
return EditorPaths::get_singleton()->get_cache_dir().path_join("editor_doc_cache.res");
23702372
}
23712373

2374+
void EditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) {
2375+
if (!ext_doc) {
2376+
ext_doc = memnew(DocTools);
2377+
}
2378+
2379+
ext_doc->load_xml(p_buffer, p_size);
2380+
2381+
if (doc) {
2382+
doc->load_xml(p_buffer, p_size);
2383+
}
2384+
}
2385+
2386+
void EditorHelp::remove_class(const String &p_class) {
2387+
if (ext_doc && ext_doc->has_doc(p_class)) {
2388+
ext_doc->remove_doc(p_class);
2389+
}
2390+
2391+
if (doc && doc->has_doc(p_class)) {
2392+
doc->remove_doc(p_class);
2393+
}
2394+
}
2395+
23722396
void EditorHelp::_load_doc_thread(void *p_udata) {
23732397
Ref<Resource> cache_res = ResourceLoader::load(get_cache_full_path());
23742398
if (cache_res.is_valid() && cache_res->get_meta("version_hash", "") == doc_version_hash) {
@@ -2416,6 +2440,11 @@ void EditorHelp::_gen_doc_thread(void *p_udata) {
24162440

24172441
void EditorHelp::_gen_extensions_docs() {
24182442
doc->generate((DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES | DocTools::GENERATE_FLAG_EXTENSION_CLASSES_ONLY));
2443+
2444+
// Append extra doc data, as it gets overridden by the generation step.
2445+
if (ext_doc) {
2446+
doc->merge_from(*ext_doc);
2447+
}
24192448
}
24202449

24212450
void EditorHelp::generate_doc(bool p_use_cache) {
@@ -2554,6 +2583,11 @@ void EditorHelp::_bind_methods() {
25542583
ADD_SIGNAL(MethodInfo("go_to_help"));
25552584
}
25562585

2586+
void EditorHelp::init_gdext_pointers() {
2587+
GDExtensionEditorHelp::editor_help_load_xml_buffer = &EditorHelp::load_xml_buffer;
2588+
GDExtensionEditorHelp::editor_help_remove_class = &EditorHelp::remove_class;
2589+
}
2590+
25572591
EditorHelp::EditorHelp() {
25582592
set_custom_minimum_size(Size2(150 * EDSCALE, 0));
25592593

editor/editor_help.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class EditorHelp : public VBoxContainer {
113113
RichTextLabel *class_desc = nullptr;
114114
HSplitContainer *h_split = nullptr;
115115
static DocTools *doc;
116+
static DocTools *ext_doc;
116117

117118
ConfirmationDialog *search_dialog = nullptr;
118119
LineEdit *search = nullptr;
@@ -209,6 +210,9 @@ class EditorHelp : public VBoxContainer {
209210
static void cleanup_doc();
210211
static String get_cache_full_path();
211212

213+
static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
214+
static void remove_class(const String &p_class);
215+
212216
void go_to_help(const String &p_help);
213217
void go_to_class(const String &p_class);
214218
void update_doc();
@@ -228,6 +232,8 @@ class EditorHelp : public VBoxContainer {
228232

229233
void update_toggle_scripts_button();
230234

235+
static void init_gdext_pointers();
236+
231237
EditorHelp();
232238
~EditorHelp();
233239
};

editor/register_editor_types.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ void register_editor_types() {
282282
ei_singleton.editor_only = true;
283283
Engine::get_singleton()->add_singleton(ei_singleton);
284284

285+
// Required as GDExtensions can register docs at init time way before this
286+
// class is actually instantiated.
287+
EditorHelp::init_gdext_pointers();
288+
285289
OS::get_singleton()->benchmark_end_measure("Editor", "Register Types");
286290
}
287291

0 commit comments

Comments
 (0)