Fix syncing, repairing, add edit method (#111)

* Fix syncing, repairing, add edit method

* comp err

* temp push up

* fixes

* fix more

* add frontend
This commit is contained in:
Geometrically
2023-05-11 10:26:00 -07:00
committed by GitHub
parent 71cf2c53f5
commit 7a0798d9d0
24 changed files with 501 additions and 352 deletions

View File

@@ -5,7 +5,9 @@ use crate::event::emit::{
loading_try_for_each_concurrent,
};
use crate::event::{LoadingBarId, LoadingBarType};
use crate::state::{LinkedData, ModrinthProject, ModrinthVersion, SideType};
use crate::state::{
LinkedData, ModrinthProject, ModrinthVersion, ProfileInstallStage, SideType,
};
use crate::util::fetch::{
fetch, fetch_advanced, fetch_json, fetch_mirrors, write, write_cached_icon,
};
@@ -76,13 +78,28 @@ enum PackDependency {
pub async fn install_pack_from_version_id(
version_id: String,
title: Option<String>,
title: String,
icon_url: Option<String>,
) -> crate::Result<PathBuf> {
let state = State::get().await?;
Box::pin(async move {
let profile = crate::api::profile_create::profile_create(
title.clone(),
"1.19.4".to_string(),
ModLoader::Vanilla,
None,
None,
icon_url.clone(),
None,
Some(true),
)
.await?;
let loading_bar = init_loading(
LoadingBarType::PackFileDownload {
profile_path: profile.clone(),
pack_name: title,
icon: icon_url,
pack_version: version_id.clone(),
},
100.0,
@@ -171,6 +188,7 @@ pub async fn install_pack_from_version_id(
Some(version.project_id),
Some(version.id),
Some(loading_bar),
profile,
)
.await
})
@@ -178,9 +196,36 @@ pub async fn install_pack_from_version_id(
}
pub async fn install_pack_from_file(path: PathBuf) -> crate::Result<PathBuf> {
let file = fs::read(path).await?;
let file = fs::read(&path).await?;
install_pack(bytes::Bytes::from(file), None, None, None, None, None).await
let file_name = path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
let profile = crate::api::profile_create::profile_create(
file_name,
"1.19.4".to_string(),
ModLoader::Vanilla,
None,
None,
None,
None,
Some(true),
)
.await?;
install_pack(
bytes::Bytes::from(file),
None,
None,
None,
None,
None,
profile,
)
.await
}
async fn install_pack(
@@ -190,6 +235,7 @@ async fn install_pack(
project_id: Option<String>,
version_id: Option<String>,
existing_loading_bar: Option<LoadingBarId>,
profile: PathBuf,
) -> crate::Result<PathBuf> {
let state = &State::get().await?;
@@ -261,25 +307,38 @@ async fn install_pack(
.into());
};
let profile_raw = crate::api::profile_create::profile_create(
override_title.unwrap_or_else(|| pack.name.clone()),
game_version.clone(),
mod_loader.unwrap_or(ModLoader::Vanilla),
loader_version.cloned(),
icon,
Some(LinkedData {
let loader_version =
crate::profile_create::get_loader_version_from_loader(
game_version.clone(),
mod_loader.unwrap_or(ModLoader::Vanilla),
loader_version.cloned(),
)
.await?;
crate::api::profile::edit(&profile, |prof| {
prof.metadata.name =
override_title.clone().unwrap_or_else(|| pack.name.clone());
prof.install_stage = ProfileInstallStage::PackInstalling;
prof.metadata.linked_data = Some(LinkedData {
project_id: project_id.clone(),
version_id: version_id.clone(),
}),
Some(true),
)
});
prof.metadata.icon = icon.clone();
prof.metadata.game_version = game_version.clone();
prof.metadata.loader_version = loader_version.clone();
async { Ok(()) }
})
.await?;
let profile = profile_raw.clone();
State::sync().await?;
let profile = profile.clone();
let result = async {
let loading_bar = init_or_edit_loading(
existing_loading_bar,
LoadingBarType::PackDownload {
profile_path: profile.clone(),
pack_name: pack.name.clone(),
icon,
pack_id: project_id,
pack_version: version_id,
},
@@ -413,7 +472,7 @@ async fn install_pack(
emit_loading(
&loading_bar,
29.9,
Some("Done extacting overrides"),
Some("Done extracting overrides"),
)
.await?;
@@ -429,19 +488,21 @@ async fn install_pack(
)?;
}
Ok::<PathBuf, crate::Error>(profile)
Ok::<PathBuf, crate::Error>(profile.clone())
}
.await;
match result {
Ok(profile) => Ok(profile),
Err(err) => {
let _ = crate::api::profile::remove(&profile_raw).await;
let _ = crate::api::profile::remove(&profile).await;
Err(err)
}
}
} else {
let _ = crate::api::profile::remove(&profile).await;
Err(crate::Error::from(crate::ErrorKind::InputError(
"No pack manifest found in mrpack".to_string(),
)))

View File

@@ -98,7 +98,7 @@ pub async fn sync(path: &Path) -> crate::Result<()> {
> = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(path) {
profile.sync().await?;
profile.sync_projects().await?;
Ok(())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
@@ -117,24 +117,18 @@ pub async fn sync(path: &Path) -> crate::Result<()> {
/// Installs/Repairs a profile
#[tracing::instrument]
pub async fn install(path: &Path) -> crate::Result<()> {
let state = State::get().await?;
let result = {
let mut profiles: tokio::sync::RwLockWriteGuard<
crate::state::Profiles,
> = state.profiles.write().await;
let profile = get(path).await?;
if let Some(profile) = profiles.0.get_mut(path) {
crate::launcher::install_minecraft(profile, None).await?;
Ok(())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
path.display().to_string(),
)
.as_error())
}
};
if let Some(profile) = profile {
crate::launcher::install_minecraft(&profile, None).await?;
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
path.display().to_string(),
)
.as_error());
}
State::sync().await?;
result
Ok(())
}
pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
@@ -145,7 +139,7 @@ pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
if let Some(profile) = profiles.0.get_mut(profile_path) {
let loading_bar = init_loading(
LoadingBarType::ProfileUpdate {
profile_uuid: profile.uuid,
profile_path: profile.path.clone(),
profile_name: profile.metadata.name.clone(),
},
100.0,
@@ -162,11 +156,15 @@ pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
100.0,
profile.projects.keys().len(),
None,
|project| update_project(profile_path, project, Some(true)),
|project| async move {
let _ = update_project(profile_path, project).await?;
Ok(())
},
)
.await?;
profile.sync().await?;
profile.sync_projects().await?;
Ok(())
} else {
@@ -182,8 +180,7 @@ pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
pub async fn update_project(
profile_path: &Path,
project_path: &Path,
should_not_sync: Option<bool>,
) -> crate::Result<()> {
) -> crate::Result<PathBuf> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
@@ -194,21 +191,33 @@ pub async fn update_project(
..
} = &project.metadata
{
let path = profile
let (path, new_version) = profile
.add_project_version(update_version.id.clone())
.await?;
if path != project_path {
profile.remove_project(project_path).await?;
profile.remove_project(project_path, Some(true)).await?;
}
if !should_not_sync.unwrap_or(false) {
profile.sync().await?;
let value = profile.projects.remove(project_path);
if let Some(mut project) = value {
if let ProjectMetadata::Modrinth {
ref mut version, ..
} = project.metadata
{
*version = Box::new(new_version);
}
profile.projects.insert(path.clone(), project);
}
return Ok(path);
}
}
Ok(())
Err(crate::ErrorKind::InputError(
"This project cannot be updated!".to_string(),
)
.as_error())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
@@ -227,13 +236,23 @@ pub async fn replace_project(
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile_path) {
let path = profile.add_project_version(version_id).await?;
let (path, new_version) =
profile.add_project_version(version_id).await?;
if path != project {
profile.remove_project(project).await?;
profile.remove_project(project, Some(true)).await?;
}
profile.sync().await?;
let value = profile.projects.remove(project);
if let Some(mut project) = value {
if let ProjectMetadata::Modrinth {
ref mut version, ..
} = project.metadata
{
*version = Box::new(new_version);
}
profile.projects.insert(path.clone(), project);
}
Ok(path)
} else {
@@ -254,9 +273,9 @@ pub async fn add_project_from_version(
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile_path) {
let path = profile.add_project_version(version_id).await?;
let (path, _) = profile.add_project_version(version_id).await?;
profile.sync().await?;
Profile::sync_projects_task(profile.path.clone());
Ok(path)
} else {
@@ -293,7 +312,7 @@ pub async fn add_project_from_path(
)
.await?;
profile.sync().await?;
Profile::sync_projects_task(profile.path.clone());
Ok(path)
} else {
@@ -335,7 +354,7 @@ pub async fn remove_project(
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile) {
profile.remove_project(project).await?;
profile.remove_project(project, None).await?;
Ok(())
} else {

View File

@@ -33,6 +33,7 @@ pub async fn profile_create_empty() -> crate::Result<PathBuf> {
None, // the icon for the profile
None,
None,
None,
)
.await
}
@@ -40,20 +41,20 @@ pub async fn profile_create_empty() -> crate::Result<PathBuf> {
// Creates a profile at the given filepath and adds it to the in-memory state
// Returns filepath at which it can be accessed in the State
#[tracing::instrument]
#[allow(clippy::too_many_arguments)]
pub async fn profile_create(
name: String, // the name of the profile, and relative path
game_version: String, // the game version of the profile
modloader: ModLoader, // the modloader to use
loader_version: Option<String>, // the modloader version to use, set to "latest", "stable", or the ID of your chosen loader. defaults to latest
icon: Option<PathBuf>, // the icon for the profile
icon_url: Option<String>, // the URL icon for a profile (ONLY USED FOR TEMPORARY PROFILES)
linked_data: Option<LinkedData>, // the linked project ID (mainly for modpacks)- used for updating
skip_install_profile: Option<bool>,
) -> crate::Result<PathBuf> {
trace!("Creating new profile. {}", name);
let state = State::get().await?;
Box::pin(async move {
let metadata = state.metadata.read().await;
let uuid = Uuid::new_v4();
let path = state.directories.profiles_dir().join(uuid.to_string());
if path.exists() {
@@ -82,66 +83,8 @@ pub async fn profile_create(
"Creating profile at path {}",
&canonicalize(&path)?.display()
);
let loader = modloader;
let loader = if loader != ModLoader::Vanilla {
let version = loader_version.unwrap_or_else(|| "latest".to_string());
let filter = |it: &LoaderVersion| match version.as_str() {
"latest" => true,
"stable" => it.stable,
id => it.id == *id || format!("{}-{}", game_version, id) == it.id,
};
let loader_data = match loader {
ModLoader::Forge => &metadata.forge,
ModLoader::Fabric => &metadata.fabric,
ModLoader::Quilt => &metadata.quilt,
_ => {
return Err(ProfileCreationError::NoManifest(
loader.to_string(),
)
.into())
}
};
let loaders = &loader_data
.game_versions
.iter()
.find(|it| {
it.id.replace(
daedalus::modded::DUMMY_REPLACE_STRING,
&game_version,
) == game_version
})
.ok_or_else(|| {
ProfileCreationError::ModloaderUnsupported(
loader.to_string(),
game_version.clone(),
)
})?
.loaders;
let loader_version = loaders
.iter()
.cloned()
.find(filter)
.or(
// If stable was searched for but not found, return latest by default
if version == "stable" {
loaders.iter().next().cloned()
} else {
None
},
)
.ok_or_else(|| {
ProfileCreationError::InvalidVersionModloader(
version,
loader.to_string(),
)
})?;
Some((loader_version, loader))
let loader = if modloader != ModLoader::Vanilla {
get_loader_version_from_loader(game_version.clone(), modloader, loader_version).await?
} else {
None
};
@@ -161,8 +104,9 @@ pub async fn profile_create(
)
.await?;
}
if let Some((loader_version, loader)) = loader {
profile.metadata.loader = loader;
profile.metadata.icon_url = icon_url;
if let Some(loader_version) = loader {
profile.metadata.loader = modloader;
profile.metadata.loader_version = Some(loader_version);
}
@@ -203,6 +147,71 @@ pub async fn profile_create(
}).await
}
pub(crate) async fn get_loader_version_from_loader(
game_version: String,
loader: ModLoader,
loader_version: Option<String>,
) -> crate::Result<Option<LoaderVersion>> {
let state = State::get().await?;
let metadata = state.metadata.read().await;
let version = loader_version.unwrap_or_else(|| "latest".to_string());
let filter = |it: &LoaderVersion| match version.as_str() {
"latest" => true,
"stable" => it.stable,
id => it.id == *id || format!("{}-{}", game_version, id) == it.id,
};
let loader_data = match loader {
ModLoader::Forge => &metadata.forge,
ModLoader::Fabric => &metadata.fabric,
ModLoader::Quilt => &metadata.quilt,
_ => {
return Err(
ProfileCreationError::NoManifest(loader.to_string()).into()
)
}
};
let loaders = &loader_data
.game_versions
.iter()
.find(|it| {
it.id
.replace(daedalus::modded::DUMMY_REPLACE_STRING, &game_version)
== game_version
})
.ok_or_else(|| {
ProfileCreationError::ModloaderUnsupported(
loader.to_string(),
game_version.clone(),
)
})?
.loaders;
let loader_version = loaders
.iter()
.cloned()
.find(filter)
.or(
// If stable was searched for but not found, return latest by default
if version == "stable" {
loaders.iter().next().cloned()
} else {
None
},
)
.ok_or_else(|| {
ProfileCreationError::InvalidVersionModloader(
version,
loader.to_string(),
)
})?;
Ok(Some(loader_version))
}
#[derive(thiserror::Error, Debug)]
pub enum ProfileCreationError {
#[error("Profile .json exists: {0}")]