/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/EnginePlugins/FmodPlugin/Resources/FmodSoundBankResourceLoader.cpp
140 строк
5 KB
Jan Krassnigg
GAL improvements (#1422)
05 ноя 2024, 22:46
Не верифицирован
05 ноя 2024, 22:46
0e9f0e4
Код
Авторство
О чём код?
#include <FmodPlugin/FmodPluginPCH.h> #include <FmodPlugin/FmodIncludes.h> #include <FmodPlugin/FmodSingleton.h> #include <FmodPlugin/Resources/FmodSoundBankResource.h> #include <Foundation/IO/FileSystem/FileReader.h> #include <Foundation/IO/FileSystem/FileSystem.h> #include <Foundation/IO/OSFile.h> #include <Foundation/Utilities/AssetFileHeader.h> ezResourceLoadData ezFmodSoundBankResourceLoader::OpenDataStream(const ezResource* pResource) { EZ_LOG_BLOCK("ezFmodSoundBankResourceLoader::OpenDataStream", pResource->GetResourceID()); LoadedData* pData = EZ_DEFAULT_NEW(LoadedData); ezResourceLoadData res; { ezFileReader SoundBankAssetFile; if (SoundBankAssetFile.Open(pResource->GetResourceID()).Failed()) return res; res.m_sResourceDescription = SoundBankAssetFile.GetFilePathRelative().GetData(); ezUInt32 uiSoundBankSize = 0; if (SoundBankAssetFile.GetFilePathRelative().EndsWith_NoCase("ezFmodSoundBank")) // a transformed asset file { // skip the asset header ezAssetFileHeader header; header.Read(SoundBankAssetFile).IgnoreResult(); ezUInt8 uiVersion = 0; SoundBankAssetFile >> uiVersion; EZ_ASSERT_DEV(uiVersion == 1, "Soundbank resource file version '{0}' is invalid", uiVersion); SoundBankAssetFile >> uiSoundBankSize; } else { // otherwise we assume it is directly an FMOD sound bank file uiSoundBankSize = (ezUInt32)SoundBankAssetFile.GetFileSize(); } if (uiSoundBankSize > 0) { pData->m_pSoundbankData = EZ_DEFAULT_NEW(ezDataBuffer); pData->m_pSoundbankData->SetCountUninitialized(uiSoundBankSize + FMOD_STUDIO_LOAD_MEMORY_ALIGNMENT); ezUInt8* pAlignedData = ezMemoryUtils::AlignBackwards(pData->m_pSoundbankData->GetData() + FMOD_STUDIO_LOAD_MEMORY_ALIGNMENT, FMOD_STUDIO_LOAD_MEMORY_ALIGNMENT); SoundBankAssetFile.ReadBytes(pAlignedData, uiSoundBankSize); // The FMOD documentation says it is fully thread-safe, so I assume we can call loadBankMemory at any time auto pStudio = ezFmod::GetSingleton()->GetStudioSystem(); // this happens when FMOD is not properly configured if (pStudio == nullptr) return res; auto fmodRes = pStudio->loadBankMemory((const char*)pAlignedData, (int)uiSoundBankSize, FMOD_STUDIO_LOAD_MEMORY_POINT, FMOD_STUDIO_LOAD_BANK_NORMAL, &pData->m_pSoundBank); // if this fails with res == FMOD_ERR_NOTREADY, that might be because two processes using FMOD are running and both have the // FMOD_STUDIO_INIT_LIVEUPDATE flag set somehow FMOD cannot handle this and bank loading then fails if (fmodRes != FMOD_OK) { // if this fails with FMOD_ERR_EVENT_ALREADY_LOADED we might have attempted to load the bank multiple times in parallel // this is not a problem, we just discard the failed second attempt if (fmodRes != FMOD_ERR_EVENT_ALREADY_LOADED) { ezLog::Error("Error '{1}' loading FMOD sound bank '{0}'", SoundBankAssetFile.GetFilePathRelative().GetData(), (ezInt32)fmodRes); } EZ_DEFAULT_DELETE(pData->m_pSoundbankData); EZ_DEFAULT_DELETE(pData); res.m_pCustomLoaderData = nullptr; res.m_pDataStream = nullptr; return res; } } #if EZ_ENABLED(EZ_SUPPORTS_FILE_STATS) { ezFileStats stat; if (ezFileSystem::GetFileStats(pResource->GetResourceID(), stat).Succeeded()) { res.m_LoadedFileModificationDate = stat.m_LastModificationTime; } } #endif } ezMemoryStreamWriter w(&pData->m_Storage); w.WriteBytes(&pData->m_pSoundBank, sizeof(FMOD::Studio::Bank*)).IgnoreResult(); w.WriteBytes(&pData->m_pSoundbankData, sizeof(ezDataBuffer*)).IgnoreResult(); res.m_pDataStream = &pData->m_Reader; res.m_pCustomLoaderData = pData; return res; } void ezFmodSoundBankResourceLoader::CloseDataStream(const ezResource* pResource, const ezResourceLoadData& loaderData) { LoadedData* pData = (LoadedData*)loaderData.m_pCustomLoaderData; EZ_DEFAULT_DELETE(pData); } bool ezFmodSoundBankResourceLoader::IsResourceOutdated(const ezResource* pResource) const { #if EZ_ENABLED(EZ_COMPILE_FOR_DEVELOPMENT) // don't try to reload a file that cannot be found ezStringBuilder sAbs; if (ezFileSystem::ResolvePath(pResource->GetResourceID(), &sAbs, nullptr).Failed()) return false; # if EZ_ENABLED(EZ_SUPPORTS_FILE_STATS) if (pResource->GetLoadedFileModificationTime().IsValid()) { ezFileStats stat; if (ezFileSystem::GetFileStats(pResource->GetResourceID(), stat).Failed()) return false; return !stat.m_LastModificationTime.Compare(pResource->GetLoadedFileModificationTime(), ezTimestamp::CompareMode::FileTimeEqual); } # endif return true; #else return false; // we cannot reload these resources without overhead, so only allow this during development #endif }