mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
style(downloads): Made all errors type-based
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
@ -28,21 +28,26 @@ pub struct GameDownloadAgent {
|
||||
pub progress: ProgressObject,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub enum GameDownloadError {
|
||||
CommunicationError(RemoteAccessError),
|
||||
ChecksumError,
|
||||
SetupError(String),
|
||||
LockError,
|
||||
Communication(RemoteAccessError),
|
||||
Checksum,
|
||||
Setup(SetupError),
|
||||
Lock,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SetupError {
|
||||
Context
|
||||
}
|
||||
|
||||
impl Display for GameDownloadError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
GameDownloadError::CommunicationError(error) => write!(f, "{}", error),
|
||||
GameDownloadError::SetupError(error) => write!(f, "{}", error),
|
||||
GameDownloadError::LockError => write!(f, "Failed to acquire lock. Something has gone very wrong internally. Please restart the application"),
|
||||
GameDownloadError::ChecksumError => write!(f, "Checksum failed to validate for download"),
|
||||
GameDownloadError::Communication(error) => write!(f, "{}", error),
|
||||
GameDownloadError::Setup(error) => write!(f, "{:?}", error),
|
||||
GameDownloadError::Lock => write!(f, "Failed to acquire lock. Something has gone very wrong internally. Please restart the application"),
|
||||
GameDownloadError::Checksum => write!(f, "Checksum failed to validate for download"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -114,13 +119,8 @@ impl GameDownloadAgent {
|
||||
.unwrap();
|
||||
|
||||
if response.status() != 200 {
|
||||
return Err(GameDownloadError::CommunicationError(
|
||||
format!(
|
||||
"Failed to download game manifest: {} {}",
|
||||
response.status(),
|
||||
response.text().unwrap()
|
||||
)
|
||||
.into(),
|
||||
return Err(GameDownloadError::Communication(
|
||||
RemoteAccessError::ManifestDownloadFailed(response.status(), response.text().unwrap())
|
||||
));
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ impl GameDownloadAgent {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(GameDownloadError::LockError)
|
||||
Err(GameDownloadError::Lock)
|
||||
}
|
||||
|
||||
pub fn generate_contexts(&self) -> Result<(), GameDownloadError> {
|
||||
@ -194,9 +194,7 @@ impl GameDownloadAgent {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(GameDownloadError::SetupError(
|
||||
"Failed to generate download contexts".to_owned(),
|
||||
))
|
||||
Err(GameDownloadError::Setup(SetupError::Context))
|
||||
}
|
||||
|
||||
pub fn run(&self) {
|
||||
|
||||
@ -1,52 +1,38 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use crate::AppState;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{AppError, AppState};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn download_game(
|
||||
game_id: String,
|
||||
game_version: String,
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<(), String> {
|
||||
/*
|
||||
info!("beginning game download...");
|
||||
|
||||
let mut download_agent = GameDownloadAgent::new(game_id.clone(), game_version.clone(), 0);
|
||||
// Setup download requires mutable
|
||||
download_agent.setup_download().unwrap();
|
||||
|
||||
let mut lock: std::sync::MutexGuard<'_, AppState> = state.lock().unwrap();
|
||||
let download_agent_ref = Arc::new(download_agent);
|
||||
lock.download_manager
|
||||
.insert(game_id, download_agent_ref.clone());
|
||||
|
||||
// Run it in another thread
|
||||
spawn(move || {
|
||||
// Run doesn't require mutable
|
||||
download_agent_ref.clone().run();
|
||||
});
|
||||
*/
|
||||
) -> Result<(), AppError> {
|
||||
|
||||
state
|
||||
.lock()
|
||||
.unwrap()
|
||||
.download_manager
|
||||
.queue_game(game_id, game_version, 0)
|
||||
.unwrap();
|
||||
Ok(())
|
||||
.map_err(|_| AppError::Signal)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_current_game_download_progress(
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<f64, String> {
|
||||
let progress = state
|
||||
) -> Result<f64, AppError> {
|
||||
match state
|
||||
.lock()
|
||||
.unwrap()
|
||||
.download_manager
|
||||
.get_current_game_download_progress()
|
||||
.unwrap_or(0.0);
|
||||
{
|
||||
Some(progress) => Ok(progress),
|
||||
None => Err(AppError::DoesNotExist),
|
||||
}
|
||||
|
||||
Ok(progress)
|
||||
}
|
||||
/*
|
||||
fn use_download_agent(
|
||||
|
||||
@ -148,7 +148,7 @@ pub fn download_game_chunk(
|
||||
.get(chunk_url)
|
||||
.header("Authorization", header)
|
||||
.send()
|
||||
.map_err(|e| GameDownloadError::CommunicationError(e.into()))?;
|
||||
.map_err(|e| GameDownloadError::Communication(e.into()))?;
|
||||
|
||||
let mut destination = DropWriter::new(ctx.path);
|
||||
|
||||
@ -160,11 +160,7 @@ pub fn download_game_chunk(
|
||||
|
||||
let content_length = response.content_length();
|
||||
if content_length.is_none() {
|
||||
return Err(GameDownloadError::CommunicationError(
|
||||
RemoteAccessError::GenericErrror(
|
||||
"Invalid download endpoint, missing Content-Length header.".to_owned(),
|
||||
),
|
||||
));
|
||||
return Err(GameDownloadError::Communication(RemoteAccessError::InvalidResponse));
|
||||
}
|
||||
|
||||
let mut pipeline = DropDownloadPipeline::new(
|
||||
@ -184,7 +180,7 @@ pub fn download_game_chunk(
|
||||
|
||||
let res = hex::encode(checksum.0);
|
||||
if res != ctx.checksum {
|
||||
return Err(GameDownloadError::ChecksumError);
|
||||
return Err(GameDownloadError::Checksum);
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
|
||||
@ -11,7 +11,7 @@ use log::info;
|
||||
|
||||
use super::{
|
||||
download_agent::{GameDownloadAgent, GameDownloadError},
|
||||
download_manager_interface::{AgentInterfaceData, DownloadManagerInterface},
|
||||
download_manager_interface::{AgentInterfaceData, DownloadManager},
|
||||
download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag},
|
||||
progress_object::ProgressObject,
|
||||
};
|
||||
@ -53,7 +53,7 @@ Behold, my madness - quexeky
|
||||
|
||||
*/
|
||||
|
||||
pub struct DownloadManager {
|
||||
pub struct DownloadManagerBuilder {
|
||||
download_agent_registry: HashMap<String, Arc<GameDownloadAgent>>,
|
||||
download_queue: Arc<Mutex<VecDeque<Arc<AgentInterfaceData>>>>,
|
||||
command_receiver: Receiver<DownloadManagerSignal>,
|
||||
@ -85,9 +85,8 @@ pub enum DownloadManagerStatus {
|
||||
Downloading,
|
||||
Paused,
|
||||
Empty,
|
||||
Error(GameDownloadError),
|
||||
Error,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub enum GameDownloadStatus {
|
||||
Downloading,
|
||||
Paused,
|
||||
@ -95,8 +94,8 @@ pub enum GameDownloadStatus {
|
||||
Error(GameDownloadError),
|
||||
}
|
||||
|
||||
impl DownloadManager {
|
||||
pub fn generate() -> DownloadManagerInterface {
|
||||
impl DownloadManagerBuilder {
|
||||
pub fn build() -> DownloadManager {
|
||||
let queue = Arc::new(Mutex::new(VecDeque::new()));
|
||||
let (command_sender, command_receiver) = channel();
|
||||
let active_progress = Arc::new(Mutex::new(None));
|
||||
@ -115,7 +114,7 @@ impl DownloadManager {
|
||||
|
||||
let terminator = spawn(|| manager.manage_queue());
|
||||
|
||||
DownloadManagerInterface::new(terminator, queue, active_progress, command_sender)
|
||||
DownloadManager::new(terminator, queue, active_progress, command_sender)
|
||||
}
|
||||
|
||||
fn manage_queue(mut self) -> Result<(), ()> {
|
||||
@ -238,8 +237,8 @@ impl DownloadManager {
|
||||
fn manage_error_signal(&self, error: GameDownloadError) {
|
||||
let current_status = self.current_game_interface.clone().unwrap();
|
||||
let mut lock = current_status.status.lock().unwrap();
|
||||
*lock = GameDownloadStatus::Error(error.clone());
|
||||
self.set_status(DownloadManagerStatus::Error(error));
|
||||
*lock = GameDownloadStatus::Error(error);
|
||||
self.set_status(DownloadManagerStatus::Error);
|
||||
}
|
||||
fn set_status(&self, status: DownloadManagerStatus) {
|
||||
*self.status.lock().unwrap() = status;
|
||||
|
||||
@ -26,7 +26,7 @@ use super::{
|
||||
/// The actual download queue may be accessed through the .edit() function,
|
||||
/// which provides raw access to the underlying queue.
|
||||
/// THIS EDITING IS BLOCKING!!!
|
||||
pub struct DownloadManagerInterface {
|
||||
pub struct DownloadManager {
|
||||
terminator: JoinHandle<Result<(), ()>>,
|
||||
download_queue: Arc<Mutex<VecDeque<Arc<AgentInterfaceData>>>>,
|
||||
progress: Arc<Mutex<Option<ProgressObject>>>,
|
||||
@ -45,7 +45,7 @@ impl From<Arc<GameDownloadAgent>> for AgentInterfaceData {
|
||||
}
|
||||
}
|
||||
|
||||
impl DownloadManagerInterface {
|
||||
impl DownloadManager {
|
||||
pub fn new(
|
||||
terminator: JoinHandle<Result<(), ()>>,
|
||||
download_queue: Arc<Mutex<VecDeque<Arc<AgentInterfaceData>>>>,
|
||||
|
||||
Reference in New Issue
Block a user