Skip to content

Commit 9ed512b

Browse files
committed
Fix keyboard listener & Delay relaunch
1 parent 4819aa3 commit 9ed512b

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

client/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
alias(libs.plugins.buildconfig)
99
}
1010

11-
version = "1.5.0"
11+
version = "1.6.0"
1212

1313
repositories {
1414
mavenCentral()

client/src/main/kotlin/Launcher.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,50 @@ import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.window.Window
1414
import androidx.compose.ui.window.application
1515
import dev.schlaubi.mastermind.core.Updater
16+
import dev.schlaubi.mastermind.core.registerKeyboardHandler
1617
import dev.schlaubi.mastermind.core.settings.settings
1718
import dev.schlaubi.mastermind.core.settings.writeSettings
1819
import dev.schlaubi.mastermind.resources.Res
1920
import dev.schlaubi.mastermind.resources.icon
2021
import dev.schlaubi.mastermind.theme.AppTheme
2122
import dev.schlaubi.mastermind.ui.GTAKiller
23+
import dev.schlaubi.mastermind.util.Loom
2224
import dev.schlaubi.mastermind.windows_helper.GTAVersion
25+
import dev.schlaubi.mastermind.windows_helper.WindowsAPI
2326
import io.github.oshai.kotlinlogging.KotlinLogging
27+
import kotlinx.coroutines.Dispatchers
28+
import kotlinx.coroutines.launch
2429
import org.jetbrains.compose.resources.painterResource
30+
import java.lang.foreign.Arena
2531

2632
private val LOG = KotlinLogging.logger { }
2733

2834
fun main() = application {
2935
Updater()
3036
LaunchedEffect(Unit) {
31-
val detectedVersion = GTAVersion.systemDefault()
32-
LOG.info { "Detected GTA version: $detectedVersion" }
33-
34-
if (detectedVersion != null) {
35-
writeSettings(settings.copy(gtaVersion = detectedVersion))
36-
}
3737
}
3838
Window(title = "GTA Killer", icon = painterResource(Res.drawable.icon), onCloseRequest = ::exitApplication) {
3939
var loading by remember { mutableStateOf(true) }
40+
val scope = rememberCoroutineScope()
41+
42+
DisposableEffect(Unit) {
43+
val arena = Arena.ofShared()
44+
scope.launch(Dispatchers.Loom) {
45+
WindowsAPI.registerKeyboardHook()
46+
registerKeyboardHandler(arena)
47+
48+
val detectedVersion = GTAVersion.systemDefault()
49+
LOG.info { "Detected GTA version: $detectedVersion" }
50+
51+
if (detectedVersion != null) {
52+
writeSettings(settings.copy(gtaVersion = detectedVersion))
53+
}
54+
55+
loading = false
56+
}
57+
58+
onDispose { arena.close() }
59+
}
4060
if (loading) {
4161
AppTheme {
4262
Scaffold {

client/src/main/kotlin/core/KeyBoardListener.kt

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package dev.schlaubi.mastermind.core
22

3-
import androidx.compose.runtime.Composable
4-
import androidx.compose.runtime.DisposableEffect
5-
import androidx.compose.runtime.SideEffect
63
import dev.schlaubi.mastermind.core.settings.settings
74
import dev.schlaubi.mastermind.util.Loom
85
import dev.schlaubi.mastermind.windows_helper.WindowsAPI
96
import kotlinx.coroutines.Dispatchers
107
import kotlinx.coroutines.runBlocking
118
import java.lang.foreign.Arena
129

13-
@Composable
14-
fun KeyBoardListener() {
15-
SideEffect { WindowsAPI.registerKeyboardHook() }
16-
DisposableEffect(Unit) {
17-
val arena = Arena.ofConfined()
18-
19-
WindowsAPI.registerKeyboardListener(arena) { keyCode ->
20-
if (keyCode == settings.hotkey) {
21-
runBlocking(Dispatchers.Loom) {
22-
reportAndKill()
23-
}
10+
fun registerKeyboardHandler(arena: Arena) {
11+
WindowsAPI.registerKeyboardListener(arena) { keyCode ->
12+
if (keyCode == settings.hotkey) {
13+
runBlocking(Dispatchers.Loom) {
14+
reportAndKill()
2415
}
2516
}
26-
27-
onDispose {
28-
arena.close()
29-
}
3017
}
3118
}
3219

client/src/main/kotlin/core/ProcessHandler.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import dev.schlaubi.mastermind.core.settings.settings
44
import dev.schlaubi.mastermind.windows_helper.WindowsAPI
55
import io.github.oshai.kotlinlogging.KotlinLogging
66
import kotlinx.coroutines.channels.Channel
7+
import kotlinx.coroutines.delay
78
import kotlinx.coroutines.flow.MutableSharedFlow
89
import kotlinx.coroutines.flow.asSharedFlow
910
import kotlin.io.path.absolutePathString
1011
import kotlin.io.path.div
1112
import kotlin.jvm.optionals.getOrNull
13+
import kotlin.time.Duration.Companion.seconds
1214

1315
private val LOG = KotlinLogging.logger { }
1416

@@ -25,6 +27,8 @@ suspend fun killGta() {
2527
if (gtaProcess.isPresent) {
2628
gtaProcess.get().destroyForcibly()
2729

30+
delay(2.seconds)
31+
2832
if (settings.autostartGta) {
2933
LOG.info { "Restarting GTA5.exe" }
3034
val gtaPath = WindowsAPI.readGtaLocation(version) / version.startBinary

0 commit comments

Comments
 (0)