From 91e29bbf5285fc6a0fd4a7e6cac8df93eca20833 Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 25 Feb 2024 17:15:29 +0400 Subject: [PATCH] feat(sound): add SI2 Log methods --- src/sound/CMakeLists.txt | 2 ++ src/sound/SI2.cpp | 13 +++++--- src/sound/SI2.hpp | 13 ++++++-- src/sound/SI2Log.cpp | 71 ++++++++++++++++++++++++++++++++++++++++ src/sound/SI2Script.cpp | 6 ++-- src/sound/SI2Script.hpp | 6 ---- 6 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 src/sound/SI2Log.cpp delete mode 100644 src/sound/SI2Script.hpp diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index 6ac8bbd..44b9ad7 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -13,4 +13,6 @@ target_link_libraries(sound PRIVATE ui util + PUBLIC + fmod ) diff --git a/src/sound/SI2.cpp b/src/sound/SI2.cpp index 7622510..a9a74d6 100644 --- a/src/sound/SI2.cpp +++ b/src/sound/SI2.cpp @@ -2,10 +2,13 @@ #include "ui/FrameScript.hpp" void SI2::RegisterScriptFunctions() { - for (int32_t i = 0; i < NUM_SCRIPT_FUNCTIONS_SI2; ++i) { - FrameScript_RegisterFunction( - SI2::s_ScriptFunctions[i].name, - SI2::s_ScriptFunctions[i].method - ); + FrameScript_Method* item = s_ScriptFunctions; + while (item->name) { + FrameScript_RegisterFunction(item->name, item->method); + item++; } } + +int32_t SI2::Init(int32_t flag) { + return 0; +} diff --git a/src/sound/SI2.hpp b/src/sound/SI2.hpp index ec41703..bbc29aa 100644 --- a/src/sound/SI2.hpp +++ b/src/sound/SI2.hpp @@ -1,17 +1,26 @@ #ifndef SOUND_SI2_HPP #define SOUND_SI2_HPP -#include "sound/SI2Script.hpp" #include "ui/Types.hpp" +#include #include +#include +#include +#include class SI2 { public: // Static variables - static FrameScript_Method s_ScriptFunctions[NUM_SCRIPT_FUNCTIONS_SI2]; + static FrameScript_Method s_ScriptFunctions[]; + static uint32_t sm_logFlags; + static HSLOG sm_log; // Static functions static void RegisterScriptFunctions(); + static int32_t Log_Init(); + static void Log_Write(const char* format, ...); + static void Log_Write(uint32_t line, const char* filename, FMOD_RESULT errcode, const char* format, ...); + static int32_t Init(int32_t flag); }; #endif diff --git a/src/sound/SI2Log.cpp b/src/sound/SI2Log.cpp new file mode 100644 index 0000000..e73e07d --- /dev/null +++ b/src/sound/SI2Log.cpp @@ -0,0 +1,71 @@ +#include "sound/SI2.hpp" +#include +#include +#include + +uint32_t SI2::sm_logFlags = SLOG_FLAG_DEFAULT; +HSLOG SI2::sm_log = nullptr; + +int32_t SI2::Log_Init() { + OsCreateDirectory("Logs", 0); + SLogCreate("Logs\\Sound.log", sm_logFlags, &sm_log); + sm_logFlags |= SLOG_FLAG_APPEND; + // return OsDeleteFile((Blizzard::File*)"Logs\\SESound.log"); + return 0; +} + +void SI2::Log_Write(const char* format, ...) { + STORM_ASSERT(format); + + char output[512] = { 0 }; + if (format[0]) { + va_list va; + va_start(va, format); + vsnprintf(output, sizeof(output), format, va); + va_end(va); + } + + Log_Write(__LINE__, __FILE__, FMOD_OK, output); +} + +void SI2::Log_Write(uint32_t line, const char* filename, FMOD_RESULT errcode, const char* format, ...) { + static uint32_t s_nNumErrors = 0; + + if (s_nNumErrors > 200) { + return; + } + + if (s_nNumErrors == 200) { + SLogWrite(sm_log, " -######## TOO MANY ERRORS. NO FURTHER ERRORS WILL BE LOGGED."); + SLogFlush(sm_log); + s_nNumErrors++; + return; + } + + STORM_ASSERT(format); + + char output[512] = { 0 }; + if (format[0]) { + va_list va; + va_start(va, format); + vsnprintf(output, sizeof(output), format, va); + va_end(va); + } + + if (errcode == FMOD_OK) { + SLogWrite(sm_log, output); + SLogFlush(sm_log); + } + + if (errcode == FMOD_ERR_HTTP_PROXY_AUTH) { + return; + } + + SLogWrite(sm_log, " -######## FMOD ERROR! (err %d) %s", errcode, FMOD_ErrorString(errcode)); + if (format[0]) { + SLogWrite(sm_log, output); + } + SLogWrite(sm_log, "%s(%d)", filename, line); + SLogFlush(sm_log); + s_nNumErrors++; +} diff --git a/src/sound/SI2Script.cpp b/src/sound/SI2Script.cpp index 3d4ed6e..138b994 100644 --- a/src/sound/SI2Script.cpp +++ b/src/sound/SI2Script.cpp @@ -1,4 +1,3 @@ -#include "sound/SI2Script.hpp" #include "sound/SI2.hpp" #include "ui/Types.hpp" #include "util/Lua.hpp" @@ -100,7 +99,7 @@ int32_t Script_VoiceChat_ActivatePrimaryCaptureCallback(lua_State* L) { WHOA_UNIMPLEMENTED(0); } -FrameScript_Method SI2::s_ScriptFunctions[NUM_SCRIPT_FUNCTIONS_SI2] = { +FrameScript_Method SI2::s_ScriptFunctions[] = { { "PlaySound", &Script_PlaySound }, { "PlayMusic", &Script_PlayMusic }, { "PlaySoundFile", &Script_PlaySoundFile }, @@ -123,5 +122,6 @@ FrameScript_Method SI2::s_ScriptFunctions[NUM_SCRIPT_FUNCTIONS_SI2] = { { "VoiceChat_IsRecordingLoopbackSound", &Script_VoiceChat_IsRecordingLoopbackSound }, { "VoiceChat_IsPlayingLoopbackSound", &Script_VoiceChat_IsPlayingLoopbackSound }, { "VoiceChat_GetCurrentMicrophoneSignalLevel", &Script_VoiceChat_GetCurrentMicrophoneSignalLevel }, - { "VoiceChat_ActivatePrimaryCaptureCallback", &Script_VoiceChat_ActivatePrimaryCaptureCallback } + { "VoiceChat_ActivatePrimaryCaptureCallback", &Script_VoiceChat_ActivatePrimaryCaptureCallback }, + { nullptr, nullptr } }; diff --git a/src/sound/SI2Script.hpp b/src/sound/SI2Script.hpp deleted file mode 100644 index 9c3b882..0000000 --- a/src/sound/SI2Script.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SOUND_SI2_SCRIPT_HPP -#define SOUND_SI2_SCRIPT_HPP - -#define NUM_SCRIPT_FUNCTIONS_SI2 23 - -#endif