Zonebuilder builds sounds & loaded_sounds now

This commit is contained in:
rackover 2021-04-08 13:01:19 +02:00
parent 4db236c852
commit e19409f550
4 changed files with 277 additions and 209 deletions

View File

@ -4,7 +4,7 @@ namespace Assets
{ {
void ILoadedSound::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) void ILoadedSound::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{ {
Components::FileSystem::File soundFile(Utils::String::VA("sounds/%s", name.data())); Components::FileSystem::File soundFile(Utils::String::VA("loaded_sound/%s", name.data()));
if (!soundFile.exists()) if (!soundFile.exists())
{ {
header->loadSnd = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).loadSnd; header->loadSnd = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).loadSnd;

View File

@ -4,10 +4,10 @@ namespace Assets
{ {
void Isnd_alias_list_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) void Isnd_alias_list_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{ {
Components::FileSystem::File aliasFile(Utils::String::VA("sounds/%s", name.data())); Components::FileSystem::File aliasFile(Utils::String::VA("sounds/%s", name.c_str()));
if (!aliasFile.exists()) if (!aliasFile.exists())
{ {
header->sound = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).sound; header->sound = Components::AssetHandler::FindOriginalAsset(this->getType(), name.c_str()).sound;
return; return;
} }
@ -18,12 +18,33 @@ namespace Assets
return; return;
} }
std::string errors;
json11::Json infoData = json11::Json::parse(aliasFile.getBuffer(), errors);
json11::Json aliasesContainer = infoData["head"];
auto aliases = aliasesContainer.array_items();
aliasList->count = aliases.size();
aliasList->aliasName = builder->getAllocator()->duplicateString(infoData["aliasName"].string_value().c_str());
for (size_t i = 0; i < aliasList->count; i++)
{
json11::Json head = aliasesContainer[i];
if (!infoData.is_object())
{
Components::Logger::Error("Failed to load sound %s!", name.c_str());
return;
}
// Allocate
aliasList->head = builder->getAllocator()->allocate<Game::snd_alias_t>(); aliasList->head = builder->getAllocator()->allocate<Game::snd_alias_t>();
if (!aliasList->head) if (!aliasList->head)
{ {
Components::Logger::Print("Error allocating memory for sound alias structure!\n"); Components::Logger::Print("Error allocating memory for sound alias structure!\n");
return; return;
} }
aliasList->head->soundFile = builder->getAllocator()->allocate<Game::SoundFile>(); aliasList->head->soundFile = builder->getAllocator()->allocate<Game::SoundFile>();
if (!aliasList->head->soundFile) if (!aliasList->head->soundFile)
{ {
@ -31,19 +52,6 @@ namespace Assets
return; return;
} }
aliasList->count = 1;
std::string errors;
json11::Json infoData = json11::Json::parse(aliasFile.getBuffer(), errors);
json11::Json head = infoData["head"][0];
if (!infoData.is_object())
{
Components::Logger::Error("Failed to load sound %s!", name.data());
return;
}
Game::snd_alias_t* alias = aliasList->head; Game::snd_alias_t* alias = aliasList->head;
// try and parse everything and if it fails then fail for the whole file // try and parse everything and if it fails then fail for the whole file
@ -72,107 +80,117 @@ namespace Assets
auto speakerMap = head["speakerMap"]; auto speakerMap = head["speakerMap"];
// Fix casing // Fix casing
if (soundFile.is_null()) { if (soundFile.is_null())
{
soundFile = head["soundfile"]; soundFile = head["soundfile"];
Components::Logger::Print("Fixed casing on %s\n", name.data()); Components::Logger::Print("Fixed casing on %s\n", name.c_str());
} }
if (type.is_null() || soundFile.is_null()) if (type.is_null() || soundFile.is_null())
{ {
//auto p = fopen("test", "w"); Components::Logger::Print("Type is %s\n", type.dump().c_str());
//fwrite(aliasFile.getBuffer().data(), aliasFile.getBuffer().length(), 1, p); Components::Logger::Print("SoundFile is %s\n", soundFile.dump().c_str());
//fflush(p); Components::Logger::Error("Failed to parse sound %s! Each alias must have at least a type and a soundFile\n", name.c_str());
//fclose(p);
//auto p2 = fopen("test2", "w");
//fwrite(infoData.dump().data(), infoData.dump().length(), 1, p2);
//fflush(p2);
//fclose(p2);
Components::Logger::Print("Type is %s\n", type.dump().data());
Components::Logger::Print("SoundFile is %s\n", soundFile.dump().data());
Components::Logger::Error("Failed to parse sound %s! Each alias must have at least a type and a soundFile\n", name.data());
return; return;
} }
#define CHECK(x, type) (x.is_##type##() || x.is_null()) #define CHECK(x, type) (x.is_##type##() || x.is_null())
// TODO: actually support all of those properties // TODO: actually support all of those properties
if (!CHECK(type, string)) { if (!CHECK(type, number))
Components::Logger::Print("%s is not string but %d (%s)\n", "type", type.type(), type.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "type", type.type(), type.dump().c_str());
} }
if (!CHECK(subtitle, string)) { if (!CHECK(subtitle, string))
Components::Logger::Print("%s is not string but %d (%s)\n", "subtitle", subtitle.type(), subtitle.dump().data()); {
Components::Logger::Print("%s is not string but %d (%s)\n", "subtitle", subtitle.type(), subtitle.dump().c_str());
} }
if (!CHECK(secondaryAliasName, string)) { if (!CHECK(secondaryAliasName, string))
Components::Logger::Print("%s is not string but %d (%s)\n", "secondaryAliasName", secondaryAliasName.type(), secondaryAliasName.dump().data()); {
Components::Logger::Print("%s is not string but %d (%s)\n", "secondaryAliasName", secondaryAliasName.type(), secondaryAliasName.dump().c_str());
} }
if (!CHECK(chainAliasName, string)) { if (!CHECK(chainAliasName, string))
Components::Logger::Print("%s is not string but %d (%s)\n", "chainAliasName", chainAliasName.type(), chainAliasName.dump().data()); {
Components::Logger::Print("%s is not string but %d (%s)\n", "chainAliasName", chainAliasName.type(), chainAliasName.dump().c_str());
} }
if (!CHECK(soundFile, string)) { if (!CHECK(soundFile, string))
Components::Logger::Print("%s is not string but %d (%s)\n", "soundFile", soundFile.type(), soundFile.dump().data()); {
Components::Logger::Print("%s is not string but %d (%s)\n", "soundFile", soundFile.type(), soundFile.dump().c_str());
} }
if (!CHECK(sequence, number)) { if (!CHECK(sequence, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "sequence", sequence.type(), sequence.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "sequence", sequence.type(), sequence.dump().c_str());
} }
if (!CHECK(volMin, number)) { if (!CHECK(volMin, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "volMin", volMin.type(), volMin.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "volMin", volMin.type(), volMin.dump().c_str());
} }
if (!CHECK(volMax, number)) { if (!CHECK(volMax, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "volMax", volMax.type(), volMax.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "volMax", volMax.type(), volMax.dump().c_str());
} }
if (!CHECK(pitchMin, number)) { if (!CHECK(pitchMin, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "pitchMin", pitchMin.type(), pitchMin.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "pitchMin", pitchMin.type(), pitchMin.dump().c_str());
} }
if (!CHECK(pitchMax, number)) { if (!CHECK(pitchMax, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "pitchMax", pitchMax.type(), pitchMax.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "pitchMax", pitchMax.type(), pitchMax.dump().c_str());
} }
if (!CHECK(probability, number)) { if (!CHECK(probability, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "probability", probability.type(), probability.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "probability", probability.type(), probability.dump().c_str());
} }
if (!CHECK(lfePercentage, number)) { if (!CHECK(lfePercentage, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "lfePercentage", lfePercentage.type(), lfePercentage.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "lfePercentage", lfePercentage.type(), lfePercentage.dump().c_str());
} }
if (!CHECK(centerPercentage, number)) { if (!CHECK(centerPercentage, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "centerPercentage", centerPercentage.type(), centerPercentage.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "centerPercentage", centerPercentage.type(), centerPercentage.dump().c_str());
} }
if (!CHECK(startDelay, number)) { if (!CHECK(startDelay, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "startDelay", startDelay.type(), startDelay.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "startDelay", startDelay.type(), startDelay.dump().c_str());
} }
if (!CHECK(volumeFalloffCurve, string)) { if (!CHECK(volumeFalloffCurve, string))
Components::Logger::Print("%s is not string but %d (%s)\n", "volumeFalloffCurve", volumeFalloffCurve.type(), volumeFalloffCurve.dump().data()); {
Components::Logger::Print("%s is not string but %d (%s)\n", "volumeFalloffCurve", volumeFalloffCurve.type(), volumeFalloffCurve.dump().c_str());
} }
if (!CHECK(envelopMin, number)) { if (!CHECK(envelopMin, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopMin", envelopMin.type(), envelopMin.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopMin", envelopMin.type(), envelopMin.dump().c_str());
} }
if (!CHECK(envelopMax, number)) { if (!CHECK(envelopMax, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopMax", envelopMax.type(), envelopMax.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopMax", envelopMax.type(), envelopMax.dump().c_str());
} }
if (!CHECK(envelopPercentage, number)) { if (!CHECK(envelopPercentage, number))
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopPercentage", envelopPercentage.type(), envelopPercentage.dump().data()); {
Components::Logger::Print("%s is not number but %d (%s)\n", "envelopPercentage", envelopPercentage.type(), envelopPercentage.dump().c_str());
} }
if (!CHECK(speakerMap, object)) { if (!CHECK(speakerMap, object))
Components::Logger::Print("%s is not object but %d (%s)\n", "speakerMap", speakerMap.type(), speakerMap.dump().data()); {
Components::Logger::Print("%s is not object but %d (%s)\n", "speakerMap", speakerMap.type(), speakerMap.dump().c_str());
} }
@ -185,18 +203,19 @@ namespace Assets
{ {
alias->soundFile->exists = true; alias->soundFile->exists = true;
alias->aliasName = aliasList->aliasName;
if (subtitle.is_string()) if (subtitle.is_string())
{ {
alias->subtitle = subtitle.string_value().data(); alias->subtitle = builder->getAllocator()->duplicateString(subtitle.string_value().c_str());
} }
if (secondaryAliasName.is_string()) if (secondaryAliasName.is_string())
{ {
alias->secondaryAliasName = secondaryAliasName.string_value().data(); alias->secondaryAliasName = builder->getAllocator()->duplicateString(secondaryAliasName.string_value().c_str());
} }
if (chainAliasName.is_string()) if (chainAliasName.is_string())
{ {
alias->chainAliasName = chainAliasName.string_value().data(); alias->chainAliasName = builder->getAllocator()->duplicateString(chainAliasName.string_value().c_str());
} }
alias->sequence = sequence.int_value(); alias->sequence = sequence.int_value();
@ -216,6 +235,50 @@ namespace Assets
alias->envelopMax = float(envelopMax.number_value()); alias->envelopMax = float(envelopMax.number_value());
alias->envelopPercentage = float(envelopPercentage.number_value()); alias->envelopPercentage = float(envelopPercentage.number_value());
// Speaker map object
if (!speakerMap.is_null())
{
alias->speakerMap = builder->getAllocator()->allocate<Game::SpeakerMap>();
if (!alias->speakerMap)
{
Components::Logger::Print("Error allocating memory for speakermap in sound alias%s!\n", alias->aliasName);
return;
}
alias->speakerMap->name = builder->getAllocator()->duplicateString(speakerMap["name"].string_value().c_str());
alias->speakerMap->isDefault = speakerMap["isDefault"].bool_value();
if (speakerMap["channelMaps"].is_array())
{
json11::Json::array channelMaps = speakerMap["channelMaps"].array_items();
assert(channelMaps.size() <= 4);
// channelMapIndex should never exceed 1
for (size_t channelMapIndex = 0; channelMapIndex < 2; channelMapIndex++)
{
// subChannelIndex should never exceed 1
for (size_t subChannelIndex = 0; subChannelIndex < 2; subChannelIndex++)
{
json11::Json channelMap = channelMaps[channelMapIndex * 2 + subChannelIndex]; // 0-3
auto speakers = channelMap["speakers"].array_items();
alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakerCount = speakers.size();
for (size_t speakerIndex = 0; speakerIndex < alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakerCount; speakerIndex++)
{
auto speaker = speakers[speakerIndex];
alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakers[speakerIndex].levels[0] = static_cast<float>(speaker["levels0"].number_value());
alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakers[speakerIndex].levels[1] = static_cast<float>(speaker["levels1"].number_value());
alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakers[speakerIndex].numLevels = static_cast<int>(speaker["numLevels"].number_value());
alias->speakerMap->channelMaps[channelMapIndex][subChannelIndex].speakers[speakerIndex].speaker = static_cast<int>(speaker["speaker"].number_value());
}
}
}
}
}
if (volumeFalloffCurve.is_string()) if (volumeFalloffCurve.is_string())
{ {
alias->volumeFalloffCurve = Components::AssetHandler::FindAssetForZone(Game::XAssetType::ASSET_TYPE_SOUND_CURVE, volumeFalloffCurve.string_value(), builder).sndCurve; alias->volumeFalloffCurve = Components::AssetHandler::FindAssetForZone(Game::XAssetType::ASSET_TYPE_SOUND_CURVE, volumeFalloffCurve.string_value(), builder).sndCurve;
@ -236,14 +299,23 @@ namespace Assets
} }
else else
{ {
Components::Logger::Error("Failed to parse sound %s! Invalid sound type %s\n", name.data(), type.string_value().c_str()); Components::Logger::Error("Failed to parse sound %s! Invalid sound type %s\n", name.c_str(), type.string_value().c_str());
return;
}
if (i == 0)
{
aliasList->head = alias;
} }
} }
else else
{ {
Components::Logger::Error("Failed to parse sound %s!\n", name.data()); Components::Logger::Error("Failed to parse sound %s!\n", name.c_str());
return; return;
} }
}
header->sound = aliasList;
#undef CHECK #undef CHECK
@ -253,7 +325,7 @@ namespace Assets
{ {
Game::snd_alias_list_t* asset = header.sound; Game::snd_alias_list_t* asset = header.sound;
for (int i = 0; i < asset->count; ++i) for (unsigned int i = 0; i < asset->count; ++i)
{ {
Game::snd_alias_t* alias = &asset->head[i]; Game::snd_alias_t* alias = &asset->head[i];
@ -302,7 +374,7 @@ namespace Assets
Game::snd_alias_t* destHead = buffer->dest<Game::snd_alias_t>(); Game::snd_alias_t* destHead = buffer->dest<Game::snd_alias_t>();
buffer->saveArray(asset->head, asset->count); buffer->saveArray(asset->head, asset->count);
for (int i = 0; i < asset->count; ++i) for (unsigned int i = 0; i < asset->count; ++i)
{ {
Game::snd_alias_t* destAlias = &destHead[i]; Game::snd_alias_t* destAlias = &destHead[i];
Game::snd_alias_t* alias = &asset->head[i]; Game::snd_alias_t* alias = &asset->head[i];

View File

@ -223,10 +223,6 @@ namespace Components
Game::XAssetHeader assetHeader = AssetHandler::FindAssetForZone(type, name, this, isSubAsset); Game::XAssetHeader assetHeader = AssetHandler::FindAssetForZone(type, name, this, isSubAsset);
if (type == Game::XAssetType::ASSET_TYPE_LOADED_SOUND) {
Logger::Print("Loading loaded_sound '%s'\n", name.data());
}
if (!assetHeader.data) if (!assetHeader.data)
{ {
Logger::Error("Error: Missing asset '%s' of type '%s'\n", name.data(), Game::DB_GetXAssetTypeName(type)); Logger::Error("Error: Missing asset '%s' of type '%s'\n", name.data(), Game::DB_GetXAssetTypeName(type));

View File

@ -906,7 +906,7 @@ namespace Game
struct MSSChannelMap struct MSSChannelMap
{ {
int speakerCount; unsigned int speakerCount;
MSSSpeakerLevels speakers[6]; MSSSpeakerLevels speakers[6];
}; };
@ -950,7 +950,7 @@ namespace Game
{ {
const char *aliasName; const char *aliasName;
snd_alias_t *head; snd_alias_t *head;
int count; unsigned int count;
}; };
struct cStaticModel_s struct cStaticModel_s