Skip to content

Commit 4fb6320

Browse files
Fix bug where if a modpack had no files available the program would crash (#508)
1 parent 9c15972 commit 4fb6320

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

libium/src/add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub async fn add(
254254

255255
for (project, pin) in cf_projects.into_iter().zip(cf_file_ids) {
256256
if let Some(i) = cf_project_ids.iter().position(|&id| id == project.id) {
257-
cf_project_ids.swap_remove(i);
257+
cf_project_ids.remove(i);
258258
}
259259

260260
let res = 'cf_check: {
@@ -359,7 +359,7 @@ pub async fn add(
359359
.iter()
360360
.position(|id| id == &project.id || project.slug.eq_ignore_ascii_case(id))
361361
{
362-
mr_project_ids.swap_remove(i);
362+
mr_project_ids.remove(i);
363363
}
364364

365365
let res = 'mr_check: {

libium/src/upgrade/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn try_from_cf_file(
9292
download_url: file
9393
.download_url
9494
.ok_or(DistributionDeniedError(file.mod_id, file.id))?,
95-
output: file.file_name.as_str().into(),
95+
output: file.file_name.into(),
9696
length: file.file_length as usize,
9797
dependencies: file
9898
.dependencies

libium/src/upgrade/modpack_downloadable.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub enum Error {
2020
ReqwestError(#[from] reqwest::Error),
2121
DownloadError(#[from] super::Error),
2222
IOError(#[from] std::io::Error),
23+
#[error("The modpack has no files available")]
24+
NoFilesAvailable,
2325
}
2426
type Result<T> = std::result::Result<T, Error>;
2527

@@ -30,12 +32,22 @@ impl ModpackIdentifier {
3032
update: impl Fn(usize) + Send,
3133
) -> Result<PathBuf> {
3234
let (_, download_data) = match self {
33-
ModpackIdentifier::CurseForgeModpack(id) => {
34-
try_from_cf_file(CURSEFORGE_API.get_mod_files(*id).await?.swap_remove(0))?
35-
}
36-
ModpackIdentifier::ModrinthModpack(id) => {
37-
from_mr_version(MODRINTH_API.version_list(id).await?.swap_remove(0))
38-
}
35+
ModpackIdentifier::CurseForgeModpack(id) => try_from_cf_file(
36+
CURSEFORGE_API
37+
.get_mod_files(*id)
38+
.await?
39+
.into_iter()
40+
.nth(0)
41+
.ok_or(Error::NoFilesAvailable)?,
42+
)?,
43+
ModpackIdentifier::ModrinthModpack(id) => from_mr_version(
44+
MODRINTH_API
45+
.version_list(id)
46+
.await?
47+
.into_iter()
48+
.nth(0)
49+
.ok_or(Error::NoFilesAvailable)?,
50+
),
3951
};
4052

4153
let cache_dir = PROJECT_DIRS.cache_dir().join("downloaded");

src/download.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub async fn clean(
3636
dupes.len(),
3737
dupes
3838
.into_iter()
39-
.map(|i| to_download.swap_remove(i).filename())
39+
.map(|i| to_download.remove(i).filename())
4040
.display(", ")
4141
)
4242
.yellow()
@@ -57,11 +57,11 @@ pub async fn clean(
5757
.position(|thing| filename == thing.filename())
5858
{
5959
// Don't download it
60-
to_download.swap_remove(index);
60+
to_download.remove(index);
6161
// Likewise, if it is already installed
6262
} else if let Some(index) = to_install.iter().position(|thing| filename == thing.0) {
6363
// Don't install it
64-
to_install.swap_remove(index);
64+
to_install.remove(index);
6565
// Or else, move the file to `directory`/.old
6666
// If the file is a `.part` file or if the move failed, delete the file
6767
} else if filename.ends_with("part")

src/subcommands/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn remove(profile: &mut Profile, to_remove: Vec<String>) -> Result<()> {
7575

7676
let mut removed = Vec::new();
7777
for index in indices_to_remove {
78-
removed.push(profile.mods.swap_remove(index).name);
78+
removed.push(profile.mods.remove(index).name);
7979
}
8080

8181
if !removed.is_empty() {

0 commit comments

Comments
 (0)