Skip to content

Commit

Permalink
Fix issues with custom music (#599)
Browse files Browse the repository at this point in the history
* Reworked parts of MusicManager code to be easier to read and see what it does
* Return true on failure to prevent original function call
* Fix custom music sometimes not playing
  • Loading branch information
segabl committed Dec 21, 2022
1 parent 4781f4a commit 520b804
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 62 deletions.
120 changes: 63 additions & 57 deletions Hooks/MusicManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ function MusicManager:post_event(name, ...)
end
end

local orig_check = MusicManager.check_music_switch
function MusicManager:check_music_switch(...)
local switches = tweak_data.levels:get_music_switches()
if switches and #switches > 0 then
Global.music_manager.current_track = switches[math.random(#switches)]
if not self:attempt_play(Global.music_manager.current_track) then
return orig_check(self, ...)
end
end
end

local orig_stop_all = MusicManager.stop_listen_all
function MusicManager:stop_listen_all(...)
if self._current_music_ext or self._current_event then
Expand Down Expand Up @@ -94,7 +83,7 @@ function MusicManager:stop_listen_all(...)
Global.music_manager.source:post_event("stop_all_music")
self._current_event = nil
self._current_track = nil
self._skip_play = nil
self._skip_play = nil
else
return orig_stop_all(self, ...)
end
Expand Down Expand Up @@ -146,63 +135,74 @@ function MusicManager:attempt_play(track, event, stop)
if event == "music_uno_fade_reset" then
return
end

if stop then
self:stop_custom()
end

local next_music
local next_event
if track and track ~= self._current_custom_track then
self._current_custom_track = nil
end
for id, music in pairs(BeardLib.MusicMods) do
if next_music then
if track == id then
-- Set current custom heist track, can return here since track and event are never set at the same time
self._current_custom_track = id
return true
elseif event == id then
-- If event matches music id it's menu music and we can also break out of the loop
next_music = music
next_event = music.preview_event
break
end
if event == id or track == id or self._current_custom_track == id then
if music.tracks and (self._current_custom_track ~= id or id == event) then
next_music = music
self._current_custom_track = id
elseif music.events and event then
-- Try finding the right event to play
for modded_event, event_tbl in pairs(music.events) do
if event == modded_event or event:ends(modded_event) then
next_music = music
next_event = event_tbl
self._current_custom_track = id
break
end
elseif event and self._current_custom_track == id then
-- If we have an event and currently playing custom music, check if it matches any music event
next_music = music
for modded_event, event_tbl in pairs(music.events) do
if event == modded_event or event:ends(modded_event) then
next_event = event_tbl
break
end
end

-- If we found a matching event we can break out of the loop, we found what to play
if next_event then
break
end
end
end
if next_music then
local next = next_event or next_music
local track_index = self:pick_track_index(next.tracks)
local next_track = next.tracks and next.tracks[track_index]
local source = next_track and (next_track.start_source or next_track.source)
if next_music.xaudio then
if not source then
BeardLib:Err("No buffer found to play for music '%s'", tostring(self._current_custom_track))
return
end
else
if not source or not DB:has(movie_ids, source:id()) then
BeardLib:Err("Source file '%s' is not loaded, music id '%s'", tostring(source), tostring(self._current_custom_track))
return true
end

if not next_event then
-- If there's no event (= none of the custom music matched the requested track/event) clear the current custom track
self._current_custom_track = nil
return
end

local track_index = self:pick_track_index(next_event.tracks)
local next_track = next_event.tracks and next_event.tracks[track_index]
local source = next_track and (next_track.start_source or next_track.source)

if next_music.xaudio then
if not source then
BeardLib:Err("No buffer found to play for music '%s'", tostring(self._current_custom_track))
return true
end
else
if not source or not DB:has(movie_ids, source:id()) then
BeardLib:Err("Source file '%s' is not loaded, music id '%s'", tostring(source), tostring(self._current_custom_track))
return true
end
local volume = next_track.volume or next.volume or next_music.volume
self._switch_at_end = (next_track.start_source or next.allow_switch and #next.tracks > 1) and {
tracks = next.tracks,
track_index = next_track.start_source and track_index or self:pick_track_index(next.tracks),
allow_switch = next.allow_switch,
xaudio = next_music.xaudio,
volume = volume
}
self:play(source, next_music.xaudio, volume)
return true
end
return next_music ~= nil
end

local volume = next_track.volume or next_event.volume or next_music.volume
self._switch_at_end = (next_track.start_source or next_event.allow_switch and #next_event.tracks > 1) and {
tracks = next_event.tracks,
track_index = next_track.start_source and track_index or self:pick_track_index(next_event.tracks),
allow_switch = next_event.allow_switch,
xaudio = next_music.xaudio,
volume = volume
}

self:play(source, next_music.xaudio, volume)

return true
end

function MusicManager:play(src, use_xaudio, custom_volume)
Expand Down Expand Up @@ -335,6 +335,12 @@ Hooks:PostHook(MusicManager, "init", "BeardLibMusicManagerInit", function(self)
end
end)

Hooks:PostHook(MusicManager, "check_music_switch", "BeardLibMusicManagerCheckMusicSwitch", function (self)
if self:attempt_play(Global.music_manager.current_track) then
Global.music_manager.source:stop()
end
end)

Hooks:PostHook(MusicManager, "load_settings", "BeardLibMusicManagerLoadSettings", function(self)
self:check_playlist()
end)
Expand Down
3 changes: 1 addition & 2 deletions Modules/Addons/HeistMusicModule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ function HeistMusic:RegisterHook()
end
end

local event = music.events[self._config.preview_event or (self.is_stealth and "suspense_4" or "assault")]
music.tracks = event and event.tracks
music.preview_event = music.events[self._config.preview_event or (self.is_stealth and "suspense_4" or "assault")] or music.events[next(music.events)]

BeardLib.MusicMods[self._config.id] = music
end
4 changes: 3 additions & 1 deletion Modules/Addons/MenuMusicModule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function MenuMusicModule:RegisterHook()
}
}
}
music.tracks = music.events.menu.tracks -- workaround for menu music being structured slightly different, should cleanup code at some point

music.preview_event = music.events.menu

BeardLib.MusicMods[self._config.id] = music
end
3 changes: 1 addition & 2 deletions Modules/Addons/MusicModule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ function MusicModule:RegisterHook()
self._mod._config.AddFiles = AddFilesModule:new(self._mod, add)
end

local event = music.events[self._config.preview_event or "assault"]
music.tracks = event and event.tracks
music.preview_event = music.events[self._config.preview_event or "assault"] or music.events[next(music.events)]

BeardLib.MusicMods[self._config.id] = music
end

0 comments on commit 520b804

Please sign in to comment.