refactor: improve error handling and code readability in replace_ui_files and check_for_corruption functions

This commit is contained in:
HorizonCode committed 2026-04-14 22:52:16 +02:00
1 parent 1aa3070ea1
commit 97f072f4ce
1 file changed
+72 -33
+72 -33
View File
@@ -615,39 +615,49 @@ pub fn replace_ui_files(folder: String, revert: bool) -> Result<(), ReplaceUIErr
} }
if dest.exists() { if dest.exists() {
std::fs::remove_file(dest) std::fs::remove_file(dest).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to remove {}: {}", name, e)))?; ReplaceUIError::IoError(format!("Failed to remove {}: {}", name, e))
})?;
} }
std::fs::copy(source, dest) std::fs::copy(source, dest).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to copy {}: {}", name, e)))?; ReplaceUIError::IoError(format!("Failed to copy {}: {}", name, e))
})?;
Ok(()) Ok(())
}; };
let restore_backup = |bak: &PathBuf, dest: &PathBuf, name: &str| -> Result<(), ReplaceUIError> { let restore_backup = |bak: &PathBuf,
dest: &PathBuf,
name: &str|
-> Result<(), ReplaceUIError> {
if bak.exists() { if bak.exists() {
if dest.exists() { if dest.exists() {
std::fs::remove_file(dest) std::fs::remove_file(dest).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to remove {}: {}", name, e)))?; ReplaceUIError::IoError(format!("Failed to remove {}: {}", name, e))
})?;
} }
std::fs::copy(bak, dest) std::fs::rename(bak, dest).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to restore {} from backup: {}", name, e)))?; ReplaceUIError::IoError(format!("Failed to rename {} from backup: {}", name, e))
})?;
} }
Ok(()) Ok(())
}; };
if !revert { if !revert {
if osu_ui.exists() && !osu_ui_bak.exists() { if osu_ui.exists() && !osu_ui_bak.exists() {
std::fs::rename(&osu_ui, &osu_ui_bak) std::fs::rename(&osu_ui, &osu_ui_bak).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to backup osu!ui.dll: {}", e)))?; ReplaceUIError::IoError(format!("Failed to backup osu!ui.dll: {}", e))
})?;
} }
if osu_seasonal.exists() && !osu_seasonal_bak.exists() { if osu_seasonal.exists() && !osu_seasonal_bak.exists() {
std::fs::rename(&osu_seasonal, &osu_seasonal_bak) std::fs::rename(&osu_seasonal, &osu_seasonal_bak).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to backup osu!seasonal.dll: {}", e)))?; ReplaceUIError::IoError(format!("Failed to backup osu!seasonal.dll: {}", e))
})?;
} }
if osu_gameplay.exists() && !osu_gameplay_bak.exists() { if osu_gameplay.exists() && !osu_gameplay_bak.exists() {
std::fs::rename(&osu_gameplay, &osu_gameplay_bak) std::fs::rename(&osu_gameplay, &osu_gameplay_bak).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to backup osu!gameplay.dll: {}", e)))?; ReplaceUIError::IoError(format!("Failed to backup osu!gameplay.dll: {}", e))
})?;
} }
copy_file(&ezpp_ui, &osu_ui, "osu!ui.dll")?; copy_file(&ezpp_ui, &osu_ui, "osu!ui.dll")?;
@@ -662,8 +672,9 @@ pub fn replace_ui_files(folder: String, revert: bool) -> Result<(), ReplaceUIErr
restore_backup(&osu_gameplay_bak, &osu_gameplay, "osu!gameplay.dll")?; restore_backup(&osu_gameplay_bak, &osu_gameplay, "osu!gameplay.dll")?;
if osu_ui.exists() && osu_seasonal.exists() && osu_gameplay.exists() { if osu_ui.exists() && osu_seasonal.exists() && osu_gameplay.exists() {
std::fs::remove_file(&temp_file) std::fs::remove_file(&temp_file).map_err(|e| {
.map_err(|e| ReplaceUIError::IoError(format!("Failed to remove marker file: {}", e)))?; ReplaceUIError::IoError(format!("Failed to remove marker file: {}", e))
})?;
} }
} }
@@ -692,37 +703,65 @@ pub async fn check_for_corruption(folder: String) -> Result<bool, CorruptionChec
if temp_file.exists() { if temp_file.exists() {
if osu_ui_bak.exists() { if osu_ui_bak.exists() {
fs::remove_file(&osu_ui) if osu_ui.exists() {
.await fs::remove_file(&osu_ui).await.map_err(|e| {
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to remove osu!ui.dll: {}", e)))?; CorruptionCheckError::IoError(format!("Failed to remove osu!ui.dll: {}", e))
fs::rename(&osu_ui_bak, &osu_ui) })?;
.await }
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to restore osu!ui.dll from backup: {}", e)))?;
fs::rename(&osu_ui_bak, &osu_ui).await.map_err(|e| {
CorruptionCheckError::IoError(format!(
"Failed to restore osu!ui.dll from backup: {}",
e
))
})?;
} }
if osu_gameplay_bak.exists() { if osu_gameplay_bak.exists() {
fs::remove_file(&osu_gameplay) if osu_gameplay.exists() {
.await fs::remove_file(&osu_gameplay).await.map_err(|e| {
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to remove osu!gameplay.dll: {}", e)))?; CorruptionCheckError::IoError(format!(
"Failed to remove osu!gameplay.dll: {}",
e
))
})?;
}
fs::rename(&osu_gameplay_bak, &osu_gameplay) fs::rename(&osu_gameplay_bak, &osu_gameplay)
.await .await
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to restore osu!gameplay.dll from backup: {}", e)))?; .map_err(|e| {
CorruptionCheckError::IoError(format!(
"Failed to restore osu!gameplay.dll from backup: {}",
e
))
})?;
} }
if osu_seasonal_bak.exists() { if osu_seasonal_bak.exists() {
fs::remove_file(&osu_seasonal) if osu_seasonal.exists() {
.await fs::remove_file(&osu_seasonal).await.map_err(|e| {
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to remove osu!seasonal.dll: {}", e)))?; CorruptionCheckError::IoError(format!(
"Failed to remove osu!seasonal.dll: {}",
e
))
})?;
}
fs::rename(&osu_seasonal_bak, &osu_seasonal) fs::rename(&osu_seasonal_bak, &osu_seasonal)
.await .await
.map_err(|e| CorruptionCheckError::IoError(format!("Failed to restore osu!seasonal.dll from backup: {}", e)))?; .map_err(|e| {
CorruptionCheckError::IoError(format!(
"Failed to restore osu!seasonal.dll from backup: {}",
e
))
})?;
} }
return Ok(false); return Ok(false);
} }
if !osu_ui.exists() || !osu_gameplay.exists() || !osu_seasonal.exists() { if !osu_ui.exists() || !osu_gameplay.exists() || !osu_seasonal.exists() {
return Ok(false); return Ok(true);
} }
Ok(true) Ok(false)
} }
#[tauri::command] #[tauri::command]