Skip to content

Commit

Permalink
Custom sounds now depend on Master * Effect volume
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestionableM committed Jul 16, 2023
1 parent 2fe148e commit 46e4d1c
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 137 deletions.
38 changes: 35 additions & 3 deletions Code/fmod_hooks.cpp → Code/Hooks/fmod_hooks.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "fmod_hooks.hpp"

#include "DirectoryManager.hpp"
#include "AudioManager.hpp"
#include "SM/DirectoryManager.hpp"
#include "SM/AudioManager.hpp"
#include "SM/GameSettings.hpp"

#include "win_include.hpp"

Expand Down Expand Up @@ -166,7 +167,7 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setVolume(FMOD::Studio::Event
FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_instance);
if (v_fake_event->isValidHook())
{
v_fake_event->channel->setVolume(volume);
v_fake_event->channel->setVolume(SM::GameSettings::GetEffectsVolume() * volume);
return FMOD_OK;
}

Expand Down Expand Up @@ -246,6 +247,31 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setPitch(FMOD::Studio::EventI
return FMODHooks::o_FMOD_Studio_EventInstance_setPitch(event_instance, pitch);
}

using v_fmod_set_parameter_function = FMOD_RESULT(*)(FakeEventDescription* fake_event, float value);

static FMOD_RESULT fake_event_desc_setPitch(FakeEventDescription* fake_event, float value)
{
return fake_event->channel->setPitch(value);
}

inline static std::unordered_map<std::string, v_fmod_set_parameter_function> g_fake_event_parameter_table =
{
{ "DLM_Pitch", fake_event_desc_setPitch }
};

FMOD_RESULT FMODHooks::h_FMOD_Studio_EventInstance_setParameterByName(FMOD::Studio::EventInstance* event_instance, const char* name, float value, bool ignoreseekspeed)
{
FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_instance);
if (v_fake_event->isValidHook())
{
auto v_iter = g_fake_event_parameter_table.find(std::string(name));
if (v_iter != g_fake_event_parameter_table.end())
return v_iter->second(v_fake_event, value);
}

return FMODHooks::o_FMOD_Studio_EventInstance_setParameterByName(event_instance, name, value, ignoreseekspeed);
}

FMOD_RESULT FMODHooks::h_FMOD_Studio_EventDescription_getLength(FMOD::Studio::EventDescription* event_desc, int* length)
{
FakeEventDescription* v_fake_event = FAKE_EVENT_CAST(event_desc);
Expand All @@ -270,6 +296,7 @@ FMOD_RESULT FMODHooks::h_FMOD_Studio_EventDescription_createInstance(FMOD::Studi
if (v_sound_data->is_3d)
{
v_channel->setMode(FMOD_3D);
v_channel->setVolume(SM::GameSettings::GetEffectsVolume());
}

FakeEventDescription* v_new_fake_event = new FakeEventDescription(v_sound_data->sound, v_channel);
Expand Down Expand Up @@ -436,6 +463,11 @@ static FMODHookData g_fmodHookData[] =
"?hasSustainPoint@EventDescription@Studio@FMOD@@QEBA?AW4FMOD_RESULT@@PEA_N@Z",
(LPVOID)FMODHooks::h_FMOD_Studio_EventDescription_hasSustainPoint,
(LPVOID*)&FMODHooks::o_FMOD_Studio_EventDescription_hasSustainPoint
},
{
"?setParameterByName@EventInstance@Studio@FMOD@@QEAA?AW4FMOD_RESULT@@PEBDM_N@Z",
(LPVOID)FMODHooks::h_FMOD_Studio_EventInstance_setParameterByName,
(LPVOID*)&FMODHooks::o_FMOD_Studio_EventInstance_setParameterByName
}
};

Expand Down
3 changes: 3 additions & 0 deletions Code/fmod_hooks.hpp → Code/Hooks/fmod_hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace FEventInstance
using SetTimelinePosition = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, int);
using GetPitch = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, float*, float*);
using SetPitch = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, float);
using SetParameterByName = FMOD_RESULT(__fastcall*)(FMOD::Studio::EventInstance*, const char*, float, bool);
}

namespace FEventDescription
Expand Down Expand Up @@ -149,6 +150,7 @@ class FMODHooks
inline static FEventInstance::SetTimelinePosition o_FMOD_Studio_EventInstance_setTimelinePosition = nullptr;
inline static FEventInstance::GetPitch o_FMOD_Studio_EventInstance_getPitch = nullptr;
inline static FEventInstance::SetPitch o_FMOD_Studio_EventInstance_setPitch = nullptr;
inline static FEventInstance::SetParameterByName o_FMOD_Studio_EventInstance_setParameterByName = nullptr;

static FMOD_RESULT h_FMOD_Studio_EventInstance_release(FMOD::Studio::EventInstance* event_instance);
static FMOD_RESULT h_FMOD_Studio_EventInstance_start(FMOD::Studio::EventInstance* event_instance);
Expand All @@ -163,6 +165,7 @@ class FMODHooks
static FMOD_RESULT h_FMOD_Studio_EventInstance_setTimelinePosition(FMOD::Studio::EventInstance* event_instance, int position);
static FMOD_RESULT h_FMOD_Studio_EventInstance_getPitch(FMOD::Studio::EventInstance* event_instance, float* pitch, float* finalpitch);
static FMOD_RESULT h_FMOD_Studio_EventInstance_setPitch(FMOD::Studio::EventInstance* event_instance, float pitch);
static FMOD_RESULT h_FMOD_Studio_EventInstance_setParameterByName(FMOD::Studio::EventInstance* event_instance, const char* name, float value, bool ignoreseekspeed);

//FMOD EVENT DESCRIPTION HOOKS

Expand Down
8 changes: 4 additions & 4 deletions Code/hooks.cpp → Code/Hooks/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "fmod_hooks.hpp"

#include "DirectoryManager.hpp"
#include "AudioManager.hpp"
#include "SM/DirectoryManager.hpp"
#include "SM/AudioManager.hpp"

#include "Utils/Console.hpp"
#include "Utils/String.hpp"
Expand Down Expand Up @@ -38,15 +38,15 @@ void load_sound_config(const std::string& key_repl)
simdjson::dom::document v_document;
if (!JsonReader::LoadParseSimdjsonCommentsC(v_wide_path, v_document, simdjson::dom::element_type::OBJECT))
{
DebugErrorL("[DLM] Couldn't load the DLM sound config file: ", config_path);
DebugErrorL("Couldn't load the DLM sound config file: ", config_path);
return;
}

const auto v_root = v_document.root();
const auto v_sound_list = v_root["soundList"];
if (!v_sound_list.is_object())
{
DebugErrorL("[DLM] No sound list: ", config_path);
DebugErrorL("No sound list: ", config_path);
return;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions Code/SM/GameSettings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "win_include.hpp"

#include <unordered_map>
#include <string>

namespace SM
{
struct GameSettings
{
std::unordered_map<std::string, int> int_settings;
std::unordered_map<std::string, float> float_settings;
std::unordered_map<std::string, std::string> string_settings;

inline static GameSettings* GetInstance()
{
return *reinterpret_cast<GameSettings**>(std::uintptr_t(GetModuleHandle(NULL)) + 0x12A7840);
}

inline float get_effect_volume()
{
auto v_iter = this->float_settings.find("EffectVolume");
if (v_iter != this->float_settings.end())
return v_iter->second;

return 1.0f;
}

inline float get_master_volume()
{
auto v_iter = this->float_settings.find("MasterVolume");
if (v_iter != this->float_settings.end())
return v_iter->second;

return 1.0f;
}

inline static float GetEffectsVolume()
{
SM::GameSettings* v_game_settings = SM::GameSettings::GetInstance();
if (v_game_settings)
return v_game_settings->get_effect_volume() * v_game_settings->get_master_volume();

return 1.0f; //Default volume
}

private:
GameSettings() = delete;
GameSettings(const GameSettings&) = delete;
GameSettings(GameSettings&&) = delete;
~GameSettings() = delete;
};
}
42 changes: 1 addition & 41 deletions Code/Utils/Console.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
#include "Console.hpp"

#if 1
namespace Engine
{
HANDLE Console::Handle = NULL;
__ConsoleOutputHandler Console::Out = {};

bool Console::CreateEngineConsole(const wchar_t* title)
{
if (Console::Handle == NULL)
{
if (AllocConsole())
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleTitleW(title);

Console::Handle = GetStdHandle(STD_OUTPUT_HANDLE);

return true;
}
}

return false;
}

bool Console::AttachToConsole()
{
if (Console::Handle == NULL)
{
Console::Handle = GetStdHandle(STD_OUTPUT_HANDLE);
return (Console::Handle != NULL);
}

return false;
}

void Console::DestroyConsole()
{
if (Console::Handle == NULL) return;

FreeConsole();
Console::Handle = NULL;
}
}
#endif
}
69 changes: 8 additions & 61 deletions Code/Utils/Console.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#if defined(_DEBUG) || !defined(_DEBUG)

#include "win_include.hpp"
#include "ConColors.hpp"

Expand Down Expand Up @@ -48,15 +46,9 @@ namespace Engine
Console::Output<const char*>("\n");
}

static bool CreateEngineConsole(const wchar_t* title);
static bool AttachToConsole();
void DestroyConsole();

static __ConsoleOutputHandler Out;

private:
static HANDLE Handle;

template<class T>
inline static void Output(const T arg)
{
Expand Down Expand Up @@ -88,13 +80,7 @@ namespace Engine
{
inline static void Output(const wchar_t* arg)
{
#if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
WriteConsoleW(Console::Handle, arg, static_cast<DWORD>(wcslen(arg)), NULL, NULL);
#endif

#if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
OutputDebugStringW(arg);
#endif
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), arg, static_cast<DWORD>(wcslen(arg)), NULL, NULL);
}
};

Expand All @@ -103,13 +89,7 @@ namespace Engine
{
inline static void Output(const char* arg)
{
#if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
WriteConsoleA(Console::Handle, arg, static_cast<DWORD>(strlen(arg)), NULL, NULL);
#endif

#if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
OutputDebugStringA(arg);
#endif
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), arg, static_cast<DWORD>(strlen(arg)), NULL, NULL);
}
};

Expand All @@ -118,13 +98,7 @@ namespace Engine
{
inline static void Output(const std::wstring& msg)
{
#if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
WriteConsoleW(Console::Handle, msg.data(), static_cast<DWORD>(msg.size()), NULL, NULL);
#endif

#if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
OutputDebugStringW(msg.data());
#endif
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg.data(), static_cast<DWORD>(msg.size()), NULL, NULL);
}
};

Expand All @@ -133,13 +107,7 @@ namespace Engine
{
inline static void Output(const std::string& msg)
{
#if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
WriteConsoleA(Console::Handle, msg.data(), static_cast<DWORD>(msg.size()), NULL, NULL);
#endif

#if defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
OutputDebugStringA(msg.data());
#endif
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), msg.data(), static_cast<DWORD>(msg.size()), NULL, NULL);
}
};

Expand All @@ -158,7 +126,7 @@ namespace Engine
QE_CREATE_CON_NUMBER_TYPE(double);

QE_CREATE_CON_OUTPUT_TYPE(void (*)(), void (*func_ptr)(), { func_ptr(); });
QE_CREATE_CON_OUTPUT_TYPE(EngineConColor, const EngineConColor& color, { SetConsoleTextAttribute(Console::Handle, static_cast<WORD>(color)); });
QE_CREATE_CON_OUTPUT_TYPE(EngineConColor, const EngineConColor& color, { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), static_cast<WORD>(color));});
QE_CREATE_CON_OUTPUT_TYPE(bool, const bool& bool_val, { ConsoleOutputType<std::string>::Output(bool_val ? "true" : "false"); });

//-------------CONSOLE OUTPUT HANDLER------------------
Expand Down Expand Up @@ -197,29 +165,8 @@ namespace Engine
};
}

#if defined(ENGINE_ENABLE_CUSTOM_CONSOLE_WINDOW) || !defined(ENGINE_ENABLE_VS_CONSOLE_OUTPUT)
#define CreateDebugConsole(title) Engine::Console::CreateEngineConsole(title)
#define AttachDebugConsole() Engine::Console::AttachToConsole()
#else
#define CreateDebugConsole(...) ((void*)0)
#define AttachDebugConsole(...) ((void*)0)
#endif

#define DebugOut(...) Engine::Console::Out(__VA_ARGS__)
#define DebugOutL(...) Engine::Console::Out(__VA_ARGS__, Engine::Console::Endl)

#define DebugWarningL(...) Engine::Console::Out(0b1101_fg, "WARNING: ", __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl)
#define DebugErrorL(...) Engine::Console::Out(0b1001_fg, "ERROR: " , __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl)
#else
#define CreateDebugConsole(title) ((void*)0)
#define AttachDebugConsole(...) ((void*)0)

#define DebugOut(...) ((void*)0)
#define DebugOutL(...) ((void*)0)

#define DebugWarningL(...) ((void*)0)
#define DebugErrorL(...) ((void*)0)
#define DebugOutL(...) Engine::Console::Out("[DLM] ", __VA_ARGS__, Engine::Console::Endl)

#define QE_CREATE_CON_OUTPUT_TYPE(...) ((void*)0)
#define QE_CREATE_CON_NUMBER_TYPE(...) ((void*)0)
#endif //EngineNamespace
#define DebugWarningL(...) Engine::Console::Out(0b1101_fg, "[DLM] WARNING: ", __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl)
#define DebugErrorL(...) Engine::Console::Out(0b1001_fg, "[DLM] ERROR: " , __FUNCTION__, "(", __LINE__, ") -> ", __VA_ARGS__, Engine::Console::Endl)
Loading

0 comments on commit 46e4d1c

Please sign in to comment.