Fix crash when importing assets in Editor (race-condition from Content Importer thread)

#1691 #1679
This commit is contained in:
Wojtek Figat
2023-10-13 15:44:18 +02:00
parent abd6881d7b
commit 9a636468e0
3 changed files with 23 additions and 21 deletions

View File

@@ -1135,17 +1135,19 @@ namespace FlaxEditor.Modules
RebuildInternal(); RebuildInternal();
Editor.ContentImporting.ImportFileEnd += ContentImporting_ImportFileDone; Editor.ContentImporting.ImportFileEnd += (obj, failed) =>
{
var path = obj.ResultUrl;
if (!failed)
FlaxEngine.Scripting.InvokeOnUpdate(() => OnImportFileDone(path));
};
_enableEvents = true; _enableEvents = true;
} }
private void ContentImporting_ImportFileDone(IFileEntryAction obj, bool failed) private void OnImportFileDone(string path)
{ {
if (failed)
return;
// Check if already has that element // Check if already has that element
var item = Find(obj.ResultUrl); var item = Find(path);
if (item is BinaryAssetItem binaryAssetItem) if (item is BinaryAssetItem binaryAssetItem)
{ {
// Get asset info from the registry (content layer will update cache it just after import) // Get asset info from the registry (content layer will update cache it just after import)

View File

@@ -55,7 +55,7 @@ namespace FlaxEditor.Modules
public event Action ImportingQueueBegin; public event Action ImportingQueueBegin;
/// <summary> /// <summary>
/// Occurs when file is being imported. /// Occurs when file is being imported. Can be called on non-main thread.
/// </summary> /// </summary>
public event Action<IFileEntryAction> ImportFileBegin; public event Action<IFileEntryAction> ImportFileBegin;
@@ -67,12 +67,12 @@ namespace FlaxEditor.Modules
public delegate void ImportFileEndDelegate(IFileEntryAction entry, bool failed); public delegate void ImportFileEndDelegate(IFileEntryAction entry, bool failed);
/// <summary> /// <summary>
/// Occurs when file importing end. /// Occurs when file importing end. Can be called on non-main thread.
/// </summary> /// </summary>
public event ImportFileEndDelegate ImportFileEnd; public event ImportFileEndDelegate ImportFileEnd;
/// <summary> /// <summary>
/// Occurs when assets importing ends. /// Occurs when assets importing ends. Can be called on non-main thread.
/// </summary> /// </summary>
public event Action ImportingQueueEnd; public event Action ImportingQueueEnd;

View File

@@ -19,25 +19,25 @@ namespace FlaxEditor.Progress.Handlers
public ImportAssetsProgress() public ImportAssetsProgress()
{ {
var importing = Editor.Instance.ContentImporting; var importing = Editor.Instance.ContentImporting;
importing.ImportingQueueBegin += OnStart; importing.ImportingQueueBegin += () => FlaxEngine.Scripting.InvokeOnUpdate(OnStart);
importing.ImportingQueueEnd += OnEnd; importing.ImportingQueueEnd += () => FlaxEngine.Scripting.InvokeOnUpdate(OnEnd);
importing.ImportFileBegin += OnImportFileBegin; importing.ImportFileBegin += OnImportFileBegin;
} }
private void OnImportFileBegin(IFileEntryAction importFileEntry) private void OnImportFileBegin(IFileEntryAction importFileEntry)
{ {
string info;
if (importFileEntry is ImportFileEntry) 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 else
_currentInfo = string.Format("Creating \'{0}\'", importFileEntry.SourceUrl); info = string.Format("Creating \'{0}\'", importFileEntry.SourceUrl);
UpdateProgress(); FlaxEngine.Scripting.InvokeOnUpdate(() =>
} {
_currentInfo = info;
private void UpdateProgress() var importing = Editor.Instance.ContentImporting;
{ var text = string.Format("{0} ({1}/{2})...", _currentInfo, importing.ImportBatchDone, importing.ImportBatchSize);
var importing = Editor.Instance.ContentImporting; OnUpdate(importing.ImportingProgress, text);
var info = string.Format("{0} ({1}/{2})...", _currentInfo, importing.ImportBatchDone, importing.ImportBatchSize); });
OnUpdate(importing.ImportingProgress, info);
} }
} }
} }