73 lines
2.3 KiB
Kotlin
73 lines
2.3 KiB
Kotlin
package org.hso.texturesyncclient.controller.net
|
|
|
|
// These types will be converted to the model.DataModel
|
|
|
|
import org.hso.texturesyncclient.model.Texture
|
|
import org.hso.texturesyncclient.model.TextureFormat
|
|
import java.lang.Exception
|
|
import java.util.*
|
|
|
|
@Suppress("ArrayInDataClass")
|
|
internal data class InternalTexture(
|
|
val id: String,
|
|
val name: String,
|
|
val tags: Array<String>,
|
|
val format: String,
|
|
val resolution: Array<Int>,
|
|
val added_on: Array<Int>,
|
|
val texture_hash: String
|
|
) {
|
|
|
|
constructor(tex: Texture) : this(
|
|
id = tex.id.toString(),
|
|
name = tex.name,
|
|
tags = tex.tags.clone(),
|
|
format = when (tex.format) {
|
|
TextureFormat.PNG -> "png"
|
|
TextureFormat.JPEG -> "jpeg"
|
|
},
|
|
resolution = arrayOf(tex.resolution.first, tex.resolution.second),
|
|
added_on = arrayOf(
|
|
tex.addedOn.get(Calendar.YEAR), //
|
|
tex.addedOn.get(Calendar.MONTH), //
|
|
tex.addedOn.get(Calendar.DAY_OF_MONTH)
|
|
),
|
|
texture_hash = bytes2hexString(tex.textureHash)
|
|
)
|
|
|
|
@Throws(ConnectionException::class)
|
|
fun toTexture(): Texture {
|
|
try {
|
|
return Texture(
|
|
id = UUID.fromString(id),
|
|
name = name,
|
|
tags = tags.clone(),
|
|
format = when (format.toLowerCase()) {
|
|
"jpeg" -> TextureFormat.JPEG
|
|
"jpg" -> TextureFormat.JPEG
|
|
"png" -> TextureFormat.PNG
|
|
else -> throw ConnectionInvalidJsonException()
|
|
},
|
|
resolution = Pair(resolution[0], resolution[1]),
|
|
addedOn = GregorianCalendar(added_on[0], added_on[1], added_on[2]),
|
|
textureHash = hexString2Bytes(texture_hash)
|
|
)
|
|
} catch (e: Exception) { // i Know, but no time :[]
|
|
throw ConnectionInvalidJsonException()
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun bytes2hexString(bytes: ByteArray): String {
|
|
val s = StringBuilder(64)
|
|
for (byte in bytes) {
|
|
s.append(String.format("%02X"), byte)
|
|
}
|
|
return s.toString()
|
|
}
|
|
|
|
private fun hexString2Bytes(bytes: String): ByteArray {
|
|
return ByteArray(bytes.length / 2) { i ->
|
|
bytes.substring(i * 2, i * 2 + 2).toInt(16).toByte()
|
|
}
|
|
} |