Skip to content
Merged
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
22 changes: 15 additions & 7 deletions packages/app-lib/src/api/pack/install_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,27 @@ pub(crate) async fn generate_pack_from_version_id_with_reporter(
instance_id: String,
reason: DownloadReason,
reporter: InstallProgressReporter,
) -> crate::Result<CreatePack> {
) -> crate::Result<Option<CreatePack>> {
let state = State::get().await?;

let version = CachedEntry::get_version(
reporter
.set_context(
InstallErrorContext::new("fetch modpack version")
.project_id(project_id.clone())
.version_id(version_id.clone())
.build(),
)
.await?;
let Some(version) = CachedEntry::get_version(
&version_id,
Some(CacheBehaviour::Bypass),
&state.pool,
&state.api_semaphore,
)
.await?
.ok_or(ErrorKind::InputError(
"invalid version ID specified".to_string(),
))?;
else {
return Ok(None);
};

// Update instance with correct loader and game version from the API version metadata,
// so the UI shows accurate info while the pack file is still downloading.
Expand Down Expand Up @@ -473,7 +481,7 @@ pub(crate) async fn generate_pack_from_version_id_with_reporter(
.await;
}

Ok(CreatePack {
Ok(Some(CreatePack {
file: CreatePackFile::Downloaded(file),
description: CreatePackDescription {
icon,
Expand All @@ -483,7 +491,7 @@ pub(crate) async fn generate_pack_from_version_id_with_reporter(
instance_id,
source_filename: None,
},
})
}))
}

#[tracing::instrument]
Expand Down
28 changes: 28 additions & 0 deletions packages/app-lib/src/api/pack/install_mrpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ async fn install_zipped_mrpack_files_with_reporter_inner(
version_id,
file_hashes,
project_ids,
mrpack_instance_paths(&file).await?,
&state.pool,
)
.await?;
Expand Down Expand Up @@ -1518,6 +1519,33 @@ pub async fn remove_all_related_files(
Ok(())
}

pub(crate) async fn remove_recorded_pack_files(
instance_id: &str,
paths: Vec<String>,
) -> crate::Result<()> {
let instance_full_path =
crate::api::instance::get_full_path(instance_id).await?;
for path in paths {
let Ok(path) = SafeRelativeUtf8UnixPathBuf::try_from(path) else {
continue;
};
if matches!(path.as_str(), "icon.png" | "options.txt")
|| crate::state::content_store::is_managed_content_path(
path.as_str(),
)
{
continue;
}
match io::remove_file(instance_full_path.join(path.as_str())).await {
Ok(_) => (),
Err(err) if err.kind() == ErrorKind::NotFound => (),
Err(err) => return Err(err.into()),
}
}

Ok(())
}

#[cfg(test)]
mod zip_reader_tests {
use super::*;
Expand Down
215 changes: 118 additions & 97 deletions packages/app-lib/src/install/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::{diagnostics, recovery, store};
use crate::ErrorKind;
use crate::analytics::{self, AppEventType};
use crate::api::pack::install_from::{
CreatePackLocation, generate_pack_from_file,
CreatePack, CreatePackLocation, generate_pack_from_file,
generate_pack_from_version_id_with_reporter, get_instance_from_pack,
get_local_pack_instance,
};
Expand Down Expand Up @@ -1756,27 +1756,15 @@ async fn remove_existing_pack_content(
)
.await?
.ok_or(ErrorKind::InputError("unknown instance".to_string()))?;
let (project_id, version_id) = match &metadata.link {
InstanceLink::ModrinthModpack {
project_id,
version_id,
} => (project_id.clone(), version_id.clone()),
InstanceLink::ServerProjectModpack {
content_project_id,
content_version_id,
..
} => (content_project_id.clone(), content_version_id.clone()),
match &metadata.link {
InstanceLink::ModrinthModpack { .. }
| InstanceLink::ServerProjectModpack { .. } => {}
InstanceLink::ImportedModpack { .. } => {
remove_existing_imported_pack_content(
instance_id,
&metadata,
state,
)
.await?;
remove_pack_content_entries(instance_id, &metadata, state).await?;
return Ok(HashSet::new());
}
_ => return Ok(HashSet::new()),
};
}

let disabled_project_ids =
crate::state::instances::commands::list_project_files(
Expand All @@ -1787,32 +1775,28 @@ async fn remove_existing_pack_content(
.into_iter()
.filter_map(|file| (!file.enabled).then_some(file.project_id?))
.collect::<HashSet<_>>();
let reporter = InstallProgressReporter::new(
job_id,
job_state.clone(),
state.pool.clone(),
);
let old_pack = generate_pack_from_version_id_with_reporter(
project_id.clone(),
version_id.clone(),
metadata.instance.name.clone(),
None,
instance_id.to_string(),
DownloadReason::Update,
reporter,
)
.await?;

crate::api::pack::install_mrpack::remove_all_related_files(
instance_id.to_string(),
old_pack.file,
)
.await?;
match linked_pack(job_id, job_state, state, &metadata).await? {
Some(old_pack) => {
crate::api::pack::install_mrpack::remove_all_related_files(
instance_id.to_string(),
old_pack.file,
)
.await?;
}
None => {
remove_pack_content_entries(instance_id, &metadata, state).await?;
Comment thread
IMB11 marked this conversation as resolved.
Resolved
crate::api::pack::install_mrpack::remove_recorded_pack_files(
instance_id,
recorded_pack_paths(&metadata, state).await?,
)
.await?;
}
}

Ok(disabled_project_ids)
}

async fn remove_existing_imported_pack_content(
async fn remove_pack_content_entries(
instance_id: &str,
metadata: &crate::state::InstanceMetadata,
state: &State,
Expand Down Expand Up @@ -2015,26 +1999,17 @@ async fn install_pack_inner(
version_id,
title,
icon_url,
} => {
reporter
.set_context(
InstallErrorContext::new("download modpack file")
.project_id(project_id.clone())
.version_id(version_id.clone())
.build(),
)
.await?;
generate_pack_from_version_id_with_reporter(
project_id,
version_id,
title,
icon_url.filter(|_| !preserve_icon && kept_icon.is_none()),
instance_id.clone(),
reason,
reporter.clone(),
)
.await?
}
} => generate_pack_from_version_id_with_reporter(
project_id,
version_id,
title,
icon_url.filter(|_| !preserve_icon && kept_icon.is_none()),
instance_id.clone(),
reason,
reporter.clone(),
)
.await?
.ok_or_else(missing_pack_version_error)?,
CreatePackLocation::FromFile { path } => {
reporter
.set_context(
Expand Down Expand Up @@ -2174,12 +2149,15 @@ async fn update_backup_scope(
..
} => {
let metadata = instance_metadata(instance_id, state).await?;
let mut packs = vec![(location.clone(), DownloadReason::Modpack)];
packs.extend(
linked_pack_location(&metadata)
.map(|location| (location, DownloadReason::Update)),
);
(instance_id, packs)
match linked_pack(job_id, job_state, state, &metadata).await? {
Some(pack) => {
paths.extend(mrpack_instance_paths(&pack.file).await?);
}
None => {
paths.extend(recorded_pack_paths(&metadata, state).await?);
}
}
(instance_id, vec![location.clone()])
}
InstallRequest::UpdateSharedInstance { instance_id } => {
let data = shared_update
Expand Down Expand Up @@ -2208,42 +2186,36 @@ async fn update_backup_scope(
let packs = data
.modpack
.clone()
.map(|modpack| {
(
shared_instance_pack_location(modpack),
DownloadReason::Modpack,
)
})
.map(shared_instance_pack_location)
.into_iter()
.collect();
(instance_id, packs)
}
_ => return Ok(None),
};
paths.extend(SYNCED_OPTION_FILES.map(str::to_string));
for (location, reason) in packs {
for location in packs {
let pack = match location {
CreatePackLocation::FromVersionId {
project_id,
version_id,
title,
..
} => {
Box::pin(generate_pack_from_version_id_with_reporter(
project_id,
version_id,
title,
None,
instance_id.clone(),
reason,
InstallProgressReporter::new(
job_id,
job_state.clone(),
state.pool.clone(),
),
))
.await?
}
} => Box::pin(generate_pack_from_version_id_with_reporter(
project_id,
version_id,
title,
None,
instance_id.clone(),
DownloadReason::Modpack,
InstallProgressReporter::new(
job_id,
job_state.clone(),
state.pool.clone(),
),
))
.await?
.ok_or_else(missing_pack_version_error)?,
CreatePackLocation::FromFile { path } => {
generate_pack_from_file(path, instance_id.clone()).await?
}
Expand All @@ -2268,9 +2240,42 @@ async fn instance_metadata(
})
}

fn linked_pack_location(
async fn linked_pack(
job_id: Uuid,
job_state: &InstallJobState,
state: &State,
metadata: &crate::state::InstanceMetadata,
) -> crate::Result<Option<CreatePack>> {
let Some((project_id, version_id)) = linked_version(metadata) else {
return Ok(None);
};
let pack = Box::pin(generate_pack_from_version_id_with_reporter(
project_id.to_string(),
version_id.to_string(),
metadata.instance.name.clone(),
None,
metadata.instance.id.clone(),
DownloadReason::Update,
InstallProgressReporter::new(
job_id,
job_state.clone(),
state.pool.clone(),
),
))
.await?;
if pack.is_none() {
tracing::warn!(
%job_id,
version_id,
"Linked modpack version no longer exists on Modrinth"
);
}
Ok(pack)
}

fn linked_version(
metadata: &crate::state::InstanceMetadata,
) -> Option<CreatePackLocation> {
) -> Option<(&str, &str)> {
match &metadata.link {
InstanceLink::ModrinthModpack {
project_id,
Expand All @@ -2280,16 +2285,32 @@ fn linked_pack_location(
content_project_id: project_id,
content_version_id: version_id,
..
} => Some(CreatePackLocation::FromVersionId {
project_id: project_id.clone(),
version_id: version_id.clone(),
title: metadata.instance.name.clone(),
icon_url: None,
}),
} => Some((project_id, version_id)),
_ => None,
}
}

async fn recorded_pack_paths(
metadata: &crate::state::InstanceMetadata,
state: &State,
) -> crate::Result<Vec<String>> {
let Some((_, version_id)) = linked_version(metadata) else {
return Ok(Vec::new());
};
Ok(crate::state::CachedEntry::get_modpack_files(
version_id,
&state.pool,
&state.api_semaphore,
)
.await?
.map(|files| files.paths)
.unwrap_or_default())
}

fn missing_pack_version_error() -> crate::Error {
ErrorKind::InputError("invalid version ID specified".to_string()).into()
}

async fn lock_install_target(
job_state: &InstallJobState,
state: &State,
Expand Down
Loading
Loading