Fixed the return value of play_sfall_sound function

* before it returned a raw pointer instead of the ID number of the
played sound.
This commit is contained in:
NovaRain
2020-03-12 11:58:48 +08:00
parent 0fd45aa37a
commit 0c0d1dd73e
6 changed files with 61 additions and 56 deletions
+2 -2
View File
@@ -296,8 +296,8 @@
0x8228 - int get_attack_type 0x8228 - int get_attack_type
0x822b - int play_sfall_sound(string file, int loop) 0x822b - int play_sfall_sound(string file, bool loop)
0x822c - void stop_sfall_sound(int ptr) 0x822c - void stop_sfall_sound(int id)
0x8235 - array string_split(string string, string split) 0x8235 - array string_split(string string, string split)
0x8237 - int atoi(string string) 0x8237 - int atoi(string string)
+2 -3
View File
@@ -37,13 +37,12 @@
#include "TalkingHeads.h" #include "TalkingHeads.h"
#include "Version.h" #include "Version.h"
#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 HRESULT (_stdcall *DDrawCreateProc)(void*, IDirectDraw**, void*);
typedef IDirect3D9* (_stdcall *D3DCreateProc)(UINT version); typedef IDirect3D9* (_stdcall *D3DCreateProc)(UINT version);
#define UNUSEDFUNCTION { DEBUGMESS("\n[SFALL] Unused function called: %s", __FUNCTION__); return DDERR_GENERIC; }
static IDirectDrawSurface* primaryDDSurface = nullptr; static IDirectDrawSurface* primaryDDSurface = nullptr;
static DWORD ResWidth; static DWORD ResWidth;
+3 -2
View File
@@ -26,14 +26,15 @@
#include "Movies.h" #include "Movies.h"
#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } }
static DWORD MoviePtrs[MaxMovies]; static DWORD MoviePtrs[MaxMovies];
char MoviePaths[MaxMovies * 65]; char MoviePaths[MaxMovies * 65];
static bool aviIsReadyToPlay = false; static bool aviIsReadyToPlay = false;
class CAllocator : public IVMRSurfaceAllocator9, IVMRImagePresenter9 { class CAllocator : public IVMRSurfaceAllocator9, IVMRImagePresenter9 {
#define SAFERELEASE(a) { if (a) { a->Release(); a = nullptr; } }
private: private:
ULONG RefCount; ULONG RefCount;
IVMRSurfaceAllocatorNotify9 *pAllocNotify; IVMRSurfaceAllocatorNotify9 *pAllocNotify;
+1 -1
View File
@@ -907,7 +907,7 @@ static void _stdcall play_sfall_sound2() {
&loopArg = opHandler.arg(1); &loopArg = opHandler.arg(1);
if (fileArg.isString() && loopArg.isInt()) { if (fileArg.isString() && loopArg.isInt()) {
long soundID = (long)PlaySfallSound(fileArg.strValue(), loopArg.asBool()); DWORD soundID = PlaySfallSound(fileArg.strValue(), loopArg.asBool());
opHandler.setReturn(soundID); opHandler.setReturn(soundID);
} else { } else {
OpcodeInvalidArgs("play_sfall_sound"); OpcodeInvalidArgs("play_sfall_sound");
+51 -46
View File
@@ -24,10 +24,10 @@
#include "Sound.h" #include "Sound.h"
#define SAFERELEASE(a) { if (a) { a->Release(); a = 0; } } #define SAFERELEASE(a) { if (a) { a->Release(); } }
struct sDSSound { struct sDSSound {
DWORD id; DWORD id; // should be first
IGraphBuilder *pGraph; IGraphBuilder *pGraph;
IMediaControl *pControl; IMediaControl *pControl;
IMediaSeeking *pSeek; IMediaSeeking *pSeek;
@@ -42,7 +42,7 @@ DWORD playID = 0;
DWORD loopID = 0; DWORD loopID = 0;
static HWND soundwindow = 0; static HWND soundwindow = 0;
static void* musicLoopPtr = nullptr; static sDSSound* musicLoopPtr = nullptr;
//static char playingMusicFile[256]; //static char playingMusicFile[256];
static void FreeSound(sDSSound* sound) { static void FreeSound(sDSSound* sound) {
@@ -56,46 +56,48 @@ static void FreeSound(sDSSound* sound) {
} }
void WipeSounds() { void WipeSounds() {
for (DWORD i = 0; i < playingSounds.size(); i++) FreeSound(playingSounds[i]); for (size_t 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 < loopingSounds.size(); i++) FreeSound(loopingSounds[i]);
playingSounds.clear(); playingSounds.clear();
loopingSounds.clear(); loopingSounds.clear();
musicLoopPtr = nullptr; musicLoopPtr = nullptr;
playID = 0;
loopID = 0;
} }
LRESULT CALLBACK SoundWndProc(HWND wnd, UINT msg, WPARAM w, LPARAM l) { LRESULT CALLBACK SoundWndProc(HWND wnd, UINT msg, WPARAM w, LPARAM l) {
if (msg == WM_APP) { if (msg == WM_APP) {
DWORD id = l; DWORD id = l;
sDSSound* dssound = nullptr; sDSSound* sound = nullptr;
if (id & 0x80000000) { 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) { if (loopingSounds[i]->id == id) {
dssound = loopingSounds[i]; sound = loopingSounds[i];
break; break;
} }
} }
} else { } else {
for (DWORD i = 0; i < playingSounds.size(); i++) { for (size_t i = 0; i < playingSounds.size(); i++) {
if (playingSounds[i]->id == id) { if (playingSounds[i]->id == id) {
dssound = playingSounds[i]; sound = playingSounds[i];
break; break;
} }
} }
} }
if (!dssound) return 0; if (!sound) return 0;
LONG e = 0; LONG e = 0;
LONG_PTR p1 = 0, p2 = 0; LONG_PTR p1 = 0, p2 = 0;
while (!FAILED(dssound->pEvent->GetEvent(&e, &p1, &p2, 0))) { while (!FAILED(sound->pEvent->GetEvent(&e, &p1, &p2, 0))) {
dssound->pEvent->FreeEventParams(e, p1, p2); sound->pEvent->FreeEventParams(e, p1, p2);
if (e == EC_COMPLETE) { if (e == EC_COMPLETE) {
if (id & 0x80000000) { if (id & 0x80000000) {
LONGLONG pos = 0; LONGLONG pos = 0;
dssound->pSeek->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 0, AM_SEEKING_NoPositioning); sound->pSeek->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 0, AM_SEEKING_NoPositioning);
dssound->pControl->Run(); sound->pControl->Run();
} else { } else {
for (DWORD i = 0; i < playingSounds.size(); i++) { for (size_t i = 0; i < playingSounds.size(); i++) {
if (playingSounds[i] == dssound) { if (playingSounds[i] == sound) {
FreeSound(dssound); FreeSound(sound);
playingSounds.erase(playingSounds.begin() + i); playingSounds.erase(playingSounds.begin() + i);
return 0; return 0;
} }
@@ -153,7 +155,7 @@ static void __cdecl SfallSoundVolume(sDSSound* sound, int type, long passVolume)
loopVolume = sfxVolume = -9999; // mute loopVolume = sfxVolume = -9999; // mute
} else if (type = 0) { // for music } else if (type = 0) { // for music
if (musicLoopPtr) { if (musicLoopPtr) {
StopSfallSound(musicLoopPtr); StopSfallSound(musicLoopPtr->id);
musicLoopPtr = nullptr; musicLoopPtr = nullptr;
} }
return; return;
@@ -187,42 +189,42 @@ static bool IsMute(bool type) {
static sDSSound* PlayingSound(wchar_t* path, bool loop) { static sDSSound* PlayingSound(wchar_t* path, bool loop) {
if (!soundwindow) CreateSndWnd(); if (!soundwindow) CreateSndWnd();
if (IsMute(loop)) return nullptr; 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; 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) { if (hr != S_OK) {
dlog_f("Error CoCreateInstance: %d", DL_INIT, hr); dlog_f("Error CoCreateInstance: %d", DL_INIT, hr);
return nullptr; return nullptr;
} }
result->pGraph->QueryInterface(IID_IMediaControl, (void**)&result->pControl); sound->pGraph->QueryInterface(IID_IMediaControl, (void**)&sound->pControl);
if (loop) if (loop)
result->pGraph->QueryInterface(IID_IMediaSeeking, (void**)&result->pSeek); sound->pGraph->QueryInterface(IID_IMediaSeeking, (void**)&sound->pSeek);
else else
result->pSeek = nullptr; sound->pSeek = nullptr;
result->pGraph->QueryInterface(IID_IMediaEventEx, (void**)&result->pEvent); sound->pGraph->QueryInterface(IID_IMediaEventEx, (void**)&sound->pEvent);
result->pEvent->SetNotifyWindow((OAHWND)soundwindow, WM_APP, id); sound->pEvent->SetNotifyWindow((OAHWND)soundwindow, WM_APP, id);
sound->pGraph->QueryInterface(IID_IBasicAudio, (void**)&sound->pAudio);
result->pGraph->QueryInterface(IID_IBasicAudio, (void**)&result->pAudio); sound->pControl->RenderFile(path);
sound->pControl->Run();
result->pControl->RenderFile(path);
result->pControl->Run();
if (loop) { if (loop) {
loopingSounds.push_back(result); loopingSounds.push_back(sound);
SfallSoundVolume(result, 0, *(DWORD*)_background_volume); // music SfallSoundVolume(sound, 0, *(DWORD*)_background_volume); // music
} else { } else {
playingSounds.push_back(result); playingSounds.push_back(sound);
SfallSoundVolume(result, 1, *(DWORD*)_sndfx_volume); SfallSoundVolume(sound, 1, *(DWORD*)_sndfx_volume);
} }
return result; return sound;
} }
static const wchar_t *SoundExtensions[] = { L"mp3", L"wma", L"wav" }; static const wchar_t *SoundExtensions[] = { L"mp3", L"wma", L"wav" };
@@ -245,7 +247,7 @@ static bool __cdecl SoundFileLoad(DWORD called, const char* path) {
bool music = (called == 0x45092B); // from gsound_background_play_ bool music = (called == 0x45092B); // from gsound_background_play_
if (music && musicLoopPtr != nullptr) { if (music && musicLoopPtr != nullptr) {
//if (found && strcmp(path, playingMusicFile) == 0) return true; // don't stop music //if (found && strcmp(path, playingMusicFile) == 0) return true; // don't stop music
StopSfallSound(musicLoopPtr); StopSfallSound(musicLoopPtr->id);
musicLoopPtr = nullptr; musicLoopPtr = nullptr;
} }
if (!isExist) return false; if (!isExist) return false;
@@ -271,18 +273,20 @@ static void __fastcall MakeMusicPath(const char* file) {
SoundFileLoad(0x45092B, pathBuf); SoundFileLoad(0x45092B, pathBuf);
} }
void* __stdcall PlaySfallSound(const char* path, bool loop) { DWORD __stdcall PlaySfallSound(const char* path, bool loop) {
wchar_t buf[256]; wchar_t buf[256];
mbstowcs_s(0, buf, path, 256); mbstowcs_s(0, buf, path, 256);
sDSSound* result = PlayingSound(buf, loop); sDSSound* sound = PlayingSound(buf, loop);
return (loop) ? result : 0; return (loop && sound) ? sound->id : 0;
} }
void __stdcall StopSfallSound(void* _ptr) { void __stdcall StopSfallSound(DWORD id) {
sDSSound* ptr = (sDSSound*)_ptr; if (!id) return;
for (DWORD i = 0; i < loopingSounds.size(); i++) { for (size_t i = 0; i < loopingSounds.size(); i++) {
if (loopingSounds[i] == ptr) { if (loopingSounds[i]->id == id) {
FreeSound(ptr); sDSSound* sound = loopingSounds[i];
sound->pControl->Stop();
FreeSound(sound);
loopingSounds.erase(loopingSounds.begin() + i); loopingSounds.erase(loopingSounds.begin() + i);
return; return;
} }
@@ -328,6 +332,7 @@ static void __declspec(naked) gmovie_play_hook_stop() {
mov eax, musicLoopPtr; mov eax, musicLoopPtr;
test eax, eax; test eax, eax;
jz skip; jz skip;
mov eax, [eax]; // musicLoopPtr->id
push ecx; push ecx;
push edx; push edx;
push eax; push eax;
+2 -2
View File
@@ -21,6 +21,6 @@
void SoundInit(); void SoundInit();
void SoundExit(); void SoundExit();
void* __stdcall PlaySfallSound(const char* path, bool loop); DWORD __stdcall PlaySfallSound(const char* path, bool loop);
void __stdcall StopSfallSound(void* ptr); void __stdcall StopSfallSound(DWORD id);
void WipeSounds(); void WipeSounds();