diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt index d08146816..718a4137d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt @@ -365,73 +365,3 @@ fun OutputStream.writeBinaryString(input: String) { writeVarIntLE(bytes.size) write(bytes) } - -private data class IndexedStreamCodec( - val condition: Predicate, - val id: Int, - val codec: IStreamCodec -) { - fun read(stream: DataInputStream, sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): T { - return codec.read(stream, sizeLimit) - } - - fun write(stream: DataOutputStream, value: Any?) { - codec.write(stream, value as T) - } -} - -private inline fun IndexedStreamCodec(id: Int, codec: StreamCodec): IndexedStreamCodec { - return IndexedStreamCodec({ it is T }, id, codec) -} - -private val codecs: List> = com.google.common.collect.ImmutableList.builder>().let { - var codecID = 0 - - it.add(IndexedStreamCodec({ it == null }, codecID++, NullValueCodec)) - - it.add(IndexedStreamCodec(codecID++, BooleanValueCodec)) - it.add(IndexedStreamCodec(codecID++, ByteValueCodec)) - it.add(IndexedStreamCodec(codecID++, ShortValueCodec)) - it.add(IndexedStreamCodec(codecID++, VarIntValueCodec)) - it.add(IndexedStreamCodec(codecID++, VarLongValueCodec)) - it.add(IndexedStreamCodec(codecID++, FloatValueCodec)) - it.add(IndexedStreamCodec(codecID++, DoubleValueCodec)) - it.add(IndexedStreamCodec(codecID++, ItemStackValueCodec)) - it.add(IndexedStreamCodec(codecID++, DecimalValueCodec)) - it.add(IndexedStreamCodec(codecID++, BigDecimalValueCodec)) - it.add(IndexedStreamCodec(codecID++, UUIDValueCodec)) - it.add(IndexedStreamCodec(codecID++, BinaryStringCodec)) - - it.build() -} - -class NotSerializableValueException(message: String) : Exception(message) - -/** - * Write arbitrary data to this stream, in exploit-free way - */ -fun DataOutputStream.writeType(value: Any?) { - for (codec in codecs) { - if (codec.condition.test(value)) { - write(codec.id) - codec.write(this, value) - return - } - } - - throw NotSerializableValueException("Value $value <${value?.let { it::class.qualifiedName }}> can not be serialized") -} - -/** - * Read arbitrary data from this stream, in exploit-free way - */ -fun DataInputStream.readType(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): Any? { - sizeLimit.accountBytes(1L) - val id = read() - - if (id >= codecs.size) { - throw IndexOutOfBoundsException("No codec for network type $id") - } - - return codecs[id].read(this, sizeLimit) -}