perform music volume lookup by lump number instead of name.

This way any setting will work for both short and long file names of the same content.
This commit is contained in:
Christoph Oelckers 2023-12-27 20:08:22 +01:00
parent 958303556f
commit eb2f263803
4 changed files with 56 additions and 40 deletions

View file

@ -75,14 +75,12 @@ float saved_relative_volume = 1.0f; // this could be used to implement an ACS Fa
MusicVolumeMap MusicVolumes;
MidiDeviceMap MidiDevices;
static FileReader DefaultOpenMusic(const char* fn)
static int DefaultFindMusic(const char* fn)
{
// This is the minimum needed to make the music system functional.
FileReader fr;
fr.OpenFile(fn);
return fr;
return -1;
}
static MusicCallbacks mus_cb = { nullptr, DefaultOpenMusic };
MusicCallbacks mus_cb = { nullptr, DefaultFindMusic };
// PUBLIC DATA DEFINITIONS -------------------------------------------------
@ -98,10 +96,44 @@ CVAR(Bool, mus_usereplaygain, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) // changi
// CODE --------------------------------------------------------------------
//==========================================================================
//
// OpenMusic
//
// opens a FileReader for the music - used as a callback to keep
// implementation details out of the core player.
//
//==========================================================================
static FileReader OpenMusic(const char* musicname)
{
FileReader reader;
if (!FileExists(musicname))
{
int lumpnum;
lumpnum = mus_cb.FindMusic(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
if (lumpnum == -1)
{
Printf("Music \"%s\" not found\n", musicname);
}
else if (fileSystem.FileLength(lumpnum) != 0)
{
reader = fileSystem.ReopenFileReader(lumpnum);
}
}
else
{
// Load an external file.
reader.OpenFile(musicname);
}
return reader;
}
void S_SetMusicCallbacks(MusicCallbacks* cb)
{
mus_cb = *cb;
if (mus_cb.OpenMusic == nullptr) mus_cb.OpenMusic = DefaultOpenMusic; // without this we are dead in the water.
if (mus_cb.FindMusic == nullptr) mus_cb.FindMusic = DefaultFindMusic; // without this we are dead in the water.
}
int MusicEnabled() // int return is for scripting
@ -521,7 +553,7 @@ static void CheckReplayGain(const char *musicname, EMidiDevice playertype, const
mod_dumb_mastervolume->Callback();
if (!mus_usereplaygain) return;
FileReader reader = mus_cb.OpenMusic(musicname);
FileReader reader = OpenMusic(musicname);
if (!reader.isOpen()) return;
int flength = (int)reader.GetLength();
auto mreader = GetMusicReader(reader); // this passes the file reader to the newly created wrapper.
@ -693,7 +725,7 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
}
// opening the music must be done by the game because it's different depending on the game's file system use.
FileReader reader = mus_cb.OpenMusic(musicname);
FileReader reader = OpenMusic(musicname);
if (!reader.isOpen()) return false;
auto m = reader.Read();
reader.Seek(0, FileReader::SeekSet);
@ -718,7 +750,8 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
}
else
{
auto volp = MusicVolumes.CheckKey(musicname);
int lumpnum = mus_cb.FindMusic(musicname);
auto volp = MusicVolumes.CheckKey(lumpnum);
if (volp)
{
mus_playing.musicVolume = *volp;

View file

@ -24,7 +24,7 @@ void S_PauseAllCustomStreams(bool on);
struct MusicCallbacks
{
FString(*LookupFileName)(const char* fn, int &order);
FileReader(*OpenMusic)(const char* fn);
int(*FindMusic)(const char* fn);
};
void S_SetMusicCallbacks(MusicCallbacks* cb);
@ -69,15 +69,17 @@ struct MidiDeviceSetting
};
typedef TMap<FName, MidiDeviceSetting> MidiDeviceMap;
typedef TMap<FName, float> MusicVolumeMap;
typedef TMap<int, float> MusicVolumeMap;
extern MidiDeviceMap MidiDevices;
extern MusicVolumeMap MusicVolumes;
extern MusicCallbacks mus_cb;
struct MusPlayingInfo
{
FString name;
ZMusic_MusicStream handle;
int lumpnum;
int baseorder;
float musicVolume;
bool loop;

View file

@ -1034,7 +1034,7 @@ static void S_AddSNDINFO (int lump)
case SI_MusicVolume: {
sc.MustGetString();
FName musname (sc.String);
int lumpnum = mus_cb.FindMusic(sc.String);
if (!sc.CheckFloat())
{
sc.MustGetString();
@ -1043,7 +1043,7 @@ static void S_AddSNDINFO (int lump)
if (!stricmp(p, "db")) sc.Float = dBToAmplitude((float)sc.Float);
else sc.ScriptError("Bad value for music volume: %s", sc.String);
}
MusicVolumes[musname] = (float)sc.Float;
if (lumpnum >= 0) MusicVolumes[lumpnum] = (float)sc.Float;
}
break;

View file

@ -177,36 +177,17 @@ static FString LookupMusic(const char* musicname, int& order)
//==========================================================================
//
// OpenMusic
// FindMusic
//
// opens a FileReader for the music - used as a callback to keep
// implementation details out of the core player.
// loops up a music resource according to the engine's rules
//
//==========================================================================
static FileReader OpenMusic(const char* musicname)
static int FindMusic(const char* musicname)
{
FileReader reader;
if (!FileExists(musicname))
{
int lumpnum;
lumpnum = fileSystem.CheckNumForFullName(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
if (lumpnum == -1)
{
Printf("Music \"%s\" not found\n", musicname);
}
else if (fileSystem.FileLength(lumpnum) != 0)
{
reader = fileSystem.ReopenFileReader(lumpnum);
}
}
else
{
// Load an external file.
reader.OpenFile(musicname);
}
return reader;
int lumpnum = fileSystem.CheckNumForFullName(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
return lumpnum;
}
//==========================================================================
@ -220,7 +201,7 @@ static FileReader OpenMusic(const char* musicname)
void S_Init()
{
// Hook up the music player with the engine specific customizations.
static MusicCallbacks cb = { LookupMusic, OpenMusic };
static MusicCallbacks cb = { LookupMusic, FindMusic };
S_SetMusicCallbacks(&cb);
// Must be up before I_InitSound.