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

Register and cleanup resource importer singletons in a predictable way #80377

Merged
Merged
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
12 changes: 4 additions & 8 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6925,8 +6925,7 @@ EditorNode::EditorNode() {

{
// Register importers at the beginning, so dialogs are created with the right extensions.
Ref<ResourceImporterTexture> import_texture;
import_texture.instantiate();
Ref<ResourceImporterTexture> import_texture = memnew(ResourceImporterTexture(true));
ResourceFormatImporter::get_singleton()->add_importer(import_texture);

Ref<ResourceImporterLayeredTexture> import_cubemap;
Expand All @@ -6944,8 +6943,7 @@ EditorNode::EditorNode() {
import_cubemap_array->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP_ARRAY);
ResourceFormatImporter::get_singleton()->add_importer(import_cubemap_array);

Ref<ResourceImporterLayeredTexture> import_3d;
import_3d.instantiate();
Ref<ResourceImporterLayeredTexture> import_3d = memnew(ResourceImporterLayeredTexture(true));
import_3d->set_mode(ResourceImporterLayeredTexture::MODE_3D);
ResourceFormatImporter::get_singleton()->add_importer(import_3d);

Expand Down Expand Up @@ -6985,12 +6983,10 @@ EditorNode::EditorNode() {
import_shader_file.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_shader_file);

Ref<ResourceImporterScene> import_scene;
import_scene.instantiate();
Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene(false, true));
ResourceFormatImporter::get_singleton()->add_importer(import_scene);

Ref<ResourceImporterScene> import_animation;
import_animation = Ref<ResourceImporterScene>(memnew(ResourceImporterScene(true)));
Ref<ResourceImporterScene> import_animation = memnew(ResourceImporterScene(true, true));
ResourceFormatImporter::get_singleton()->add_importer(import_animation);

{
Expand Down
11 changes: 9 additions & 2 deletions editor/import/resource_importer_layered_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,19 @@ bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_p

ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = nullptr;

ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() {
singleton = this;
ResourceImporterLayeredTexture::ResourceImporterLayeredTexture(bool p_singleton) {
// This should only be set through the EditorNode.
if (p_singleton) {
singleton = this;
}

mode = MODE_CUBEMAP;
}

ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
if (singleton == this) {
singleton = nullptr;
}
}

void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) {
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_layered_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ResourceImporterLayeredTexture : public ResourceImporter {

void set_mode(Mode p_mode) { mode = p_mode; }

ResourceImporterLayeredTexture();
ResourceImporterLayeredTexture(bool p_singleton = false);
~ResourceImporterLayeredTexture();
};

Expand Down
23 changes: 18 additions & 5 deletions editor/import/resource_importer_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2607,15 +2607,28 @@ void ResourceImporterScene::ResourceImporterScene::show_advanced_options(const S
SceneImportSettings::get_singleton()->open_settings(p_path, animation_importer);
}

ResourceImporterScene::ResourceImporterScene(bool p_animation_import) {
if (p_animation_import) {
animation_singleton = this;
} else {
scene_singleton = this;
ResourceImporterScene::ResourceImporterScene(bool p_animation_import, bool p_singleton) {
// This should only be set through the EditorNode.
if (p_singleton) {
if (p_animation_import) {
animation_singleton = this;
} else {
scene_singleton = this;
}
}

animation_importer = p_animation_import;
}

ResourceImporterScene::~ResourceImporterScene() {
if (animation_singleton == this) {
animation_singleton = nullptr;
}
if (scene_singleton == this) {
scene_singleton = nullptr;
}
}

void ResourceImporterScene::add_importer(Ref<EditorSceneFormatImporter> p_importer, bool p_first_priority) {
ERR_FAIL_COND(p_importer.is_null());
if (p_first_priority) {
Expand Down
3 changes: 2 additions & 1 deletion editor/import/resource_importer_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ class ResourceImporterScene : public ResourceImporter {

virtual bool can_import_threaded() const override { return false; }

ResourceImporterScene(bool p_animation_import = false);
ResourceImporterScene(bool p_animation_import = false, bool p_singleton = false);
~ResourceImporterScene();

template <class M>
static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<ImporterMesh> &p_mesh, const M &p_options, float p_applied_root_scale);
Expand Down
6 changes: 4 additions & 2 deletions editor/import/resource_importer_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,12 @@ bool ResourceImporterTexture::are_import_settings_valid(const String &p_path) co

ResourceImporterTexture *ResourceImporterTexture::singleton = nullptr;

ResourceImporterTexture::ResourceImporterTexture() {
if (!singleton) {
ResourceImporterTexture::ResourceImporterTexture(bool p_singleton) {
// This should only be set through the EditorNode.
if (p_singleton) {
singleton = this;
}

CompressedTexture2D::request_3d_callback = _texture_reimport_3d;
CompressedTexture2D::request_roughness_callback = _texture_reimport_roughness;
CompressedTexture2D::request_normal_callback = _texture_reimport_normal;
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ResourceImporterTexture : public ResourceImporter {
virtual bool are_import_settings_valid(const String &p_path) const override;
virtual String get_import_settings_string() const override;

ResourceImporterTexture();
ResourceImporterTexture(bool p_singleton = false);
~ResourceImporterTexture();
};

Expand Down