示例#1
0
 inline void                     AddRef() { IncrementReferenceCount(); }
示例#2
0
    Asset* AssetLibrary::Load(const char* filePathOrIdentifier, AssetType explicitAssetType)
    {
        // When an asset is cached, the absolute path is used to retrieve the cached object. It is possible, however, for an asset to not actually
        // be loaded from a file, in which case filePathOrIdentifier is used as the unique identifier without any modification.

        char absolutePathOrIdentifier[DRFS_MAX_PATH];
        if (!drfs_find_absolute_path(m_pVFS, filePathOrIdentifier, absolutePathOrIdentifier, sizeof(absolutePathOrIdentifier)))
        {
            // The file could not be found, but there may be a metadata file. It is possible that the data for an asset is
            // entirely defined in the metadata file, we'll look for that file too.
            char metadataPath[DRFS_MAX_PATH];
            drpath_copy_and_append_extension(metadataPath, DRFS_MAX_PATH, filePathOrIdentifier, "gtdata");

            if (drfs_find_absolute_path(m_pVFS, metadataPath, absolutePathOrIdentifier, sizeof(absolutePathOrIdentifier)))
            {
                // The metadata file was found. Later on we'll load the metadata for real, so we'll need to remove the ".gtdata" extension beforehand.
                drpath_remove_extension(absolutePathOrIdentifier);
            }
            else
            {
                // The file nor it's metadata file could not be found, but the asset loader might be using it as a unique identifier, so we just use it as-is in this case.
                strcpy_s(absolutePathOrIdentifier, filePathOrIdentifier);
            }
        }


        Asset* pAsset = nullptr;
        dr_lock_mutex(m_mutex);
        {
            auto iExistingAsset = m_loadedAssets.Find(absolutePathOrIdentifier);
            if (iExistingAsset == nullptr)
            {
                AssetAllocator* pAllocator = nullptr;

                AssetType assetType = explicitAssetType;
                if (assetType == AssetType_Unknown)
                {
                    pAllocator = this->FindAllocatorAndTypeByPath(absolutePathOrIdentifier, assetType);
                }
                else
                {
                    pAllocator = this->FindAllocatorByType(assetType);
                }


                if (pAllocator != nullptr)
                {
                    pAsset = pAllocator->CreateAsset(absolutePathOrIdentifier, assetType);
                    if (pAsset != nullptr)
                    {
                        // Load the metadata first. It does not matter if this fails so the return value doesn't need to be checked.
                        char metadataAbsolutePath[DRFS_MAX_PATH];
                        drpath_copy_and_append_extension(metadataAbsolutePath, DRFS_MAX_PATH, absolutePathOrIdentifier, "gtdata");
                        pAsset->LoadMetadata(metadataAbsolutePath, m_pVFS);


                        // Load the asset after the metadata.
                        if (pAsset->Load(absolutePathOrIdentifier, m_pVFS))
                        {
                            m_loadedAssets.Add(absolutePathOrIdentifier, pAsset);
                        }
                        else
                        {
                            // Failed to load the asset.
                            pAllocator->DeleteAsset(pAsset);
                            pAsset = nullptr;
                        }
                    }
                    else
                    {
                        // Failed to create an asset of the given type.
                        pAsset = nullptr;
                    }
                }
                else
                {
                    // Could not find a supported allocator.
                    pAsset = nullptr;
                }
            }
            else
            {
                auto pExistingAsset = iExistingAsset->value;
                assert(pExistingAsset != nullptr);
                {
                    pExistingAsset->IncrementReferenceCount();
                }

                pAsset = iExistingAsset->value;
            }
        }
        dr_unlock_mutex(m_mutex);

        return pAsset;
    }