Fix crash when importing assets in Editor (race-condition from Content Importer thread)
#1691 #1679
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace FlaxEditor.Modules
|
||||
public event Action ImportingQueueBegin;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when file is being imported.
|
||||
/// Occurs when file is being imported. Can be called on non-main thread.
|
||||
/// </summary>
|
||||
public event Action<IFileEntryAction> ImportFileBegin;
|
||||
|
||||
@@ -67,12 +67,12 @@ namespace FlaxEditor.Modules
|
||||
public delegate void ImportFileEndDelegate(IFileEntryAction entry, bool failed);
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when file importing end.
|
||||
/// Occurs when file importing end. Can be called on non-main thread.
|
||||
/// </summary>
|
||||
public event ImportFileEndDelegate ImportFileEnd;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when assets importing ends.
|
||||
/// Occurs when assets importing ends. Can be called on non-main thread.
|
||||
/// </summary>
|
||||
public event Action ImportingQueueEnd;
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user