diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b4b4622..0617c9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,9 +14,17 @@ jobs: name: Build strategy: - fail-fast: false + fail-fast: true matrix: - os: [windows-latest, ubuntu-latest, macos-latest] + include: + - target: windows + os: windows-latest + - target: linux + os: ubuntu-latest + - target: macos + os: macos-latest + - target: jvm + os: ubuntu-latest runs-on: ${{ matrix.os }} steps: @@ -29,11 +37,22 @@ jobs: distribution: 'zulu' java-version: '16' - - name: Build Project - run: ./gradlew build -x test - - env: - ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }} - ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_NEXUS_PASSWORD }} - ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.SIGNING_PRIVATE_KEY }} - ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }} \ No newline at end of file + - uses: gradle/actions/setup-gradle@v3 + + - name: Publish to Maven Central + run: | + if [ ${{ matrix.target }} == jvm ]; then + ./gradlew compileKotlinJvm + else + case ${{ matrix.os }} in + windows-latest) + ./gradlew compileKotlinMingwX64 + ;; + ubuntu-latest) + ./gradlew compileKotlinLinuxX64 + ;; + macos-latest) + ./gradlew compileKotlinMacosX64 + ;; + esac + fi diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 08a611c..0c471c6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -53,7 +53,7 @@ jobs: ./gradlew publishMingwPublicationToMavenCentral ;; ubuntu-latest) - ./gradlew publishLinuxX64PublicationToMavenCentral publishLinuxArm64PublicationToMavenCentral publishJvmPublicationToMavenCentral + ./gradlew publishLinuxX64PublicationToMavenCentral publishLinuxArm64PublicationToMavenCentral ;; macos-latest) ./gradlew publishMacosX64PublicationToMavenCentral publishMacosArm64PublicationToMavenCentral @@ -64,4 +64,4 @@ jobs: ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }} ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_NEXUS_PASSWORD }} ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.SIGNING_PRIVATE_KEY }} - ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }} \ No newline at end of file + ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }} diff --git a/src/mingwX64Main/kotlin/io/github/reblast/kpresence/ipc/Connection.kt b/src/mingwX64Main/kotlin/io/github/reblast/kpresence/ipc/Connection.kt index 89af685..f2ef895 100644 --- a/src/mingwX64Main/kotlin/io/github/reblast/kpresence/ipc/Connection.kt +++ b/src/mingwX64Main/kotlin/io/github/reblast/kpresence/ipc/Connection.kt @@ -3,16 +3,16 @@ package io.github.reblast.kpresence.ipc import io.github.reblast.kpresence.utils.putInt import io.github.reblast.kpresence.utils.reverseBytes import kotlinx.cinterop.* -import platform.posix.* +import platform.windows.* actual class Connection { - private var pipe = -1 + private var pipe: HANDLE? = null actual fun open() { for (i in 0..9) { - val pipeHandle = open("\\\\.\\pipe\\discord-ipc-$i", O_RDWR) + val pipeHandle = CreateFileW("\\\\.\\pipe\\discord-ipc-$i", GENERIC_READ or GENERIC_WRITE.convert(), 0u, null, OPEN_EXISTING.convert(), 0u, null) - if (pipeHandle == -1) continue + if (pipeHandle == INVALID_HANDLE_VALUE) continue else { pipe = pipeHandle return @@ -22,29 +22,39 @@ actual class Connection { throw RuntimeException("Could not connect to the pipe!") } - actual fun read(bufferSize: Int): ByteArray { - if (pipe == -1) throw IllegalStateException("Not connected") - - val buffer = ByteArray(bufferSize) - val bytesRead = read(pipe, buffer.refTo(0), buffer.size.convert()) - - return buffer.copyOf(bytesRead) + actual fun read(bufferSize: Int): ByteArray = memScoped { + pipe?.let { handle -> + val buffer = ByteArray(bufferSize) + val bytesRead = alloc() + + val success = buffer.usePinned { + ReadFile(handle, it.addressOf(0), bufferSize.convert(), bytesRead.ptr, null) + } + + if (success == FALSE) throw RuntimeException("Error reading from socket") + + return buffer.copyOf(bytesRead.value.toInt()) + } ?: throw IllegalStateException("Not connected") } actual fun write(opcode: Int, data: String) { - if (pipe == -1) throw IllegalStateException("Not connected") - - val bytes = data.encodeToByteArray() - val buffer = ByteArray(bytes.size + 8) - - buffer.putInt(opcode.reverseBytes()) - buffer.putInt(bytes.size.reverseBytes(), 4) - bytes.copyInto(buffer, 8) - - write(pipe, buffer.refTo(0), buffer.size.toUInt()) + pipe?.let { handle -> + val bytes = data.encodeToByteArray() + val buffer = ByteArray(bytes.size + 8) + + buffer.putInt(opcode.reverseBytes()) + buffer.putInt(bytes.size.reverseBytes(), 4) + bytes.copyInto(buffer, 8) + + val success = buffer.usePinned { + WriteFile(handle, it.addressOf(0), buffer.size.convert(), null, null) + } + + if (success == FALSE) throw RuntimeException("Error reading from socket") + } ?: throw IllegalStateException("Not connected") } actual fun close() { - close(pipe) + CloseHandle(pipe) } } \ No newline at end of file