Files
modrinth/packages/app-lib/src/install/recovery.rs
T
Truman GaoandProspector e79e0d024b feat: app onboarding and library (#6923)
* refactor: remove unused legacy onboarded flag

* sqlx prepare

* feat: add onboarding checklist app db and add vue provider

* feat: add welcome page

* feat: add onboarding checklist component

* feat: move checklist visibility source of truth in app db

* feat: split up import modpack button

* feat-small: style tweak

* feat: remove discover section from homepage and add library

* refactor: move welcome screen to component

* refactor: move grid display into /library

* refactor: rename existing components

* refactor: split up the current library page files

* feat: add create group modal

* feat: add instance group definitions

* feat: update accordian for instance groups

* feat: add delete group

* feat: implement rename instance groups

* refactor: move instance group api out

* feat: add context menu for groups

* feat: style instance items in library

* feat: add selected instance item state

* feat: implement action bar for selection

* feat: drag and drop to move between groups

* feat: show ungrouped with header

* feat: improve edit group name

* small fix

* small style fixes

* feat: improve selection behaviour

* fix: dont toggle group when context menu open

* feat: drag and drop multiple selected

* small

* feat: add remove from group in action bar

* feat: add delay on icon hiding in inline input

* feat: improve icon hiding delay

* fix: bad routes.js

* feat: implement sort and filter options

* refactor: dont prefix filenames

* feat: change open/collapse group target

* feat: improve ungrouped behaviour

* feat: add shift to select multiple instances

* fix: icon disappear transition

* feat: add esc while dragging to cancel

* feat: update sorting and moved label

* feat: update context menu

* fix: multiple context menus can be opened

* refactor: instance groups to be identified by ID instead of name, names become display data

* feat: creating new group always adds instance

* feat: create group button, fix instance selection, and increase group name max length

* feat: strokes

* fix: show getting started checklist in all sidebar

* feat: set size on microsoft login screen

* fix: use ButtonStyled in Chips component

* feat: creating groups with selected instances and fix some moving selection and duplication bugs

* pnpm prepr

* feat: add tooltip for instance already in group

* feat: create instance with content in creation flow modal

- search modpack becomes search projects
- creation flow modal context receives prepareProjectInstall and createProjectInstall callbacks, from content-install.ts (shared with ContentInstallModal)

* feat: update instance card styles

* refactor: separate out instance card content

* update styles

* refactor: instance card

* fix: width of instance

* feat: update empty state

* feat: update sort and grouping dropdowns

* feat: better context menu options

* feat: add group action buttons

* feat: add/remove favourites group

* remove manual sort

* feat: improve the "New group" jank

* feat: add group by instance type and change group sorting

* feat: welcome screen styles match figma

* feat: not signed in skins demo

* feat: new hosting empty state

* feat: new instance card style

* feat: add tooltip and spice up animation more

* feat: improve the transitions for groups after dropping many instances

* feat: set instance group membership for bulk moving

* feat: remove fade out

* fix: allow dragging from buttons inside instance card

* fix: library page load in behaviour

* small fix

* feat: add group ordering and reordering

* feat: add drag to reorder groups

* fix: switching page not clearing selected instances

* style fixes

* style fix

* fix spacing

* feat: jump in changes

* feat: add styled newly created instances

* feat: improve jump in cards styles

* fix: welcome screen style

* fix: empty state styles

* feat: max 5 jump in items

* feat: max new instances to 3

* fix: search icon

* feat: clear selection after move

* feat: skins list use check icon styles

* feat: list will be deleted instances and default sort last played

* fix: stroke

* remove: no more group settings in instance general settings

* fix: resolving modpack server install

* feat: icon editor

* feat: add three hue shifted variants

* fix: surprise me doesnt pick symbol

* feat: add customize icon to create instance modal

* feat: skins loading state style

* fix: welcome logo

* fix: copy

* fix: left over usage of ButtonStyled from main's refactor

* feat: some leaked merge stuff

* feat: server empty state styles

* remove: instance helper text

* remove: auto link in set up stage

* feat: auto focus on search when modal open

* fix: delete instance modal style

* remove: modpack meta repeating

* fix: icon

* fix: font weight

* fix: dropdown filter preview heights

* pnpm prepr

* fix: sign into modrinth flashing change in position

* feat: add avatar transparent corner gets padding

* feat: drag and hold over group opens group

* feat: add random icon for custom instance

* feat: update loading icon

* polish: empty state for search

* polish: jump in cards

* fix: avatar could skip checking for image if tauri loads it first

* feat: add icon customize and randomizer in content install modal for add to new instances

* fix: avatar cannot read image pixels due to cors

* refactor: popup notifications toast type for instance sharing notifications

* feat: apply random icons modal and add popup notification for it

* polish: style

* fix: set_webview_visible

* QA

* qa

* feat: add inner white 15 opacity stroke around avatar

* add to catalog

* pnpm prepr

* fix: app event import

* feat: add new icons and backgrounds for icon editor

* feat: recents list generate from randomized icons and save icon in editor + icon editor modal fixes

* qa

* refactor: recipe -> config

* fix: event bus

* polish

* feat: add safe guard against randomizing empty icons

* feat: navigate to instance after creation

* fix: icon file names

* i18n on welcome screen

* i18n pass on PR

* fix: inconsistent max group name length

* refactor: use sqlx macro

* feat: add instance icon background selection drag

* feat: update catalog order

* fix: remove gradient for welcome screen

* remove: frog

* fix: min width

* feat: update corner size

* fix: context menu styles

* prepr

* fix: instance name trimming

* reorder

* fix: instance card loading state stayed until hovering

* feat: add icon randomizer blacklist

* blog

* prepr

---------

Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com>
2026-08-18 01:08:09 +00:00

497 lines
15 KiB
Rust

use super::events::emit_install_job;
use super::model::{
InstallCleanup, InstallErrorView, InstallInterruptReason,
InstallJobDisplay, InstallJobEventKind, InstallJobState, InstallJobStatus,
InstallPhaseDetails, InstallPhaseId, InstallRequest, InstallTarget,
};
use super::store;
use crate::event::InstancePayloadType;
use crate::event::emit::emit_instance;
use crate::state::instances::adapters::sqlite::{content_rows, instance_rows};
use crate::state::{
ContentEntry, ContentSetRemoteRef, ContentSetRemoteRefType,
ContentSetSyncProvider, ContentSetSyncState, InstanceFile,
InstanceMetadata, State,
};
use async_walkdir::WalkDir;
use chrono::Utc;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use uuid::Uuid;
const SHARED_INSTANCE_ROLLBACK_FILE: &str = "rollback.json";
const SHARED_INSTANCE_ROLLBACK_INSTANCE_DIR: &str = "instance";
#[derive(Deserialize, Serialize)]
struct SharedInstanceUpdateRollback {
files: Vec<InstanceFile>,
entries: Vec<ContentEntry>,
}
pub(super) async fn prepare_shared_instance_update_backup(
job_id: Uuid,
metadata: &InstanceMetadata,
state: &State,
) -> crate::Result<PathBuf> {
let staging_dir = state
.directories
.metadata_dir()
.join("install_job_backups")
.join(job_id.to_string());
if tokio::fs::try_exists(&staging_dir).await? {
crate::util::io::remove_dir_all(&staging_dir).await?;
}
crate::util::io::create_dir_all(&staging_dir).await?;
let result = async {
let files = content_rows::get_instance_files(
&metadata.instance.id,
&state.pool,
)
.await?;
let entries = content_rows::get_content_entries(
&metadata.applied_content_set.id,
&state.pool,
)
.await?;
let snapshot = SharedInstanceUpdateRollback { files, entries };
let instance_path = state
.directories
.instances_dir()
.join(&metadata.instance.path);
copy_directory(
&instance_path,
&staging_dir.join(SHARED_INSTANCE_ROLLBACK_INSTANCE_DIR),
state,
)
.await?;
crate::util::io::write(
staging_dir.join(SHARED_INSTANCE_ROLLBACK_FILE),
serde_json::to_vec(&snapshot)?,
)
.await?;
Ok::<(), crate::Error>(())
}
.await;
if result.is_err() {
let _ = crate::util::io::remove_dir_all(&staging_dir).await;
}
result?;
Ok(staging_dir)
}
pub(super) async fn clear_staging_dir(job_state: &InstallJobState) {
let Some(staging_dir) = &job_state.paths.staging_dir else {
return;
};
if let Err(error) = crate::util::io::remove_dir_all(staging_dir).await
&& error.kind() != std::io::ErrorKind::NotFound
{
tracing::warn!(
path = %staging_dir.display(),
"Failed to remove install rollback backup: {error}"
);
}
}
async fn restore_shared_instance_update(
staging_dir: &Path,
rollback: &super::model::InstallRollbackState,
state: &State,
) -> crate::Result<()> {
let snapshot = serde_json::from_slice::<SharedInstanceUpdateRollback>(
&crate::util::io::read(staging_dir.join(SHARED_INSTANCE_ROLLBACK_FILE))
.await?,
)?;
let instance_path = state
.directories
.instances_dir()
.join(&rollback.instance.instance.path);
if tokio::fs::try_exists(&instance_path).await? {
crate::util::io::remove_dir_all(&instance_path).await?;
}
copy_directory(
&staging_dir.join(SHARED_INSTANCE_ROLLBACK_INSTANCE_DIR),
&instance_path,
state,
)
.await?;
content_rows::restore_instance_content_snapshot(
&rollback.instance.instance.id,
&snapshot.files,
&snapshot.entries,
&state.pool,
)
.await?;
restore_instance_metadata(&rollback.instance, state).await?;
Ok(())
}
async fn restore_instance_metadata(
metadata: &InstanceMetadata,
state: &State,
) -> crate::Result<()> {
let content_set_id = metadata.applied_content_set.id.as_str();
let mut tx = state.pool.begin().await?;
instance_rows::update_instance(&metadata.instance, &mut tx).await?;
content_rows::update_content_set(&metadata.applied_content_set, &mut tx)
.await?;
instance_rows::upsert_instance_link(
&metadata.instance.id,
&metadata.link,
&mut tx,
)
.await?;
instance_rows::set_shared_instance_attachment(
&metadata.instance.id,
metadata.shared_instance.as_ref(),
&mut tx,
)
.await?;
instance_rows::replace_instance_groups(
&metadata.instance.id,
&metadata.group_ids,
&mut tx,
)
.await?;
instance_rows::upsert_instance_launch_overrides(
&metadata.launch_overrides,
&mut tx,
)
.await?;
content_rows::delete_content_set_remote_ref(
content_set_id,
ContentSetRemoteRefType::SharedContentSet,
&mut tx,
)
.await?;
content_rows::delete_content_set_sync_state(content_set_id, &mut tx)
.await?;
if let Some(attachment) = &metadata.shared_instance {
content_rows::upsert_content_set_remote_ref(
&ContentSetRemoteRef {
content_set_id: content_set_id.to_string(),
ref_type: ContentSetRemoteRefType::SharedContentSet,
ref_id: attachment.id.clone(),
},
&mut tx,
)
.await?;
content_rows::upsert_content_set_sync_state(
&ContentSetSyncState {
content_set_id: content_set_id.to_string(),
provider: ContentSetSyncProvider::SharedInstance,
applied_update_id: attachment
.applied_version
.map(|value| value.to_string()),
latest_available_update_id: attachment
.latest_version
.map(|value| value.to_string()),
checked_at: Some(Utc::now()),
status: attachment.status,
},
&mut tx,
)
.await?;
}
tx.commit().await?;
Ok(())
}
async fn copy_directory(
source: &Path,
target: &Path,
state: &State,
) -> crate::Result<()> {
crate::util::io::create_dir_all(target).await?;
let mut walker = WalkDir::new(source);
while let Some(entry) = walker.next().await {
let entry = entry.map_err(|error| {
crate::ErrorKind::FSError(format!(
"Failed to read instance backup path: {error}"
))
})?;
let entry_path = entry.path();
let relative_path = entry_path.strip_prefix(source)?;
let target_path = target.join(relative_path);
let file_type = entry.file_type().await?;
if file_type.is_dir() {
crate::util::io::create_dir_all(&target_path).await?;
} else if file_type.is_file() {
crate::util::fetch::copy(
&entry_path,
&target_path,
&state.io_semaphore,
)
.await?;
} else if file_type.is_symlink() {
copy_symlink(&entry_path, &target_path).await?;
}
}
Ok(())
}
async fn copy_symlink(source: &Path, target: &Path) -> crate::Result<()> {
if let Some(parent) = target.parent() {
crate::util::io::create_dir_all(parent).await?;
}
let link_target = tokio::fs::read_link(source).await?;
#[cfg(unix)]
tokio::fs::symlink(link_target, target).await?;
#[cfg(windows)]
{
let metadata = tokio::fs::metadata(source).await?;
if metadata.is_dir() {
tokio::fs::symlink_dir(link_target, target).await?;
} else {
tokio::fs::symlink_file(link_target, target).await?;
}
}
Ok(())
}
pub async fn recover_interrupted_jobs(state: &State) -> crate::Result<()> {
let jobs = store::list_interrupted_candidates(state).await?;
for job in jobs {
let job_id = job.id;
if let Err(error) = recover_interrupted_job(job, state).await {
tracing::error!(
"Error recovering interrupted install job {job_id}: {error}"
);
}
}
Ok(())
}
async fn recover_interrupted_job(
mut job: store::InstallJobRecord,
state: &State,
) -> crate::Result<()> {
if job.state.display.is_none() {
job.state.display = display_from_request(&job.state);
}
if let Some(instance_id) = target_instance_id(&job.state.target)
&& instance_rows::get_instance_by_id(instance_id, &state.pool)
.await?
.is_none()
{
let canceled_phase = job.state.progress.phase;
job.state.error = Some(InstallErrorView::from_message(
"canceled",
canceled_phase,
"Install canceled because the instance was deleted",
));
job.state.record_event(InstallJobEventKind::JobCanceled {
phase: canceled_phase,
});
if let Some(record) = store::finish_active(
job.id,
InstallJobStatus::Canceled,
&job.state,
state,
)
.await?
{
store::dismiss(job.id, state).await?;
clear_staging_dir(&job.state).await;
emit_install_job(&record.snapshot()).await?;
}
return Ok(());
}
let interrupted_phase = job.state.progress.phase;
job.state.record_event(InstallJobEventKind::Interrupted {
reason: InstallInterruptReason::AppClosed,
phase: interrupted_phase,
});
job.state.progress.phase = InstallPhaseId::RollingBack;
job.state.progress.progress = None;
job.state.progress.details = InstallPhaseDetails::Empty;
job.state.error = Some(InstallErrorView::from_message(
"app_closed",
interrupted_phase,
"App closed while install was running",
));
job.state
.record_event(InstallJobEventKind::RollbackStarted {
cleanup: job.state.cleanup.clone(),
});
let cleanup_succeeded = match apply_cleanup(&job.state, state).await {
Ok(()) => {
job.state
.record_event(InstallJobEventKind::RollbackCompleted);
clear_deleted_new_instance_id(&mut job.state);
true
}
Err(error) => {
tracing::error!(
"Error cleaning up interrupted install job {}: {error}",
job.id
);
job.state.rollback_error = Some(InstallErrorView::from_error(
"rollback_error",
InstallPhaseId::RollingBack,
&error,
None,
));
job.state.record_event(InstallJobEventKind::RollbackFailed {
message: error.to_string(),
});
false
}
};
if let Some(record) = store::finish_active(
job.id,
InstallJobStatus::Interrupted,
&job.state,
state,
)
.await?
{
if cleanup_succeeded {
clear_staging_dir(&job.state).await;
}
emit_install_job(&record.snapshot()).await?;
}
Ok(())
}
fn target_instance_id(target: &InstallTarget) -> Option<&str> {
match target {
InstallTarget::NewInstance { instance_id } => instance_id.as_deref(),
InstallTarget::ExistingInstance { instance_id } => Some(instance_id),
}
}
fn clear_deleted_new_instance_id(job_state: &mut InstallJobState) {
if matches!(job_state.cleanup, InstallCleanup::DeleteNewInstance { .. }) {
job_state.target = InstallTarget::NewInstance { instance_id: None };
job_state.cleanup =
InstallCleanup::DeleteNewInstance { instance_id: None };
}
}
fn display_from_request(state: &InstallJobState) -> Option<InstallJobDisplay> {
match &state.request {
InstallRequest::CreateInstance { name, icon_path, .. } => {
Some(InstallJobDisplay {
title: name.clone(),
icon: icon_path.clone(),
})
}
InstallRequest::CreateModpackInstance { location, .. } => match location {
crate::api::pack::install_from::CreatePackLocation::FromVersionId {
title,
icon_url,
..
} => Some(InstallJobDisplay {
title: title.clone(),
icon: icon_url.clone(),
}),
crate::api::pack::install_from::CreatePackLocation::FromFile {
..
} => None,
},
InstallRequest::CreateSharedInstance { data } => {
Some(InstallJobDisplay {
title: data.name.clone(),
icon: data
.modpack
.as_ref()
.and_then(|modpack| modpack.icon_url.clone()),
})
}
InstallRequest::ImportInstance {
instance_folder, ..
} => Some(InstallJobDisplay {
title: instance_folder.clone(),
icon: None,
}),
InstallRequest::DuplicateInstance { .. }
| InstallRequest::InstallExistingInstance { .. }
| InstallRequest::InstallPackToExistingInstance { .. }
| InstallRequest::UpdateSharedInstance { .. } => {
state.rollback.as_ref().map(|rollback| InstallJobDisplay {
title: rollback.instance.instance.name.clone(),
icon: rollback.instance.instance.icon_path.clone(),
})
}
}
}
pub async fn apply_cleanup(
job_state: &InstallJobState,
state: &State,
) -> crate::Result<()> {
match &job_state.cleanup {
InstallCleanup::DeleteNewInstance { instance_id } => {
if let Some(instance_id) = instance_id {
if crate::state::get_instance(instance_id, &state.pool)
.await?
.is_some()
{
crate::state::remove_instance(instance_id, state).await?;
}
if let Err(error) =
emit_instance(instance_id, InstancePayloadType::Removed)
.await
{
tracing::warn!(
"Failed to emit removed instance {instance_id}: {error}"
);
}
}
}
InstallCleanup::RestoreExistingInstance { instance_id } => {
if let Some(rollback) = &job_state.rollback {
if matches!(
&job_state.request,
InstallRequest::UpdateSharedInstance { .. }
) && let Some(staging_dir) = &job_state.paths.staging_dir
{
restore_shared_instance_update(
staging_dir,
rollback,
state,
)
.await?;
} else {
crate::state::instances::commands::set_instance_install_stage(
instance_id,
rollback.install_stage,
&state.pool,
)
.await?;
}
if let Err(error) =
emit_instance(instance_id, InstancePayloadType::Edited)
.await
{
tracing::warn!(
"Failed to emit restored instance {instance_id}: {error}"
);
}
}
}
}
Ok(())
}