Move away from .unwrap()

This commit is contained in:
2026-02-17 15:14:34 -07:00
parent 5e94a8ac5f
commit b94326a671

View File

@@ -1,33 +1,76 @@
use sha2::{Digest, Sha512}; use sha2::{Digest, Sha512};
use std::fs::{remove_dir_all};
use std::io::Cursor;
use std::{ use std::{
fs::{self, remove_dir_all}, fs::{File, create_dir_all},
io::Cursor, io::{copy},
path::PathBuf, path::PathBuf,
process::Command, process::Command
}; };
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
use tauri_plugin_os::platform; use tauri_plugin_os::platform;
use zip::ZipArchive; use zip::ZipArchive;
fn unzip_to_dir(bytes: &[u8], target: &PathBuf) -> std::io::Result<()> { #[cfg(unix)]
let reader = Cursor::new(bytes); use std::os::unix::fs::PermissionsExt;
let mut zip = ZipArchive::new(reader).unwrap();
for i in 0..zip.len() { fn should_skip(name: &str) -> bool {
let mut file = zip.by_index(i).unwrap(); name.starts_with("__MACOSX/")
let outpath = target.join(file.mangled_name()); || name == "__MACOSX"
|| name.ends_with("/.DS_Store")
|| name.ends_with(".DS_Store")
}
if file.name().ends_with('/') { async fn unzip_to_dir(bytes: &[u8], target: &PathBuf) -> String {
fs::create_dir_all(&outpath)?; let bytes = bytes.to_vec();
} else { let target = target.clone();
if let Some(p) = outpath.parent() {
fs::create_dir_all(p)?; let res = tauri::async_runtime::spawn_blocking(move || -> Result<(), String> {
let reader = Cursor::new(bytes);
let mut archive = ZipArchive::new(reader).map_err(|e| e.to_string())?;
for i in 0..archive.len() {
let mut entry = archive.by_index(i).map_err(|e| e.to_string())?;
let name = entry.name();
if should_skip(name) {
continue;
}
let outpath = target.join(name);
if entry.is_dir() {
create_dir_all(&outpath).map_err(|e| e.to_string())?;
#[cfg(unix)]
if let Some(mode) = entry.unix_mode() {
std::fs::set_permissions(&outpath, std::fs::Permissions::from_mode(mode))
.map_err(|e| e.to_string())?;
}
} else {
if let Some(parent) = outpath.parent() {
create_dir_all(parent).map_err(|e| e.to_string())?;
}
let mut outfile = File::create(&outpath).map_err(|e| e.to_string())?;
copy(&mut entry, &mut outfile).map_err(|e| e.to_string())?;
#[cfg(unix)]
if let Some(mode) = entry.unix_mode() {
std::fs::set_permissions(&outpath, std::fs::Permissions::from_mode(mode))
.map_err(|e| e.to_string())?;
}
} }
let mut outfile = fs::File::create(&outpath)?;
std::io::copy(&mut file, &mut outfile)?;
} }
Ok(())
})
.await;
match res {
Ok(Ok(())) => "1".into(),
_ => "-1".into(),
} }
Ok(())
} }
fn get_sha512_hash(data: &[u8]) -> String { fn get_sha512_hash(data: &[u8]) -> String {
@@ -54,60 +97,51 @@ async fn download(app: AppHandle, url: String, hash: String) -> String {
return "-2".to_string(); return "-2".to_string();
} }
let bin_path = app.path().app_local_data_dir().unwrap().join("bin"); let bin_path = match app.path().app_local_data_dir() {
Ok(p) => p.join("bin"),
Err(_) => return "-1".to_string(),
};
let _ = tokio::fs::create_dir_all(&bin_path).await; let _ = tokio::fs::create_dir_all(&bin_path).await;
if let Err(_) = unzip_to_dir(&bytes, &bin_path) { let unzip_res = unzip_to_dir(&bytes, &bin_path).await;
return "-3".to_string(); if unzip_res == "-1" {
return "-1".to_string();
} }
drop(bytes); drop(bytes);
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
use std::{fs, os::unix::fs::PermissionsExt};
let executable_path = if cfg!(target_os = "linux") {
bin_path.join("lncvrt-games-launcher")
} else {
bin_path
.join("Lncvrt Games Launcher.app")
.join("Contents")
.join("MacOS")
.join("lncvrt-games-launcher")
};
let mut perms = fs::metadata(&executable_path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&executable_path, perms).unwrap();
}
return "1".to_string(); return "1".to_string();
} }
#[allow(unused_variables)] #[allow(unused_variables)]
#[tauri::command] #[tauri::command]
fn load(app: AppHandle) { fn load(app: AppHandle) {
let bin_path = app.path().app_local_data_dir().unwrap().join("bin"); let bin_path = match app.path().app_local_data_dir() {
Ok(p) => p.join("bin"),
Err(_) => return,
};
if !bin_path.exists() { if !bin_path.exists() {
return; return;
} }
if platform() == "macos" { if platform() == "macos" {
Command::new("open") if let Err(_) = Command::new("open")
.arg("Lncvrt Games Launcher.app") .arg("Lncvrt Games Launcher.app")
.current_dir(&bin_path) .current_dir(&bin_path)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game on macOS");
}
} else if platform() == "linux" { } else if platform() == "linux" {
Command::new("./lncvrt-games-launcher") if let Err(_) = Command::new("./lncvrt-games-launcher")
.current_dir(&bin_path) .current_dir(&bin_path)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game on macOS");
}
} else if platform() == "windows" { } else if platform() == "windows" {
Command::new(&bin_path.join("lncvrt-games-launcher.exe")) if let Err(_) = Command::new(&bin_path.join("lncvrt-games-launcher.exe"))
.current_dir(&bin_path) .current_dir(&bin_path)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game on macOS");
}
} }
app.exit(0); app.exit(0);
@@ -117,7 +151,10 @@ fn load(app: AppHandle) {
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
.setup(|app| { .setup(|app| {
let app_local_data_dir = app.path().app_local_data_dir().unwrap(); let app_local_data_dir = match app.path().app_local_data_dir() {
Ok(p) => p,
Err(_) => return Ok(()),
};
let downloads_dir = app_local_data_dir.join("downloads"); let downloads_dir = app_local_data_dir.join("downloads");
let updates_dir = app_local_data_dir.join("updates"); let updates_dir = app_local_data_dir.join("updates");
let bin_dir = app_local_data_dir.join("bin"); let bin_dir = app_local_data_dir.join("bin");