implement 'TextureReplace' NetOp
This commit is contained in:
parent
972d199154
commit
67401817a0
|
@ -5,6 +5,7 @@ import javafx.scene.image.Image
|
||||||
import org.hso.texturesyncclient.model.Sha256
|
import org.hso.texturesyncclient.model.Sha256
|
||||||
import org.hso.texturesyncclient.model.Texture
|
import org.hso.texturesyncclient.model.Texture
|
||||||
import java.io.*
|
import java.io.*
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
import java.lang.RuntimeException
|
import java.lang.RuntimeException
|
||||||
import java.net.*
|
import java.net.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -214,17 +215,102 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class, ConnectionException::class, IllegalArgumentException::class)
|
||||||
|
@Synchronized
|
||||||
|
private fun replaceTexture(old: Texture?, new : Texture?, image : ByteArray? ) {
|
||||||
|
val io = getStreams()
|
||||||
|
|
||||||
|
val obj = JsonObject()
|
||||||
|
obj.add("replace_texture", {
|
||||||
|
val inner = JsonObject()
|
||||||
|
if(old != null) {
|
||||||
|
inner.add("old", Gson().toJsonTree(InternalTexture(old), InternalTexture::class.java))
|
||||||
|
} else {
|
||||||
|
inner.add("old", null)
|
||||||
|
}
|
||||||
|
if(new != null) {
|
||||||
|
inner.add("new", Gson().toJsonTree(InternalTexture(new), InternalTexture::class.java))
|
||||||
|
} else {
|
||||||
|
inner.add("new", null)
|
||||||
|
}
|
||||||
|
inner
|
||||||
|
}())
|
||||||
|
|
||||||
|
JsonPackage(obj).write(io.second)
|
||||||
|
|
||||||
|
when (val pkg = Package.read(io.first)) {
|
||||||
|
is JsonPackage -> {
|
||||||
|
if (pkg.content == JsonPrimitive(true)) {
|
||||||
|
// everthing is fine!
|
||||||
|
return
|
||||||
|
} else if (image != null) {
|
||||||
|
// should be { "get_texture_file": { texture_hash : <Hash> }}
|
||||||
|
// we don't check, since there is no good way to handle it.
|
||||||
|
|
||||||
|
BinaryPackage(image).write(io.second)
|
||||||
|
when (val ipkg = Package.read(io.first)) {
|
||||||
|
is JsonPackage -> {
|
||||||
|
if(ipkg.content != JsonPrimitive(true)) {
|
||||||
|
// Protokoll Assertion failed
|
||||||
|
throw ConnectionUnexpecedPacketException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is BinaryPackage -> throw ConnectionUnexpecedPacketException()
|
||||||
|
is ErrorPackage -> throw ConnectionErrorException(ipkg)
|
||||||
|
else -> throw RuntimeException("Unreachable")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ErrorPackage(404, "Texture not found!").write(io.second)
|
||||||
|
close() // gets re-opened on next request.
|
||||||
|
throw IllegalArgumentException("Image Argument was needed.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is BinaryPackage -> throw ConnectionUnexpecedPacketException()
|
||||||
|
is ErrorPackage -> throw ConnectionErrorException(pkg)
|
||||||
|
else -> throw RuntimeException("Unreachable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class, ConnectionException::class, IllegalArgumentException::class)
|
||||||
|
@Synchronized
|
||||||
|
fun uploadTexture(texture: Texture, image : ByteArray ) {
|
||||||
|
if(texture.textureHash != Sha256(image)) {
|
||||||
|
throw IllegalArgumentException("Sha256 of Image does not Match with Texture.")
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceTexture(null, texture, image)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class, ConnectionException::class, IllegalArgumentException::class)
|
||||||
|
@Synchronized
|
||||||
|
fun updateTexture(old: Texture, new : Texture, image : ByteArray ) {
|
||||||
|
if(new.textureHash != Sha256(image)) {
|
||||||
|
throw IllegalArgumentException("Sha256 of Image does not Match with Texture.")
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceTexture(old, new, image)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class, ConnectionException::class, IllegalArgumentException::class)
|
||||||
|
@Synchronized
|
||||||
|
fun deleteTexture(texture : Texture) {
|
||||||
|
replaceTexture(texture, null , null)
|
||||||
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
@Synchronized
|
@Synchronized
|
||||||
override fun close() {
|
override fun close() {
|
||||||
if (output != null) {
|
if (output != null) {
|
||||||
output!!.close()
|
output!!.close()
|
||||||
|
output = null
|
||||||
}
|
}
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
input!!.close()
|
input!!.close()
|
||||||
|
input = null
|
||||||
}
|
}
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
socket!!.close()
|
socket!!.close()
|
||||||
|
socket = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,4 +56,19 @@ class Sha256 {
|
||||||
}
|
}
|
||||||
return s.toString()
|
return s.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as Sha256
|
||||||
|
|
||||||
|
if (!hashBytes.contentEquals(other.hashBytes)) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return hashBytes.contentHashCode()
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue