Register and cleanup resource importer singletons in a predictable way

This commit is contained in:
Yuri Sizov 2023-08-07 17:00:46 +02:00
parent f2acfb1ffc
commit 237515d0ed
7 changed files with 39 additions and 20 deletions

View File

@ -6925,8 +6925,7 @@ EditorNode::EditorNode() {
{ {
// Register importers at the beginning, so dialogs are created with the right extensions. // Register importers at the beginning, so dialogs are created with the right extensions.
Ref<ResourceImporterTexture> import_texture; Ref<ResourceImporterTexture> import_texture = memnew(ResourceImporterTexture(true));
import_texture.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_texture); ResourceFormatImporter::get_singleton()->add_importer(import_texture);
Ref<ResourceImporterLayeredTexture> import_cubemap; Ref<ResourceImporterLayeredTexture> import_cubemap;
@ -6944,8 +6943,7 @@ EditorNode::EditorNode() {
import_cubemap_array->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP_ARRAY); import_cubemap_array->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP_ARRAY);
ResourceFormatImporter::get_singleton()->add_importer(import_cubemap_array); ResourceFormatImporter::get_singleton()->add_importer(import_cubemap_array);
Ref<ResourceImporterLayeredTexture> import_3d; Ref<ResourceImporterLayeredTexture> import_3d = memnew(ResourceImporterLayeredTexture(true));
import_3d.instantiate();
import_3d->set_mode(ResourceImporterLayeredTexture::MODE_3D); import_3d->set_mode(ResourceImporterLayeredTexture::MODE_3D);
ResourceFormatImporter::get_singleton()->add_importer(import_3d); ResourceFormatImporter::get_singleton()->add_importer(import_3d);
@ -6985,12 +6983,10 @@ EditorNode::EditorNode() {
import_shader_file.instantiate(); import_shader_file.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_shader_file); ResourceFormatImporter::get_singleton()->add_importer(import_shader_file);
Ref<ResourceImporterScene> import_scene; Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene(false, true));
import_scene.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_scene); ResourceFormatImporter::get_singleton()->add_importer(import_scene);
Ref<ResourceImporterScene> import_animation; Ref<ResourceImporterScene> import_animation = memnew(ResourceImporterScene(true, true));
import_animation = Ref<ResourceImporterScene>(memnew(ResourceImporterScene(true)));
ResourceFormatImporter::get_singleton()->add_importer(import_animation); ResourceFormatImporter::get_singleton()->add_importer(import_animation);
{ {

View File

@ -474,12 +474,19 @@ bool ResourceImporterLayeredTexture::are_import_settings_valid(const String &p_p
ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = nullptr; ResourceImporterLayeredTexture *ResourceImporterLayeredTexture::singleton = nullptr;
ResourceImporterLayeredTexture::ResourceImporterLayeredTexture() { ResourceImporterLayeredTexture::ResourceImporterLayeredTexture(bool p_singleton) {
singleton = this; // This should only be set through the EditorNode.
if (p_singleton) {
singleton = this;
}
mode = MODE_CUBEMAP; mode = MODE_CUBEMAP;
} }
ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() { ResourceImporterLayeredTexture::~ResourceImporterLayeredTexture() {
if (singleton == this) {
singleton = nullptr;
}
} }
void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) { void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source_file, Ref<LayeredTextureImport> r_texture_import) {

View File

@ -119,7 +119,7 @@ public:
void set_mode(Mode p_mode) { mode = p_mode; } void set_mode(Mode p_mode) { mode = p_mode; }
ResourceImporterLayeredTexture(); ResourceImporterLayeredTexture(bool p_singleton = false);
~ResourceImporterLayeredTexture(); ~ResourceImporterLayeredTexture();
}; };

View File

@ -2607,15 +2607,28 @@ void ResourceImporterScene::ResourceImporterScene::show_advanced_options(const S
SceneImportSettings::get_singleton()->open_settings(p_path, animation_importer); SceneImportSettings::get_singleton()->open_settings(p_path, animation_importer);
} }
ResourceImporterScene::ResourceImporterScene(bool p_animation_import) { ResourceImporterScene::ResourceImporterScene(bool p_animation_import, bool p_singleton) {
if (p_animation_import) { // This should only be set through the EditorNode.
animation_singleton = this; if (p_singleton) {
} else { if (p_animation_import) {
scene_singleton = this; animation_singleton = this;
} else {
scene_singleton = this;
}
} }
animation_importer = p_animation_import; 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) { void ResourceImporterScene::add_importer(Ref<EditorSceneFormatImporter> p_importer, bool p_first_priority) {
ERR_FAIL_COND(p_importer.is_null()); ERR_FAIL_COND(p_importer.is_null());
if (p_first_priority) { if (p_first_priority) {

View File

@ -296,7 +296,8 @@ public:
virtual bool can_import_threaded() const override { return false; } 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> template <class M>
static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<ImporterMesh> &p_mesh, const M &p_options, float p_applied_root_scale); static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<ImporterMesh> &p_mesh, const M &p_options, float p_applied_root_scale);

View File

@ -789,10 +789,12 @@ bool ResourceImporterTexture::are_import_settings_valid(const String &p_path) co
ResourceImporterTexture *ResourceImporterTexture::singleton = nullptr; ResourceImporterTexture *ResourceImporterTexture::singleton = nullptr;
ResourceImporterTexture::ResourceImporterTexture() { ResourceImporterTexture::ResourceImporterTexture(bool p_singleton) {
if (!singleton) { // This should only be set through the EditorNode.
if (p_singleton) {
singleton = this; singleton = this;
} }
CompressedTexture2D::request_3d_callback = _texture_reimport_3d; CompressedTexture2D::request_3d_callback = _texture_reimport_3d;
CompressedTexture2D::request_roughness_callback = _texture_reimport_roughness; CompressedTexture2D::request_roughness_callback = _texture_reimport_roughness;
CompressedTexture2D::request_normal_callback = _texture_reimport_normal; CompressedTexture2D::request_normal_callback = _texture_reimport_normal;

View File

@ -107,7 +107,7 @@ public:
virtual bool are_import_settings_valid(const String &p_path) const override; virtual bool are_import_settings_valid(const String &p_path) const override;
virtual String get_import_settings_string() const override; virtual String get_import_settings_string() const override;
ResourceImporterTexture(); ResourceImporterTexture(bool p_singleton = false);
~ResourceImporterTexture(); ~ResourceImporterTexture();
}; };