From 6568fe9a845407b5d55fd1fde2fd3c19153469b1 Mon Sep 17 00:00:00 2001 From: "Mr. Big Britches" <111622360+MrBigBritches@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:39:40 -0700 Subject: [PATCH] Implement Missing NGS2 System Functions Lock & Unlock (#1542) * Implement NGS2 SystemLock * Implement NGS2 SystemUnlock * Add guard for system_handle value of 0 * Return correct error code from system_handle guard * Validate NGS2 system handles before locking --------- Co-authored-by: nmzik --- src/libs/libAudio.cpp | 2 ++ src/libs/ngs2.cpp | 48 +++++++++++++++++++++++++++++++++++-------- src/libs/ngs2.h | 2 ++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/libs/libAudio.cpp b/src/libs/libAudio.cpp index 704d2b29e..2069a82b6 100644 --- a/src/libs/libAudio.cpp +++ b/src/libs/libAudio.cpp @@ -1147,6 +1147,8 @@ LIB_DEFINE(InitAudio_1_Ngs2) { LIB_FUNC("mPYgU4oYpuY", Ngs2::Ngs2SystemCreateWithAllocator); LIB_FUNC("u-WrYDaJA3k", Ngs2::Ngs2SystemDestroy); LIB_FUNC("vU7TQ62pItw", Ngs2::Ngs2SystemGetInfo); + LIB_FUNC("gThZqM5PYlQ", Ngs2::Ngs2SystemLock); + LIB_FUNC("JXRC5n0RQls", Ngs2::Ngs2SystemUnlock); LIB_FUNC("U546k6orxQo", Ngs2::Ngs2RackCreateWithAllocator); LIB_FUNC("cLV4aiT9JpA", Ngs2::Ngs2RackCreate); LIB_FUNC("0eFLVCfWVds", Ngs2::Ngs2RackQueryBufferSize); diff --git a/src/libs/ngs2.cpp b/src/libs/ngs2.cpp index fbb4ed9f9..3c89831ff 100644 --- a/src/libs/ngs2.cpp +++ b/src/libs/ngs2.cpp @@ -28,6 +28,8 @@ namespace Ngs2 { LIB_NAME("Ngs2", "Ngs2"); +constexpr int32_t NGS2_ERROR_INVALID_SYSTEM_HANDLE = + static_cast(0x804a8201u); constexpr int32_t NGS2_ERROR_INVALID_OUT_ADDRESS = static_cast(0x804a8010u); constexpr int32_t NGS2_ERROR_INVALID_NUM_WAVEFORM_BLOCKS = @@ -889,6 +891,15 @@ static Ngs2Internal* Ngs2CreateSystemInternal(const Ngs2SystemOption* optio return ngs; } +static Ngs2Internal* Ngs2FindSystem(uintptr_t system_handle) { + for (auto* ngs = g_ngs_list; ngs != nullptr; ngs = ngs->next) { + if (reinterpret_cast(ngs) == system_handle) { + return ngs; + } + } + return nullptr; +} + static bool Ngs2RackIsCustom(Ngs2RackType type) { switch (type) { case Ngs2RackType::CustomSubmixer: @@ -1110,7 +1121,6 @@ int KYTY_SYSV_ABI Ngs2SystemGetInfo(uintptr_t system_handle, Ngs2SystemInfo* inf size_t info_size) { constexpr int32_t ERROR_INVALID_OUT_ADDRESS = static_cast(0x804a8010u); constexpr int32_t ERROR_INVALID_OUT_SIZE = static_cast(0x804a8011u); - constexpr int32_t ERROR_INVALID_SYSTEM_HANDLE = static_cast(0x804a8201u); if (info == nullptr) { return ERROR_INVALID_OUT_ADDRESS; @@ -1119,13 +1129,9 @@ int KYTY_SYSV_ABI Ngs2SystemGetInfo(uintptr_t system_handle, Ngs2SystemInfo* inf return ERROR_INVALID_OUT_SIZE; } - auto* ngs = reinterpret_cast(system_handle); - auto* current = g_ngs_list; - while (current != nullptr && current != ngs) { - current = current->next; - } - if (current == nullptr) { - return ERROR_INVALID_SYSTEM_HANDLE; + auto* ngs = Ngs2FindSystem(system_handle); + if (ngs == nullptr) { + return NGS2_ERROR_INVALID_SYSTEM_HANDLE; } Common::LockGuard lock(ngs->mutex); @@ -2625,6 +2631,32 @@ int KYTY_SYSV_ABI Ngs2VoiceGetStateFlags(uintptr_t voice_handle, uint32_t* state return OK; } +int KYTY_SYSV_ABI Ngs2SystemLock(uintptr_t system_handle) { + PRINT_NAME(); + + auto* ngs = Ngs2FindSystem(system_handle); + if (ngs == nullptr) { + return NGS2_ERROR_INVALID_SYSTEM_HANDLE; + } + + ngs->mutex.Lock(); + + return OK; +} + +int KYTY_SYSV_ABI Ngs2SystemUnlock(uintptr_t system_handle) { + PRINT_NAME(); + + auto* ngs = Ngs2FindSystem(system_handle); + if (ngs == nullptr) { + return NGS2_ERROR_INVALID_SYSTEM_HANDLE; + } + + ngs->mutex.Unlock(); + + return OK; +} + } // namespace Ngs2 } // namespace Libs::Audio diff --git a/src/libs/ngs2.h b/src/libs/ngs2.h index fe814ed2f..bb4cf236c 100644 --- a/src/libs/ngs2.h +++ b/src/libs/ngs2.h @@ -33,6 +33,8 @@ int KYTY_SYSV_ABI Ngs2SystemCreate(const Ngs2SystemOption* option, const Ngs2ContextBufferInfo* buffer_info, uintptr_t* handle); int KYTY_SYSV_ABI Ngs2SystemGetInfo(uintptr_t system_handle, Ngs2SystemInfo* info, size_t info_size); +int KYTY_SYSV_ABI Ngs2SystemLock(uintptr_t system_handle); +int KYTY_SYSV_ABI Ngs2SystemUnlock(uintptr_t system_handle); int KYTY_SYSV_ABI Ngs2SystemSetGrainSamples(uintptr_t system_handle, uint32_t num_samples); int KYTY_SYSV_ABI Ngs2RackQueryBufferSize(uint32_t rack_id, const Ngs2RackOption* option, Ngs2ContextBufferInfo* buffer_info);