|
| 1 | +use std::{fmt::Debug, future::Future}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + downloads::{ |
| 5 | + progress::ProgressSender, |
| 6 | + traits::{DownloadResult, Downloader}, |
| 7 | + DownloadQueue, |
| 8 | + }, |
| 9 | + game_paths::GamePaths, |
| 10 | + instance::marker::ProfileDownloader, |
| 11 | + PinnedFutureWithBounds, |
| 12 | +}; |
| 13 | + |
| 14 | +use super::{vanilla::Vanilla, ToLoaderProfile}; |
| 15 | + |
| 16 | +pub struct VanillaCombinedDownloader<T> { |
| 17 | + version: String, |
| 18 | + game_paths: GamePaths, |
| 19 | + vanilla: Vanilla, |
| 20 | + loader: T, |
| 21 | +} |
| 22 | + |
| 23 | +impl<T> Debug for VanillaCombinedDownloader<T> { |
| 24 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | + f.debug_struct("VanillaCombinedDownloader") |
| 26 | + .field("version", &self.version) |
| 27 | + .field("game_paths", &self.game_paths) |
| 28 | + .field("vanilla", &self.vanilla) |
| 29 | + .field("loader", &"(loader)") |
| 30 | + .finish() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl VanillaCombinedDownloader<()> { |
| 35 | + pub async fn new(game_version: impl Into<String>, game_paths: GamePaths) -> anyhow::Result<Self> { |
| 36 | + let version = game_version.into(); |
| 37 | + let vanilla = Vanilla::new(&version, game_paths.clone()).await?; |
| 38 | + |
| 39 | + Ok(Self { |
| 40 | + version, |
| 41 | + game_paths, |
| 42 | + vanilla, |
| 43 | + loader: (), |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + pub async fn with_loader<T, F, Fut>(self, fun: F) -> anyhow::Result<VanillaCombinedDownloader<T>> |
| 48 | + where |
| 49 | + F: FnOnce(String, GamePaths) -> Fut, |
| 50 | + Fut: Future<Output = anyhow::Result<T>>, |
| 51 | + T: ProfileDownloader, |
| 52 | + { |
| 53 | + let loader = (fun)(self.version.clone(), self.game_paths.clone()).await?; |
| 54 | + |
| 55 | + Ok(VanillaCombinedDownloader { |
| 56 | + version: self.version, |
| 57 | + game_paths: self.game_paths, |
| 58 | + vanilla: self.vanilla, |
| 59 | + loader, |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<T: ToLoaderProfile> ToLoaderProfile for VanillaCombinedDownloader<T> { |
| 65 | + fn to_profile(&self) -> crate::instance::loader::LoaderProfile { |
| 66 | + self.loader.to_profile() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[async_trait::async_trait] |
| 71 | +impl<T: ProfileDownloader + 'static> Downloader for VanillaCombinedDownloader<T> { |
| 72 | + type Data = DownloadResult; |
| 73 | + |
| 74 | + fn total(&self) -> u32 { |
| 75 | + self.vanilla.total() + self.loader.total() |
| 76 | + } |
| 77 | + |
| 78 | + async fn download(self: Box<Self>, sender: &dyn ProgressSender<Self::Data>) { |
| 79 | + let downloader = DownloadQueue::new().with_downloader(self.vanilla).with_downloader(self.loader); |
| 80 | + let downloader = Box::new(downloader); |
| 81 | + downloader.download(sender).await; |
| 82 | + } |
| 83 | + |
| 84 | + fn io(&self) -> PinnedFutureWithBounds<anyhow::Result<()>> { |
| 85 | + let vanilla_io = self.vanilla.io(); |
| 86 | + let loader_io = self.loader.io(); |
| 87 | + |
| 88 | + Box::pin(async move { |
| 89 | + vanilla_io.await?; |
| 90 | + loader_io.await |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[async_trait::async_trait] |
| 96 | +impl Downloader for VanillaCombinedDownloader<()> { |
| 97 | + type Data = DownloadResult; |
| 98 | + |
| 99 | + fn total(&self) -> u32 { |
| 100 | + self.vanilla.total() |
| 101 | + } |
| 102 | + |
| 103 | + async fn download(self: Box<Self>, sender: &dyn ProgressSender<Self::Data>) { |
| 104 | + Box::new(self.vanilla).download(sender).await; |
| 105 | + } |
| 106 | + |
| 107 | + fn io(&self) -> PinnedFutureWithBounds<anyhow::Result<()>> { |
| 108 | + self.vanilla.io() |
| 109 | + } |
| 110 | +} |
0 commit comments