Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite using Windows API. #7

Merged
merged 2 commits into from
Apr 6, 2024
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
39 changes: 29 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
- 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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
./gradlew publishMingwPublicationToMavenCentral
;;
ubuntu-latest)
./gradlew publishLinuxX64PublicationToMavenCentral publishLinuxArm64PublicationToMavenCentral publishJvmPublicationToMavenCentral
./gradlew publishLinuxX64PublicationToMavenCentral publishLinuxArm64PublicationToMavenCentral
;;
macos-latest)
./gradlew publishMacosX64PublicationToMavenCentral publishMacosArm64PublicationToMavenCentral
Expand All @@ -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 }}
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UIntVar>()

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)
}
}