diff --git a/README.md b/README.md index c193618..db1c495 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Implementation of Colyseus client using Kotlin ```groovy dependencies { - implementation 'io.github.doorbash:colyseus-kotlin:0.14.0-alpha.2' + implementation 'io.github.doorbash:colyseus-kotlin:0.14.0-alpha.3' } ``` diff --git a/build.gradle b/build.gradle index 82d8a1d..c33400a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.4.0' + ext.kotlin_version = '1.4.10' repositories { google() jcenter() @@ -18,7 +18,7 @@ apply plugin: 'com.novoda.bintray-release' apply plugin: 'kotlin' group 'io.github.doorbash' -version '0.14.0-alpha.2' +version '0.14.0-alpha.3' repositories { mavenCentral() @@ -38,7 +38,7 @@ dependencies { publish { def groupProjectID = 'io.github.doorbash' def artifactProjectID = 'colyseus-kotlin' - def publishVersionID = '0.14.0-alpha.2' + def publishVersionID = '0.14.0-alpha.3' userOrg = 'doorbash' repoName = 'io.github.doorbash' groupId = groupProjectID diff --git a/src/main/java/io/colyseus/Client.kt b/src/main/java/io/colyseus/Client.kt index f823216..5e76e50 100644 --- a/src/main/java/io/colyseus/Client.kt +++ b/src/main/java/io/colyseus/Client.kt @@ -187,13 +187,11 @@ class Client(private val endpoint: String) { @Throws(UnsupportedEncodingException::class) private fun buildEndpoint(room: JsonNode, options: LinkedHashMap): String { val charset = "UTF-8" - var i = 0 val params = StringBuilder() - for (name in options.keys) { + for ((i, name) in options.keys.withIndex()) { if (i > 0) params.append("&") params.append(URLEncoder.encode(name, charset)).append("=").append(URLEncoder.encode(options[name].toString(), charset)) - i++ } - return endpoint + "/" + room["processId"].asText() + "/" + room["roomId"].asText() + "?" + params.toString() + return "$endpoint/${room["processId"].asText()}/${room["roomId"].asText()}?$params" } } \ No newline at end of file diff --git a/src/main/java/io/colyseus/serializer/schema/Decoder.kt b/src/main/java/io/colyseus/serializer/schema/Decoder.kt index 26146ba..a244bdb 100644 --- a/src/main/java/io/colyseus/serializer/schema/Decoder.kt +++ b/src/main/java/io/colyseus/serializer/schema/Decoder.kt @@ -7,63 +7,76 @@ import java.nio.charset.StandardCharsets object Decoder { fun decodePrimitiveType(type: String?, bytes: ByteArray, it: Iterator): Any { return when (type) { - "string" -> return decodeString(bytes, it) - "number" -> return decodeNumber(bytes, it) - "int8" -> return decodeInt8(bytes, it) - "uint8" -> return decodeUint8(bytes, it) - "int16" -> return decodeInt16(bytes, it) - "uint16" -> return decodeUint16(bytes, it) - "int32" -> return decodeInt32(bytes, it) - "uint32" -> return decodeUint32(bytes, it) - "int64" -> return decodeInt64(bytes, it) - "uint64" -> return decodeUint64(bytes, it) - "float32" -> return decodeFloat32(bytes, it) - "float64" -> return decodeFloat64(bytes, it) - "boolean" -> return decodeBoolean(bytes, it) - else -> return Any() + "string" -> decodeString(bytes, it) + "number" -> decodeNumber(bytes, it) + "int8" -> decodeInt8(bytes, it) + "uint8" -> decodeUint8(bytes, it) + "int16" -> decodeInt16(bytes, it) + "uint16" -> decodeUint16(bytes, it) + "int32" -> decodeInt32(bytes, it) + "uint32" -> decodeUint32(bytes, it) + "int64" -> decodeInt64(bytes, it) + "uint64" -> decodeUint64(bytes, it) + "float32" -> decodeFloat32(bytes, it) + "float64" -> decodeFloat64(bytes, it) + "boolean" -> decodeBoolean(bytes, it) + else -> Any() } } fun decodeNumber(bytes: ByteArray, it: Iterator): Float { val prefix: Int = bytes[it.offset++].toInt() and 0xFF - if (prefix < 128) { - // positive fixint - return prefix.toFloat() - } else if (prefix == 0xca) { - // float 32 - return decodeFloat32(bytes, it) - } else if (prefix == 0xcb) { - // float 64 - return decodeFloat64(bytes, it).toFloat() - } else if (prefix == 0xcc) { - // uint 8 - return decodeUint8(bytes, it).toFloat() - } else if (prefix == 0xcd) { - // uint 16 - return decodeUint16(bytes, it).toFloat() - } else if (prefix == 0xce) { - // uint 32 - return decodeUint32(bytes, it).toFloat() - } else if (prefix == 0xcf) { - // uint 64 - return decodeUint64(bytes, it).toFloat() - } else if (prefix == 0xd0) { - // int 8 - return decodeInt8(bytes, it).toFloat() - } else if (prefix == 0xd1) { - // int 16 - return decodeInt16(bytes, it).toFloat() - } else if (prefix == 0xd2) { - // int 32 - return decodeInt32(bytes, it).toFloat() - } else if (prefix == 0xd3) { - // int 64 - return decodeInt64(bytes, it).toFloat() - } else if (prefix > 0xdf) { - // negative fixint - return ((0xff - prefix + 1) * -1).toFloat() + when { + prefix < 128 -> { + // positive fixint + return prefix.toFloat() + } + prefix == 0xca -> { + // float 32 + return decodeFloat32(bytes, it) + } + prefix == 0xcb -> { + // float 64 + return decodeFloat64(bytes, it).toFloat() + } + prefix == 0xcc -> { + // uint 8 + return decodeUint8(bytes, it).toFloat() + } + prefix == 0xcd -> { + // uint 16 + return decodeUint16(bytes, it).toFloat() + } + prefix == 0xce -> { + // uint 32 + return decodeUint32(bytes, it).toFloat() + } + prefix == 0xcf -> { + // uint 64 + return decodeUint64(bytes, it).toFloat() + } + prefix == 0xd0 -> { + // int 8 + return decodeInt8(bytes, it).toFloat() + } + prefix == 0xd1 -> { + // int 16 + return decodeInt16(bytes, it).toFloat() + } + prefix == 0xd2 -> { + // int 32 + return decodeInt32(bytes, it).toFloat() + } + prefix == 0xd3 -> { + // int 64 + return decodeInt64(bytes, it).toFloat() + } + prefix > 0xdf -> { + // negative fixint + return ((0xff - prefix + 1) * -1).toFloat() + } + else -> return Float.NaN } - return Float.NaN } fun decodeInt8(bytes: ByteArray, it: Iterator): Byte { @@ -130,15 +143,20 @@ object Decoder { fun decodeString(bytes: ByteArray, it: Iterator): String { val prefix: Int = bytes[it.offset++].toInt() and 0xFF var length = 0 - if (prefix < 0xc0) { - // fixstr - length = prefix and 0x1f - } else if (prefix == 0xd9) { - length = decodeUint8(bytes, it).toInt() - } else if (prefix == 0xda) { - length = decodeUint16(bytes, it) - } else if (prefix == 0xdb) { - length = decodeUint32(bytes, it).toInt() + when { + prefix < 0xc0 -> { + // fixstr + length = prefix and 0x1f + } + prefix == 0xd9 -> { + length = decodeUint8(bytes, it).toInt() + } + prefix == 0xda -> { + length = decodeUint16(bytes, it) + } + prefix == 0xdb -> { + length = decodeUint32(bytes, it).toInt() + } } val _bytes = ByteArray(length) System.arraycopy(bytes, it.offset, _bytes, 0, length)