Fix error when loading surface from not yet loaded asset

This commit is contained in:
Wojtek Figat
2022-05-06 18:30:49 +02:00
parent 4cedd0f30c
commit 78d6fe6b50
9 changed files with 25 additions and 6 deletions

View File

@@ -350,7 +350,7 @@ namespace ContentLoadingManagerImpl
extern ConcurrentTaskQueue<ContentLoadTask> Tasks; extern ConcurrentTaskQueue<ContentLoadTask> Tasks;
}; };
bool Asset::WaitForLoaded(double timeoutInMilliseconds) bool Asset::WaitForLoaded(double timeoutInMilliseconds) const
{ {
// This function is used many time when some parts of the engine need to wait for asset loading end (it may fail but has to end). // This function is used many time when some parts of the engine need to wait for asset loading end (it may fail but has to end).
// But it cannot be just a simple active-wait loop. // But it cannot be just a simple active-wait loop.
@@ -376,7 +376,7 @@ bool Asset::WaitForLoaded(double timeoutInMilliseconds)
// If running on a main thread we can flush asset `Loaded` event // If running on a main thread we can flush asset `Loaded` event
if (IsInMainThread()) if (IsInMainThread())
{ {
Content::tryCallOnLoaded(this); Content::tryCallOnLoaded((Asset*)this);
} }
return false; return false;
@@ -467,7 +467,7 @@ bool Asset::WaitForLoaded(double timeoutInMilliseconds)
// If running on a main thread we can flush asset `Loaded` event // If running on a main thread we can flush asset `Loaded` event
if (IsInMainThread() && IsLoaded()) if (IsInMainThread() && IsLoaded())
{ {
Content::tryCallOnLoaded(this); Content::tryCallOnLoaded((Asset*)this);
} }
return _isLoaded == 0; return _isLoaded == 0;

View File

@@ -154,7 +154,7 @@ public:
/// </summary> /// </summary>
/// <param name="timeoutInMilliseconds">Custom timeout value in milliseconds.</param> /// <param name="timeoutInMilliseconds">Custom timeout value in milliseconds.</param>
/// <returns>True if cannot load that asset (failed or has been cancelled), otherwise false.</returns> /// <returns>True if cannot load that asset (failed or has been cancelled), otherwise false.</returns>
API_FUNCTION() bool WaitForLoaded(double timeoutInMilliseconds = 30000.0); API_FUNCTION() bool WaitForLoaded(double timeoutInMilliseconds = 30000.0) const;
/// <summary> /// <summary>
/// Initializes asset data as virtual asset. /// Initializes asset data as virtual asset.

View File

@@ -129,6 +129,8 @@ bool AnimationGraph::InitAsAnimation(SkinnedModel* baseModel, Animation* anim, b
BytesContainer AnimationGraph::LoadSurface() BytesContainer AnimationGraph::LoadSurface()
{ {
if (!IsVirtual() && WaitForLoaded())
return BytesContainer();
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (IsVirtual()) if (IsVirtual())

View File

@@ -54,6 +54,8 @@ AssetChunksFlag AnimationGraphFunction::getChunksToPreload() const
BytesContainer AnimationGraphFunction::LoadSurface() const BytesContainer AnimationGraphFunction::LoadSurface() const
{ {
BytesContainer result; BytesContainer result;
if (WaitForLoaded())
return result;
ScopeLock lock(Locker); ScopeLock lock(Locker);
result.Link(GraphData); result.Link(GraphData);
return result; return result;

View File

@@ -476,6 +476,8 @@ void Material::InitCompilationOptions(ShaderCompilationOptions& options)
BytesContainer Material::LoadSurface(bool createDefaultIfMissing) BytesContainer Material::LoadSurface(bool createDefaultIfMissing)
{ {
BytesContainer result; BytesContainer result;
if (WaitForLoaded())
return result;
ScopeLock lock(Locker); ScopeLock lock(Locker);
// Check if has that chunk // Check if has that chunk

View File

@@ -75,6 +75,8 @@ AssetChunksFlag MaterialFunction::getChunksToPreload() const
BytesContainer MaterialFunction::LoadSurface() BytesContainer MaterialFunction::LoadSurface()
{ {
BytesContainer result; BytesContainer result;
if (WaitForLoaded())
return result;
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (HasChunk(0)) if (HasChunk(0))
{ {
@@ -89,6 +91,8 @@ BytesContainer MaterialFunction::LoadSurface()
bool MaterialFunction::LoadSurface(MaterialGraph& graph) bool MaterialFunction::LoadSurface(MaterialGraph& graph)
{ {
if (WaitForLoaded())
return true;
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (HasChunk(0)) if (HasChunk(0))
{ {

View File

@@ -2116,6 +2116,8 @@ const VisualScript::Field* VisualScript::FindField(const StringAnsiView& name) c
BytesContainer VisualScript::LoadSurface() BytesContainer VisualScript::LoadSurface()
{ {
if (WaitForLoaded())
return BytesContainer();
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (!LoadChunks(GET_CHUNK_FLAG(0))) if (!LoadChunks(GET_CHUNK_FLAG(0)))
{ {
@@ -2124,7 +2126,6 @@ BytesContainer VisualScript::LoadSurface()
result.Copy(data->Data); result.Copy(data->Data);
return result; return result;
} }
LOG(Warning, "\'{0}\' surface data is missing.", ToString()); LOG(Warning, "\'{0}\' surface data is missing.", ToString());
return BytesContainer(); return BytesContainer();
} }

View File

@@ -335,6 +335,8 @@ void ParticleEmitter::InitCompilationOptions(ShaderCompilationOptions& options)
BytesContainer ParticleEmitter::LoadSurface(bool createDefaultIfMissing) BytesContainer ParticleEmitter::LoadSurface(bool createDefaultIfMissing)
{ {
BytesContainer result; BytesContainer result;
if (WaitForLoaded())
return result;
ScopeLock lock(Locker); ScopeLock lock(Locker);
// Check if has that chunk // Check if has that chunk

View File

@@ -84,6 +84,8 @@ AssetChunksFlag ParticleEmitterFunction::getChunksToPreload() const
bool ParticleEmitterFunction::LoadSurface(ParticleEmitterGraphCPU& graph) bool ParticleEmitterFunction::LoadSurface(ParticleEmitterGraphCPU& graph)
{ {
if (WaitForLoaded())
return true;
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (HasChunk(0)) if (HasChunk(0))
{ {
@@ -102,6 +104,8 @@ bool ParticleEmitterFunction::LoadSurface(ParticleEmitterGraphCPU& graph)
BytesContainer ParticleEmitterFunction::LoadSurface() BytesContainer ParticleEmitterFunction::LoadSurface()
{ {
BytesContainer result; BytesContainer result;
if (WaitForLoaded())
return result;
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (HasChunk(0)) if (HasChunk(0))
{ {
@@ -118,6 +122,8 @@ BytesContainer ParticleEmitterFunction::LoadSurface()
bool ParticleEmitterFunction::LoadSurface(ParticleEmitterGraphGPU& graph) bool ParticleEmitterFunction::LoadSurface(ParticleEmitterGraphGPU& graph)
{ {
if (WaitForLoaded())
return true;
ScopeLock lock(Locker); ScopeLock lock(Locker);
if (HasChunk(0)) if (HasChunk(0))
{ {