Skip to content

Commit

Permalink
Add support for scene/resource customization in export plugins
Browse files Browse the repository at this point in the history
EditorExportPlugin adds a set of callbacks to allow customizing scenes, resources or subresources in all files exported:
* Can take scene files, resource files and subresources in all of them.
* Uses a cache for the converted files if nothing changes, so this work only happens if a file is modified.
* Uses hashing to differentiate export configuration caches.
* Removed the previous conversion code to binary, as this one uses existing stuff.

This API is useful in several scenarios:
* Needed by the "server" export platform to get rid of textures, meshes, audio, etc.
* Needed by text to binary converters.
* Needed by eventual optimizations such as shader precompiling on export, mesh merging and optimization, etc.

This is a draft, feedback is very welcome.
  • Loading branch information
reduz committed Sep 1, 2022
1 parent c40855f commit ef17c46
Show file tree
Hide file tree
Showing 13 changed files with 709 additions and 92 deletions.
26 changes: 26 additions & 0 deletions core/io/config_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/io/file_access_encrypted.h"
#include "core/os/keyboard.h"
#include "core/string/string_builder.h"
#include "core/variant/variant_parser.h"

PackedStringArray ConfigFile::_get_sections() const {
Expand Down Expand Up @@ -130,6 +131,28 @@ void ConfigFile::erase_section_key(const String &p_section, const String &p_key)
}
}

String ConfigFile::encode_to_text() const {
StringBuilder sb;
bool first = true;
for (const KeyValue<String, HashMap<String, Variant>> &E : values) {
if (first) {
first = false;
} else {
sb.append("\n");
}
if (!E.key.is_empty()) {
sb.append("[" + E.key + "]\n\n");
}

for (const KeyValue<String, Variant> &F : E.value) {
String vstr;
VariantWriter::write_to_string(F.value, vstr);
sb.append(F.key.property_name_encode() + "=" + vstr + "\n");
}
}
return sb.as_string();
}

Error ConfigFile::save(const String &p_path) {
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
Expand Down Expand Up @@ -295,6 +318,7 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream)
void ConfigFile::clear() {
values.clear();
}

void ConfigFile::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value);
ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant()));
Expand All @@ -312,6 +336,8 @@ void ConfigFile::_bind_methods() {
ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse);
ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);

ClassDB::bind_method(D_METHOD("encode_to_text"), &ConfigFile::encode_to_text);

BIND_METHOD_ERR_RETURN_DOC("load", ERR_FILE_CANT_OPEN);

ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
Expand Down
2 changes: 2 additions & 0 deletions core/io/config_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ConfigFile : public RefCounted {
Error load(const String &p_path);
Error parse(const String &p_data);

String encode_to_text() const; // used by exporter

void clear();

Error load_encrypted(const String &p_path, const Vector<uint8_t> &p_key);
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/ConfigFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
Removes the entire contents of the config.
</description>
</method>
<method name="encode_to_text" qualifiers="const">
<return type="String" />
<description>
Obtain the text version of this config file (the same text that would be written to a file).
</description>
</method>
<method name="erase_section">
<return type="void" />
<param index="0" name="section" type="String" />
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/EditorExportPlatform.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorExportPlatform" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
</class>
57 changes: 57 additions & 0 deletions doc/classes/EditorExportPlugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,51 @@
<tutorials>
</tutorials>
<methods>
<method name="_begin_customize_resources" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="platform" type="EditorExportPlatform" />
<param index="1" name="features" type="PackedStringArray" />
<description>
Return true if this plugin will customize resources based on the platform and features used.
</description>
</method>
<method name="_begin_customize_scenes" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="platform" type="EditorExportPlatform" />
<param index="1" name="features" type="PackedStringArray" />
<description>
Return true if this plugin will customize scenes based on the platform and features used.
</description>
</method>
<method name="_customize_resource" qualifiers="virtual">
<return type="Resource" />
<param index="0" name="resource" type="Resource" />
<param index="1" name="path" type="String" />
<description>
Customize a resource. If changes are made to it, return the same or a new resource. Otherwise, return [code]null[/code].
The [i]path[/i] argument is only used when customizing an actual file, otherwise this means that this resource is part of another one and it will be empty.
</description>
</method>
<method name="_customize_scene" qualifiers="virtual">
<return type="Node" />
<param index="0" name="scene" type="Node" />
<param index="1" name="path" type="String" />
<description>
Customize a scene. If changes are made to it, return the same or a new scene. Otherwise, return [code]null[/code]. If a new scene is returned, it is up to you to dispose of the old one.
</description>
</method>
<method name="_end_customize_resources" qualifiers="virtual">
<return type="void" />
<description>
This is called when the customization process for resources ends.
</description>
</method>
<method name="_end_customize_scenes" qualifiers="virtual">
<return type="void" />
<description>
This is called when the customization process for scenes ends.
</description>
</method>
<method name="_export_begin" qualifiers="virtual">
<return type="void" />
<param index="0" name="features" type="PackedStringArray" />
Expand All @@ -36,6 +81,18 @@
Calling [method skip] inside this callback will make the file not included in the export.
</description>
</method>
<method name="_get_customization_configuration_hash" qualifiers="virtual const">
<return type="int" />
<description>
Return a hash based on the configuration passed (for both scenes and resources). This helps keep separate caches for separate export configurations.
</description>
</method>
<method name="_get_name" qualifiers="virtual const">
<return type="String" />
<description>
Return the name identifier of this plugin (for future identification by the exporter).
</description>
</method>
<method name="add_file">
<return type="void" />
<param index="0" name="path" type="String" />
Expand Down
6 changes: 1 addition & 5 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4107,6 +4107,7 @@ void EditorNode::register_editor_types() {
GDREGISTER_CLASS(EditorSyntaxHighlighter);
GDREGISTER_ABSTRACT_CLASS(EditorInterface);
GDREGISTER_CLASS(EditorExportPlugin);
GDREGISTER_ABSTRACT_CLASS(EditorExportPlatform);
GDREGISTER_CLASS(EditorResourceConversionPlugin);
GDREGISTER_CLASS(EditorSceneFormatImporter);
GDREGISTER_CLASS(EditorScenePostImportPlugin);
Expand Down Expand Up @@ -7418,11 +7419,6 @@ EditorNode::EditorNode() {
editor_plugins_force_over = memnew(EditorPluginList);
editor_plugins_force_input_forwarding = memnew(EditorPluginList);

Ref<EditorExportTextSceneToBinaryPlugin> export_text_to_binary_plugin;
export_text_to_binary_plugin.instantiate();

EditorExport::get_singleton()->add_export_plugin(export_text_to_binary_plugin);

Ref<GDExtensionExportPlugin> gdextension_export_plugin;
gdextension_export_plugin.instantiate();

Expand Down
2 changes: 2 additions & 0 deletions editor/export/editor_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ EditorExport::EditorExport() {

singleton = this;
set_process(true);

GLOBAL_DEF("editor/export/convert_text_resources_to_binary", true);
}

EditorExport::~EditorExport() {
Expand Down
Loading

0 comments on commit ef17c46

Please sign in to comment.