diff --git a/artifacts/scripting/sfall opcode list.txt b/artifacts/scripting/sfall opcode list.txt index 6b34f703..0b24ce04 100644 --- a/artifacts/scripting/sfall opcode list.txt +++ b/artifacts/scripting/sfall opcode list.txt @@ -296,8 +296,8 @@ 0x8228 - int get_attack_type -0x822b - int play_sfall_sound(string file, int loop) -0x822c - void stop_sfall_sound(int ptr) +0x822b - int play_sfall_sound(string file, bool loop) +0x822c - void stop_sfall_sound(int id) 0x8235 - array string_split(string string, string split) 0x8237 - int atoi(string string) diff --git a/sfall/Modules/Graphics.cpp b/sfall/Modules/Graphics.cpp index db8830c3..903e9b8f 100644 --- a/sfall/Modules/Graphics.cpp +++ b/sfall/Modules/Graphics.cpp @@ -40,13 +40,12 @@ namespace sfall { -#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } } +#define UNUSEDFUNCTION { DEBUGMESS("\n[SFALL] Unused function called: %s", __FUNCTION__); return DDERR_GENERIC; } +#define SAFERELEASE(a) { if (a) { a->Release(); a = nullptr; } } typedef HRESULT (_stdcall *DDrawCreateProc)(void*, IDirectDraw**, void*); typedef IDirect3D9* (_stdcall *D3DCreateProc)(UINT version); -#define UNUSEDFUNCTION { DEBUGMESS("\n[SFALL] Unused function called: %s", __FUNCTION__); return DDERR_GENERIC; } - static IDirectDrawSurface* primaryDDSurface = nullptr; static DWORD ResWidth; diff --git a/sfall/Modules/Movies.cpp b/sfall/Modules/Movies.cpp index d5685820..09905268 100644 --- a/sfall/Modules/Movies.cpp +++ b/sfall/Modules/Movies.cpp @@ -31,14 +31,15 @@ namespace sfall { -#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } } - static DWORD MoviePtrs[MaxMovies]; char MoviePaths[MaxMovies * 65]; static bool aviIsReadyToPlay = false; class CAllocator : public IVMRSurfaceAllocator9, IVMRImagePresenter9 { + +#define SAFERELEASE(a) { if (a) { a->Release(); a = nullptr; } } + private: ULONG RefCount; IVMRSurfaceAllocatorNotify9 *pAllocNotify; diff --git a/sfall/Modules/ScriptShaders.cpp b/sfall/Modules/ScriptShaders.cpp index 72757122..05000a7a 100644 --- a/sfall/Modules/ScriptShaders.cpp +++ b/sfall/Modules/ScriptShaders.cpp @@ -27,7 +27,7 @@ namespace sfall { -#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } } +#define SAFERELEASE(a) { if (a) { a->Release(); a = nullptr; } } static size_t shadersSize; diff --git a/sfall/Modules/Scripting/Handlers/Misc.cpp b/sfall/Modules/Scripting/Handlers/Misc.cpp index 184eaf8c..77d6c234 100644 --- a/sfall/Modules/Scripting/Handlers/Misc.cpp +++ b/sfall/Modules/Scripting/Handlers/Misc.cpp @@ -616,7 +616,7 @@ void sf_get_attack_type(OpcodeContext& ctx) { } void sf_play_sfall_sound(OpcodeContext& ctx) { - long soundID = (long)Sound::PlaySfallSound(ctx.arg(0).strValue(), ctx.arg(1).asBool()); + DWORD soundID = Sound::PlaySfallSound(ctx.arg(0).strValue(), ctx.arg(1).asBool()); ctx.setReturn(soundID); } diff --git a/sfall/Modules/Sound.cpp b/sfall/Modules/Sound.cpp index 3b0964f0..7cb713b0 100644 --- a/sfall/Modules/Sound.cpp +++ b/sfall/Modules/Sound.cpp @@ -28,10 +28,10 @@ namespace sfall { -#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } } +#define SAFERELEASE(a) { if (a) { a->Release(); } } struct sDSSound { - DWORD id; + DWORD id; // should be first IGraphBuilder *pGraph; IMediaControl *pControl; IMediaSeeking *pSeek; @@ -46,7 +46,7 @@ DWORD playID = 0; DWORD loopID = 0; static HWND soundwindow = 0; -static void* musicLoopPtr = nullptr; +static sDSSound* musicLoopPtr = nullptr; //static char playingMusicFile[256]; static void FreeSound(sDSSound* sound) { @@ -60,46 +60,48 @@ static void FreeSound(sDSSound* sound) { } static void WipeSounds() { - for (DWORD i = 0; i < playingSounds.size(); i++) FreeSound(playingSounds[i]); - for (DWORD i = 0; i < loopingSounds.size(); i++) FreeSound(loopingSounds[i]); + for (size_t i = 0; i < playingSounds.size(); i++) FreeSound(playingSounds[i]); + for (size_t i = 0; i < loopingSounds.size(); i++) FreeSound(loopingSounds[i]); playingSounds.clear(); loopingSounds.clear(); musicLoopPtr = nullptr; + playID = 0; + loopID = 0; } LRESULT CALLBACK SoundWndProc(HWND wnd, UINT msg, WPARAM w, LPARAM l) { if (msg == WM_APP) { DWORD id = l; - sDSSound* dssound = nullptr; + sDSSound* sound = nullptr; if (id & 0x80000000) { - for (DWORD i = 0; i < loopingSounds.size(); i++) { + for (size_t i = 0; i < loopingSounds.size(); i++) { if (loopingSounds[i]->id == id) { - dssound = loopingSounds[i]; + sound = loopingSounds[i]; break; } } } else { - for (DWORD i = 0; i < playingSounds.size(); i++) { + for (size_t i = 0; i < playingSounds.size(); i++) { if (playingSounds[i]->id == id) { - dssound = playingSounds[i]; + sound = playingSounds[i]; break; } } } - if (!dssound) return 0; + if (!sound) return 0; LONG e = 0; LONG_PTR p1 = 0, p2 = 0; - while (!FAILED(dssound->pEvent->GetEvent(&e, &p1, &p2, 0))) { - dssound->pEvent->FreeEventParams(e, p1, p2); + while (!FAILED(sound->pEvent->GetEvent(&e, &p1, &p2, 0))) { + sound->pEvent->FreeEventParams(e, p1, p2); if (e == EC_COMPLETE) { if (id & 0x80000000) { LONGLONG pos = 0; - dssound->pSeek->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 0, AM_SEEKING_NoPositioning); - dssound->pControl->Run(); + sound->pSeek->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 0, AM_SEEKING_NoPositioning); + sound->pControl->Run(); } else { - for (DWORD i = 0; i < playingSounds.size(); i++) { - if (playingSounds[i] == dssound) { - FreeSound(dssound); + for (size_t i = 0; i < playingSounds.size(); i++) { + if (playingSounds[i] == sound) { + FreeSound(sound); playingSounds.erase(playingSounds.begin() + i); return 0; } @@ -157,7 +159,7 @@ static void __cdecl SfallSoundVolume(sDSSound* sound, int type, long passVolume) loopVolume = sfxVolume = -9999; // mute } else if (type = 0) { // for music if (musicLoopPtr) { - Sound::StopSfallSound(musicLoopPtr); + Sound::StopSfallSound(musicLoopPtr->id); musicLoopPtr = nullptr; } return; @@ -191,42 +193,42 @@ static bool IsMute(bool type) { static sDSSound* PlayingSound(wchar_t* path, bool loop) { if (!soundwindow) CreateSndWnd(); + if (IsMute(loop)) return nullptr; - sDSSound* result = new sDSSound(); + sDSSound* sound = new sDSSound(); - DWORD id = (loop) ? loopID++ : playID++; + DWORD id = (loop) ? ++loopID : ++playID; if (loop) id |= 0x80000000; - result->id = id; + sound->id = id; - HRESULT hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&result->pGraph); + HRESULT hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&sound->pGraph); if (hr != S_OK) { dlog_f("Error CoCreateInstance: %d", DL_INIT, hr); return nullptr; } - result->pGraph->QueryInterface(IID_IMediaControl, (void**)&result->pControl); + sound->pGraph->QueryInterface(IID_IMediaControl, (void**)&sound->pControl); if (loop) - result->pGraph->QueryInterface(IID_IMediaSeeking, (void**)&result->pSeek); + sound->pGraph->QueryInterface(IID_IMediaSeeking, (void**)&sound->pSeek); else - result->pSeek = nullptr; + sound->pSeek = nullptr; - result->pGraph->QueryInterface(IID_IMediaEventEx, (void**)&result->pEvent); - result->pEvent->SetNotifyWindow((OAHWND)soundwindow, WM_APP, id); + sound->pGraph->QueryInterface(IID_IMediaEventEx, (void**)&sound->pEvent); + sound->pEvent->SetNotifyWindow((OAHWND)soundwindow, WM_APP, id); + sound->pGraph->QueryInterface(IID_IBasicAudio, (void**)&sound->pAudio); - result->pGraph->QueryInterface(IID_IBasicAudio, (void**)&result->pAudio); - - result->pControl->RenderFile(path); - result->pControl->Run(); + sound->pControl->RenderFile(path); + sound->pControl->Run(); if (loop) { - loopingSounds.push_back(result); - SfallSoundVolume(result, 0, *(DWORD*)FO_VAR_background_volume); // music + loopingSounds.push_back(sound); + SfallSoundVolume(sound, 0, *(DWORD*)FO_VAR_background_volume); // music } else { - playingSounds.push_back(result); - SfallSoundVolume(result, 1, *(DWORD*)FO_VAR_sndfx_volume); + playingSounds.push_back(sound); + SfallSoundVolume(sound, 1, *(DWORD*)FO_VAR_sndfx_volume); } - return result; + return sound; } static const wchar_t *SoundExtensions[] = { L"mp3", L"wma", L"wav" }; @@ -249,7 +251,7 @@ static bool __cdecl SoundFileLoad(DWORD called, const char* path) { bool music = (called == 0x45092B); // from gsound_background_play_ if (music && musicLoopPtr != nullptr) { //if (found && strcmp(path, playingMusicFile) == 0) return true; // don't stop music - Sound::StopSfallSound(musicLoopPtr); + Sound::StopSfallSound(musicLoopPtr->id); musicLoopPtr = nullptr; } if (!isExist) return false; @@ -275,18 +277,20 @@ static void __fastcall MakeMusicPath(const char* file) { SoundFileLoad(0x45092B, pathBuf); } -void* Sound::PlaySfallSound(const char* path, bool loop) { +DWORD Sound::PlaySfallSound(const char* path, bool loop) { wchar_t buf[256]; mbstowcs_s(0, buf, path, 256); - sDSSound* result = PlayingSound(buf, loop); - return (loop) ? result : 0; + sDSSound* sound = PlayingSound(buf, loop); + return (loop && sound) ? sound->id : 0; } -void __stdcall Sound::StopSfallSound(void* _ptr) { - sDSSound* ptr = (sDSSound*)_ptr; - for (DWORD i = 0; i < loopingSounds.size(); i++) { - if (loopingSounds[i] == ptr) { - FreeSound(ptr); +void __stdcall Sound::StopSfallSound(DWORD id) { + if (!id) return; + for (size_t i = 0; i < loopingSounds.size(); i++) { + if (loopingSounds[i]->id == id) { + sDSSound* sound = loopingSounds[i]; + sound->pControl->Stop(); + FreeSound(sound); loopingSounds.erase(loopingSounds.begin() + i); return; } @@ -332,6 +336,7 @@ static void __declspec(naked) gmovie_play_hook_stop() { mov eax, musicLoopPtr; test eax, eax; jz skip; + mov eax, [eax]; // musicLoopPtr->id push ecx; push edx; push eax; diff --git a/sfall/Modules/Sound.h b/sfall/Modules/Sound.h index 0543c52e..a2d71d30 100644 --- a/sfall/Modules/Sound.h +++ b/sfall/Modules/Sound.h @@ -29,8 +29,8 @@ public: void init(); void exit() override; - static void* PlaySfallSound(const char* path, bool loop); - static void __stdcall StopSfallSound(void* ptr); + static DWORD PlaySfallSound(const char* path, bool loop); + static void __stdcall StopSfallSound(DWORD id); }; }