From 861d77ea4bc39b609aa6dabc500c03f1716e54b9 Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Tue, 22 Aug 2023 22:04:23 +0200 Subject: [PATCH] Add music volume slider --- data/zonetool/localizedstrings/english.json | 3 ++ src/client/component/sound.cpp | 37 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/data/zonetool/localizedstrings/english.json b/data/zonetool/localizedstrings/english.json index 19ea2692..a91caf00 100644 --- a/data/zonetool/localizedstrings/english.json +++ b/data/zonetool/localizedstrings/english.json @@ -64,6 +64,9 @@ "LUA_MENU_INTRO": "Intro movie", "LUA_MENU_INTRO_DESC": "Show or skip intro movie with companies' logos on startup.", + "MENU_MUSIC_VOLUME": "Music Volume", + "MENU_MUSIC_VOLUME_DESC": "Move the slider to adjust the volume of the music.", + "MENU_SYSINFO_CUSTOMER_SUPPORT_LINK": "Github Page:", "MENU_SYSINFO_CUSTOMER_SUPPORT_URL": "https://github.com/fedddddd/h2-mod", "MENU_SYSINFO_DONATION_LINK": "Donation link:", diff --git a/src/client/component/sound.cpp b/src/client/component/sound.cpp index 03b7d752..6eb8e2a7 100644 --- a/src/client/component/sound.cpp +++ b/src/client/component/sound.cpp @@ -13,6 +13,9 @@ namespace sound { namespace { + game::dvar_t* snd_music_volume = nullptr; + game::dvar_t** snd_music_disabled_for_custom_sndtrack = nullptr; + void com_sprintf_raw_sound_localized_stub(char* buffer, int size, const char* fmt, const char* lang, const char* name, const char* extension) { @@ -35,6 +38,35 @@ namespace sound return snd_is_music_playing_hook.invoke(a1); } + + float get_snd_volume(game::snd_alias_t* sound, float original_volume) + { + // original code + if (sound->dspBusIndex == *reinterpret_cast(0x151B9AA40) && + snd_music_disabled_for_custom_sndtrack && + (*snd_music_disabled_for_custom_sndtrack)->current.enabled) + { + return 0.f; + } + + if (sound->dspBusIndex == 11 || sound->volModIndex == 5) + { + return original_volume * snd_music_volume->current.value; + } + + return original_volume; + } + + void snd_update_channel_stub(utils::hook::assembler& a) + { + a.pushad64(); + a.mov(rcx, qword_ptr(rdi, 0xD0)); + a.call_aligned(get_snd_volume); + a.movaps(xmm1, xmm0); + a.popad64(); + + a.jmp(0x1407CD8C7); + } } class component final : public component_interface @@ -42,12 +74,17 @@ namespace sound public: void post_unpack() override { + snd_music_volume = dvars::register_float("snd_musicVolume", 1.f, 0.0f, 1.f, game::DVAR_FLAG_SAVED, "Music volume scale"); + snd_music_disabled_for_custom_sndtrack = reinterpret_cast(0x151B818C8); + // remove raw/sound or raw/language/sound prefix when loading raw sounds utils::hook::call(0x140622FEF, com_sprintf_raw_sound_localized_stub); utils::hook::call(0x14062306C, com_sprintf_raw_sound_stub); // fix playing non-existing music crashing snd_is_music_playing_hook.create(0x1407C58A0, snd_is_music_playing_stub); + + utils::hook::jump(0x1407CD8A5, utils::hook::assemble(snd_update_channel_stub), true); } }; }