Open-source multiplayer game server compatible with the RuneScape client https://www.openrs2.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
openrs2/cache/src/main/kotlin/org/openrs2/cache/ChecksumTable.kt

69 lines
1.8 KiB

package org.openrs2.cache
import io.netty.buffer.ByteBuf
import org.openrs2.buffer.crc32
import org.openrs2.buffer.use
public class ChecksumTable(
public val entries: MutableList<Int> = mutableListOf()
) {
public fun write(buf: ByteBuf) {
for (entry in entries) {
buf.writeInt(entry)
}
var checksum = 1234
for (entry in entries) {
checksum = (checksum shl 1) + entry
}
buf.writeInt(checksum)
}
public companion object {
@JvmStatic
public fun create(store: Store): ChecksumTable {
val table = ChecksumTable()
var nextArchive = 0
for (archive in store.list(0)) {
val entry = try {
store.read(0, archive).use { buf ->
buf.crc32()
}
} catch (ex: StoreCorruptException) {
// see the equivalent comment in Js5MasterIndex::create
continue
}
for (i in nextArchive until archive) {
table.entries += 0
}
table.entries += entry
nextArchive = archive + 1
}
return table
}
@JvmStatic
public fun read(buf: ByteBuf): ChecksumTable {
val table = ChecksumTable()
var expectedChecksum = 1234
while (buf.readableBytes() >= 8) {
val entry = buf.readInt()
table.entries += entry
expectedChecksum = (expectedChecksum shl 1) + entry
}
val actualChecksum = buf.readInt()
require(expectedChecksum == actualChecksum)
return table
}
}
}