From 67ea4d0c2ef566a615600c77ed32dcd621319261 Mon Sep 17 00:00:00 2001 From: Mathieu Schroeter Date: Mon, 11 Dec 2017 18:42:49 +0100 Subject: [PATCH] Look music for user dir, fallback to the game dir Then it's possible to overload the original musicis by providing yours in the user directory (in music subdir). --- src/event.cxx | 28 ++++++++++++++++++++-------- src/misc.cxx | 24 ++++++++++++++++++++---- src/misc.h | 6 +++++- src/sound.cxx | 9 ++++----- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/event.cxx b/src/event.cxx index 22ff6f4..315a1ab 100644 --- a/src/event.cxx +++ b/src/event.cxx @@ -3362,18 +3362,29 @@ CEvent::ChangePhase (Uint32 phase) music = m_pDecor->GetMusic (); if (music > 0) { - const std::string exts[] = {"ogg", "mid"}; + static const std::string exts[] = {"ogg", "mid"}; + static const Location locs[] = {LOCATION_USER, LOCATION_BASE}; + std::string absolute; - filename = string_format ( - "music/music%.3d.%s", music - 1, - exts[g_restoreMidi ? 1 : 0].c_str ()); - if (!FileExists (filename)) + // Look for music in the user directory, then in the game directory. + for (size_t i = 0; i < countof (locs); ++i) + { filename = string_format ( "music/music%.3d.%s", music - 1, - exts[g_restoreMidi ? 0 : 1].c_str ()); + exts[g_restoreMidi ? 1 : 0].c_str ()); + if (!FileExists (filename, absolute, locs[i])) + filename = string_format ( + "music/music%.3d.%s", music - 1, + exts[g_restoreMidi ? 0 : 1].c_str ()); + + if (FileExists (filename, absolute, locs[i])) + break; + + absolute = ""; + } m_pSound->StopMusic (); - m_pSound->PlayMusic (filename); + m_pSound->PlayMusic (absolute); } } } @@ -4403,7 +4414,8 @@ CEvent::StartMovie (const std::string & pFilename) if (!m_bMovie) return false; - if (!FileExists (pFilename)) + std::string absolute; + if (!FileExists (pFilename, absolute)) return false; HideMouse (true); diff --git a/src/misc.cxx b/src/misc.cxx index de244e9..3884ec7 100644 --- a/src/misc.cxx +++ b/src/misc.cxx @@ -157,12 +157,28 @@ AddUserPath (std::string & pFilename) } bool -FileExists (const std::string & filename) +FileExists ( + const std::string & filename, std::string & absolute, enum Location location) { - const auto path = GetBaseDir () + filename; - FILE * file; + absolute = filename; + FILE * file; - file = fopen (path.c_str (), "rb"); + switch (location) + { + case LOCATION_BASE: + absolute = GetBaseDir () + filename; + break; + + case LOCATION_USER: + AddUserPath (absolute); + break; + + default: + case LOCATION_ABSOLUTE: + break; + } + + file = fopen (absolute.c_str (), "rb"); if (file == nullptr) return false; diff --git a/src/misc.h b/src/misc.h index db50f86..d1a5c98 100644 --- a/src/misc.h +++ b/src/misc.h @@ -37,7 +37,11 @@ std::string GetBaseDir (); std::string GetShareDir (); std::string GetLocale (); extern void AddUserPath (std::string & pFilename); -bool FileExists (const std::string & filename); + +enum Location { LOCATION_ABSOLUTE, LOCATION_BASE, LOCATION_USER }; +bool FileExists ( + const std::string & filename, std::string & absolute, + Location location = LOCATION_BASE); template std::string diff --git a/src/sound.cxx b/src/sound.cxx index 6e0790b..2969914 100644 --- a/src/sound.cxx +++ b/src/sound.cxx @@ -326,20 +326,19 @@ CSound::PlayImage (Sounds channel, Point pos, Sint32 rank) bool CSound::PlayMusic (const std::string & lpszMIDIFilename) { - std::string path = GetBaseDir (); - if (m_midiVolume == 0) return true; + if (lpszMIDIFilename.empty ()) + return false; + Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100); m_lastMidiVolume = m_midiVolume; - path += lpszMIDIFilename; - if (m_pMusic) Mix_FreeMusic (m_pMusic); - m_pMusic = Mix_LoadMUS (path.c_str ()); + m_pMusic = Mix_LoadMUS (lpszMIDIFilename.c_str ()); if (!m_pMusic) { printf ("%s\n", Mix_GetError ());