Updating + Profile Repairs + Performance Improvements (#97)

* repairing

* Main framework for updating

* add jsconfig

* more work

* Improve performance

* Finish updating

* run lint
This commit is contained in:
Geometrically
2023-04-26 10:28:08 -07:00
committed by GitHub
parent c53104c28e
commit f0b8a708a3
48 changed files with 1217 additions and 894 deletions

View File

@@ -47,7 +47,7 @@ pub async fn authenticate(
})?;
let credentials = flow.extract_credentials(&state.io_semaphore).await?;
users.insert(&credentials)?;
users.insert(&credentials).await?;
if state.settings.read().await.default_user.is_none() {
let mut settings = state.settings.write().await;
@@ -65,7 +65,7 @@ pub async fn refresh(user: uuid::Uuid) -> crate::Result<Credentials> {
let mut users = state.users.write().await;
let io_sempahore = &state.io_semaphore;
futures::future::ready(users.get(user)?.ok_or_else(|| {
futures::future::ready(users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"Tried to refresh nonexistent user with ID {user}"
))
@@ -75,7 +75,7 @@ pub async fn refresh(user: uuid::Uuid) -> crate::Result<Credentials> {
if chrono::offset::Utc::now() > credentials.expires {
inner::refresh_credentials(&mut credentials, io_sempahore).await?;
}
users.insert(&credentials)?;
users.insert(&credentials).await?;
Ok(credentials)
})
.await
@@ -89,14 +89,10 @@ pub async fn remove_user(user: uuid::Uuid) -> crate::Result<()> {
if state.settings.read().await.default_user == Some(user) {
let mut settings = state.settings.write().await;
settings.default_user = users
.0
.first()?
.map(|it| uuid::Uuid::from_slice(&it.0))
.transpose()?;
settings.default_user = users.0.values().next().map(|it| it.id);
}
users.remove(user)?;
users.remove(user).await?;
Ok(())
}
@@ -106,15 +102,15 @@ pub async fn has_user(user: uuid::Uuid) -> crate::Result<bool> {
let state = State::get().await?;
let users = state.users.read().await;
users.contains(user)
Ok(users.contains(user))
}
/// Get a copy of the list of all user credentials
#[tracing::instrument]
pub async fn users() -> crate::Result<Box<[Credentials]>> {
pub async fn users() -> crate::Result<Vec<Credentials>> {
let state = State::get().await?;
let users = state.users.read().await;
users.iter().collect()
Ok(users.0.values().cloned().collect())
}
/// Get a specific user by user ID
@@ -123,7 +119,7 @@ pub async fn users() -> crate::Result<Box<[Credentials]>> {
pub async fn get_user(user: uuid::Uuid) -> crate::Result<Credentials> {
let state = State::get().await?;
let users = state.users.read().await;
let user = users.get(user)?.ok_or_else(|| {
let user = users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"Tried to get nonexistent user with ID {user}"
))

View File

@@ -40,10 +40,10 @@ pub async fn autodetect_java_globals() -> crate::Result<JavaGlobals> {
// this can be overwritten by the user a profile-by-profile basis
pub async fn get_optimal_jre_key(profile: &Profile) -> crate::Result<String> {
let state = State::get().await?;
let metadata = state.metadata.read().await;
// Fetch version info from stored profile game_version
let version = state
.metadata
let version = metadata
.minecraft
.versions
.iter()
@@ -60,6 +60,7 @@ pub async fn get_optimal_jre_key(profile: &Profile) -> crate::Result<String> {
&state,
version,
profile.metadata.loader_version.as_ref(),
None,
)
.await?;
let optimal_key = match version_info

View File

@@ -5,7 +5,7 @@ pub use daedalus::modded::Manifest;
#[tracing::instrument]
pub async fn get_minecraft_versions() -> crate::Result<VersionManifest> {
let state = State::get().await?;
let tags = state.metadata.minecraft.clone();
let tags = state.metadata.read().await.minecraft.clone();
Ok(tags)
}
@@ -13,7 +13,7 @@ pub async fn get_minecraft_versions() -> crate::Result<VersionManifest> {
#[tracing::instrument]
pub async fn get_fabric_versions() -> crate::Result<Manifest> {
let state = State::get().await?;
let tags = state.metadata.fabric.clone();
let tags = state.metadata.read().await.fabric.clone();
Ok(tags)
}
@@ -21,7 +21,7 @@ pub async fn get_fabric_versions() -> crate::Result<Manifest> {
#[tracing::instrument]
pub async fn get_forge_versions() -> crate::Result<Manifest> {
let state = State::get().await?;
let tags = state.metadata.forge.clone();
let tags = state.metadata.read().await.forge.clone();
Ok(tags)
}

View File

@@ -1,8 +1,10 @@
use crate::config::MODRINTH_API_URL;
use crate::data::ModLoader;
use crate::event::emit::{init_loading, loading_try_for_each_concurrent};
use crate::event::emit::{
emit_loading, init_loading, loading_try_for_each_concurrent,
};
use crate::event::LoadingBarType;
use crate::state::{ModrinthProject, ModrinthVersion, SideType};
use crate::state::{LinkedData, ModrinthProject, ModrinthVersion, SideType};
use crate::util::fetch::{
fetch, fetch_json, fetch_mirrors, write, write_cached_icon,
};
@@ -218,13 +220,18 @@ async fn install_pack(
};
let pack_name = pack.name.clone();
let profile = crate::api::profile_create::profile_create(
pack.name,
game_version.clone(),
mod_loader.unwrap_or(ModLoader::Vanilla),
loader_version,
icon,
project_id.clone(),
Some(LinkedData {
project_id: project_id.clone(),
version_id: version_id.clone(),
}),
Some(true),
)
.await?;
@@ -238,6 +245,7 @@ async fn install_pack(
"Downloading modpack...",
)
.await?;
let num_files = pack.files.len();
use futures::StreamExt;
loading_try_for_each_concurrent(
@@ -245,7 +253,7 @@ async fn install_pack(
.map(Ok::<PackFile, crate::Error>),
None,
Some(&loading_bar),
100.0,
80.0,
num_files,
None,
|project| {
@@ -344,11 +352,22 @@ async fn install_pack(
.await
};
emit_loading(&loading_bar, 0.05, Some("Extracting overrides")).await?;
extract_overrides("overrides".to_string()).await?;
extract_overrides("client_overrides".to_string()).await?;
emit_loading(&loading_bar, 0.1, Some("Done extacting overrides"))
.await?;
super::profile::sync(&profile).await?;
if let Some(profile) = crate::api::profile::get(&profile).await? {
crate::launcher::install_minecraft(&profile, Some(loading_bar))
.await?;
} else {
emit_loading(&loading_bar, 0.1, Some("Done extacting overrides"))
.await?;
}
Ok(profile)
} else {
Err(crate::Error::from(crate::ErrorKind::InputError(

View File

@@ -1,4 +1,7 @@
//! Theseus profile management interface
use crate::event::emit::{init_loading, loading_try_for_each_concurrent};
use crate::event::LoadingBarType;
use crate::state::ProjectMetadata;
use crate::{
auth::{self, refresh},
event::{emit::emit_profile, ProfilePayloadType},
@@ -22,7 +25,7 @@ pub async fn remove(path: &Path) -> crate::Result<()> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get(path) {
if let Some(profile) = profiles.remove(path).await? {
emit_profile(
profile.uuid,
profile.path.clone(),
@@ -32,8 +35,6 @@ pub async fn remove(path: &Path) -> crate::Result<()> {
.await?;
}
profiles.remove(path).await?;
Ok(())
}
@@ -108,20 +109,151 @@ pub async fn sync(path: &Path) -> crate::Result<()> {
result
}
/// Add a project from a version
/// Installs/Repairs a profile
#[tracing::instrument]
pub async fn add_project_from_version(
profile: &Path,
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;
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())
}
};
State::sync().await?;
result
}
pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile_path) {
let loading_bar = init_loading(
LoadingBarType::ProfileUpdate {
profile_uuid: profile.uuid,
profile_name: profile.metadata.name.clone(),
},
100.0,
"Updating profile...",
)
.await?;
use futures::StreamExt;
loading_try_for_each_concurrent(
futures::stream::iter(profile.projects.keys())
.map(Ok::<&PathBuf, crate::Error>),
None,
Some(&loading_bar),
100.0,
profile.projects.len(),
None,
|project| update_project(profile_path, project, Some(true)),
)
.await?;
profile.sync().await?;
Ok(())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
)
.as_error())
}
}
pub async fn update_project(
profile_path: &Path,
project_path: &Path,
should_not_sync: Option<bool>,
) -> crate::Result<()> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile_path) {
if let Some(project) = profile.projects.get(project_path) {
if let ProjectMetadata::Modrinth {
update_version: Some(update_version),
..
} = &project.metadata
{
let path = profile
.add_project_version(update_version.id.clone())
.await?;
if path != project_path {
profile.remove_project(project_path).await?;
}
if !should_not_sync.unwrap_or(false) {
profile.sync().await?;
}
}
}
Ok(())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
)
.as_error())
}
}
/// Replaces a project given a new version ID
pub async fn replace_project(
profile_path: &Path,
project: &Path,
version_id: String,
) -> crate::Result<PathBuf> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile) {
profile.add_project_version(version_id).await
if let Some(profile) = profiles.0.get_mut(profile_path) {
let path = profile.add_project_version(version_id).await?;
if path != project {
profile.remove_project(project).await?;
}
profile.sync().await?;
Ok(path)
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile.display().to_string(),
profile_path.display().to_string(),
)
.as_error())
}
}
/// Add a project from a version
#[tracing::instrument]
pub async fn add_project_from_version(
profile_path: &Path,
version_id: String,
) -> crate::Result<PathBuf> {
let state = State::get().await?;
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?;
profile.sync().await?;
Ok(path)
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
)
.as_error())
}
@@ -130,14 +262,14 @@ pub async fn add_project_from_version(
/// Add a project from an FS path
#[tracing::instrument]
pub async fn add_project_from_path(
profile: &Path,
profile_path: &Path,
path: &Path,
project_type: Option<String>,
) -> crate::Result<PathBuf> {
let state = State::get().await?;
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(profile) {
if let Some(profile) = profiles.0.get_mut(profile_path) {
let file = fs::read(path).await?;
let file_name = path
.file_name()
@@ -145,16 +277,20 @@ pub async fn add_project_from_path(
.to_string_lossy()
.to_string();
profile
let path = profile
.add_project_bytes(
&file_name,
bytes::Bytes::from(file),
project_type.and_then(|x| serde_json::from_str(&x).ok()),
)
.await
.await?;
profile.sync().await?;
Ok(path)
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile.display().to_string(),
profile_path.display().to_string(),
)
.as_error())
}
@@ -214,7 +350,7 @@ pub async fn run(path: &Path) -> crate::Result<Arc<RwLock<MinecraftChild>>> {
} else {
// If no default account, try to use a logged in account
let users = auth::users().await?;
let last_account = users.iter().next();
let last_account = users.first();
if let Some(last_account) = last_account {
refresh(last_account.id).await?
} else {
@@ -233,6 +369,7 @@ pub async fn run_credentials(
) -> crate::Result<Arc<RwLock<MinecraftChild>>> {
let state = State::get().await?;
let settings = state.settings.read().await;
let metadata = state.metadata.read().await;
let profile = get(path).await?.ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"Tried to run a nonexistent or unloaded profile at path {}!",
@@ -240,8 +377,7 @@ pub async fn run_credentials(
))
})?;
let version = state
.metadata
let version = metadata
.minecraft
.versions
.iter()
@@ -256,6 +392,7 @@ pub async fn run_credentials(
&state,
version,
profile.metadata.loader_version.as_ref(),
None,
)
.await?;
let pre_launch_hooks =
@@ -358,9 +495,6 @@ pub async fn run_credentials(
};
let mc_process = crate::launcher::launch_minecraft(
&profile.metadata.game_version,
&profile.metadata.loader_version,
&profile.path,
java_install,
java_args,
env_args,

View File

@@ -1,4 +1,5 @@
//! Theseus profile management interface
use crate::state::LinkedData;
use crate::{
event::{emit::emit_profile, ProfilePayloadType},
jre,
@@ -29,6 +30,7 @@ pub async fn profile_create_empty() -> crate::Result<PathBuf> {
None, // the modloader version to use, set to "latest", "stable", or the ID of your chosen loader
None, // the icon for the profile
None,
None,
)
.await
}
@@ -42,9 +44,11 @@ pub async fn profile_create(
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
linked_project_id: Option<String>, // the linked project ID (mainly for modpacks)- used for updating
linked_data: Option<LinkedData>, // the linked project ID (mainly for modpacks)- used for updating
skip_install_profile: Option<bool>,
) -> crate::Result<PathBuf> {
let state = State::get().await?;
let metadata = state.metadata.read().await;
let uuid = Uuid::new_v4();
let path = state.directories.profiles_dir().join(uuid.to_string());
@@ -86,8 +90,8 @@ pub async fn profile_create(
};
let loader_data = match loader {
ModLoader::Forge => &state.metadata.forge,
ModLoader::Fabric => &state.metadata.fabric,
ModLoader::Forge => &metadata.forge,
ModLoader::Fabric => &metadata.fabric,
_ => {
return Err(ProfileCreationError::NoManifest(
loader.to_string(),
@@ -157,7 +161,7 @@ pub async fn profile_create(
profile.metadata.loader_version = Some(loader_version);
}
profile.metadata.linked_project_id = linked_project_id;
profile.metadata.linked_data = linked_data;
// Attempts to find optimal JRE for the profile from the JavaGlobals
// Finds optimal key, and see if key has been set in JavaGlobals
@@ -179,11 +183,15 @@ pub async fn profile_create(
ProfilePayloadType::Created,
)
.await?;
{
let mut profiles = state.profiles.write().await;
profiles.insert(profile).await?;
profiles.insert(profile.clone()).await?;
}
if !skip_install_profile.unwrap_or(false) {
crate::launcher::install_minecraft(&profile, None).await?;
}
State::sync().await?;
Ok(path)

View File

@@ -1,18 +1,16 @@
//! Theseus tag management interface
pub use crate::{
state::{
Category, DonationPlatform, GameVersion, License, Loader, TagBundle,
},
state::{Category, DonationPlatform, GameVersion, Loader, Tags},
State,
};
// Get bundled set of tags
#[tracing::instrument]
pub async fn get_tag_bundle() -> crate::Result<TagBundle> {
pub async fn get_tag_bundle() -> crate::Result<Tags> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_tag_bundle()
Ok(tags.get_tag_bundle())
}
/// Get category tags
@@ -21,7 +19,7 @@ pub async fn get_category_tags() -> crate::Result<Vec<Category>> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_categories()
Ok(tags.get_categories())
}
/// Get report type tags
@@ -30,7 +28,7 @@ pub async fn get_report_type_tags() -> crate::Result<Vec<String>> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_report_types()
Ok(tags.get_report_types())
}
/// Get loader tags
@@ -39,7 +37,7 @@ pub async fn get_loader_tags() -> crate::Result<Vec<Loader>> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_loaders()
Ok(tags.get_loaders())
}
/// Get game version tags
@@ -48,16 +46,7 @@ pub async fn get_game_version_tags() -> crate::Result<Vec<GameVersion>> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_game_versions()
}
/// Get license tags
#[tracing::instrument]
pub async fn get_license_tags() -> crate::Result<Vec<License>> {
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_licenses()
Ok(tags.get_game_versions())
}
/// Get donation platform tags
@@ -67,5 +56,5 @@ pub async fn get_donation_platform_tags() -> crate::Result<Vec<DonationPlatform>
let state = State::get().await?;
let tags = state.tags.read().await;
tags.get_donation_platforms()
Ok(tags.get_donation_platforms())
}