From 41c0b5a9aad05ec9524f48b3d0260740eb18e9a1 Mon Sep 17 00:00:00 2001 From: Enebe Date: Wed, 28 Jan 2026 11:51:02 -0300 Subject: [PATCH 1/3] Reversed SFX and BGM managers --- src/HandleManager.hpp | 2 +- src/SokuAddresses.hpp | 2 ++ src/SoundData.hpp | 8 +++++++ src/SoundManager.cpp | 9 +++++++ src/SoundManager.hpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/HandleManager.hpp b/src/HandleManager.hpp index f931cfb..b1d78e0 100644 --- a/src/HandleManager.hpp +++ b/src/HandleManager.hpp @@ -83,7 +83,6 @@ namespace SokuLib template class HandleManagerEx { - static_assert(std::has_virtual_destructor::value); public: Vector vector; Vector usedIndexes; @@ -122,6 +121,7 @@ namespace SokuLib if (usedIndexes.at(index) == (id >> 16)) { usedIndexes[index] = 0; unusedIndexes.push_back(index); + static_assert(std::has_virtual_destructor::value); vector.at(index)->~T(); } mutex.unlock(); diff --git a/src/SokuAddresses.hpp b/src/SokuAddresses.hpp index 57cfe2f..ca94819 100644 --- a/src/SokuAddresses.hpp +++ b/src/SokuAddresses.hpp @@ -174,6 +174,8 @@ namespace SokuLib ADDR_RCHARID = 0x00899D30, ADDR_INPUT_MANAGER_CLUSTER = 0x0089A248, ADDR_MENU_LIST = 0x0089A884, + ADDR_SFX_MANAGER = 0x0089F9F8, + ADDR_BGM_MANAGER = 0x0089FE50, ADDR_TEXTURE_MANAGER = 0x0089FF08, ADDR_WINDOW_HWND = 0x0089FF90, ADDR_LOAD_GRAPHICS_THREAD = 0x0089FFF4, diff --git a/src/SoundData.hpp b/src/SoundData.hpp index 7238b9b..a17ba14 100644 --- a/src/SoundData.hpp +++ b/src/SoundData.hpp @@ -54,6 +54,14 @@ namespace SokuLib int bufferSize; }; + class DS3DBuffer { + public: + void** vtable; + void* dsHandle; // IDirectSoundBuffer* + int bufferSize; + void* ds3dHandle; // IDirectSound3DBuffer* + }; + class BgmBuffer { public: String filename; diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 09eae1a..84806cf 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -4,9 +4,18 @@ #include "SokuAddresses.hpp" #include "SoundManager.hpp" +#include "UnionCast.hpp" namespace SokuLib { void (* const playSEWaveBuffer)(int id) = reinterpret_cast(ADDR_PLAY_SE_WAVE_BUFFER); void (* const playNetBell)(int id) = reinterpret_cast(ADDR_PLAY_NET_BELL); + + SFXManager*& SFXManager::instance = *reinterpret_cast(ADDR_SFX_MANAGER); + BGMManager*& BGMManager::instance = *reinterpret_cast(ADDR_BGM_MANAGER); + + unsigned int SFXManager::load(unsigned int& id, const char* soundPath) { return (this->*union_cast(0x401AF0))(id, soundPath); } + bool SFXManager::unload(unsigned int id) { return (this->*union_cast(0x401BD0))(id); } + void SFXManager::play(unsigned int id) { return (this->*union_cast(0x401D50))(id); } + } \ No newline at end of file diff --git a/src/SoundManager.hpp b/src/SoundManager.hpp index 1c57c91..c0cb678 100644 --- a/src/SoundManager.hpp +++ b/src/SoundManager.hpp @@ -5,6 +5,8 @@ #ifndef SOKULIB_SOUNDMANAGER_HPP #define SOKULIB_SOUNDMANAGER_HPP +#include "HandleManager.hpp" +#include "SoundData.hpp" namespace SokuLib { @@ -78,6 +80,60 @@ namespace SokuLib { ((void (*)(const char *))0x43ff10)(path); } + + // size = 0x458 + struct SFXManager { + HandleManager handlesA; + HandleManager handleB; + DSBuffer buffers[32]; + DS3DBuffer buffers3D[32]; + char unknown448[8]; + float volume, volumeCoef; + + static SFXManager*& instance; // 0x89F9F8 + + // void initialize(); // 0x401990 + // void unloadBuffers(); // 0x401A80 + unsigned int load(unsigned int& id, const char* soundPath); // 0x401AF0 + bool unload(unsigned int id); // 0x401BD0 + // void setVolume(float value); // 0x401C20 + // void setItemVolume(unsigned int id, float value); // 0x401CE0 + void play(unsigned int id); // 0x401D50 + }; + + // size = 0xB8 + struct BGMManager { + HANDLE threadAHandle; + LPDWORD threadAID; + HANDLE threadBHandle; + LPDWORD threadBID; + HANDLE eventHandle10; + HANDLE eventHandle14; + bool isActive; + // offset 3 + + CriticalSection criticalSection; + HandleManagerEx handles; + List unknownList88; // playing? data type is probably the handleID + List unknownList94; // queued? + List unknownListA0; // stopped? + float volume, volumeCoef; + char unknownB4[0x04]; + + static BGMManager*& instance; // 0x89FE50 + + // 0x899d5c == current_bgm_id + + // void initialize(); // 0x403740 + // void unloadBuffers(); // 0x4037d0 + // unsigned int* create(unsigned int& id); // 0x4039A0 + // bool load(unsigned int id, const char* soundPath, bool useListB, float unknown); // 0x4039C0 + // void somethingA(unsigned int id, int time); // 0x403B20 + // void somethingB(unsigned int id); // 0x403BB0 + // void somethingC(unsigned int id, int time, int startTime, bool alwaysTrue); // 0x403C20 (playFadeIn?) + // void somethingD(unsigned int id, int time, int startTime, float unknown); // 0x403C70 (stopFadeOut?) + // void setItemVolume(unsigned int id, float); // 0x403D10 + }; } From dee40b779b425efd1324e7026497c9d9770ecb55 Mon Sep 17 00:00:00 2001 From: Neto Becker Date: Thu, 29 Jan 2026 20:28:53 +0000 Subject: [PATCH 2/3] HandleManagerEx review --- src/HandleManager.hpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/HandleManager.hpp b/src/HandleManager.hpp index b1d78e0..49f722e 100644 --- a/src/HandleManager.hpp +++ b/src/HandleManager.hpp @@ -90,7 +90,16 @@ namespace SokuLib unsigned int nextBase = 0; CriticalSection mutex; - virtual ~HandleManagerEx() { for(auto t : vector) delete t; } + virtual ~HandleManagerEx() { + // when the destructor is virtual + // the memory allocation is always removed + // from the heap the class was defined + // for others we can choose the heap + for(auto t : vector) { + if constexpr (std::has_virtual_destructor::value) delete t; + else SokuLib::Delete(t); + } + } template inline T* Allocate(unsigned int& retId, Args... args) { @@ -108,7 +117,9 @@ namespace SokuLib index = vector.size(); if(++nextBase > 0xffff) nextBase = 1; retId = (index & 0xffff) | (nextBase << 16); - vector.push_back(ret = new T(args...)); + if constexpr(std::has_virtual_destructor::value) ret = new T(args...); + else ret = SokuLib::New(sizeof(T), args...); + vector.push_back(ret); usedIndexes.push_back(retId >> 16); } mutex.unlock(); @@ -121,7 +132,6 @@ namespace SokuLib if (usedIndexes.at(index) == (id >> 16)) { usedIndexes[index] = 0; unusedIndexes.push_back(index); - static_assert(std::has_virtual_destructor::value); vector.at(index)->~T(); } mutex.unlock(); From bd93b56af7be22dbdc93678ccfaaf7384ab4b320 Mon Sep 17 00:00:00 2001 From: Enebe Date: Sat, 31 Jan 2026 18:21:24 -0300 Subject: [PATCH 3/3] Mistake, static memory --- src/SoundManager.cpp | 4 ++-- src/SoundManager.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 84806cf..359992b 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -11,8 +11,8 @@ namespace SokuLib void (* const playSEWaveBuffer)(int id) = reinterpret_cast(ADDR_PLAY_SE_WAVE_BUFFER); void (* const playNetBell)(int id) = reinterpret_cast(ADDR_PLAY_NET_BELL); - SFXManager*& SFXManager::instance = *reinterpret_cast(ADDR_SFX_MANAGER); - BGMManager*& BGMManager::instance = *reinterpret_cast(ADDR_BGM_MANAGER); + SFXManager& SFXManager::instance = *reinterpret_cast(ADDR_SFX_MANAGER); + BGMManager& BGMManager::instance = *reinterpret_cast(ADDR_BGM_MANAGER); unsigned int SFXManager::load(unsigned int& id, const char* soundPath) { return (this->*union_cast(0x401AF0))(id, soundPath); } bool SFXManager::unload(unsigned int id) { return (this->*union_cast(0x401BD0))(id); } diff --git a/src/SoundManager.hpp b/src/SoundManager.hpp index c0cb678..c6a4b4a 100644 --- a/src/SoundManager.hpp +++ b/src/SoundManager.hpp @@ -90,7 +90,7 @@ namespace SokuLib char unknown448[8]; float volume, volumeCoef; - static SFXManager*& instance; // 0x89F9F8 + static SFXManager& instance; // 0x89F9F8 // void initialize(); // 0x401990 // void unloadBuffers(); // 0x401A80 @@ -120,7 +120,7 @@ namespace SokuLib float volume, volumeCoef; char unknownB4[0x04]; - static BGMManager*& instance; // 0x89FE50 + static BGMManager& instance; // 0x89FE50 // 0x899d5c == current_bgm_id