From c07d5385755d536482dc2a80e3920495c1a7ce99 Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:39:45 +0100 Subject: [PATCH] fix: Launcher imports should work --- packages/core/src/api/cluster/create.rs | 72 +++++++++++++++++---- packages/core/src/api/package/from.rs | 4 ++ packages/core/src/api/package/import/mod.rs | 6 +- 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/packages/core/src/api/cluster/create.rs b/packages/core/src/api/cluster/create.rs index e28262ed..ac7f1127 100644 --- a/packages/core/src/api/cluster/create.rs +++ b/packages/core/src/api/cluster/create.rs @@ -10,21 +10,13 @@ use interpulse::api::modded::LoaderVersion; use onelauncher_utils::io::{self, canonicalize}; use std::path::PathBuf; -/// Creates a [`Cluster`] and adds it to the memory [`State`]. +/// Creates an unfinished [`Cluster`] used for building onto it and adds it to the memory [`State`]. /// Returns a relative filepath ([`ClusterPath`]) which can be used to access the cluster. #[tracing::instrument] #[onelauncher_macros::memory] #[allow(clippy::too_many_arguments)] -pub async fn create_cluster( +pub async fn create_unfinished_cluster( mut name: String, - mc_version: String, - mod_loader: Loader, - loader_version: Option, - icon: Option, - icon_url: Option, - package_data: Option, - skip: Option, - skip_watch: Option, ) -> crate::Result { name = cluster::sanitize_cluster_name(&name); tracing::trace!("creating new cluster {}", name); @@ -60,14 +52,70 @@ pub async fn create_cluster( "creating cluster at path {}", &canonicalize(&path)?.display() ); + + let cluster = Cluster::new(uuid, name, String::from("none")).await?; + let result = async { + { + state + .clusters + .write() + .await + .insert(cluster.clone(), false) + .await?; + } + + State::sync().await?; + let state = State::get().await?; + let mut packages = state.packages.write().await; + packages.add_cluster(&state.directories, cluster.cluster_path()).await; + + Ok(cluster.cluster_path()) + } + .await; + + match result { + Ok(cluster) => Ok(cluster), + Err(err) => { + let _ = cluster::remove(&cluster.cluster_path()).await; + + Err(err) + } + } +} + +/// Creates a [`Cluster`] and adds it to the memory [`State`]. +/// Returns a relative filepath ([`ClusterPath`]) which can be used to access the cluster. +#[tracing::instrument] +#[onelauncher_macros::memory] +#[allow(clippy::too_many_arguments)] +pub async fn create_cluster( + name: String, + mc_version: String, + mod_loader: Loader, + loader_version: Option, + icon: Option, + icon_url: Option, + package_data: Option, + skip: Option, + skip_watch: Option, +) -> crate::Result { + let mut cluster = cluster::get(&create_unfinished_cluster(name).await?) + .await? + .ok_or(anyhow::anyhow!("failed to get cluster"))?; + + let state = State::get().await?; + let loader = if mod_loader == Loader::Vanilla { None } else { get_loader_version(mc_version.clone(), mod_loader, loader_version).await? }; - let mut cluster = Cluster::new(uuid, name, mc_version).await?; let result = async { + cluster.meta.mc_version = mc_version; + cluster.meta.loader = mod_loader; + cluster.meta.loader_version = loader.clone(); + if let Some(ref icon) = icon { let bytes = io::read(state.directories.caches_dir().await.join(icon)).await?; cluster @@ -93,7 +141,7 @@ pub async fn create_cluster( } send_cluster( - uuid, + cluster.uuid, &cluster.cluster_path(), &cluster.meta.name, ClusterPayloadType::Created, diff --git a/packages/core/src/api/package/from.rs b/packages/core/src/api/package/from.rs index 8f7b8a46..fd5eb170 100644 --- a/packages/core/src/api/package/from.rs +++ b/packages/core/src/api/package/from.rs @@ -369,6 +369,10 @@ pub async fn set_cluster_information( .await? }; + // crate::api::cluster::create::create_cluster( + // description.override_title.clone().unwrap_or_else(|| backup_name.to_string()), + // mc_version, mod_loader, loader_version, icon, icon_url, package_data, skip, skip_watch) + crate::api::cluster::edit(&cluster_path, |cl| { cl.meta.name = description .override_title diff --git a/packages/core/src/api/package/import/mod.rs b/packages/core/src/api/package/import/mod.rs index 6476a69c..099d28ac 100644 --- a/packages/core/src/api/package/import/mod.rs +++ b/packages/core/src/api/package/import/mod.rs @@ -129,8 +129,7 @@ pub async fn import_instances( instances: Vec, ) -> crate::Result<()> { for instance in instances { - let cluster_path = ClusterPath::new(&instance); - import_instance(cluster_path, import, base_path.clone(), instance).await?; + import_instance(import, base_path.clone(), instance).await?; } Ok(()) @@ -139,13 +138,14 @@ pub async fn import_instances( #[tracing::instrument] #[onelauncher_macros::memory] pub async fn import_instance( - cluster_path: ClusterPath, import: ImportType, base_path: PathBuf, instance_path: String, ) -> crate::Result<()> { tracing::debug!("importing instance from {instance_path}"); + let cluster_path = crate::cluster::create::create_unfinished_cluster(instance_path.clone()).await?; + let result = match import { ImportType::MultiMC | ImportType::PrismLauncher => { multibased::import_mmc(base_path, instance_path, cluster_path.clone()).await