Skip to content

Commit ebb07e8

Browse files
committed
reformat and fix permission handling
1 parent 10bc3fc commit ebb07e8

File tree

12 files changed

+125
-80
lines changed

12 files changed

+125
-80
lines changed

src-tauri/src/cli.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ impl CliArgs {
3939

4040
if let Some(ref gamepath) = self.gamepath {
4141
validation::validate_file_path(gamepath)?;
42-
42+
4343
let gta_exe = format!("{}/{}", gamepath, GTA_SA_EXECUTABLE);
4444
if !std::path::Path::new(&gta_exe).exists() {
45-
return Err(LauncherError::NotFound(
46-
format!("GTA San Andreas executable not found at: {}", gta_exe)
47-
));
45+
return Err(LauncherError::NotFound(format!(
46+
"GTA San Andreas executable not found at: {}",
47+
gta_exe
48+
)));
4849
}
4950
}
5051

@@ -81,4 +82,4 @@ Options:
8182
None => String::new(),
8283
}
8384
}
84-
}
85+
}

src-tauri/src/commands.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{injector, samp};
1+
use crate::{errors::LauncherError, injector, samp};
22
use log::info;
33

44
#[tauri::command]
@@ -12,9 +12,18 @@ pub async fn inject(
1212
password: &str,
1313
discord: bool,
1414
) -> std::result::Result<(), String> {
15-
injector::run_samp(name, ip, port, exe, dll, omp_file, password, discord)
16-
.await
17-
.map_err(|e| e.to_string())
15+
match injector::run_samp(name, ip, port, exe, dll, omp_file, password, discord).await {
16+
Ok(_) => Ok(()),
17+
Err(e) => {
18+
log::warn!("{}", e);
19+
match e {
20+
LauncherError::AccessDenied(_) => {
21+
return Err("need_admin".to_string());
22+
}
23+
_ => return Err(e.to_string()),
24+
}
25+
}
26+
}
1827
}
1928

2029
#[tauri::command]

src-tauri/src/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ pub const SAMP_USERDATA_PATH: &str = r"\GTA San Andreas User Files\SAMP\USERDATA
5252

5353
pub const ERROR_DIRECTORY_EXISTS: i32 = 183;
5454
pub const ERROR_ACCESS_DENIED: i32 = 5;
55-
pub const ERROR_ELEVATION_REQUIRED: i32 = 740;
55+
pub const ERROR_ELEVATION_REQUIRED: i32 = 740;

src-tauri/src/errors.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ impl fmt::Display for LauncherError {
3131
LauncherError::Storage(msg) => write!(f, "Storage error: {}", msg),
3232
LauncherError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
3333
LauncherError::NotFound(msg) => write!(f, "Not found: {}", msg),
34-
LauncherError::AccessDenied(msg) => write!(f, "Access denied - administrator privileges required: {}", msg),
34+
LauncherError::AccessDenied(msg) => write!(
35+
f,
36+
"Access denied - administrator privileges required: {}",
37+
msg
38+
),
3539
LauncherError::InternalError(msg) => write!(f, "Internal error: {}", msg),
3640
}
3741
}
@@ -82,4 +86,4 @@ impl From<LauncherError> for tauri::InvokeError {
8286
fn from(err: LauncherError) -> Self {
8387
tauri::InvokeError::from(err.to_string())
8488
}
85-
}
89+
}

src-tauri/src/helpers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ pub fn copy_files(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> crate::error
8686

8787
for entry_result in files {
8888
let entry = entry_result.map_err(|e| crate::errors::LauncherError::from(e))?;
89-
let ty = entry.file_type().map_err(|e| crate::errors::LauncherError::from(e))?;
89+
let ty = entry
90+
.file_type()
91+
.map_err(|e| crate::errors::LauncherError::from(e))?;
9092
if ty.is_dir() {
9193
let dir_path = dest.as_ref().join(entry.file_name());
9294

src-tauri/src/injector.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ pub async fn run_samp(
6464
info!("[injector.rs] Process creation failed: {}", e);
6565

6666
match e.raw_os_error() {
67-
Some(ERROR_ELEVATION_REQUIRED) => {
68-
Err(LauncherError::AccessDenied("Unable to open game process".to_string()))
69-
}
67+
Some(ERROR_ELEVATION_REQUIRED) => Err(LauncherError::AccessDenied(
68+
"Unable to open game process".to_string(),
69+
)),
70+
Some(ERROR_ACCESS_DENIED) => Err(LauncherError::AccessDenied(
71+
"Unable to open game process".to_string(),
72+
)),
7073
_ => Err(LauncherError::Process(format!(
7174
"Failed to spawn process: {}",
7275
e
@@ -167,10 +170,19 @@ pub fn inject_dll(child: u32, dll_path: &str, times: u32, waiting_for_vorbis: bo
167170
}
168171
Err(e) => {
169172
info!("[injector.rs] Failed to access process: {}", e);
170-
Err(LauncherError::Process(format!(
171-
"Failed to access process: {}",
172-
e
173-
)))
173+
174+
match e.raw_os_error() {
175+
Some(ERROR_ELEVATION_REQUIRED) => Err(LauncherError::AccessDenied(
176+
"Unable to open game process".to_string(),
177+
)),
178+
Some(ERROR_ACCESS_DENIED) => Err(LauncherError::AccessDenied(
179+
"Unable to open game process".to_string(),
180+
)),
181+
_ => Err(LauncherError::Process(format!(
182+
"Failed to access process: {}",
183+
e
184+
))),
185+
}
174186
}
175187
}
176188
}

src-tauri/src/ipc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,20 @@ pub fn listen_for_ipc(app_handle: AppHandle) {
132132
pub fn send_message_to_game(id: i32, message: &str) -> Result<()> {
133133
use std::io::Write;
134134

135-
let mut streams = GAME_STREAMS
136-
.lock()
137-
.map_err(|_| crate::errors::LauncherError::InternalError("Failed to acquire stream lock".to_string()))?;
135+
let mut streams = GAME_STREAMS.lock().map_err(|_| {
136+
crate::errors::LauncherError::InternalError("Failed to acquire stream lock".to_string())
137+
})?;
138138

139139
if let Some(stream) = streams.get_mut(&id) {
140140
let full_message = format!("{}\n", message);
141-
stream
142-
.write_all(full_message.as_bytes())
143-
.map_err(|e| crate::errors::LauncherError::Network(format!("Failed to write to stream: {}", e)))?;
141+
stream.write_all(full_message.as_bytes()).map_err(|e| {
142+
crate::errors::LauncherError::Network(format!("Failed to write to stream: {}", e))
143+
})?;
144144
Ok(())
145145
} else {
146146
log::warn!("No IPC stream found for process ID: {}", id);
147-
Err(crate::errors::LauncherError::NotFound("no_stream_found".to_string()))
147+
Err(crate::errors::LauncherError::NotFound(
148+
"no_stream_found".to_string(),
149+
))
148150
}
149151
}

src-tauri/src/query.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ pub async fn query_server(
524524
}
525525
}
526526

527-
serde_json::to_string(&result)
528-
.map_err(|e| LauncherError::SerdeJson(e))
527+
serde_json::to_string(&result).map_err(|e| LauncherError::SerdeJson(e))
529528
}
530529
Err(e) => Err(e),
531530
}

src-tauri/src/rpcs.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,41 @@ struct CopyFilesToGtaSaParams {
3939

4040
fn get_checksum_of_files(list: Vec<String>) -> Result<Vec<String>> {
4141
let mut result = Vec::new();
42-
42+
4343
for file in list {
44-
let mut f = File::open(&file)
45-
.map_err(|e| LauncherError::Io(e))?;
46-
44+
let mut f = File::open(&file).map_err(|e| LauncherError::Io(e))?;
45+
4746
let mut contents = Vec::new();
4847
f.read_to_end(&mut contents)
4948
.map_err(|e| LauncherError::Io(e))?;
50-
49+
5150
let digest = compute(&contents);
5251
let checksum_entry = format!("{}|{:x}", file, digest);
5352
result.push(checksum_entry);
5453
}
55-
54+
5655
Ok(result)
5756
}
5857

5958
fn extract_7z(path: &str, output_path: &str) -> Result<()> {
60-
decompress_file(path, output_path)
61-
.map_err(|e| LauncherError::InternalError(format!("Failed to extract archive '{}': {}", path, e)))
59+
decompress_file(path, output_path).map_err(|e| {
60+
LauncherError::InternalError(format!("Failed to extract archive '{}': {}", path, e))
61+
})
6262
}
6363

64-
fn copy_files_to_gtasa(src: &str, gtasa_dir: &str) -> Result<()> {
65-
helpers::copy_files(src, gtasa_dir)
66-
.map_err(|_| LauncherError::InternalError("Failed to copy files".to_string()))
64+
fn copy_files_to_gtasa(src: &str, gtasa_dir: &str) -> std::result::Result<(), String> {
65+
match helpers::copy_files(src, gtasa_dir) {
66+
Ok(_) => Ok(()),
67+
Err(e) => {
68+
log::warn!("{}", e);
69+
match e {
70+
LauncherError::AccessDenied(_) => {
71+
return Err("need_admin".to_string());
72+
}
73+
_ => return Err(e.to_string()),
74+
}
75+
}
76+
}
6777
}
6878

6979
async fn rpc_handler(
@@ -114,7 +124,7 @@ pub async fn initialize_rpc() -> Result<()> {
114124
.service(web::resource("/rpc/{method}").route(web::post().to(rpc_handler)))
115125
})
116126
.bind(format!("127.0.0.1:{}", RPC_PORT))
117-
.map_err(|e| LauncherError::from(e))?
127+
.map_err(|e| LauncherError::from(e))?
118128
.run()
119129
.await
120130
.map_err(|e| LauncherError::from(e))

src-tauri/src/samp.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,25 @@ pub fn get_samp_favorite_list() -> String {
5050

5151
#[cfg(target_os = "windows")]
5252
pub fn get_gtasa_path() -> String {
53-
let key_result = RegKey::predef(HKEY_CURRENT_USER)
54-
.open_subkey_with_flags(SAMP_REGISTRY_KEY, KEY_READ);
55-
53+
let key_result =
54+
RegKey::predef(HKEY_CURRENT_USER).open_subkey_with_flags(SAMP_REGISTRY_KEY, KEY_READ);
55+
5656
match key_result {
57-
Ok(key) => {
58-
match key.get_value::<String, _>("gta_sa_exe") {
59-
Ok(path) => path.replace(&format!("\\{}", GTA_SA_EXECUTABLE), ""),
60-
Err(_) => String::new(),
61-
}
62-
}
57+
Ok(key) => match key.get_value::<String, _>("gta_sa_exe") {
58+
Ok(path) => path.replace(&format!("\\{}", GTA_SA_EXECUTABLE), ""),
59+
Err(_) => String::new(),
60+
},
6361
Err(_) => String::new(),
6462
}
6563
}
6664

6765
#[cfg(target_os = "windows")]
6866
pub fn get_nickname() -> String {
69-
let key_result = RegKey::predef(HKEY_CURRENT_USER)
70-
.open_subkey_with_flags(SAMP_REGISTRY_KEY, KEY_READ);
71-
67+
let key_result =
68+
RegKey::predef(HKEY_CURRENT_USER).open_subkey_with_flags(SAMP_REGISTRY_KEY, KEY_READ);
69+
7270
match key_result {
73-
Ok(key) => {
74-
key.get_value::<String, _>("PlayerName")
75-
.unwrap_or_default()
76-
}
71+
Ok(key) => key.get_value::<String, _>("PlayerName").unwrap_or_default(),
7772
Err(_) => String::new(),
7873
}
7974
}

0 commit comments

Comments
 (0)