Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
doorbash committed Oct 4, 2020
1 parent b81dc4e commit 6154807
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.4.0'
ext.kotlin_version = '1.4.10'
repositories {
google()
jcenter()
Expand All @@ -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()
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/colyseus/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,11 @@ class Client(private val endpoint: String) {
@Throws(UnsupportedEncodingException::class)
private fun buildEndpoint(room: JsonNode, options: LinkedHashMap<String, Any?>): 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"
}
}
138 changes: 78 additions & 60 deletions src/main/java/io/colyseus/serializer/schema/Decoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6154807

Please sign in to comment.