You've already forked AstralRinth
forked from didirus/AstralRinth
Event handling (#75)
* working on amcros * fleshed out draft * added feature support * finished loading * Fixed issue with multiple data types in macro * Working, and added more loading uses * added window scopes * clippy, fmt * working other variants * fmt; clippy * prettier * refactored emissions to use increment * fixed deadlock * doc changes * clippy, prettier * uuid change * restructured events to util * loading restructure * merge fixes * comments mistake * better cfg tauri feature structuring * added extra fields to some loading enum variants * removed Option<> * added pack + version labels * doc change
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
use super::settings::{Hooks, MemorySettings, WindowSize};
|
||||
use crate::config::MODRINTH_API_URL;
|
||||
use crate::data::DirectoryInfo;
|
||||
use crate::event::emit::{
|
||||
emit_profile, init_loading, loading_try_for_each_concurrent,
|
||||
};
|
||||
use crate::event::{LoadingBarType, ProfilePayloadType};
|
||||
use crate::state::projects::Project;
|
||||
use crate::state::{ModrinthVersion, ProjectType};
|
||||
use crate::util::fetch::{fetch, fetch_json, write, write_cached_icon};
|
||||
@@ -17,6 +21,7 @@ use std::{
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::{fs, sync::RwLock};
|
||||
use uuid::Uuid;
|
||||
|
||||
const PROFILE_JSON_PATH: &str = "profile.json";
|
||||
|
||||
@@ -28,6 +33,7 @@ pub const CURRENT_FORMAT_VERSION: u32 = 1;
|
||||
// Represent a Minecraft instance.
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Profile {
|
||||
pub uuid: Uuid, // todo: will be used in restructure to refer to profiles
|
||||
pub path: PathBuf,
|
||||
pub metadata: ProfileMetadata,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -90,6 +96,7 @@ pub struct JavaSettings {
|
||||
impl Profile {
|
||||
#[tracing::instrument]
|
||||
pub async fn new(
|
||||
uuid: Uuid,
|
||||
name: String,
|
||||
version: String,
|
||||
path: PathBuf,
|
||||
@@ -102,6 +109,7 @@ impl Profile {
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
uuid,
|
||||
path: canonicalize(path)?,
|
||||
metadata: ProfileMetadata {
|
||||
name,
|
||||
@@ -368,7 +376,14 @@ impl Profiles {
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn insert(&mut self, profile: Profile) -> crate::Result<&Self> {
|
||||
pub async fn insert(&mut self, profile: Profile) -> crate::Result<&Self> {
|
||||
emit_profile(
|
||||
profile.uuid,
|
||||
profile.path.clone(),
|
||||
&profile.metadata.name,
|
||||
ProfilePayloadType::Added,
|
||||
)
|
||||
.await?;
|
||||
self.0.insert(
|
||||
canonicalize(&profile.path)?
|
||||
.to_str()
|
||||
@@ -387,6 +402,7 @@ impl Profiles {
|
||||
path: &'a Path,
|
||||
) -> crate::Result<&Self> {
|
||||
self.insert(Self::read_profile_from_dir(&canonicalize(path)?).await?)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
@@ -404,9 +420,21 @@ impl Profiles {
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn sync(&self) -> crate::Result<&Self> {
|
||||
stream::iter(self.0.iter())
|
||||
.map(Ok::<_, crate::Error>)
|
||||
.try_for_each_concurrent(None, |(path, profile)| async move {
|
||||
let loading_bar = init_loading(
|
||||
LoadingBarType::ProfileSync,
|
||||
100.0,
|
||||
"Syncing profiles...",
|
||||
)
|
||||
.await?;
|
||||
let num_futs = self.0.len();
|
||||
loading_try_for_each_concurrent(
|
||||
stream::iter(self.0.iter()).map(Ok::<_, crate::Error>),
|
||||
None,
|
||||
Some(&loading_bar),
|
||||
100.0,
|
||||
num_futs,
|
||||
None,
|
||||
|(path, profile)| async move {
|
||||
let json = serde_json::to_vec(&profile)?;
|
||||
|
||||
let json_path = Path::new(&path.to_string_lossy().to_string())
|
||||
@@ -414,8 +442,9 @@ impl Profiles {
|
||||
|
||||
fs::write(json_path, json).await?;
|
||||
Ok::<_, crate::Error>(())
|
||||
})
|
||||
.await?;
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user