mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-25 21:03:15 -04:00
Fix city sounds not being played on released game versions (#7268)
This commit is contained in:
parent
884a16d632
commit
466560abba
@ -1,6 +1,5 @@
|
||||
package com.unciv.ui.audio
|
||||
|
||||
import com.badlogic.gdx.Files
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.audio.Music
|
||||
import com.badlogic.gdx.files.FileHandle
|
||||
@ -13,36 +12,40 @@ import com.unciv.utils.Log
|
||||
class CityAmbiencePlayer(
|
||||
city: CityInfo
|
||||
) : Disposable {
|
||||
private val soundsLocation = Files.FileType.Local
|
||||
private var playingCitySound: Music? = null
|
||||
val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
|
||||
|
||||
init {
|
||||
play(city)
|
||||
}
|
||||
|
||||
private fun getFile(path: String) =
|
||||
if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable)
|
||||
Gdx.files.external(path)
|
||||
else Gdx.files.local(path)
|
||||
private fun getFile(path: String): FileHandle {
|
||||
val internal = Gdx.files.internal(path)
|
||||
if (internal.exists()) return internal
|
||||
return Gdx.files.local(path)
|
||||
}
|
||||
|
||||
private fun getSoundFolders() = sequence {
|
||||
val visualMods = UncivGame.Current.settings.visualMods
|
||||
val mods = UncivGame.Current.gameInfo!!.gameParameters.getModsAndBaseRuleset()
|
||||
yieldAll(
|
||||
(visualMods + mods).asSequence()
|
||||
.map { getFile("mods")
|
||||
.child(it).child("sounds") }
|
||||
)
|
||||
val modSoundFolders = (visualMods + mods).asSequence()
|
||||
.map { modName ->
|
||||
getFile("mods")
|
||||
.child(modName)
|
||||
.child("sounds")
|
||||
}
|
||||
|
||||
yieldAll(modSoundFolders)
|
||||
yield(getFile("sounds"))
|
||||
}
|
||||
|
||||
private fun getSoundFile(fileName: String): FileHandle? = getSoundFolders()
|
||||
.filter { it.exists() && it.isDirectory }
|
||||
.flatMap { it.list().asSequence() }
|
||||
// ensure only normal files with common sound extension
|
||||
.filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions }
|
||||
.firstOrNull { it.name().contains(fileName) }
|
||||
private fun getSoundFile(fileName: String): FileHandle? {
|
||||
return getSoundFolders()
|
||||
.filter { it.exists() && it.isDirectory }
|
||||
.flatMap { it.list().asSequence() }
|
||||
// ensure only normal files with common sound extension
|
||||
.filter { !it.isDirectory && it.extension() in MusicController.gdxSupportedFileExtensions }
|
||||
.firstOrNull { it.nameWithoutExtension() == fileName }
|
||||
}
|
||||
|
||||
private fun play(city: CityInfo) {
|
||||
if (UncivGame.Current.settings.citySoundsVolume == 0f) return
|
||||
@ -50,8 +53,7 @@ class CityAmbiencePlayer(
|
||||
if (playingCitySound != null)
|
||||
stop()
|
||||
try {
|
||||
val file = FileHandle(getSoundFile(city.civInfo.getEra().citySound).toString())
|
||||
playingCitySound = Gdx.audio.newMusic(file)
|
||||
playingCitySound = Gdx.audio.newMusic(getSoundFile(city.civInfo.getEra().citySound))
|
||||
playingCitySound?.volume = UncivGame.Current.settings.citySoundsVolume
|
||||
playingCitySound?.isLooping = true
|
||||
playingCitySound?.play()
|
||||
|
@ -35,7 +35,7 @@ class MusicController {
|
||||
private const val defaultFadingStepGdx = 1f / (defaultFadeDuration * ticksPerSecondGdx)
|
||||
private const val defaultFadingStepOwn = 1f / (defaultFadeDuration * ticksPerSecondOwn)
|
||||
private const val musicHistorySize = 8 // number of names to keep to avoid playing the same in short succession
|
||||
private val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
|
||||
val gdxSupportedFileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
|
||||
|
||||
private fun getFile(path: String) =
|
||||
if (musicLocation == FileType.External && Gdx.files.isExternalStorageAvailable)
|
||||
@ -271,7 +271,7 @@ class MusicController {
|
||||
.filter { it.exists() && it.isDirectory }
|
||||
.flatMap { it.list().asSequence() }
|
||||
// ensure only normal files with common sound extension
|
||||
.filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions }
|
||||
.filter { it.exists() && !it.isDirectory && it.extension() in gdxSupportedFileExtensions }
|
||||
|
||||
/** Choose adequate entry from [getAllMusicFiles] */
|
||||
private fun chooseFile(prefix: String, suffix: String, flags: EnumSet<MusicTrackChooserFlags>): FileHandle? {
|
||||
@ -310,7 +310,7 @@ class MusicController {
|
||||
val fileNameParts = fileName.split('/')
|
||||
val modName = if (fileNameParts.size > 1 && fileNameParts[0] == "mods") fileNameParts[1] else ""
|
||||
var trackName = fileNameParts[if (fileNameParts.size > 3 && fileNameParts[2] == "music") 3 else 1]
|
||||
for (extension in fileExtensions)
|
||||
for (extension in gdxSupportedFileExtensions)
|
||||
trackName = trackName.removeSuffix(".$extension")
|
||||
fireOnChange(modName + (if (modName.isEmpty()) "" else ": ") + trackName)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user