Improve sound bank code

This commit is contained in:
Jan 2023-11-26 12:59:21 +01:00
parent 20af6c4ba5
commit d29dc082e2
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
4 changed files with 61 additions and 62 deletions

View File

@ -1,6 +1,6 @@
#include "SoundBank.h"
#include "Utils/FileUtils.h"
#include "Utils/ObjStream.h"
#include "zlib.h"
#include <cstring>
@ -114,7 +114,7 @@ SoundBankEntryInputStream::SoundBankEntryInputStream()
{
}
SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, SoundAssetBankEntry entry)
SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, const SoundAssetBankEntry& entry)
: m_stream(std::move(stream)),
m_entry(entry)
{
@ -122,7 +122,7 @@ SoundBankEntryInputStream::SoundBankEntryInputStream(std::unique_ptr<std::istrea
bool SoundBankEntryInputStream::IsOpen() const
{
return m_stream.get() != nullptr;
return static_cast<bool>(m_stream);
}
bool SoundBank::ReadHeader()
@ -134,15 +134,15 @@ bool SoundBank::ReadHeader()
return false;
}
if (m_header.magic != MAGIC)
if (m_header.magic != sndbank_consts::MAGIC)
{
std::cout << "Invalid sndbank magic 0x" << std::hex << m_header.magic << std::endl;
return false;
}
if (m_header.version != VERSION)
if (m_header.version != sndbank_consts::VERSION)
{
std::cout << "Unsupported sndbank version " << m_header.version << " (should be " << VERSION << ")" << std::endl;
std::cout << "Unsupported sndbank version " << m_header.version << " (should be " << sndbank_consts::VERSION << ")" << std::endl;
return false;
}
@ -159,13 +159,14 @@ bool SoundBank::ReadHeader()
return false;
}
if (m_header.entryCount && (m_header.entryOffset <= 0 || m_header.entryOffset + sizeof(SoundAssetBankEntry) * m_header.entryCount > m_file_size))
if (m_header.entryCount
&& (m_header.entryOffset <= 0 || m_header.entryOffset + static_cast<int64_t>(sizeof(SoundAssetBankEntry) * m_header.entryCount) > m_file_size))
{
std::cout << "Invalid sndbank entry offset " << m_header.entryOffset << " (filesize is " << m_file_size << ")" << std::endl;
return false;
}
if (m_header.checksumOffset <= 0 || m_header.checksumOffset + sizeof(SoundAssetBankChecksum) * m_header.entryCount > m_file_size)
if (m_header.checksumOffset <= 0 || m_header.checksumOffset + static_cast<int64_t>(sizeof(SoundAssetBankChecksum) * m_header.entryCount) > m_file_size)
{
std::cout << "Invalid sndbank checksum offset " << m_header.checksumOffset << " (filesize is " << m_file_size << ")" << std::endl;
return false;

View File

@ -3,10 +3,7 @@
#include "ObjContainer/ObjContainerReferenceable.h"
#include "ObjContainer/ObjContainerRepository.h"
#include "ObjContainer/SoundBank/SoundBankTypes.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/ClassUtils.h"
#include "Utils/FileUtils.h"
#include "Utils/ObjStream.h"
#include "Zone/Zone.h"
#include <istream>
@ -14,20 +11,42 @@
class SoundBankEntryInputStream
{
public:
std::unique_ptr<std::istream> m_stream;
SoundAssetBankEntry m_entry;
SoundBankEntryInputStream();
SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, SoundAssetBankEntry entry);
SoundBankEntryInputStream(std::unique_ptr<std::istream> stream, const SoundAssetBankEntry& entry);
_NODISCARD bool IsOpen() const;
std::unique_ptr<std::istream> m_stream;
SoundAssetBankEntry m_entry;
};
class SoundBank final : public ObjContainerReferenceable
{
static constexpr uint32_t MAGIC = FileUtils::MakeMagic32('2', 'U', 'X', '#');
static constexpr uint32_t VERSION = 14u;
bool ReadHeader();
bool ReadEntries();
bool ReadChecksums();
public:
SoundBank(std::string fileName, std::unique_ptr<std::istream> stream, int64_t fileSize);
~SoundBank() override = default;
SoundBank(const SoundBank& other) = delete;
SoundBank(SoundBank&& other) noexcept = default;
SoundBank& operator=(const SoundBank& other) = delete;
SoundBank& operator=(SoundBank&& other) noexcept = default;
static std::string GetFileNameForDefinition(bool streamed, const char* zone, const char* language);
std::string GetName() override;
bool Initialize();
_NODISCARD const std::vector<std::string>& GetDependencies() const;
_NODISCARD bool VerifyChecksum(const SoundAssetBankChecksum& checksum) const;
_NODISCARD SoundBankEntryInputStream GetEntryStream(unsigned int id) const;
static ObjContainerRepository<SoundBank, Zone> Repository;
private:
std::string m_file_name;
std::unique_ptr<std::istream> m_stream;
int64_t m_file_size;
@ -38,28 +57,4 @@ class SoundBank final : public ObjContainerReferenceable
std::vector<SoundAssetBankEntry> m_entries;
std::vector<SoundAssetBankChecksum> m_checksums;
std::unordered_map<unsigned int, size_t> m_entries_by_id;
bool ReadHeader();
bool ReadEntries();
bool ReadChecksums();
public:
static ObjContainerRepository<SoundBank, Zone> Repository;
static std::string GetFileNameForDefinition(bool streamed, const char* zone, const char* language);
SoundBank(std::string fileName, std::unique_ptr<std::istream> stream, int64_t fileSize);
~SoundBank() override = default;
SoundBank(const SoundBank& other) = delete;
SoundBank(SoundBank&& other) noexcept = default;
SoundBank& operator=(const SoundBank& other) = delete;
SoundBank& operator=(SoundBank&& other) noexcept = default;
std::string GetName() override;
bool Initialize();
_NODISCARD const std::vector<std::string>& GetDependencies() const;
_NODISCARD bool VerifyChecksum(const SoundAssetBankChecksum& checksum) const;
_NODISCARD SoundBankEntryInputStream GetEntryStream(unsigned int id) const;
};

View File

@ -1,30 +1,32 @@
#pragma once
#include "Utils/FileUtils.h"
#include <cstdint>
class SoundBankConsts
namespace sndbank_consts
{
SoundBankConsts() = default;
static constexpr uint32_t MAGIC = FileUtils::MakeMagic32('2', 'U', 'X', '#');
static constexpr uint32_t VERSION = 14u;
public:
static constexpr unsigned OFFSET_DATA_START = 0x800;
};
}; // namespace sndbank_consts
struct SoundAssetBankChecksum
{
char checksumBytes[16];
uint8_t checksumBytes[16];
};
struct SoundAssetBankHeader
{
unsigned int magic; // + 0x0
unsigned int version; // + 0x4
unsigned int entrySize; // + 0x8
unsigned int checksumSize; // + 0xC
unsigned int dependencySize; // + 0x10
unsigned int entryCount; // + 0x14
unsigned int dependencyCount; // + 0x18
unsigned int pad32; // + 0x1C
uint32_t magic; // + 0x0
uint32_t version; // + 0x4
uint32_t entrySize; // + 0x8
uint32_t checksumSize; // + 0xC
uint32_t dependencySize; // + 0x10
uint32_t entryCount; // + 0x14
uint32_t dependencyCount; // + 0x18
uint32_t pad32; // + 0x1C
int64_t fileSize; // + 0x20
int64_t entryOffset; // + 0x28
int64_t checksumOffset; // + 0x30
@ -34,12 +36,12 @@ struct SoundAssetBankHeader
struct SoundAssetBankEntry
{
unsigned int id;
unsigned int size;
unsigned int offset;
unsigned int frameCount;
unsigned char frameRateIndex;
unsigned char channelCount;
unsigned char looping;
unsigned char format;
uint32_t id;
uint32_t size;
uint32_t offset;
uint32_t frameCount;
uint8_t frameRateIndex;
uint8_t channelCount;
uint8_t looping;
uint8_t format;
};

View File

@ -5,6 +5,7 @@
#include "Sound/WavWriter.h"
#include "Utils/ClassUtils.h"
#include <cassert>
#include <filesystem>
#include <fstream>
#include <unordered_set>