mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-15 01:01:25 +10:00
refactor(download manager): Renamed GameDonwloadError to ApplicationDownloadError and moved
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
41
src-tauri/src/download_manager/application_download_error.rs
Normal file
41
src-tauri/src/download_manager/application_download_error.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use std::{fmt::{Display, Formatter}, io};
|
||||
|
||||
use crate::remote::RemoteAccessError;
|
||||
|
||||
// TODO: Rename / separate from downloads
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ApplicationDownloadError {
|
||||
Communication(RemoteAccessError),
|
||||
Checksum,
|
||||
Setup(SetupError),
|
||||
Lock,
|
||||
IoError(io::ErrorKind),
|
||||
DownloadError,
|
||||
}
|
||||
|
||||
impl Display for ApplicationDownloadError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ApplicationDownloadError::Communication(error) => write!(f, "{}", error),
|
||||
ApplicationDownloadError::Setup(error) => write!(f, "An error occurred while setting up the download: {}", error),
|
||||
ApplicationDownloadError::Lock => write!(f, "Failed to acquire lock. Something has gone very wrong internally. Please restart the application"),
|
||||
ApplicationDownloadError::Checksum => write!(f, "Checksum failed to validate for download"),
|
||||
ApplicationDownloadError::IoError(error) => write!(f, "{}", error),
|
||||
ApplicationDownloadError::DownloadError => write!(f, "Download failed. See Download Manager status for specific error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SetupError {
|
||||
Context,
|
||||
}
|
||||
|
||||
impl Display for SetupError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
SetupError::Context => write!(f, "Failed to generate contexts for download"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,14 +12,32 @@ use std::{
|
||||
use log::info;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::downloads::download_agent::GameDownloadError;
|
||||
use crate::downloads::download_agent::{GameDownloadAgent};
|
||||
|
||||
use super::{download_manager_builder::{CurrentProgressObject, DownloadableQueueStandin}, downloadable::Downloadable, queue::Queue};
|
||||
use super::{application_download_error::ApplicationDownloadError, download_manager_builder::{CurrentProgressObject, DownloadableQueueStandin}, downloadable::Downloadable, queue::Queue};
|
||||
|
||||
pub enum DownloadType {
|
||||
Game,
|
||||
Tool,
|
||||
}
|
||||
impl DownloadType {
|
||||
pub fn generate(
|
||||
&self,
|
||||
id: String,
|
||||
version: String,
|
||||
target_download_dir: usize,
|
||||
sender: Sender<DownloadManagerSignal>) -> Box<dyn Downloadable + Send + Sync> {
|
||||
return Box::new(match self {
|
||||
DownloadType::Game => GameDownloadAgent::new(
|
||||
id.clone(),
|
||||
version,
|
||||
target_download_dir,
|
||||
sender.clone(),
|
||||
),
|
||||
DownloadType::Tool => todo!(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub enum DownloadManagerSignal {
|
||||
/// Resumes (or starts) the DownloadManager
|
||||
@ -40,7 +58,7 @@ pub enum DownloadManagerSignal {
|
||||
/// Removes a given application
|
||||
Remove(String),
|
||||
/// Any error which occurs in the agent
|
||||
Error(GameDownloadError),
|
||||
Error(ApplicationDownloadError),
|
||||
/// Pushes UI update
|
||||
UpdateUIQueue,
|
||||
UpdateUIStats(usize, usize), //kb/s and seconds
|
||||
@ -54,7 +72,7 @@ pub enum DownloadManagerStatus {
|
||||
Downloading,
|
||||
Paused,
|
||||
Empty,
|
||||
Error(GameDownloadError),
|
||||
Error(ApplicationDownloadError),
|
||||
Finished,
|
||||
}
|
||||
|
||||
|
||||
@ -12,13 +12,13 @@ use log::{error, info};
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
use crate::{
|
||||
db::{Database, ApplicationStatus, ApplicationTransientStatus}, download_manager::download_manager::DownloadStatus, downloads::download_agent::{GameDownloadAgent, GameDownloadError}, library::{
|
||||
db::{ApplicationStatus, ApplicationTransientStatus, Database}, download_manager::download_manager::{DownloadStatus, DownloadType}, downloads::download_agent::{GameDownloadAgent}, library::{
|
||||
on_game_complete, push_application_update, QueueUpdateEvent,
|
||||
QueueUpdateEventQueueData, StatsUpdateEvent,
|
||||
}, state::GameStatusManager, DB
|
||||
};
|
||||
|
||||
use super::{download_manager::{DownloadManager, DownloadManagerSignal, DownloadManagerStatus}, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, downloadable::Downloadable, progress_object::ProgressObject, queue::Queue};
|
||||
use super::{application_download_error::ApplicationDownloadError, download_manager::{DownloadManager, DownloadManagerSignal, DownloadManagerStatus}, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, downloadable::Downloadable, progress_object::ProgressObject, queue::Queue};
|
||||
|
||||
/*
|
||||
|
||||
@ -59,7 +59,7 @@ Behold, my madness - quexeky
|
||||
|
||||
// Refactored to consolidate this type. It's a monster.
|
||||
pub type CurrentProgressObject = Arc<Mutex<Option<Arc<ProgressObject>>>>;
|
||||
pub type DownloadAgent = Arc<Mutex<dyn Downloadable + Send + Sync>>;
|
||||
pub type DownloadAgent = Arc<Mutex<Box<dyn Downloadable + Send + Sync>>>;
|
||||
|
||||
pub struct DownloadManagerBuilder {
|
||||
download_agent_registry: HashMap<String, DownloadAgent>,
|
||||
@ -272,7 +272,7 @@ impl DownloadManagerBuilder {
|
||||
spawn(move || match remove_dir_all(install_dir) {
|
||||
Err(e) => {
|
||||
sender
|
||||
.send(DownloadManagerSignal::Error(GameDownloadError::IoError(
|
||||
.send(DownloadManagerSignal::Error(ApplicationDownloadError::IoError(
|
||||
e.kind(),
|
||||
)))
|
||||
.unwrap();
|
||||
@ -345,8 +345,11 @@ impl DownloadManagerBuilder {
|
||||
if let Err(error) =
|
||||
on_game_complete(id, version, install_dir, &self.app_handle)
|
||||
{
|
||||
error!("failed to mark game as completed: {}", error);
|
||||
// TODO mark game as remote so user can retry
|
||||
self.sender
|
||||
.send(DownloadManagerSignal::Error(
|
||||
ApplicationDownloadError::Communication(error),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -374,7 +377,7 @@ impl DownloadManagerBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
let download_agent = Arc::new(Mutex::new(GameDownloadAgent::new(
|
||||
let download_agent = Arc::new(Mutex::new(DownloadType::Game.generate(
|
||||
id.clone(),
|
||||
version,
|
||||
target_download_dir,
|
||||
@ -386,9 +389,9 @@ impl DownloadManagerBuilder {
|
||||
let interface_data = DownloadableQueueStandin {
|
||||
id: id.clone(),
|
||||
status: Mutex::new(agent_status),
|
||||
progress: download_agent_lock.progress.clone(),
|
||||
progress: download_agent_lock.progress()
|
||||
};
|
||||
let version_name = download_agent_lock.version.clone();
|
||||
let version_name = download_agent_lock.version().clone();
|
||||
|
||||
drop(download_agent_lock);
|
||||
|
||||
@ -485,8 +488,7 @@ impl DownloadManagerBuilder {
|
||||
.send(DownloadManagerSignal::UpdateUIQueue)
|
||||
.unwrap();
|
||||
}
|
||||
fn manage_error_signal(&mut self, error: GameDownloadError) {
|
||||
error!("{}", error);
|
||||
fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
|
||||
let current_status = self.current_download_agent.clone().unwrap();
|
||||
|
||||
self.stop_and_wait_current_download();
|
||||
|
||||
@ -1,17 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::downloads::download_agent::GameDownloadError;
|
||||
|
||||
use super::{
|
||||
download_thread_control_flag::DownloadThreadControl,
|
||||
progress_object::ProgressObject,
|
||||
application_download_error::ApplicationDownloadError, download_thread_control_flag::DownloadThreadControl, progress_object::ProgressObject
|
||||
};
|
||||
|
||||
pub trait Downloadable: Sync {
|
||||
fn get_progress_object(&self) -> Arc<ProgressObject>;
|
||||
fn version(&self) -> String;
|
||||
fn id(&self) -> String;
|
||||
fn download(&mut self) -> Result<(), GameDownloadError>;
|
||||
fn download(&mut self) -> Result<(), ApplicationDownloadError>;
|
||||
fn progress(&self) -> Arc<ProgressObject>;
|
||||
fn control_flag(&self) -> DownloadThreadControl;
|
||||
fn install_dir(&self) -> String;
|
||||
|
||||
@ -3,4 +3,5 @@ pub mod download_manager_builder;
|
||||
pub mod progress_object;
|
||||
pub mod queue;
|
||||
pub mod download_thread_control_flag;
|
||||
pub mod downloadable;
|
||||
pub mod downloadable;
|
||||
pub mod application_download_error;
|
||||
Reference in New Issue
Block a user