Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype: stream unpacking archives #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ version = "0.5.3"
features = ["v1_4"]

[dependencies.anime-game-core]
git = "https://github.com/an-anime-team/anime-game-core"
branch = "v2.0.0-rewrite"
# path = "../anime-game-core"
# git = "https://github.com/an-anime-team/anime-game-core"
# branch = "v2.0.0-rewrite"
path = "../anime-game-core"

# [dependencies.rfd]
# version = "0.11"
Expand Down
1 change: 1 addition & 0 deletions assets/locales/en/tasks.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tasks-preparing-transition = Preparing transition...
tasks-finishing-transition = Finishing transition...
tasks-downloading = Downloading...
tasks-unpacking = Unpacking...
tasks-stream-unpacking = Downloading and unpacking...
tasks-deleting-files = Deleting files...
tasks-pre-transition-code = Running task pre-transition code...
tasks-transition-code = Running task transition code...
Expand Down
1 change: 1 addition & 0 deletions assets/locales/ru/tasks.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tasks-preparing-transition = Подготовка перехода...
tasks-finishing-transition = Завершение перехода...
tasks-downloading = Загрузка...
tasks-unpacking = Распаковка...
tasks-stream-unpacking = Загрузка и распаковка...
tasks-deleting-files = Удаление файлов...
tasks-pre-transition-code = Выполнение обработчика перед переходом...
tasks-transition-code = Выполнение обработчика перехода...
Expand Down
165 changes: 106 additions & 59 deletions src/ui/components/tasks_queue/download_diff_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::atomic::{
Ordering
};

use anime_game_core::archive::stream::StreamArchive;
use anime_game_core::updater::{
UpdaterExt,
BasicUpdater,
Expand Down Expand Up @@ -47,6 +48,7 @@ pub enum Status {
RunPreTransitionCode,
Downloading,
Unpacking,
StreamUnpacking,
RunTransitionCode,
FinishingTransition,
RunPostTransitionCode
Expand Down Expand Up @@ -132,96 +134,140 @@ impl QueuedTask for DownloadDiffQueuedTask {

match diff_info {
DiffInfo::Archive { size: _, uri } => {
// Download archive
let downloader = Downloader::new(&uri);

let downloader = Downloader::new(uri);
if let Ok(sa) = StreamArchive::from_uri(&uri) {
let mut status_file = downloader.file_name();
status_file.push_str(".progress");

let archive = transition.transition_path()
.join(downloader.file_name());
let mut updater = sa.stream_unpack(
transition.transition_path(),
status_file
)?;

let mut updater = downloader.download(&archive)?;
while !updater.is_finished() {
// TODO: add timeouts

while !updater.is_finished() {
// TODO: add timeouts
sender.send((
Status::StreamUnpacking,
updater.current(),
updater.total()
))?;
}
} else {
// Download archive

sender.send((
Status::Downloading,
updater.current(),
updater.total()
))?;
}
let archive = transition.transition_path()
.join(downloader.file_name());

// Extract archive
let mut updater = downloader.download(&archive)?;

let Some(mut updater) = archive::extract(&archive, transition.transition_path()) else {
anyhow::bail!("Failed to extract files from the archive: {:?}", archive);
};
while !updater.is_finished() {
// TODO: add timeouts

while let Ok(false) = updater.status() {
// TODO: add timeouts
sender.send((
Status::Downloading,
updater.current(),
updater.total()
))?;
}

sender.send((
Status::Unpacking,
updater.current(),
updater.total()
))?;
}
// Extract archive

// Delete archive
let Some(mut updater) = archive::extract(&archive, transition.transition_path()) else {
anyhow::bail!("Failed to extract files from the archive: {:?}", archive);
};

std::fs::remove_file(archive)?;
}
while let Ok(false) = updater.status() {
// TODO: add timeouts

DiffInfo::Segments { size, segments } => {
// Download segments
sender.send((
Status::Unpacking,
updater.current(),
updater.total()
))?;
}

let mut archives = vec![];
let mut downloaded = 0;
// Delete archive

for uri in segments {
let downloader = Downloader::new(uri);
std::fs::remove_file(archive)?;
}
}

let archive = transition.transition_path()
.join(downloader.file_name());
DiffInfo::Segments { size, segments } => {
let uris = segments.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>();

let mut updater = downloader.download(&archive)?;
if let Ok(sa) = StreamArchive::from_uris(&uris, true) {
let mut status_file = Downloader::new(uris[0]).file_name();
status_file.push_str(".progress");

archives.push(archive);
let mut updater = sa.stream_unpack(
transition.transition_path(),
status_file
)?;

while !updater.is_finished() {
// TODO: add timeouts

sender.send((
Status::Downloading,
downloaded + updater.current(),
size
// updater.total()
Status::StreamUnpacking,
updater.current(),
updater.total()
))?;
}
} else {
// Download segments

downloaded += updater.total();
}
let mut archives = vec![];
let mut downloaded = 0;

// Extract segments
for uri in uris {
let downloader = Downloader::new(uri);

let Some(mut updater) = archive::extract(&archives[0], transition.transition_path()) else {
anyhow::bail!("Failed to extract files from segmented archive: {:?}", archives[0]);
};
let archive = transition.transition_path()
.join(downloader.file_name());

while let Ok(false) = updater.status() {
// TODO: add timeouts
let mut updater = downloader.download(&archive)?;

sender.send((
Status::Unpacking,
updater.current(),
updater.total()
))?;
}
archives.push(archive);

// Delete segments
while !updater.is_finished() {
// TODO: add timeouts

for archive in archives {
std::fs::remove_file(archive)?;
sender.send((
Status::Downloading,
downloaded + updater.current(),
size
// updater.total()
))?;
}

downloaded += updater.total();
}

// Extract segments

let Some(mut updater) = archive::extract(&archives[0], transition.transition_path()) else {
anyhow::bail!("Failed to extract files from segmented archive: {:?}", archives[0]);
};

while let Ok(false) = updater.status() {
// TODO: add timeouts

sender.send((
Status::Unpacking,
updater.current(),
updater.total()
))?;
}

// Delete segments

for archive in archives {
std::fs::remove_file(archive)?;
}
}
}

Expand Down Expand Up @@ -387,6 +433,7 @@ impl ResolvedTask for DownloadDiffResolvedTask {
BasicStatus::Working(Status::RunPreTransitionCode) => TaskStatus::RunPreTransitionCode,
BasicStatus::Working(Status::Downloading) => TaskStatus::Downloading,
BasicStatus::Working(Status::Unpacking) => TaskStatus::Unpacking,
BasicStatus::Working(Status::StreamUnpacking) => TaskStatus::StreamUnpacking,
BasicStatus::Working(Status::RunTransitionCode) => TaskStatus::RunTransitionCode,
BasicStatus::Working(Status::FinishingTransition) => TaskStatus::FinishingTransition,
BasicStatus::Working(Status::RunPostTransitionCode) => TaskStatus::RunPostTransitionCode,
Expand Down
5 changes: 3 additions & 2 deletions src/ui/components/tasks_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ impl SimpleAsyncComponent for TasksQueueComponent {
TaskStatus::PreparingTransition => (true, tr!("tasks-preparing-transition")),
TaskStatus::FinishingTransition => (true, tr!("tasks-finishing-transition")),

TaskStatus::Downloading => (false, tr!("tasks-downloading")),
TaskStatus::Unpacking => (false, tr!("tasks-unpacking")),
TaskStatus::Downloading => (false, tr!("tasks-downloading")),
TaskStatus::Unpacking => (false, tr!("tasks-unpacking")),
TaskStatus::StreamUnpacking => (false, tr!("tasks-stream-unpacking")),

TaskStatus::DeletingFiles => (true, tr!("tasks-deleting-files")),

Expand Down
1 change: 1 addition & 0 deletions src/ui/components/tasks_queue/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum TaskStatus {
RunPreTransitionCode,
Downloading,
Unpacking,
StreamUnpacking,
RunTransitionCode,
FinishingTransition,
RunPostTransitionCode,
Expand Down
Loading