Skip to content

Instantly share code, notes, and snippets.

@Royal724
Last active August 22, 2024 11:00
Show Gist options
  • Save Royal724/a3f35ef61bb74007aff93f058b15f814 to your computer and use it in GitHub Desktop.
Save Royal724/a3f35ef61bb74007aff93f058b15f814 to your computer and use it in GitHub Desktop.
Dat for FMM24
package com.example.datedit.data
import android.content.Context
import android.net.Uri
import java.io.BufferedInputStream
import java.io.File
import java.io.InputStream
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.time.LocalDate
import java.time.format.DateTimeFormatter
enum class Type {
Byte, Int16, Int32
}
class DatReader(filename: String, context: Context) {
private val stream: InputStream = BufferedInputStream(File(context.filesDir, filename).inputStream())
fun readColors(count: Int): List<String> {
val list = mutableListOf<String>()
repeat(count) {
val color = readInt16().toString(16)
list.add("0".repeat(6 - color.length) + color)
}
return list
}
fun readNBytes(len: Int): ByteArray {
val result = ByteArray(len)
stream.read(result)
return result
}
fun readInt32(): Int {
return ByteBuffer.wrap(readNBytes(4)).order(ByteOrder.LITTLE_ENDIAN).int
}
fun readInt16(): Int {
val n = ByteBuffer.wrap(readNBytes(2)).order(ByteOrder.LITTLE_ENDIAN).short.toInt()
return if (n < 0) 65536 + n else n
}
fun readByte(): Int {
val byte = stream.read()
return if (byte < 0) 256 + byte else byte
}
fun readString(): String {
return readNBytes(readInt32()).toString(Charsets.UTF_8)
}
fun readInt(size: Type): Int {
return when (size) {
Type.Byte -> readByte()
Type.Int16 -> readInt16()
Type.Int32 -> readInt32()
}
}
fun readList(sizeLen: Type, elementLen: Type): List<Int> {
val list = mutableListOf<Int>()
val size = readInt(sizeLen)
repeat(size) {
list.add(readInt(elementLen))
}
return list
}
fun readListList(sizeLen: Type, elementLens: List<Type>): List<List<Int>> {
val list = mutableListOf<List<Int>>()
val size = readInt(sizeLen)
repeat(size) {
val element = mutableListOf<Int>()
for (len in elementLens) {
element.add(readInt(len))
}
list.add(element)
}
return list
}
fun readDate(): String {
val day = readInt16()
val year = readInt16()
val date = LocalDate.of(year, 1, 1).plusDays(day.toLong())
return date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))
}
fun readPartner(): List<List<String>> {
val list = mutableListOf<List<String>>()
val size = readInt16()
repeat(size) {
list.add(
listOf(
readInt32().toString(),
readInt32().toString(),
readInt32().toString(),
readDate(),
readDate(),
readByte().toString()
)
)
}
return list
}
fun close() {
stream.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment