mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-27 13:55:54 -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
|
package com.unciv.ui.audio
|
||||||
|
|
||||||
import com.badlogic.gdx.Files
|
|
||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
import com.badlogic.gdx.audio.Music
|
import com.badlogic.gdx.audio.Music
|
||||||
import com.badlogic.gdx.files.FileHandle
|
import com.badlogic.gdx.files.FileHandle
|
||||||
@ -13,36 +12,40 @@ import com.unciv.utils.Log
|
|||||||
class CityAmbiencePlayer(
|
class CityAmbiencePlayer(
|
||||||
city: CityInfo
|
city: CityInfo
|
||||||
) : Disposable {
|
) : Disposable {
|
||||||
private val soundsLocation = Files.FileType.Local
|
|
||||||
private var playingCitySound: Music? = null
|
private var playingCitySound: Music? = null
|
||||||
val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
play(city)
|
play(city)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getFile(path: String) =
|
private fun getFile(path: String): FileHandle {
|
||||||
if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable)
|
val internal = Gdx.files.internal(path)
|
||||||
Gdx.files.external(path)
|
if (internal.exists()) return internal
|
||||||
else Gdx.files.local(path)
|
return Gdx.files.local(path)
|
||||||
|
}
|
||||||
|
|
||||||
private fun getSoundFolders() = sequence {
|
private fun getSoundFolders() = sequence {
|
||||||
val visualMods = UncivGame.Current.settings.visualMods
|
val visualMods = UncivGame.Current.settings.visualMods
|
||||||
val mods = UncivGame.Current.gameInfo!!.gameParameters.getModsAndBaseRuleset()
|
val mods = UncivGame.Current.gameInfo!!.gameParameters.getModsAndBaseRuleset()
|
||||||
yieldAll(
|
val modSoundFolders = (visualMods + mods).asSequence()
|
||||||
(visualMods + mods).asSequence()
|
.map { modName ->
|
||||||
.map { getFile("mods")
|
getFile("mods")
|
||||||
.child(it).child("sounds") }
|
.child(modName)
|
||||||
)
|
.child("sounds")
|
||||||
|
}
|
||||||
|
|
||||||
|
yieldAll(modSoundFolders)
|
||||||
yield(getFile("sounds"))
|
yield(getFile("sounds"))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSoundFile(fileName: String): FileHandle? = getSoundFolders()
|
private fun getSoundFile(fileName: String): FileHandle? {
|
||||||
.filter { it.exists() && it.isDirectory }
|
return getSoundFolders()
|
||||||
.flatMap { it.list().asSequence() }
|
.filter { it.exists() && it.isDirectory }
|
||||||
// ensure only normal files with common sound extension
|
.flatMap { it.list().asSequence() }
|
||||||
.filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions }
|
// ensure only normal files with common sound extension
|
||||||
.firstOrNull { it.name().contains(fileName) }
|
.filter { !it.isDirectory && it.extension() in MusicController.gdxSupportedFileExtensions }
|
||||||
|
.firstOrNull { it.nameWithoutExtension() == fileName }
|
||||||
|
}
|
||||||
|
|
||||||
private fun play(city: CityInfo) {
|
private fun play(city: CityInfo) {
|
||||||
if (UncivGame.Current.settings.citySoundsVolume == 0f) return
|
if (UncivGame.Current.settings.citySoundsVolume == 0f) return
|
||||||
@ -50,8 +53,7 @@ class CityAmbiencePlayer(
|
|||||||
if (playingCitySound != null)
|
if (playingCitySound != null)
|
||||||
stop()
|
stop()
|
||||||
try {
|
try {
|
||||||
val file = FileHandle(getSoundFile(city.civInfo.getEra().citySound).toString())
|
playingCitySound = Gdx.audio.newMusic(getSoundFile(city.civInfo.getEra().citySound))
|
||||||
playingCitySound = Gdx.audio.newMusic(file)
|
|
||||||
playingCitySound?.volume = UncivGame.Current.settings.citySoundsVolume
|
playingCitySound?.volume = UncivGame.Current.settings.citySoundsVolume
|
||||||
playingCitySound?.isLooping = true
|
playingCitySound?.isLooping = true
|
||||||
playingCitySound?.play()
|
playingCitySound?.play()
|
||||||
|
@ -35,7 +35,7 @@ class MusicController {
|
|||||||
private const val defaultFadingStepGdx = 1f / (defaultFadeDuration * ticksPerSecondGdx)
|
private const val defaultFadingStepGdx = 1f / (defaultFadeDuration * ticksPerSecondGdx)
|
||||||
private const val defaultFadingStepOwn = 1f / (defaultFadeDuration * ticksPerSecondOwn)
|
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 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) =
|
private fun getFile(path: String) =
|
||||||
if (musicLocation == FileType.External && Gdx.files.isExternalStorageAvailable)
|
if (musicLocation == FileType.External && Gdx.files.isExternalStorageAvailable)
|
||||||
@ -271,7 +271,7 @@ class MusicController {
|
|||||||
.filter { it.exists() && it.isDirectory }
|
.filter { it.exists() && it.isDirectory }
|
||||||
.flatMap { it.list().asSequence() }
|
.flatMap { it.list().asSequence() }
|
||||||
// ensure only normal files with common sound extension
|
// 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] */
|
/** Choose adequate entry from [getAllMusicFiles] */
|
||||||
private fun chooseFile(prefix: String, suffix: String, flags: EnumSet<MusicTrackChooserFlags>): FileHandle? {
|
private fun chooseFile(prefix: String, suffix: String, flags: EnumSet<MusicTrackChooserFlags>): FileHandle? {
|
||||||
@ -310,7 +310,7 @@ class MusicController {
|
|||||||
val fileNameParts = fileName.split('/')
|
val fileNameParts = fileName.split('/')
|
||||||
val modName = if (fileNameParts.size > 1 && fileNameParts[0] == "mods") fileNameParts[1] else ""
|
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]
|
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")
|
trackName = trackName.removeSuffix(".$extension")
|
||||||
fireOnChange(modName + (if (modName.isEmpty()) "" else ": ") + trackName)
|
fireOnChange(modName + (if (modName.isEmpty()) "" else ": ") + trackName)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user