Skip to content

Commit

Permalink
Fix syncing, repairing, add edit method (#111)
Browse files Browse the repository at this point in the history
* Fix syncing, repairing, add edit method

* comp err

* temp push up

* fixes

* fix more

* add frontend
  • Loading branch information
Geometrically committed May 11, 2023
1 parent 71cf2c5 commit 7a0798d
Show file tree
Hide file tree
Showing 24 changed files with 502 additions and 353 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gui-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
- name: Install dependencies
run: yarn install --immutable --immutable-cache --check-cache
- name: Run Lint
run: npm run lint
run: yarn run lint
- name: Build
run: npm run build
run: yarn run build
99 changes: 80 additions & 19 deletions theseus/src/api/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -171,16 +188,44 @@ pub async fn install_pack_from_version_id(
Some(version.project_id),
Some(version.id),
Some(loading_bar),
profile,
)
.await
})
.await
}

pub async fn install_pack_from_file(path: PathBuf) -> crate::Result<PathBuf> {
let file = fs::read(path).await?;

install_pack(bytes::Bytes::from(file), None, None, None, None, None).await
let file = fs::read(&path).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(
Expand All @@ -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?;

Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -413,7 +472,7 @@ async fn install_pack(
emit_loading(
&loading_bar,
29.9,
Some("Done extacting overrides"),
Some("Done extracting overrides"),
)
.await?;

Expand All @@ -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(),
)))
Expand Down
87 changes: 53 additions & 34 deletions theseus/src/api/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<()> {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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;

Expand All @@ -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(),
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 7a0798d

Please sign in to comment.