diff --git a/Source/Engine/Content/Assets/Material.cpp b/Source/Engine/Content/Assets/Material.cpp index ad373d660..3c2330c93 100644 --- a/Source/Engine/Content/Assets/Material.cpp +++ b/Source/Engine/Content/Assets/Material.cpp @@ -187,6 +187,9 @@ Asset::LoadResult Material::load() #endif ) { + // Guard file with the lock during shader generation (prevents FlaxStorage::Tick from messing with the file) + auto lock = Storage->Lock(); + // Prepare MaterialGenerator generator; generator.Error.Bind(&OnGeneratorError); diff --git a/Source/Engine/Content/Storage/ContentStorageManager.cpp b/Source/Engine/Content/Storage/ContentStorageManager.cpp index 949341e25..381266ac9 100644 --- a/Source/Engine/Content/Storage/ContentStorageManager.cpp +++ b/Source/Engine/Content/Storage/ContentStorageManager.cpp @@ -252,6 +252,7 @@ void ContentStorageSystem::Job(int32 index) { PROFILE_CPU_NAMED("ContentStorage.Job"); + const double time = Platform::GetTimeSeconds(); ScopeLock lock(Locker); for (auto i = StorageMap.Begin(); i.IsNotEnd(); ++i) { @@ -271,7 +272,7 @@ void ContentStorageSystem::Job(int32 index) } else { - storage->Tick(); + storage->Tick(time); } } } diff --git a/Source/Engine/Content/Storage/FlaxStorage.cpp b/Source/Engine/Content/Storage/FlaxStorage.cpp index 646592b54..c3e377110 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.cpp +++ b/Source/Engine/Content/Storage/FlaxStorage.cpp @@ -1398,19 +1398,18 @@ void FlaxStorage::Dispose() _version = 0; } -void FlaxStorage::Tick() +void FlaxStorage::Tick(double time) { - // Check if chunks are locked + // Skip if file is in use if (Platform::AtomicRead(&_chunksLock) != 0) return; - const double now = Platform::GetTimeSeconds(); bool wasAnyUsed = false; const float unusedDataChunksLifetime = ContentStorageManager::UnusedDataChunksLifetime.GetTotalSeconds(); for (int32 i = 0; i < _chunks.Count(); i++) { auto chunk = _chunks.Get()[i]; - const bool wasUsed = (now - chunk->LastAccessTime) < unusedDataChunksLifetime; + const bool wasUsed = (time - chunk->LastAccessTime) < unusedDataChunksLifetime; if (!wasUsed && chunk->IsLoaded()) { chunk->Unload(); @@ -1418,7 +1417,7 @@ void FlaxStorage::Tick() wasAnyUsed |= wasUsed; } - // Release file handles in none of chunks was not used + // Release file handles in none of chunks is in use if (!wasAnyUsed && Platform::AtomicRead(&_chunksLock) == 0) { CloseFileHandles(); diff --git a/Source/Engine/Content/Storage/FlaxStorage.h b/Source/Engine/Content/Storage/FlaxStorage.h index 865b3e656..09e8c0bdd 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.h +++ b/Source/Engine/Content/Storage/FlaxStorage.h @@ -416,7 +416,7 @@ public: /// /// Ticks this instance. /// - void Tick(); + void Tick(double time); #if USE_EDITOR void OnRename(const StringView& newPath);