From dec2ae96cbf67bd9e9ddb979e6b33a4e6624e5ca Mon Sep 17 00:00:00 2001 From: njohnson Date: Tue, 28 Apr 2026 20:54:37 -0400 Subject: [PATCH] Add loader logic from IW4. --- .../Game/IW3/Sound/LoaderSoundCurveIW3.cpp | 80 +++++++++++++++++++ .../Game/IW3/Sound/LoaderSoundCurveIW3.h | 13 +++ 2 files changed, 93 insertions(+) create mode 100644 src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.cpp create mode 100644 src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.h diff --git a/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.cpp b/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.cpp new file mode 100644 index 00000000..fabbd089 --- /dev/null +++ b/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.cpp @@ -0,0 +1,80 @@ +#include "LoaderSoundCurveIW3.h" + +#include "Game/IW3/IW3.h" +#include "ObjLoading.h" +#include "Parsing/Graph2D/Graph2DReader.h" +#include "Pool/GlobalAssetPool.h" +#include "Sound/SoundCurveCommon.h" +#include "Utils/Logging/Log.h" + +#include +#include +#include +#include + +using namespace IW3; + +namespace +{ + class LoaderSoundCurve final : public AssetCreator + { + public: + LoaderSoundCurve(MemoryManager& memory, ISearchPath& searchPath) + : m_memory(memory), + m_search_path(searchPath) + { + } + + AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override + { + const auto fileName = sound_curve::GetFileNameForAssetName(assetName); + const auto file = m_search_path.Open(fileName); + if (!file.IsOpen()) + return AssetCreationResult::NoAction(); + + const auto sndCurveData = graph2d::Read("sound curve", "SNDCURVE", *file.m_stream, fileName, assetName); + + if (!sndCurveData) + return AssetCreationResult::Failure(); + + if (sndCurveData->knots.size() > std::extent_v) + { + con::error("Failed to load SndCurve \"{}\": Too many knots ({})", assetName, sndCurveData->knots.size()); + return AssetCreationResult::Failure(); + } + + auto* sndCurve = m_memory.Alloc(); + sndCurve->filename = m_memory.Dup(assetName.c_str()); + sndCurve->knotCount = static_cast(sndCurveData->knots.size()); + + for (auto i = 0u; i < std::extent_v; i++) + { + if (i < sndCurveData->knots.size()) + { + const auto& [x, y] = sndCurveData->knots[i]; + sndCurve->knots[i][0] = static_cast(x); + sndCurve->knots[i][1] = static_cast(y); + } + else + { + sndCurve->knots[i][0] = 0; + sndCurve->knots[i][1] = 0; + } + } + + return AssetCreationResult::Success(context.AddAsset(assetName, sndCurve)); + } + + private: + MemoryManager& m_memory; + ISearchPath& m_search_path; + }; +} // namespace + +namespace sound_curve +{ + std::unique_ptr> CreateLoaderIW3(MemoryManager& memory, ISearchPath& searchPath) + { + return std::make_unique(memory, searchPath); + } +} // namespace sound_curve diff --git a/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.h b/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.h new file mode 100644 index 00000000..08dae2ad --- /dev/null +++ b/src/ObjLoading/Game/IW3/Sound/LoaderSoundCurveIW3.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Asset/IAssetCreator.h" +#include "Game/IW3/IW3.h" +#include "SearchPath/ISearchPath.h" +#include "Utils/MemoryManager.h" + +#include + +namespace sound_curve +{ + std::unique_ptr> CreateLoaderIW3(MemoryManager& memory, ISearchPath& searchPath); +} // namespace sound_curve