| 603 | } |
| 604 | |
| 605 | bool Asset::onLoad(LoadAssetTask* task) |
| 606 | { |
| 607 | // It may fail when task is cancelled and new one was created later (don't crash but just end with an error) |
| 608 | if (task->Asset.Get() != this || Platform::AtomicRead(&_loadingTask) == 0) |
| 609 | return true; |
| 610 | LogContextScope logContext(GetID()); |
| 611 | |
| 612 | Locker.Lock(); |
| 613 | |
| 614 | // Load asset |
| 615 | LoadResult result; |
| 616 | #if USE_EDITOR |
| 617 | auto& deprecatedFlag = ContentDeprecatedFlags.Get(); |
| 618 | bool prevDeprecated = deprecatedFlag; |
| 619 | deprecatedFlag = false; |
| 620 | #endif |
| 621 | { |
| 622 | PROFILE_CPU_ASSET(this); |
| 623 | result = loadAsset(); |
| 624 | } |
| 625 | const bool isLoaded = result == LoadResult::Ok; |
| 626 | const bool failed = !isLoaded; |
| 627 | #if USE_EDITOR |
| 628 | const bool isDeprecated = deprecatedFlag; |
| 629 | deprecatedFlag = prevDeprecated; |
| 630 | #endif |
| 631 | Platform::AtomicStore(&_loadState, (int64)(isLoaded ? LoadState::Loaded : LoadState::LoadFailed)); |
| 632 | if (failed) |
| 633 | { |
| 634 | LOG(Error, "Loading asset \'{0}\' result: {1}.", ToString(), ToString(result)); |
| 635 | } |
| 636 | |
| 637 | // Unlink task |
| 638 | Platform::AtomicStore(&_loadingTask, 0); |
| 639 | ASSERT(failed || IsLoaded() == isLoaded); |
| 640 | |
| 641 | Locker.Unlock(); |
| 642 | |
| 643 | // Send event |
| 644 | if (isLoaded) |
| 645 | { |
| 646 | // Register event `OnLoaded` invoke on main thread |
| 647 | // We don't want to fire it here because current thread is content loader. |
| 648 | // This allows to reduce mutexes and locks (max one frame delay isn't hurting but provides more safety) |
| 649 | Content::onAssetLoaded(this); |
| 650 | } |
| 651 | |
| 652 | #if USE_EDITOR |
| 653 | // Auto-save deprecated assets to get rid of data in an old format |
| 654 | if (isDeprecated && isLoaded && !IsVirtual() && !GetPath().StartsWith(StringUtils::GetDirectoryName(Globals::TemporaryFolder))) |
| 655 | { |
| 656 | PROFILE_CPU_NAMED("Asset.Save"); |
| 657 | LOG(Info, "Resaving asset '{}' that uses deprecated data format", ToString()); |
| 658 | if (Save()) |
| 659 | { |
| 660 | LOG(Error, "Failed to resave asset '{}'", ToString()); |
| 661 | } |
| 662 | } |
no test coverage detected