2017-01-20 08:36:52 -05:00
|
|
|
#include "STDInclude.hpp"
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
namespace Assets
|
|
|
|
{
|
2018-12-17 08:29:18 -05:00
|
|
|
void ILoadedSound::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
|
2017-02-12 15:19:04 -05:00
|
|
|
{
|
2021-04-08 07:01:19 -04:00
|
|
|
Components::FileSystem::File soundFile(Utils::String::VA("loaded_sound/%s", name.data()));
|
2017-02-13 16:24:11 -05:00
|
|
|
if (!soundFile.exists())
|
|
|
|
{
|
2017-02-12 15:19:04 -05:00
|
|
|
header->loadSnd = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).loadSnd;
|
2017-02-13 16:24:11 -05:00
|
|
|
return;
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Game::LoadedSound* sound = builder->getAllocator()->allocate<Game::LoadedSound>();
|
|
|
|
if (!sound)
|
|
|
|
{
|
|
|
|
Components::Logger::Print("Error allocating memory for sound structure!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:24:11 -05:00
|
|
|
Game::LoadedSound* reference = nullptr;
|
|
|
|
if (!reference) reference = Game::DB_FindXAssetHeader(Game::ASSET_TYPE_LOADED_SOUND, "weapons/c4_detpack/c4_drop_dirt1.wav").loadSnd;
|
2017-02-12 15:19:04 -05:00
|
|
|
|
2017-04-15 10:48:43 -04:00
|
|
|
std::memcpy(sound, reference, sizeof(Game::LoadedSound));
|
2017-02-12 15:19:04 -05:00
|
|
|
sound->sound.data = nullptr;
|
|
|
|
|
|
|
|
Utils::Stream::Reader reader(builder->getAllocator(), soundFile.getBuffer());
|
|
|
|
|
|
|
|
unsigned int chunkIDBuffer = reader.read<unsigned int>();
|
|
|
|
if (chunkIDBuffer != 0x46464952) // RIFF
|
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading sound '%s' failed, header is invalid!", name.data());
|
2017-02-13 11:24:07 -05:00
|
|
|
return;
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int chunkSize = reader.read<unsigned int>();
|
|
|
|
|
|
|
|
unsigned int format = reader.read<unsigned int>();
|
|
|
|
if (format != 0x45564157) // WAVE
|
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading sound '%s' failed, header is invalid!", name.data());
|
2017-02-13 11:24:07 -05:00
|
|
|
return;
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!sound->sound.data && !reader.end())
|
|
|
|
{
|
|
|
|
chunkIDBuffer = reader.read<unsigned int>();
|
|
|
|
chunkSize = reader.read<unsigned int>();
|
|
|
|
|
|
|
|
switch (chunkIDBuffer)
|
|
|
|
{
|
|
|
|
case 0x20746D66: // fmt
|
|
|
|
if (chunkSize >= 16)
|
|
|
|
{
|
|
|
|
sound->sound.info.format = reader.read<short>();
|
|
|
|
if (sound->sound.info.format != 1 && sound->sound.info.format != 17)
|
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading sound '%s' failed, invalid format!", name.data());
|
2017-02-13 11:24:07 -05:00
|
|
|
return;
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sound->sound.info.channels = reader.read<short>();
|
|
|
|
sound->sound.info.rate = reader.read<int>();
|
2021-04-12 18:48:01 -04:00
|
|
|
sound->sound.info.samples = reader.read<int>();
|
2017-02-12 15:19:04 -05:00
|
|
|
sound->sound.info.block_size = reader.read<short>();
|
|
|
|
sound->sound.info.bits = reader.read<short>();
|
|
|
|
|
|
|
|
// skip any extra parameters
|
|
|
|
if (chunkSize > 16)
|
|
|
|
{
|
2017-04-15 10:48:43 -04:00
|
|
|
reader.seekRelative(chunkSize - 16);
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x61746164: // data
|
|
|
|
sound->sound.info.data_len = chunkSize;
|
|
|
|
sound->sound.data = reader.readArray<char>(chunkSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (chunkSize > 0)
|
|
|
|
{
|
2017-04-15 10:48:43 -04:00
|
|
|
reader.seekRelative(chunkSize);
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sound->sound.info.data_ptr)
|
|
|
|
{
|
|
|
|
Components::Logger::Error(0, "Reading sound '%s' failed, invalid format!", name.data());
|
2017-02-13 11:24:07 -05:00
|
|
|
return;
|
2017-02-12 15:19:04 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 11:24:07 -05:00
|
|
|
sound->name = builder->getAllocator()->duplicateString(name.c_str());
|
2017-02-12 15:19:04 -05:00
|
|
|
header->loadSnd = sound;
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
void ILoadedSound::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
|
|
|
{
|
|
|
|
AssertSize(Game::LoadedSound, 44);
|
|
|
|
|
|
|
|
Utils::Stream* buffer = builder->getBuffer();
|
|
|
|
Game::LoadedSound* asset = header.loadSnd;
|
|
|
|
Game::LoadedSound* dest = buffer->dest<Game::LoadedSound>();
|
|
|
|
buffer->save(asset);
|
|
|
|
|
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
|
|
|
|
|
|
|
|
if (asset->name)
|
|
|
|
{
|
|
|
|
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
|
|
|
|
Utils::Stream::ClearPointer(&dest->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_TEMP);
|
|
|
|
|
2017-02-12 15:19:04 -05:00
|
|
|
if (asset->sound.data)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-02-12 15:19:04 -05:00
|
|
|
if (builder->hasPointer(asset->sound.data))
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-02-12 15:19:04 -05:00
|
|
|
dest->sound.data = builder->getPointer(asset->sound.data);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-12 15:19:04 -05:00
|
|
|
builder->storePointer(asset->sound.data);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2017-02-12 15:19:04 -05:00
|
|
|
buffer->saveArray(asset->sound.data, asset->sound.info.data_len);
|
|
|
|
Utils::Stream::ClearPointer(&dest->sound.data);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->popBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->popBlock();
|
|
|
|
}
|
|
|
|
}
|