From de2744cb2aa5a57361be8ad6e254c7eecf324766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 14 Jun 2022 21:13:25 +0200 Subject: [PATCH] Use `ReadFile` to read opus sounds in editor --- src/game/editor/editor.cpp | 43 +++++++++----------------------------- src/game/editor/io.cpp | 17 ++------------- 2 files changed, 12 insertions(+), 48 deletions(-) diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 4c2ae233f..d3f967e74 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -3744,36 +3744,26 @@ void CEditor::AddSound(const char *pFileName, int StorageType, void *pUser) } // load external - IOHANDLE SoundFile = pEditor->Storage()->OpenFile(pFileName, IOFLAG_READ, StorageType); - if(!SoundFile) + void *pData; + unsigned DataSize; + if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize)) { dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName); return; } - // read the whole file into memory - int DataSize = io_length(SoundFile); - - if(DataSize <= 0) - { - io_close(SoundFile); - dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName); - return; - } - - void *pData = malloc((unsigned)DataSize); - io_read(SoundFile, pData, (unsigned)DataSize); - io_close(SoundFile); - // load sound int SoundId = pEditor->Sound()->LoadOpusFromMem(pData, (unsigned)DataSize, true); if(SoundId == -1) + { + free(pData); return; + } // add sound CEditorSound *pSound = new CEditorSound(pEditor); pSound->m_SoundID = SoundId; - pSound->m_DataSize = (unsigned)DataSize; + pSound->m_DataSize = DataSize; pSound->m_pData = pData; str_copy(pSound->m_aName, aBuf, sizeof(pSound->m_aName)); pEditor->m_Map.m_vpSounds.push_back(pSound); @@ -3796,27 +3786,14 @@ void CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser) CEditor *pEditor = (CEditor *)pUser; // load external - IOHANDLE SoundFile = pEditor->Storage()->OpenFile(pFileName, IOFLAG_READ, StorageType); - if(!SoundFile) + void *pData; + unsigned DataSize; + if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize)) { dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName); return; } - // read the whole file into memory - int DataSize = io_length(SoundFile); - - if(DataSize <= 0) - { - io_close(SoundFile); - dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName); - return; - } - - void *pData = malloc((unsigned)DataSize); - io_read(SoundFile, pData, (unsigned)DataSize); - io_close(SoundFile); - CEditorSound *pSound = pEditor->m_Map.m_vpSounds[pEditor->m_SelectedSound]; // unload sample diff --git a/src/game/editor/io.cpp b/src/game/editor/io.cpp index 8037be67d..d8a4ff5f6 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/io.cpp @@ -545,22 +545,9 @@ int CEditorMap::Load(class IStorage *pStorage, const char *pFileName, int Storag str_format(aBuf, sizeof(aBuf), "mapres/%s.opus", pName); // load external - IOHANDLE SoundFile = pStorage->OpenFile(pName, IOFLAG_READ, IStorage::TYPE_ALL); - if(SoundFile) + if(pStorage->ReadFile(pName, IStorage::TYPE_ALL, &pSound->m_pData, &pSound->m_DataSize)) { - // read the whole file into memory - pSound->m_DataSize = io_length(SoundFile); - - if(pSound->m_DataSize > 0) - { - pSound->m_pData = malloc(pSound->m_DataSize); - io_read(SoundFile, pSound->m_pData, pSound->m_DataSize); - } - io_close(SoundFile); - if(pSound->m_DataSize > 0) - { - pSound->m_SoundID = m_pEditor->Sound()->LoadOpusFromMem(pSound->m_pData, pSound->m_DataSize, true); - } + pSound->m_SoundID = m_pEditor->Sound()->LoadOpusFromMem(pSound->m_pData, pSound->m_DataSize, true); } } else