Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ dependencies {
implementation("org.ow2.asm:asm-commons:9.2")
implementation("org.ow2.asm:asm-tree:9.2")
implementation("org.snakeyaml:snakeyaml-engine:2.3")

testImplementation(platform("io.kotest:kotest-bom:4.6.3"))
testImplementation("io.kotest:kotest-framework-api")
testImplementation("io.kotest:kotest-assertions-core")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
testRuntimeOnly(platform("io.kotest:kotest-bom:4.6.3"))
testRuntimeOnly("io.kotest:kotest-runner-junit5")
}

tasks.jar {
Expand All @@ -41,6 +48,10 @@ tasks.jar {
from(provider { zipTree(shadowJar.archiveFile) })
}

tasks.test {
useJUnitPlatform()
}

gradlePlugin.isAutomatedPublishing = false

pluginBundle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.anatawa12.modPatching.binary

import com.anatawa12.modPatching.binary.internal.readFully
import com.anatawa12.modPatching.binary.internal.isSameDataStream
import com.anatawa12.modPatching.source.internal.readTextOr
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFile
Expand Down Expand Up @@ -67,30 +67,6 @@ open class ListModifiedClasses : DefaultTask() {
file.asFile.apply { parentFile.mkdirs() }.writeText(body)
}

private fun isSameDataStream(head: InputStream, streams: List<InputStream>): Boolean {
if (streams.isEmpty()) return true

val buf0 = ByteArray(1024)
val buf1 = ByteArray(1024)
var size0: Int
var size1: Int
while (true) {
size0 = head.readFully(buf0)

for (stream1 in streams) {
size1 = stream1.readFully(buf1)
// size changed (in stream): not same
if (size0 != size1) return false
for (i in 0 until size0) {
if (buf0[i] != buf1[i]) return false
}
}

// all elements are read and same: same stream
if (size0 == -1) return true
}
}

private class MultiClose<C : Closeable>(private val internals: List<C>) : Closeable, List<C> by internals {
override fun close() {
var exception: Throwable? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,33 @@ fun InputStream.readFully(buf: ByteArray): Int {
var cursor = 0
while (cursor != buf.size) {
val read = read(buf, cursor, buf.size - cursor)
if (read == -1) return -1
if (read == -1) break
cursor += read
}
if (cursor == 0) return -1
return cursor// == buf.size
}

fun isSameDataStream(stream0: InputStream, stream1: InputStream): Boolean {
fun isSameDataStream(head: InputStream, streams: List<InputStream>): Boolean {
if (streams.isEmpty()) return true

val buf0 = ByteArray(1024)
val buf1 = ByteArray(1024)
var size0: Int
var size1: Int
while (true) {
size0 = stream0.readFully(buf0)
size1 = stream1.readFully(buf1)
// size changed (in stream): not same
if (size0 != size1) return false
size0 = head.readFully(buf0)

for (stream1 in streams) {
size1 = stream1.readFully(buf1)
// size changed (in stream): not same
if (size0 != size1) return false
for (i in 0 until size0) {
if (buf0[i] != buf1[i]) return false
}
}

// all elements are read and same: same stream
if (size0 == -1) return true
for (i in 0 until size0) {
if (buf0[i] != buf1[i]) return false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.anatawa12.modPatching.binary.internal

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.io.ByteArrayInputStream

class UtilKtTest : FunSpec({
context("readFully") {
test("small byte array") {
val bytes = ByteArray(128) { it.toByte() }
val buffer = ByteArray(1024)
ByteArrayInputStream(bytes).readFully(buffer) shouldBe bytes.size
buffer.copyOf(128) shouldBe bytes
}

test("long byte array") {
val bytes = ByteArray(1024 + 128) { it.toByte() }
val buffer = ByteArray(1024)
val bais = ByteArrayInputStream(bytes)
bais.readFully(buffer) shouldBe 1024
buffer shouldBe bytes.copyOf(1024)

bais.readFully(buffer) shouldBe 128
buffer.copyOf(128) shouldBe bytes.copyOfRange(1024, 1024 + 128)
}
}

context("isSameDataStream") {
test("small same") {
val bytes = ByteArray(128) { it.toByte() }
val bais0 = ByteArrayInputStream(bytes)
val bais1 = ByteArrayInputStream(bytes)
isSameDataStream(bais0, listOf(bais1)) shouldBe true
}

test("long same") {
val bytes = ByteArray(1024 + 128) { it.toByte() }
val bais0 = ByteArrayInputStream(bytes)
val bais1 = ByteArrayInputStream(bytes)
isSameDataStream(bais0, listOf(bais1)) shouldBe true
}

test("small difference") {
val bytes0 = ByteArray(128) { it.toByte() }
val bytes1 = ByteArray(128) { it.toByte() }
bytes1[120] = 11
val bais0 = ByteArrayInputStream(bytes0)
val bais1 = ByteArrayInputStream(bytes1)
isSameDataStream(bais0, listOf(bais1)) shouldBe false
}

test("long difference at head") {
val bytes0 = ByteArray(1024 + 128) { it.toByte() }
val bytes1 = ByteArray(1024 + 128) { it.toByte() }
bytes1[120] = 11
val bais0 = ByteArrayInputStream(bytes0)
val bais1 = ByteArrayInputStream(bytes1)
isSameDataStream(bais0, listOf(bais1)) shouldBe false
}

test("long difference at last") {
val bytes0 = ByteArray(1024 + 128) { it.toByte() }
val bytes1 = ByteArray(1024 + 128) { it.toByte() }
bytes1[1024 + 120] = 11
val bais0 = ByteArrayInputStream(bytes0)
val bais1 = ByteArrayInputStream(bytes1)
isSameDataStream(bais0, listOf(bais1)) shouldBe false
}
}
})