Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "dedicated server" export mode which can strip unneeded visual resources (superseded) #69546

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ void Resource::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");

#ifdef TOOLS_ENABLED
ClassDB::bind_method(D_METHOD("set_dedicated_server_export_type", "server_export_type"), &Resource::set_dedicated_server_export_type);
ClassDB::bind_method(D_METHOD("get_dedicated_server_export_type"), &Resource::get_dedicated_server_export_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "resource_dedicated_server_export_type", PROPERTY_HINT_ENUM, "Strip,Keep"), "set_dedicated_server_export_type", "get_dedicated_server_export_type");

BIND_ENUM_CONSTANT(DEDICATED_SERVER_EXPORT_STRIP);
BIND_ENUM_CONSTANT(DEDICATED_SERVER_EXPORT_KEEP);
#endif
Comment on lines +441 to +448
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know this breaks the API compatibility between editor and templates.
Godot has 2 API levels, a "core" one, and and "editor" one (see ClassDB::API_CORE and ClassDB::API_EDITOR usage in main.cpp).
The 2 API levels should not be mixed, and the ClassDB::API_CORE should be the same between editor and templates.

If we go with this approach of special methods which only works in editor, we should still bind the methods and constants in templates, and leave them with an empty implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was me attempting to not weight the templates down with APIs that they don't need. If we end up keeping something on Resource, I'll dig into ClassDB::API_CORE vs ClassDB::API_EDITOR and see if there's a better way to make these properties editor only.


MethodInfo get_rid_bind("_get_rid");
get_rid_bind.return_val.type = Variant::RID;

Expand Down
18 changes: 18 additions & 0 deletions core/io/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Resource : public RefCounted {
static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); }
virtual String get_base_extension() const { return "res"; }

#ifdef TOOLS_ENABLED
enum DedicatedServerExportType {
DEDICATED_SERVER_EXPORT_STRIP,
DEDICATED_SERVER_EXPORT_KEEP,
};
#endif

private:
HashSet<ObjectID> owners;

Expand All @@ -67,6 +74,7 @@ class Resource : public RefCounted {
uint64_t last_modified_time = 0;
uint64_t import_last_modified_time = 0;
String import_path;
DedicatedServerExportType dedicated_server_export_type = DEDICATED_SERVER_EXPORT_STRIP;
#endif

bool local_to_scene = false;
Expand Down Expand Up @@ -133,6 +141,12 @@ class Resource : public RefCounted {
void set_import_path(const String &p_path) { import_path = p_path; }
String get_import_path() const { return import_path; }

void set_dedicated_server_export_type(DedicatedServerExportType p_server_export_type) {
dedicated_server_export_type = p_server_export_type;
emit_changed();
}
DedicatedServerExportType get_dedicated_server_export_type() const { return dedicated_server_export_type; }

#endif

void set_as_translation_remapped(bool p_remapped);
Expand All @@ -150,6 +164,10 @@ class Resource : public RefCounted {
~Resource();
};

#ifdef TOOLS_ENABLED
VARIANT_ENUM_CAST(Resource::DedicatedServerExportType);
#endif

class ResourceCache {
friend class Resource;
friend class ResourceLoader; //need the lock
Expand Down
29 changes: 27 additions & 2 deletions core/io/resource_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy

int lines = 0;
String error_text;
String section;
bool path_found = false; //first match must have priority
while (true) {
assign = Variant();
Expand All @@ -76,7 +77,14 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy
return err;
}

if (!assign.is_empty()) {
if (assign.is_empty()) {
if (!next_tag.name.is_empty()) {
section = next_tag.name;
}
continue;
}

if (section == "remap") {
if (!path_found && assign.begins_with("path.") && r_path_and_type.path.is_empty()) {
String feature = assign.get_slicec('.', 1);
if (OS::get_singleton()->has_feature(feature)) {
Expand All @@ -103,8 +111,15 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy
}
}

} else if (next_tag.name != "remap") {
} else {
#ifdef TOOLS_ENABLED
if (section == "params" && assign == "dedicated_server/server_export_type") {
r_path_and_type.dedicated_server_export_type = (Resource::DedicatedServerExportType)(int)value;
break;
}
#else
break;
#endif // TOOLS_ENABLED
}
}

Expand Down Expand Up @@ -141,6 +156,7 @@ Ref<Resource> ResourceFormatImporter::load(const String &p_path, const String &p
if (res.is_valid()) {
res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
res->set_import_path(pat.path);
res->set_dedicated_server_export_type(pat.dedicated_server_export_type);
}
#endif

Expand Down Expand Up @@ -476,6 +492,15 @@ void ResourceImporter::_bind_methods() {
BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
}

#ifdef TOOLS_ENABLED
void ResourceImporter::get_editor_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) {
get_import_options(p_path, r_options, p_preset);

// Add default editor options.
r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, "dedicated_server/server_export_type", PROPERTY_HINT_ENUM, "Strip,Keep"), 0));
}
#endif

void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority) {
ERR_FAIL_COND(p_importer.is_null());
if (p_first_priority) {
Expand Down
7 changes: 7 additions & 0 deletions core/io/resource_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ResourceFormatImporter : public ResourceFormatLoader {
String group_file;
Variant metadata;
uint64_t uid = ResourceUID::INVALID_ID;
#ifdef TOOLS_ENABLED
Resource::DedicatedServerExportType dedicated_server_export_type;
#endif
};

Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid = nullptr) const;
Expand Down Expand Up @@ -145,6 +148,10 @@ class ResourceImporter : public RefCounted {
virtual Error import_group_file(const String &p_group_file, const HashMap<String, HashMap<StringName, Variant>> &p_source_file_options, const HashMap<String, String> &p_base_paths) { return ERR_UNAVAILABLE; }
virtual bool are_import_settings_valid(const String &p_path) const { return true; }
virtual String get_import_settings_string() const { return String(); }

#ifdef TOOLS_ENABLED
void get_editor_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0);
#endif
};

VARIANT_ENUM_CAST(ResourceImporter::ImportOrder);
Expand Down
11 changes: 11 additions & 0 deletions doc/classes/Resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
</method>
</methods>
<members>
<member name="resource_dedicated_server_export_type" type="int" setter="set_dedicated_server_export_type" getter="get_dedicated_server_export_type" enum="Resource.DedicatedServerExportType" default="0">
Controls how this resource should be handled in a dedicated server export.
</member>
<member name="resource_local_to_scene" type="bool" setter="set_local_to_scene" getter="is_local_to_scene" default="false">
If [code]true[/code], the resource is duplicated for each instance of all scenes using it. At run-time, the resource can be modified in one scene without affecting other instances (see [method PackedScene.instantiate]).
[b]Note:[/b] Changing this property at run-time has no effect on already created duplicate resources.
Expand All @@ -107,4 +110,12 @@
</description>
</signal>
</signals>
<constants>
<constant name="DEDICATED_SERVER_EXPORT_STRIP" value="0" enum="DedicatedServerExportType">
Replace this resource with a placeholder (if available) in a dedicated server export.
</constant>
<constant name="DEDICATED_SERVER_EXPORT_KEEP" value="1" enum="DedicatedServerExportType">
Keep this resource in a dedicated server export.
</constant>
</constants>
</class>
2 changes: 1 addition & 1 deletion editor/doc_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ void DocTools::generate(bool p_basic_types) {
Variant default_value;

if (name == "EditorSettings") {
if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "script") {
if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "resource_dedicated_server_export_type" || E.name == "script") {
// Don't include spurious properties in the generated EditorSettings class reference.
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
List<ResourceImporter::ImportOption> options;
importer->get_import_options(p_files[i], &options);
importer->get_editor_import_options(p_files[i], &options);
//set default values
for (const ResourceImporter::ImportOption &E : options) {
source_file_options[p_files[i]][E.option.name] = E.default_value;
Expand Down Expand Up @@ -1748,7 +1748,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
//store options in provided order, to avoid file changing. Order is also important because first match is accepted first.

List<ResourceImporter::ImportOption> options;
importer->get_import_options(file, &options);
importer->get_editor_import_options(file, &options);
//set default values
for (const ResourceImporter::ImportOption &F : options) {
String base = F.option.name;
Expand Down Expand Up @@ -1885,7 +1885,7 @@ void EditorFileSystem::_reimport_file(const String &p_file, const HashMap<String
//mix with default params, in case a parameter is missing

List<ResourceImporter::ImportOption> opts;
importer->get_import_options(p_file, &opts);
importer->get_editor_import_options(p_file, &opts);
for (const ResourceImporter::ImportOption &E : opts) {
if (!params.has(E.option.name)) { //this one is not present
params[E.option.name] = E.default_value;
Expand Down
6 changes: 6 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
#include "editor/plugins/asset_library_editor_plugin.h"
#include "editor/plugins/canvas_item_editor_plugin.h"
#include "editor/plugins/debugger_editor_plugin.h"
#include "editor/plugins/dedicated_server_export_plugin.h"
#include "editor/plugins/editor_preview_plugins.h"
#include "editor/plugins/editor_resource_conversion_plugin.h"
#include "editor/plugins/gdextension_export_plugin.h"
Expand Down Expand Up @@ -7332,6 +7333,11 @@ EditorNode::EditorNode() {

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

Ref<DedicatedServerExportPlugin> dedicated_server_export_plugin;
dedicated_server_export_plugin.instantiate();

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

Ref<PackedSceneEditorTranslationParserPlugin> packed_scene_translation_parser_plugin;
packed_scene_translation_parser_plugin.instantiate();
EditorTranslationParser::get_singleton()->add_parser(packed_scene_translation_parser_plugin, EditorTranslationParser::STANDARD);
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_sectioned_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SectionedInspectorFilter : public Object {
for (PropertyInfo &pi : pinfo) {
int sp = pi.name.find("/");

if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name == "resource_local_to_scene" || pi.name.begins_with("script/") || pi.name.begins_with("_global_script")) { //skip resource stuff
if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name == "resource_local_to_scene" || pi.name == "resource_dedicated_server_export_type" || pi.name.begins_with("script/") || pi.name.begins_with("_global_script")) { //skip resource stuff
continue;
}

Expand Down Expand Up @@ -243,7 +243,7 @@ void SectionedInspector::update_category_list() {
continue;
}

if (pi.name.contains(":") || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
if (pi.name.contains(":") || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name == "resource_dedicated_server_export_type" || pi.name.begins_with("_global_script")) {
continue;
}

Expand Down
33 changes: 25 additions & 8 deletions editor/export/editor_export_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ String EditorExportPlatform::_export_customize(const String &p_path, LocalVector
return p_path; // do none
}

String import_path = p_path + ".import";
bool import_exists = FileAccess::exists(import_path);

// Check if a cache exists
if (export_cache.has(p_path)) {
FileExportCache &fec = export_cache[p_path];
Expand All @@ -667,20 +670,22 @@ String EditorExportPlatform::_export_customize(const String &p_path, LocalVector
// Destination file exists (was not erased) or not needed

uint64_t mod_time = FileAccess::get_modified_time(p_path);
if (fec.source_modified_time == mod_time) {
uint64_t import_mod_time = import_exists ? FileAccess::get_modified_time(import_path) : 0;
if (fec.source_modified_time == mod_time && fec.import_modified_time == import_mod_time) {
// Cached (modified time matches).
fec.used = true;
return fec.saved_path.is_empty() ? p_path : fec.saved_path;
}

String md5 = FileAccess::get_md5(p_path);
if (FileAccess::exists(p_path + ".import")) {
if (import_exists) {
// Also consider the import file in the string
md5 += FileAccess::get_md5(p_path + ".import");
md5 += FileAccess::get_md5(import_path);
}
if (fec.source_md5 == md5) {
// Cached (md5 matches).
fec.source_modified_time = mod_time;
fec.import_modified_time = import_mod_time;
fec.used = true;
return fec.saved_path.is_empty() ? p_path : fec.saved_path;
}
Expand All @@ -690,11 +695,12 @@ String EditorExportPlatform::_export_customize(const String &p_path, LocalVector
FileExportCache fec;
fec.used = true;
fec.source_modified_time = FileAccess::get_modified_time(p_path);
fec.import_modified_time = import_exists ? FileAccess::get_modified_time(import_path) : 0;

String md5 = FileAccess::get_md5(p_path);
if (FileAccess::exists(p_path + ".import")) {
if (import_exists) {
// Also consider the import file in the string
md5 += FileAccess::get_md5(p_path + ".import");
md5 += FileAccess::get_md5(import_path);
}

fec.source_md5 = md5;
Expand Down Expand Up @@ -963,12 +969,20 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &
String l = f->get_line();
while (l != String()) {
Vector<String> fields = l.split("::");
if (fields.size() == 4) {
if (fields.size() >= 4) {
FileExportCache fec;
String path = fields[0];
fec.source_md5 = fields[1].strip_edges();
fec.source_modified_time = fields[2].strip_edges().to_int();
fec.saved_path = fields[3];
if (fields.size() == 5) {
fec.import_modified_time = fields[3].strip_edges().to_int();
fec.saved_path = fields[4];
} else {
// Handle file_cache files from older versions that only
// have 4 fields (no import_modified_time).
fec.import_modified_time = 0;
fec.saved_path = fields[3];
}
fec.used = false; // Assume unused until used.
export_cache[path] = fec;
}
Expand Down Expand Up @@ -1184,7 +1198,7 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &
if (f.is_valid()) {
for (const KeyValue<String, FileExportCache> &E : export_cache) {
if (E.value.used) { // May be old, unused
String l = E.key + "::" + E.value.source_md5 + "::" + itos(E.value.source_modified_time) + "::" + E.value.saved_path;
String l = E.key + "::" + E.value.source_md5 + "::" + itos(E.value.source_modified_time) + "::" + itos(E.value.import_modified_time) + "::" + E.value.saved_path;
f->store_line(l);
}
}
Expand Down Expand Up @@ -1214,6 +1228,9 @@ Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &
}
}
}
for (int i = 0; i < export_plugins.size(); i++) {
custom_list.append_array(export_plugins[i]->_get_export_features(Ref<EditorExportPlatform>(this), p_debug));
}

ProjectSettings::CustomMap custom_map;
if (path_remaps.size()) {
Expand Down
1 change: 1 addition & 0 deletions editor/export/editor_export_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class EditorExportPlatform : public RefCounted {

struct FileExportCache {
uint64_t source_modified_time = 0;
uint64_t import_modified_time = 0;
String source_md5;
String saved_path;
bool used = false;
Expand Down
3 changes: 3 additions & 0 deletions editor/export/editor_export_platform_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "editor_export_platform_pc.h"

#include "core/config/project_settings.h"
#include "editor/plugins/dedicated_server_export_plugin.h"

void EditorExportPlatformPC::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
if (p_preset->get("texture_format/s3tc")) {
Expand Down Expand Up @@ -61,6 +62,8 @@ void EditorExportPlatformPC::get_export_options(List<ExportOption> *r_options) {
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/no_bptc_fallbacks"), true));

DedicatedServerExportPlugin::add_export_options(r_options);
}

String EditorExportPlatformPC::get_name() const {
Expand Down
6 changes: 6 additions & 0 deletions editor/export/editor_export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ String EditorExportPlugin::_get_name() const {
return ret;
}

PackedStringArray EditorExportPlugin::_get_export_features(const Ref<EditorExportPlatform> &p_platform, bool p_debug) const {
PackedStringArray ret;
GDVIRTUAL_CALL(_get_export_features, p_platform, p_debug, ret);
return ret;
}

void EditorExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {
}

Expand Down
Loading