Skip to content

Commit

Permalink
fix: Launcher imports should work
Browse files Browse the repository at this point in the history
  • Loading branch information
LynithDev committed Nov 1, 2024
1 parent ef4154c commit c07d538
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
72 changes: 60 additions & 12 deletions packages/core/src/api/cluster/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
icon: Option<PathBuf>,
icon_url: Option<String>,
package_data: Option<PackageData>,
skip: Option<bool>,
skip_watch: Option<bool>,
) -> crate::Result<ClusterPath> {
name = cluster::sanitize_cluster_name(&name);
tracing::trace!("creating new cluster {}", name);
Expand Down Expand Up @@ -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<String>,
icon: Option<PathBuf>,
icon_url: Option<String>,
package_data: Option<PackageData>,
skip: Option<bool>,
skip_watch: Option<bool>,
) -> crate::Result<ClusterPath> {
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
Expand All @@ -93,7 +141,7 @@ pub async fn create_cluster(
}

send_cluster(
uuid,
cluster.uuid,
&cluster.cluster_path(),
&cluster.meta.name,
ClusterPayloadType::Created,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/api/package/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ pub async fn set_cluster_information<S: ::std::hash::BuildHasher>(
.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
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/api/package/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ pub async fn import_instances(
instances: Vec<String>,
) -> 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(())
Expand All @@ -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
Expand Down

0 comments on commit c07d538

Please sign in to comment.