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

Profile mods page #119

Merged
merged 5 commits into from
May 19, 2023
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
49 changes: 37 additions & 12 deletions theseus/src/api/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ pub async fn install(path: &Path) -> crate::Result<()> {

#[tracing::instrument]
#[theseus_macros::debug_pin]
pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
pub async fn update_all(
profile_path: &Path,
) -> crate::Result<HashMap<PathBuf, PathBuf>> {
if let Some(profile) = get(profile_path, None).await? {
let loading_bar = init_loading(
LoadingBarType::ProfileUpdate {
Expand All @@ -137,24 +139,49 @@ pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
)
.await?;

let keys = profile
.projects
.into_iter()
.filter(|(_, project)| {
matches!(
&project.metadata,
ProjectMetadata::Modrinth {
update_version: Some(_),
..
}
)
})
.map(|x| x.0)
.collect::<Vec<_>>();
let len = keys.len();

let map = Arc::new(RwLock::new(HashMap::new()));

use futures::StreamExt;
loading_try_for_each_concurrent(
futures::stream::iter(profile.projects.keys())
.map(Ok::<&PathBuf, crate::Error>),
futures::stream::iter(keys).map(Ok::<PathBuf, crate::Error>),
None,
Some(&loading_bar),
100.0,
profile.projects.keys().len(),
len,
None,
|project| async move {
let _ = update_project(profile_path, project).await?;
|project| async {
let map = map.clone();

async move {
let new_path =
update_project(profile_path, &project).await?;

map.write().await.insert(project, new_path);

Ok(())
Ok(())
}
.await
},
)
.await?;

Ok(())
Ok(Arc::try_unwrap(map).unwrap().into_inner())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
Expand Down Expand Up @@ -271,11 +298,9 @@ pub async fn add_project_from_path(
pub async fn toggle_disable_project(
profile: &Path,
project: &Path,
) -> crate::Result<()> {
) -> crate::Result<PathBuf> {
if let Some(profile) = get(profile, None).await? {
profile.toggle_disable_project(project).await?;

Ok(())
Ok(profile.toggle_disable_project(project).await?)
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile.display().to_string(),
Expand Down
28 changes: 21 additions & 7 deletions theseus/src/state/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl Profile {
let paths = profile.get_profile_project_paths()?;

let projects = crate::state::infer_data_from_files(
profile,
profile.clone(),
paths,
state.directories.caches_dir(),
&state.io_semaphore,
Expand All @@ -205,6 +205,14 @@ impl Profile {
if let Some(profile) = new_profiles.0.get_mut(&path) {
profile.projects = projects;
}

emit_profile(
profile.uuid,
profile.path,
&profile.metadata.name,
ProfilePayloadType::Synced,
)
.await?;
} else {
tracing::warn!(
"Unable to fetch single profile projects: path {path:?} invalid",
Expand Down Expand Up @@ -409,7 +417,7 @@ impl Profile {
pub async fn toggle_disable_project(
&self,
path: &Path,
) -> crate::Result<()> {
) -> crate::Result<PathBuf> {
let state = State::get().await?;
if let Some(mut project) = {
let mut profiles = state.profiles.write().await;
Expand All @@ -425,6 +433,12 @@ impl Profile {

if path.extension().map_or(false, |ext| ext == "disabled") {
project.disabled = false;
new_path.set_file_name(
path.file_name()
.unwrap_or_default()
.to_string_lossy()
.replace(".disabled", ""),
);
} else {
new_path.set_file_name(format!(
"{}.disabled",
Expand All @@ -437,17 +451,17 @@ impl Profile {

let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(&self.path) {
profile.projects.insert(new_path, project);
profile.projects.insert(new_path.clone(), project);
}

Ok(new_path)
} else {
return Err(crate::ErrorKind::InputError(format!(
Err(crate::ErrorKind::InputError(format!(
"Project path does not exist: {:?}",
path
))
.into());
.into())
}

Ok(())
}

pub async fn remove_project(
Expand Down
30 changes: 19 additions & 11 deletions theseus/src/state/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ pub async fn infer_data_from_files(
return_projects.insert(
path,
Project {
disabled: false,
disabled: file_name.ends_with(".disabled"),
metadata: ProjectMetadata::Modrinth {
project: Box::new(project.clone()),
version: Box::new(version.clone()),
Expand All @@ -364,9 +364,17 @@ pub async fn infer_data_from_files(
.filter(|x| x.team_id == project.team)
.cloned()
.collect::<Vec<_>>(),
update_version: update_versions
.get(&hash)
.map(|val| Box::new(val.clone())),
update_version: if let Some(value) =
update_versions.get(&hash)
{
if value.id != version.id {
Some(Box::new(value.clone()))
} else {
None
}
} else {
None
},
incompatible: !version.loaders.contains(
&profile
.metadata
Expand Down Expand Up @@ -404,7 +412,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
metadata: ProjectMetadata::Unknown,
file_name,
},
Expand Down Expand Up @@ -458,7 +466,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(
Expand Down Expand Up @@ -524,7 +532,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(if pack.name.is_empty() {
Expand Down Expand Up @@ -589,7 +597,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(pack.name.unwrap_or(pack.id)),
Expand Down Expand Up @@ -654,7 +662,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(
Expand Down Expand Up @@ -719,7 +727,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: None,
Expand All @@ -739,7 +747,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Unknown,
},
Expand Down
23 changes: 11 additions & 12 deletions theseus_gui/src-tauri/src/api/profile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::api::Result;
use daedalus::modded::LoaderVersion;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use theseus::prelude::*;
use uuid::Uuid;
Expand Down Expand Up @@ -32,7 +33,7 @@ pub async fn profile_get(
#[theseus_macros::debug_pin]
pub async fn profile_list(
clear_projects: Option<bool>,
) -> Result<std::collections::HashMap<PathBuf, Profile>> {
) -> Result<HashMap<PathBuf, Profile>> {
let res = profile::list(clear_projects).await?;
Ok(res)
}
Expand Down Expand Up @@ -71,9 +72,10 @@ pub async fn profile_install(path: &Path) -> Result<()> {
/// invoke('profile_update_all')
#[tauri::command]
#[theseus_macros::debug_pin]
pub async fn profile_update_all(path: &Path) -> Result<()> {
profile::update_all(path).await?;
Ok(())
pub async fn profile_update_all(
path: &Path,
) -> Result<HashMap<PathBuf, PathBuf>> {
Ok(profile::update_all(path).await?)
}

/// Updates a specified project
Expand All @@ -83,9 +85,8 @@ pub async fn profile_update_all(path: &Path) -> Result<()> {
pub async fn profile_update_project(
path: &Path,
project_path: &Path,
) -> Result<()> {
profile::update_project(path, project_path).await?;
Ok(())
) -> Result<PathBuf> {
Ok(profile::update_project(path, project_path).await?)
}

// Adds a project to a profile from a version ID
Expand All @@ -96,8 +97,7 @@ pub async fn profile_add_project_from_version(
path: &Path,
version_id: String,
) -> Result<PathBuf> {
let res = profile::add_project_from_version(path, version_id).await?;
Ok(res)
Ok(profile::add_project_from_version(path, version_id).await?)
}

// Adds a project to a profile from a path
Expand All @@ -121,9 +121,8 @@ pub async fn profile_add_project_from_path(
pub async fn profile_toggle_disable_project(
path: &Path,
project_path: &Path,
) -> Result<()> {
profile::toggle_disable_project(path, project_path).await?;
Ok(())
) -> Result<PathBuf> {
Ok(profile::toggle_disable_project(path, project_path).await?)
}

// Removes a project from a profile
Expand Down
24 changes: 13 additions & 11 deletions theseus_gui/src/components/ui/Instance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ onUnmounted(() => unlisten())
<PlayIcon />
</div>
<div v-else-if="modLoading === true && playing === false" class="cta loading-cta">
<AnimatedLogo class="loading" />
<AnimatedLogo class="loading-indicator" />
</div>
<div
v-else-if="playing === true"
Expand All @@ -210,6 +210,18 @@ onUnmounted(() => unlisten())
</template>
<style lang="scss">
.loading-indicator {
width: 2.5rem !important;
height: 2.5rem !important;
svg {
width: 2.5rem !important;
height: 2.5rem !important;
}
}
</style>
<style lang="scss" scoped>
.instance-small-card {
background-color: var(--color-bg) !important;
display: flex;
Expand Down Expand Up @@ -329,16 +341,6 @@ onUnmounted(() => unlisten())
display: flex;
justify-content: center;
align-items: center;
.loading {
width: 2.5rem !important;
height: 2.5rem !important;
svg {
width: 2.5rem !important;
height: 2.5rem !important;
}
}
}
}
Expand Down
Loading