| 709 | } |
| 710 | |
| 711 | bool FlaxStorage::LoadAssetChunk(FlaxChunk* chunk) |
| 712 | { |
| 713 | PROFILE_MEM(ContentFiles); |
| 714 | ASSERT(IsLoaded()); |
| 715 | ASSERT(chunk != nullptr && _chunks.Contains(chunk)); |
| 716 | |
| 717 | #if PLATFORM_THREADS_LIMIT > 1 |
| 718 | // Protect against loading the same chunk from multiple threads at once |
| 719 | while (Platform::InterlockedCompareExchange(&chunk->IsLoading, 1, 0) != 0) |
| 720 | Platform::Sleep(1); |
| 721 | SCOPE_EXIT{ Platform::AtomicStore(&chunk->IsLoading, 0); }; |
| 722 | #endif |
| 723 | if (chunk->IsLoaded()) |
| 724 | return false; |
| 725 | |
| 726 | // Ensure that asset is in a file |
| 727 | if (chunk->ExistsInFile() == false) |
| 728 | { |
| 729 | LOG(Warning, "Cannot load chunk from {0}. It doesn't exist in storage.", ToString()); |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | LockChunks(); |
| 734 | |
| 735 | // Open file |
| 736 | auto stream = OpenFile(); |
| 737 | bool failed = stream == nullptr; |
| 738 | if (!failed) |
| 739 | { |
| 740 | // Seek |
| 741 | stream->SetPosition(chunk->LocationInFile.Address); |
| 742 | |
| 743 | if (stream->HasError()) |
| 744 | { |
| 745 | // Sometimes stream->HasError() from setposition. result in a crash or missing media in release (stream _file._handle = nullptr). |
| 746 | // When retrying, it looks like it works and we can continue. We need this to success. |
| 747 | |
| 748 | for (int retry = 0; retry < 5; retry++) |
| 749 | { |
| 750 | Platform::Sleep(50); |
| 751 | stream = OpenFile(); |
| 752 | failed = stream == nullptr; |
| 753 | if (!failed) |
| 754 | { |
| 755 | stream->SetPosition(chunk->LocationInFile.Address); |
| 756 | if (!stream->HasError()) |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (!stream || stream->HasError()) |
| 763 | { |
| 764 | failed = true; |
| 765 | UnlockChunks(); |
| 766 | LOG(Warning, "SetPosition failed on chunk {0}.", ToString()); |
| 767 | return failed; |
| 768 | } |
no test coverage detected