Skip to content

Commit 8519b44

Browse files
committed
Allow submitting documentation to the Godot editor
1 parent 36847f6 commit 8519b44

File tree

8 files changed

+104
-2
lines changed

8 files changed

+104
-2
lines changed

gdextension/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 (*GDExtensionsInterfaceEditorHelpLoadXML)(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 (*GDExtensionsInterfaceEditorHelpLoadXMLAndLen)(const char *p_data, GDExtensionInt p_size);
2644+
26202645
#ifdef __cplusplus
26212646
}
26222647
#endif

include/godot_cpp/godot.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ extern "C" GDExtensionInterfaceClassdbUnregisterExtensionClass gdextension_inter
187187
extern "C" GDExtensionInterfaceGetLibraryPath gdextension_interface_get_library_path;
188188
extern "C" GDExtensionInterfaceEditorAddPlugin gdextension_interface_editor_add_plugin;
189189
extern "C" GDExtensionInterfaceEditorRemovePlugin gdextension_interface_editor_remove_plugin;
190+
extern "C" GDExtensionsInterfaceEditorHelpLoadXML gdextension_interface_editor_help_load_xml_from_utf8_chars;
191+
extern "C" GDExtensionsInterfaceEditorHelpLoadXMLAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len;
190192

191193
} // namespace internal
192194

src/godot.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ GDExtensionInterfaceClassdbUnregisterExtensionClass gdextension_interface_classd
193193
GDExtensionInterfaceGetLibraryPath gdextension_interface_get_library_path = nullptr;
194194
GDExtensionInterfaceEditorAddPlugin gdextension_interface_editor_add_plugin = nullptr;
195195
GDExtensionInterfaceEditorRemovePlugin gdextension_interface_editor_remove_plugin = nullptr;
196+
GDExtensionsInterfaceEditorHelpLoadXML gdextension_interface_editor_help_load_xml_from_utf8_chars = nullptr;
197+
GDExtensionsInterfaceEditorHelpLoadXMLAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len = nullptr;
196198

197199
} // namespace internal
198200

@@ -430,6 +432,8 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
430432
LOAD_PROC_ADDRESS(get_library_path, GDExtensionInterfaceGetLibraryPath);
431433
LOAD_PROC_ADDRESS(editor_add_plugin, GDExtensionInterfaceEditorAddPlugin);
432434
LOAD_PROC_ADDRESS(editor_remove_plugin, GDExtensionInterfaceEditorRemovePlugin);
435+
LOAD_PROC_ADDRESS(editor_help_load_xml_from_utf8_chars, GDExtensionsInterfaceEditorHelpLoadXML);
436+
LOAD_PROC_ADDRESS(editor_help_load_xml_from_utf8_chars_and_len, GDExtensionsInterfaceEditorHelpLoadXMLAndLen);
433437

434438
r_initialization->initialize = initialize_level;
435439
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,38 @@ def options(opts, env):
209209
target_tool.options(opts)
210210

211211

212+
def make_doc_header(target, source, env):
213+
dst = str(target[0])
214+
g = open(dst, "w", encoding="utf-8")
215+
buf = ""
216+
docbegin = ""
217+
docend = ""
218+
for src in source:
219+
src_path = str(src)
220+
if not src_path.endswith(".xml"):
221+
continue
222+
with open(src_path, "r", encoding="utf-8") as f:
223+
content = f.read()
224+
buf += content
225+
226+
buf = (docbegin + buf + docend).encode("utf-8")
227+
decomp_size = len(buf)
228+
229+
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
230+
g.write("#ifndef _GODOT_CPP_DOC_DATA_RAW_H\n")
231+
g.write("#define _GODOT_CPP_DOC_DATA_RAW_H\n")
232+
g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
233+
g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
234+
g.write("static const unsigned char _doc_data_uncompressed[] = {\n")
235+
for i in range(len(buf)):
236+
g.write("\t" + str(buf[i]) + ",\n")
237+
g.write("};\n")
238+
239+
g.write("#endif")
240+
241+
g.close()
242+
243+
212244
def generate(env):
213245
# Default num_jobs to local cpu count if not user specified.
214246
# SCons has a peculiarity where user-specified options won't be overridden
@@ -317,6 +349,7 @@ def generate(env):
317349

318350
# Builders
319351
env.Append(BUILDERS={"GodotCPPBindings": Builder(action=scons_generate_bindings, emitter=scons_emit_files)})
352+
env.Append(BUILDERS={"GodotCPPDocHeader": Builder(action=make_doc_header)})
320353
env.AddMethod(_godot_cpp, "GodotCPP")
321354

322355

0 commit comments

Comments
 (0)