Skip to content

Commit 92e718f

Browse files
committed
Allow submitting documentation to the Godot editor
1 parent 54fe2f9 commit 92e718f

File tree

8 files changed

+105
-3
lines changed

8 files changed

+105
-3
lines changed

gdextension/gdextension_interface.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,31 @@ typedef void (*GDExtensionInterfaceEditorAddPlugin)(GDExtensionConstStringNamePt
28352835
*/
28362836
typedef void (*GDExtensionInterfaceEditorRemovePlugin)(GDExtensionConstStringNamePtr p_class_name);
28372837

2838+
/**
2839+
* @name editor_help_load_xml_from_utf8_chars
2840+
* @since 4.3
2841+
*
2842+
* Loads new XML-formatted documentation data in the editor.
2843+
*
2844+
* The provided pointer can be immediately freed once the function returns.
2845+
*
2846+
* @param p_data A pointer to an UTF-8 encoded C string (null terminated).
2847+
*/
2848+
typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars)(const char *p_data);
2849+
2850+
/**
2851+
* @name editor_help_load_xml_from_utf8_chars_and_len
2852+
* @since 4.3
2853+
*
2854+
* Loads new XML-formatted documentation data in the editor.
2855+
*
2856+
* The provided pointer can be immediately freed once the function returns.
2857+
*
2858+
* @param p_data A pointer to an UTF-8 encoded C string.
2859+
* @param p_size The number of bytes (not code units).
2860+
*/
2861+
typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen)(const char *p_data, GDExtensionInt p_size);
2862+
28382863
#ifdef __cplusplus
28392864
}
28402865
#endif

include/godot_cpp/godot.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ extern "C" GDExtensionInterfaceClassdbUnregisterExtensionClass gdextension_inter
190190
extern "C" GDExtensionInterfaceGetLibraryPath gdextension_interface_get_library_path;
191191
extern "C" GDExtensionInterfaceEditorAddPlugin gdextension_interface_editor_add_plugin;
192192
extern "C" GDExtensionInterfaceEditorRemovePlugin gdextension_interface_editor_remove_plugin;
193+
extern "C" GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars gdextension_interface_editor_help_load_xml_from_utf8_chars;
194+
extern "C" GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len;
193195

194196
} // namespace internal
195197

src/godot.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ GDExtensionInterfaceClassdbUnregisterExtensionClass gdextension_interface_classd
196196
GDExtensionInterfaceGetLibraryPath gdextension_interface_get_library_path = nullptr;
197197
GDExtensionInterfaceEditorAddPlugin gdextension_interface_editor_add_plugin = nullptr;
198198
GDExtensionInterfaceEditorRemovePlugin gdextension_interface_editor_remove_plugin = nullptr;
199+
GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars gdextension_interface_editor_help_load_xml_from_utf8_chars = nullptr;
200+
GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len = nullptr;
199201

200202
} // namespace internal
201203

@@ -436,6 +438,8 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
436438
LOAD_PROC_ADDRESS(get_library_path, GDExtensionInterfaceGetLibraryPath);
437439
LOAD_PROC_ADDRESS(editor_add_plugin, GDExtensionInterfaceEditorAddPlugin);
438440
LOAD_PROC_ADDRESS(editor_remove_plugin, GDExtensionInterfaceEditorRemovePlugin);
441+
LOAD_PROC_ADDRESS(editor_help_load_xml_from_utf8_chars, GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars);
442+
LOAD_PROC_ADDRESS(editor_help_load_xml_from_utf8_chars_and_len, GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen);
439443

440444
r_initialization->initialize = initialize_level;
441445
r_initialization->deinitialize = deinitialize_level;

test/SConstruct

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ env = SConscript("../SConstruct")
1616
env.Append(CPPPATH=["src/"])
1717
sources = Glob("src/*.cpp")
1818

19+
doc_header = env.GodotCPPDocHeader("src/doc_data.gen.h", source=Glob("doc_classes/*.xml"))
20+
#sources.append(doc_header)
21+
1922
if env["platform"] == "macos":
2023
library = env.SharedLibrary(
2124
"project/bin/libgdexample.{}.{}.framework/libgdexample.{}.{}".format(
@@ -40,4 +43,4 @@ else:
4043
source=sources,
4144
)
4245

43-
Default(library)
46+
Default(doc_header, library)

test/doc_classes/Example.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="Example" inherits="Control">
3+
<brief_description>
4+
A test control defined in GDExtension.
5+
</brief_description>
6+
<description>
7+
A control used for the automated GDExtension tests.
8+
</description>
9+
<tutorials>
10+
</tutorials>
11+
<methods>
12+
<method name="simple_func">
13+
<return type="void" />
14+
<description>
15+
Tests a simple function call.
16+
</description>
17+
</method>
18+
</methods>
19+
<members>
20+
</members>
21+
<signals>
22+
</signals>
23+
<constants>
24+
</constants>
25+
</class>

test/project/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ config_version=5
1212

1313
config/name="GDExtension Test Project"
1414
run/main_scene="res://main.tscn"
15-
config/features=PackedStringArray("4.2")
15+
config/features=PackedStringArray("4.3")
1616
config/icon="res://icon.png"
1717

1818
[native_extensions]

test/src/register_types.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@
1414
#include "example.h"
1515
#include "tests.h"
1616

17+
#if defined(TOOLS_ENABLED) || defined(DEBUG_ENABLED)
18+
#include "doc_data.gen.h"
19+
#endif
20+
1721
using namespace godot;
1822

1923
void initialize_example_module(ModuleInitializationLevel p_level) {
24+
#if defined(TOOLS_ENABLED) || defined(DEBUG_ENABLED)
25+
if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
26+
internal::gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len(reinterpret_cast<const char *>(_doc_data_uncompressed), _doc_data_uncompressed_size);
27+
}
28+
#endif
29+
2030
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
2131
return;
2232
}

tools/godotcpp.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,38 @@ def options(opts, env):
325325
tool.options(opts)
326326

327327

328+
def make_doc_header(target, source, env):
329+
dst = str(target[0])
330+
g = open(dst, "w", encoding="utf-8")
331+
buf = ""
332+
docbegin = ""
333+
docend = ""
334+
for src in source:
335+
src_path = str(src)
336+
if not src_path.endswith(".xml"):
337+
continue
338+
with open(src_path, "r", encoding="utf-8") as f:
339+
content = f.read()
340+
buf += content
341+
342+
buf = (docbegin + buf + docend).encode("utf-8")
343+
decomp_size = len(buf)
344+
345+
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
346+
g.write("#ifndef _GODOT_CPP_DOC_DATA_RAW_H\n")
347+
g.write("#define _GODOT_CPP_DOC_DATA_RAW_H\n")
348+
g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
349+
g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
350+
g.write("static const unsigned char _doc_data_uncompressed[] = {\n")
351+
for i in range(len(buf)):
352+
g.write("\t" + str(buf[i]) + ",\n")
353+
g.write("};\n")
354+
355+
g.write("#endif")
356+
357+
g.close()
358+
359+
328360
def generate(env):
329361
# Default num_jobs to local cpu count if not user specified.
330362
# SCons has a peculiarity where user-specified options won't be overridden
@@ -451,7 +483,8 @@ def generate(env):
451483
# Builders
452484
env.Append(
453485
BUILDERS={
454-
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files)
486+
"GodotCPPBindings": Builder(action=Action(scons_generate_bindings, "$GENCOMSTR"), emitter=scons_emit_files),
487+
"GodotCPPDocHeader": Builder(action=make_doc_header),
455488
}
456489
)
457490
env.AddMethod(_godot_cpp, "GodotCPP")

0 commit comments

Comments
 (0)