From 9a636468e0d6378be117317dedd913e6dbb11727 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 13 Oct 2023 15:44:18 +0200 Subject: [PATCH] Fix crash when importing assets in Editor (race-condition from Content Importer thread) #1691 #1679 --- .../Editor/Modules/ContentDatabaseModule.cs | 14 ++++++----- .../Editor/Modules/ContentImportingModule.cs | 6 ++--- .../Progress/Handlers/ImportAssetsProgress.cs | 24 +++++++++---------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Source/Editor/Modules/ContentDatabaseModule.cs b/Source/Editor/Modules/ContentDatabaseModule.cs index 8b7371e44..b1de78456 100644 --- a/Source/Editor/Modules/ContentDatabaseModule.cs +++ b/Source/Editor/Modules/ContentDatabaseModule.cs @@ -1135,17 +1135,19 @@ namespace FlaxEditor.Modules RebuildInternal(); - Editor.ContentImporting.ImportFileEnd += ContentImporting_ImportFileDone; + Editor.ContentImporting.ImportFileEnd += (obj, failed) => + { + var path = obj.ResultUrl; + if (!failed) + FlaxEngine.Scripting.InvokeOnUpdate(() => OnImportFileDone(path)); + }; _enableEvents = true; } - private void ContentImporting_ImportFileDone(IFileEntryAction obj, bool failed) + private void OnImportFileDone(string path) { - if (failed) - return; - // Check if already has that element - var item = Find(obj.ResultUrl); + var item = Find(path); if (item is BinaryAssetItem binaryAssetItem) { // Get asset info from the registry (content layer will update cache it just after import) diff --git a/Source/Editor/Modules/ContentImportingModule.cs b/Source/Editor/Modules/ContentImportingModule.cs index c94ba650e..85dd50d35 100644 --- a/Source/Editor/Modules/ContentImportingModule.cs +++ b/Source/Editor/Modules/ContentImportingModule.cs @@ -55,7 +55,7 @@ namespace FlaxEditor.Modules public event Action ImportingQueueBegin; /// - /// Occurs when file is being imported. + /// Occurs when file is being imported. Can be called on non-main thread. /// public event Action ImportFileBegin; @@ -67,12 +67,12 @@ namespace FlaxEditor.Modules public delegate void ImportFileEndDelegate(IFileEntryAction entry, bool failed); /// - /// Occurs when file importing end. + /// Occurs when file importing end. Can be called on non-main thread. /// public event ImportFileEndDelegate ImportFileEnd; /// - /// Occurs when assets importing ends. + /// Occurs when assets importing ends. Can be called on non-main thread. /// public event Action ImportingQueueEnd; diff --git a/Source/Editor/Progress/Handlers/ImportAssetsProgress.cs b/Source/Editor/Progress/Handlers/ImportAssetsProgress.cs index 7bbefbc2d..fa1e943f0 100644 --- a/Source/Editor/Progress/Handlers/ImportAssetsProgress.cs +++ b/Source/Editor/Progress/Handlers/ImportAssetsProgress.cs @@ -19,25 +19,25 @@ namespace FlaxEditor.Progress.Handlers public ImportAssetsProgress() { var importing = Editor.Instance.ContentImporting; - importing.ImportingQueueBegin += OnStart; - importing.ImportingQueueEnd += OnEnd; + importing.ImportingQueueBegin += () => FlaxEngine.Scripting.InvokeOnUpdate(OnStart); + importing.ImportingQueueEnd += () => FlaxEngine.Scripting.InvokeOnUpdate(OnEnd); importing.ImportFileBegin += OnImportFileBegin; } private void OnImportFileBegin(IFileEntryAction importFileEntry) { + string info; if (importFileEntry is ImportFileEntry) - _currentInfo = string.Format("Importing \'{0}\'", System.IO.Path.GetFileName(importFileEntry.SourceUrl)); + info = string.Format("Importing \'{0}\'", System.IO.Path.GetFileName(importFileEntry.SourceUrl)); else - _currentInfo = string.Format("Creating \'{0}\'", importFileEntry.SourceUrl); - UpdateProgress(); - } - - private void UpdateProgress() - { - var importing = Editor.Instance.ContentImporting; - var info = string.Format("{0} ({1}/{2})...", _currentInfo, importing.ImportBatchDone, importing.ImportBatchSize); - OnUpdate(importing.ImportingProgress, info); + info = string.Format("Creating \'{0}\'", importFileEntry.SourceUrl); + FlaxEngine.Scripting.InvokeOnUpdate(() => + { + _currentInfo = info; + var importing = Editor.Instance.ContentImporting; + var text = string.Format("{0} ({1}/{2})...", _currentInfo, importing.ImportBatchDone, importing.ImportBatchSize); + OnUpdate(importing.ImportingProgress, text); + }); } } }