Skip to content

Commit 2585c4d

Browse files
committed
Gta install location improvements
- Propagate rust errors to JVM - Use steam paths
1 parent 32a1dbc commit 2585c4d

File tree

7 files changed

+64
-20
lines changed

7 files changed

+64
-20
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.6.4"
11+
version = "1.6.5"
1212

1313
repositories {
1414
mavenCentral()

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ suspend fun killGta() {
3434

3535
delay(2.seconds)
3636

37-
if (settings.autostartGta) {
38-
LOG.info { "Restarting GTA5.exe" }
39-
try {
40-
val gtaPath = WindowsAPI.readGtaLocation(version) / version.startBinary
41-
Runtime.getRuntime().exec(arrayOf(gtaPath.absolutePathString()))
42-
} catch (e: Exception) {
43-
_events.emit(Event.RestartError(e))
44-
}
45-
}
4637
} else {
4738
LOG.error { "GTA5.exe not found" }
4839
_events.emit(Event.GtaProcessNotFound(version.process))
4940
}
41+
42+
if (settings.autostartGta) {
43+
LOG.info { "Restarting GTA5.exe" }
44+
try {
45+
val gtaPath = WindowsAPI.readGtaLocation(version) / version.startBinary
46+
Runtime.getRuntime().exec(arrayOf(gtaPath.absolutePathString()))
47+
} catch (e: Exception) {
48+
LOG.error(e) { "Could not restart GTA5.exe" }
49+
_events.emit(Event.RestartError(e))
50+
}
51+
}
5052
}

windows_helper/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

windows_helper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ windows-registry = "0.5.0"
1313
mki = "0.2.3"
1414
once_cell = "1.20.3"
1515
runas = "1.2.0"
16+
log = "0.4.29"
1617

1718
[[bin]]
1819
name = "generate-headers"

windows_helper/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ tasks {
8080
"--include-struct", "Vec_uint8",
8181
"--include-struct", "Vec_uint8_t",
8282
"--include-struct", "slice_ref_Vec_uint8",
83+
"--include-struct", "GtaInstallLocationResult",
8384
header,
8485
)
8586
}
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
1+
use std::fmt::Display;
12
use enum_ordinalize::Ordinalize;
2-
use safer_ffi::ffi_export;
33
use safer_ffi::::repr_c;
4+
use safer_ffi::{derive_ReprC, ffi_export};
45
use windows_registry::LOCAL_MACHINE;
6+
use log::debug;
57

68
#[derive(Ordinalize)]
79
enum GTAVersion {
810
Legacy,
911
Enhanced,
1012
}
1113

14+
impl Display for GTAVersion {
15+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16+
let version = match self {
17+
GTAVersion::Legacy => "Legacy",
18+
GTAVersion::Enhanced => "Enhanced"
19+
};
20+
21+
write!(f, "{:?}", version)
22+
}
23+
}
24+
25+
#[derive_ReprC]
26+
#[repr(C)]
27+
struct GtaInstallLocationResult {
28+
is_error: bool,
29+
content: repr_c::String,
30+
}
31+
1232
#[ffi_export]
13-
fn read_gta_location(gta_version: i8) -> repr_c::String {
33+
fn read_gta_location(gta_version: i8) -> GtaInstallLocationResult {
1434
let version = GTAVersion::from_ordinal(gta_version).unwrap();
15-
let location = _read_gta_location(version).unwrap();
16-
location.into()
35+
36+
match _read_gta_location(version) {
37+
Ok(location) => GtaInstallLocationResult {
38+
is_error: false,
39+
content: location.into(),
40+
},
41+
Err(error) => GtaInstallLocationResult {
42+
is_error: true,
43+
content: error.to_string().into(),
44+
},
45+
}
1746
}
1847

1948
#[ffi_export]
2049
fn detect_version() -> i8 {
2150
match _detect_version() {
2251
None => -1,
23-
Some(version) => version.ordinal()
52+
Some(version) => version.ordinal(),
2453
}
2554
}
2655

@@ -37,17 +66,22 @@ fn _detect_version() -> Option<GTAVersion> {
3766
}
3867
}
3968

40-
fn _read_gta_location(gta_version: GTAVersion) -> windows_registry::Result<String> {
41-
let key = LOCAL_MACHINE.open(gta_version.registry_location())?;
69+
fn _read_gta_location(gta_version: GTAVersion, ) -> windows_registry::Result<String> {
70+
let steam_key = LOCAL_MACHINE.open("SOFTWARE\\WOW6432Node\\Rockstar Games\\Steam");
71+
let is_steam = steam_key.is_ok();
72+
debug!("Reading GTA V install location, version: {}, is_steam: {}", gta_version, is_steam);
73+
74+
let key = LOCAL_MACHINE.open(gta_version.registry_location(is_steam))?;
4275

4376
key.get_string("InstallFolder")
4477
}
4578

4679
impl GTAVersion {
47-
fn registry_location(&self) -> &str {
80+
fn registry_location(&self, is_steam: bool) -> &str {
4881
match self {
82+
GTAVersion::Enhanced if !is_steam => "SOFTWARE\\WOW6432Node\\Rockstar Games\\GTAV Enhanced",
83+
GTAVersion::Enhanced => "SOFTWARE\\WOW6432Node\\Rockstar Games\\GTA V Enhanced",
4984
GTAVersion::Legacy => "SOFTWARE\\WOW6432Node\\Rockstar Games\\Grand Theft Auto V",
50-
GTAVersion::Enhanced => "SOFTWARE\\WOW6432Node\\Rockstar Games\\GTAV Enhanced"
5185
}
5286
}
5387
}

windows_helper/src/main/kotlin/Extensions.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ private fun SegmentAllocator.allocateCStrings(vararg values: String) = slice_ref
5050

5151
object WindowsAPI {
5252
fun readGtaLocation(version: GTAVersion) = Arena.ofConfined().use {
53-
Path(readString { WindowsHelper.read_gta_location(it, version.ordinal.toByte())})
53+
val result = WindowsHelper.read_gta_location(it, version.ordinal.toByte())
54+
val string = readString { GtaInstallLocationResult.content(result) }
55+
val isError = GtaInstallLocationResult.is_error(result)
56+
57+
if (isError) throw RuntimeException(string)
58+
Path(string)
5459
}
5560

5661
fun registerKeyboardHook() = WindowsHelper.register_keyboard_hook()

0 commit comments

Comments
 (0)