Android: Fix off-by-one error in playlist.txt

Before this, line k of the playlist file corresponded to track k-1 on
Android. After this, line k corresponds to track k, which is consistent
with other platforms.

Playlists generated in previous versions of system3-sdl2 will be
automatically converted.
This commit is contained in:
kichikuou
2022-03-11 17:05:57 +09:00
parent 8a6a81b334
commit 82ce12e82c
2 changed files with 20 additions and 4 deletions
@@ -196,7 +196,7 @@ private class CddaPlayer(private val playlistPath: File) {
private var playerPaused = false
fun start(track: Int, loop: Boolean) {
val f = playlist.elementAtOrNull(track)
val f = playlist.elementAtOrNull(track - 1)
if (f.isNullOrEmpty()) {
Log.w("cddaStart", "No playlist entry for track $track")
return
@@ -46,7 +46,8 @@ private const val FAILURE = 2
class Launcher private constructor(private val rootDir: File) {
companion object {
const val TITLE_FILE = "title.txt"
const val PLAYLIST_FILE = "playlist.txt"
const val PLAYLIST_FILE = "playlist2.txt"
const val OLD_PLAYLIST_FILE = "playlist.txt"
fun getInstance(rootDir: File): Launcher {
if (gLauncher == null) {
@@ -121,6 +122,7 @@ class Launcher private constructor(private val rootDir: File) {
val titleFile = File(path, TITLE_FILE)
val title = titleFile.readText()
games.add(Entry(path, title, titleFile.lastModified()))
migratePlaylist(path)
} catch (e: IOException) {
// Incomplete game installation. Delete it.
path.deleteRecursively()
@@ -207,6 +209,20 @@ class Launcher private constructor(private val rootDir: File) {
}
}
// System3-sdl2 <=0.7.0 had a bug where playlist had an extra empty line at
// the beginning of the file. This migrates playlist.txt created by an old
// version of system3-sdl2 to playlist2.txt.
private fun migratePlaylist(dir: File) {
val oldPlaylist = File(dir, OLD_PLAYLIST_FILE)
if (!oldPlaylist.exists())
return
var tracks = oldPlaylist.readLines()
if (!tracks.isEmpty())
tracks = tracks.subList(1, tracks.size)
File(dir, PLAYLIST_FILE).writeText(tracks.joinToString("\n"))
oldPlaylist.delete()
}
class InstallFailureException(val msgId: Int) : Exception()
// A helper class which generates playlist.txt in the game root directory.
@@ -223,8 +239,8 @@ class Launcher private constructor(private val rootDir: File) {
}
audioRegex.matchEntire(name)?.let {
val track = it.groupValues[1].toInt()
if (track < audioFiles.size)
audioFiles[track] = path
if (0 < track && track <= audioFiles.size)
audioFiles[track - 1] = path
}
}