Respect visualMods for Sound - CheckBox, formats, modchange detect (#4295)

* Respect visualMods for Sound - CheckBox, formats, modchange detect

* Respect visualMods for Sound - german patch
This commit is contained in:
SomeTroglodyte
2021-06-29 20:31:57 +02:00
committed by GitHub
parent 168f984a65
commit 4d16b3fcf8
4 changed files with 56 additions and 30 deletions

View File

@ -990,8 +990,7 @@ Current mods = Installierte Modifikationen
Downloadable mods = Verfügbare Modifikationen
Next page = Nächste Seite
Open Github page = Auf Github öffnen
Enable as permanent visual mod = Als permanente visuelle Modifikation aktivieren
Disable as permanent visual mod = Als permanente visuelle Modifikation deaktivieren
Permanent audiovisual mod = Permanente audiovisuelle Mod
Installed = Installiert
Downloaded! = Heruntergeladen!
Could not download mod = Konnte Modifikation nicht herunterladen

View File

@ -991,8 +991,7 @@ Current mods =
Downloadable mods =
Next page =
Open Github page =
Enable as permanent visual mod =
Disable as permanent visual mod =
Permanent audiovisual mod =
Installed =
Downloaded! =
Could not download mod =

View File

@ -419,20 +419,18 @@ class ModManagementScreen: PickerScreen(disableScroll = true) {
val visualMods = game.settings.visualMods
val isVisual = visualMods.contains(mod.name)
modStateImages[mod.name]?.isVisual = isVisual
if (!isVisual) {
modActionTable.add("Enable as permanent visual mod".toTextButton().onClick {
val visualCheckBox = CheckBox("Permanent audiovisual mod", skin)
visualCheckBox.isChecked = isVisual
modActionTable.add(visualCheckBox)
visualCheckBox.onChange {
if (visualCheckBox.isChecked)
visualMods.add(mod.name)
game.settings.save()
ImageGetter.setNewRuleset(ImageGetter.ruleset)
refreshModActions(mod)
})
} else {
modActionTable.add("Disable as permanent visual mod".toTextButton().onClick {
else
visualMods.remove(mod.name)
game.settings.save()
ImageGetter.setNewRuleset(ImageGetter.ruleset)
refreshModActions(mod)
})
game.settings.save()
ImageGetter.setNewRuleset(ImageGetter.ruleset)
refreshModActions(mod)
}
modActionTable.row()
}
@ -502,4 +500,10 @@ class ModManagementScreen: PickerScreen(disableScroll = true) {
modStateImages.remove(mod.name)
refreshInstalledModTable()
}
override fun resize(width: Int, height: Int) {
if (stage.viewport.screenWidth != width || stage.viewport.screenHeight != height) {
game.setScreen(ModManagementScreen())
}
}
}

View File

@ -16,42 +16,66 @@ import java.io.File
* app lifetime.
*/
object Sounds {
private enum class SupportedExtensions { mp3, ogg, wav } // Gdx won't do aac/m4a
private val soundMap = HashMap<UncivSound, Sound?>()
private val separator = File.separator // just a shorthand for readability
private var modListHash = Int.MIN_VALUE
/** Ensure cache is not outdated _and_ build list of folders to look for sounds */
/** Ensure cache is not outdated */
private fun checkCache() {
if (!UncivGame.isCurrentInitialized()) return
val game = UncivGame.Current
// Get a hash covering all mods - quickly, so don't map cast or copy the Set types
val hash1 = if (game.isGameInfoInitialized()) game.gameInfo.ruleSet.mods.hashCode() else 0
val newHash = hash1.xor(game.settings.visualMods.hashCode())
// If hash the same, leave the cache as is
if (modListHash != Int.MIN_VALUE && modListHash == newHash) return
// Seems the mod list has changed - clear the cache
for (sound in soundMap.values) sound?.dispose()
soundMap.clear()
modListHash = newHash
}
/** Build list of folders to look for sounds */
private fun getFolders(): Sequence<String> {
if (!UncivGame.isCurrentInitialized() || !UncivGame.Current.isGameInfoInitialized()) // Allow sounds from main menu
if (!UncivGame.isCurrentInitialized())
// Sounds before main menu shouldn't happen, but just in case return a default ""
// which translates to the built-in assets/sounds folder
return sequenceOf("")
val game = UncivGame.Current
// Allow mod sounds - preferentially so they can override built-in sounds
val modList = UncivGame.Current.gameInfo.ruleSet.mods
val newHash = modList.hashCode()
if (modListHash == Int.MIN_VALUE || modListHash != newHash) {
// Seems the mod list has changed - start over
for (sound in soundMap.values) sound?.dispose()
soundMap.clear()
modListHash = newHash
}
// Should we also look in UncivGame.Current.settings.visualMods?
// audiovisual mods first, these are already available when game.gameInfo is not
val modList: MutableSet<String> = game.settings.visualMods
if (game.isGameInfoInitialized())
modList.addAll(game.gameInfo.ruleSet.mods) // Sounds from game mods
return modList.asSequence()
.map { "mods$separator$it$separator" } +
sequenceOf("")
sequenceOf("") // represents builtin sounds folder
}
fun get(sound: UncivSound): Sound? {
checkCache()
if (sound in soundMap) return soundMap[sound]
val fileName = sound.value
var file: FileHandle? = null
for (modFolder in getFolders()) {
val path = "${modFolder}sounds$separator$fileName.mp3"
for ( (modFolder, extension) in getFolders().flatMap {
folder -> SupportedExtensions.values().asSequence().map { folder to it }
} ) {
val path = "${modFolder}sounds$separator$fileName.${extension.name}"
file = Gdx.files.internal(path)
if (file.exists()) break
}
val newSound =
if (file == null || !file.exists()) null
else Gdx.audio.newSound(file)
// Store Sound for reuse or remember that the actual file is missing
soundMap[sound] = newSound
return newSound