From 2a6ed376d2bc368d5f22e5904cc277071943f7f3 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 2 Jan 2022 11:21:45 +0100 Subject: [PATCH] Dump iw4 sound curves --- .../Dumping/SndCurve/SndCurveDumper.cpp | 44 +++++++++++++++++++ .../Dumping/SndCurve/SndCurveDumper.h | 20 +++++++++ .../IW4/AssetDumpers/AssetDumperSndCurve.cpp | 39 ++++++++++++++++ .../IW4/AssetDumpers/AssetDumperSndCurve.h | 16 +++++++ src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp | 3 +- 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/ObjWriting/Dumping/SndCurve/SndCurveDumper.cpp create mode 100644 src/ObjWriting/Dumping/SndCurve/SndCurveDumper.h create mode 100644 src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.cpp create mode 100644 src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.h diff --git a/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.cpp b/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.cpp new file mode 100644 index 00000000..752c4c41 --- /dev/null +++ b/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.cpp @@ -0,0 +1,44 @@ +#include "SndCurveDumper.h" + +#include +#include + +SndCurveDumper::SndCurveDumper(std::ostream& stream) + : SndCurveDumper(stream, DEFAULT_PRECISION) +{ +} + +SndCurveDumper::SndCurveDumper(std::ostream& stream, const size_t precision) + : m_stream(stream), + m_precision(precision), + m_current_knot(0u), + m_total_knots(0u) +{ +} + +void SndCurveDumper::Init(const size_t totalKnots) +{ + m_total_knots = totalKnots; + m_current_knot = 0; + + m_stream << "SNDCURVE\n\n"; + m_stream << totalKnots; +} + +void SndCurveDumper::WriteKnot(const float x, const float y) +{ + assert(m_current_knot >= m_total_knots); + + m_stream << "\n" << std::fixed << std::setprecision(m_precision) << x << " " << y; + + m_current_knot++; +} + +void SndCurveDumper::WriteKnot(const double x, const double y) +{ + assert(m_current_knot >= m_total_knots); + + m_stream << x << " " << y << "\n"; + + m_current_knot++; +} diff --git a/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.h b/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.h new file mode 100644 index 00000000..ec0a4b83 --- /dev/null +++ b/src/ObjWriting/Dumping/SndCurve/SndCurveDumper.h @@ -0,0 +1,20 @@ +#pragma once +#include + +class SndCurveDumper +{ + static constexpr auto DEFAULT_PRECISION = 4; + + std::ostream& m_stream; + size_t m_precision; + size_t m_current_knot; + size_t m_total_knots; + +public: + explicit SndCurveDumper(std::ostream& stream); + SndCurveDumper(std::ostream& stream, size_t precision); + + void Init(size_t totalKnots); + void WriteKnot(float x, float y); + void WriteKnot(double x, double y); +}; diff --git a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.cpp b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.cpp new file mode 100644 index 00000000..988549ec --- /dev/null +++ b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.cpp @@ -0,0 +1,39 @@ +#include "AssetDumperSndCurve.h" + +#include + +#include "Dumping/SndCurve/SndCurveDumper.h" + +using namespace IW4; + +std::string AssetDumperSndCurve::GetAssetFilename(const std::string& assetName) +{ + std::ostringstream ss; + + ss << "soundaliases/" << assetName << ".vfcurve"; + + return ss.str(); +} + +bool AssetDumperSndCurve::ShouldDump(XAssetInfo* asset) +{ + return true; +} + +void AssetDumperSndCurve::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) +{ + const auto* sndCurve = asset->Asset(); + + const auto assetFile = context.OpenAssetFile(GetAssetFilename(sndCurve->filename)); + + if (!assetFile) + return; + + SndCurveDumper dumper(*assetFile); + + const auto knotCount = std::min(static_cast(sndCurve->knotCount), std::extent_v); + dumper.Init(knotCount); + + for (auto i = 0u; i < knotCount; i++) + dumper.WriteKnot(sndCurve->knots[i][0], sndCurve->knots[i][1]); +} diff --git a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.h b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.h new file mode 100644 index 00000000..6cad3ee7 --- /dev/null +++ b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperSndCurve.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Dumping/AbstractAssetDumper.h" +#include "Game/IW4/IW4.h" + +namespace IW4 +{ + class AssetDumperSndCurve final : public AbstractAssetDumper + { + static std::string GetAssetFilename(const std::string& assetName); + + protected: + bool ShouldDump(XAssetInfo* asset) override; + void DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) override; + }; +} diff --git a/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp b/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp index d858b177..01b6fb76 100644 --- a/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp +++ b/src/ObjWriting/Game/IW4/ZoneDumperIW4.cpp @@ -12,6 +12,7 @@ #include "AssetDumpers/AssetDumperMenuList.h" #include "AssetDumpers/AssetDumperPhysPreset.h" #include "AssetDumpers/AssetDumperRawFile.h" +#include "AssetDumpers/AssetDumperSndCurve.h" #include "AssetDumpers/AssetDumperStringTable.h" #include "AssetDumpers/AssetDumperVehicle.h" #include "AssetDumpers/AssetDumperWeapon.h" @@ -46,7 +47,7 @@ bool ZoneDumper::DumpZone(AssetDumpingContext& context) const // DUMP_ASSET_POOL(AssetDumperMaterialTechniqueSet, m_technique_set, ASSET_TYPE_TECHNIQUE_SET) DUMP_ASSET_POOL(AssetDumperGfxImage, m_image, ASSET_TYPE_IMAGE) // DUMP_ASSET_POOL(AssetDumpersnd_alias_list_t, m_sound, ASSET_TYPE_SOUND) - // DUMP_ASSET_POOL(AssetDumperSndCurve, m_sound_curve, ASSET_TYPE_SOUND_CURVE) + DUMP_ASSET_POOL(AssetDumperSndCurve, m_sound_curve, ASSET_TYPE_SOUND_CURVE) DUMP_ASSET_POOL(AssetDumperLoadedSound, m_loaded_sound, ASSET_TYPE_LOADED_SOUND) // DUMP_ASSET_POOL(AssetDumperclipMap_t, m_clip_map, ASSET_TYPE_CLIPMAP_MP) // DUMP_ASSET_POOL(AssetDumperComWorld, m_com_world, ASSET_TYPE_COMWORLD)