mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
33 lines
925 B
Kotlin
33 lines
925 B
Kotlin
package net.torvald.terrarum.serialise
|
|
|
|
import net.torvald.terrarum.AppLoader
|
|
import java.io.File
|
|
import java.io.FileInputStream
|
|
|
|
|
|
|
|
object SavegameLedger {
|
|
|
|
private val SAVE_DIRECTORY = File(AppLoader.defaultSaveDir)
|
|
|
|
fun hasSavegameDirectory() = SAVE_DIRECTORY.exists() && SAVE_DIRECTORY.isDirectory
|
|
|
|
private fun peekFewBytes(file: File, length: Int): ByteArray {
|
|
val buffer = ByteArray(length)
|
|
val `is` = FileInputStream(file)
|
|
if (`is`.read(buffer) != buffer.size) {
|
|
throw InternalError()
|
|
}
|
|
`is`.close()
|
|
return buffer
|
|
}
|
|
private val MAGIC_TEVD = "TEVd".toByteArray()
|
|
|
|
fun getSavefileList(): List<File>? {
|
|
return if (!hasSavegameDirectory()) null
|
|
else SAVE_DIRECTORY.listFiles().filter { it.isFile && peekFewBytes(it, 4) contentEquals MAGIC_TEVD }
|
|
}
|
|
|
|
fun getSavefileCount() = getSavefileList()?.count() ?: 0
|
|
|
|
} |