Library improvements (#126)

* Base impl

* Add grouping

* Fix some styling things

* Run linter

* add missing features

* add dev mode

---------

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
This commit is contained in:
Adrian O.V
2023-06-02 18:36:10 -04:00
committed by GitHub
parent 72fc215641
commit e0e9c3f166
15 changed files with 398 additions and 88 deletions

View File

@@ -1,6 +1,6 @@
//! Authentication flow interface
use crate::{launcher::auth as inner, State};
use futures::prelude::*;
use chrono::Utc;
use tokio::sync::oneshot;
use crate::state::AuthTask;
@@ -71,22 +71,20 @@ pub async fn refresh(user: uuid::Uuid) -> crate::Result<Credentials> {
let state = State::get().await?;
let mut users = state.users.write().await;
let fetch_semaphore = &state.fetch_semaphore;
futures::future::ready(users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"You are not logged in with a Minecraft account!"
))
let mut credentials = users.get(user).ok_or_else(|| {
crate::ErrorKind::OtherError(
"You are not logged in with a Minecraft account!".to_string(),
)
.as_error()
}))
.and_then(|mut credentials| async move {
if chrono::offset::Utc::now() > credentials.expires {
inner::refresh_credentials(&mut credentials, fetch_semaphore)
.await?;
}
users.insert(&credentials).await?;
Ok(credentials)
})
.await
})?;
let fetch_semaphore = &state.fetch_semaphore;
if Utc::now() > credentials.expires {
inner::refresh_credentials(&mut credentials, fetch_semaphore).await?;
}
users.insert(&credentials).await?;
Ok(credentials)
}
/// Remove a user account from the database

View File

@@ -9,6 +9,7 @@ use crate::{
state::{self as st, MinecraftChild},
State,
};
use chrono::Utc;
use daedalus as d;
use daedalus::minecraft::VersionInfo;
use dunce::canonicalize;
@@ -432,6 +433,13 @@ pub async fn launch_minecraft(
let stdout_log_path = logs_dir.join("stdout.log");
let stderr_log_path = logs_dir.join("stderr.log");
crate::api::profile::edit(&profile.path, |prof| {
prof.metadata.last_played = Some(Utc::now());
async { Ok(()) }
})
.await?;
// Create Minecraft child by inserting it into the state
// This also spawns the process and prepares the subsequent processes
let mut state_children = state.children.write().await;

View File

@@ -10,6 +10,7 @@ use crate::util::fetch::{
fetch, fetch_json, write, write_cached_icon, IoSemaphore,
};
use crate::State;
use chrono::{DateTime, Utc};
use daedalus::get_hash;
use daedalus::modded::LoaderVersion;
use dunce::canonicalize;
@@ -30,9 +31,6 @@ const PROFILE_JSON_PATH: &str = "profile.json";
pub(crate) struct Profiles(pub HashMap<PathBuf, Profile>);
// TODO: possibly add defaults to some of these values
pub const CURRENT_FORMAT_VERSION: u32 = 1;
#[derive(
Serialize, Deserialize, Clone, Copy, Debug, Default, Eq, PartialEq,
)]
@@ -75,13 +73,24 @@ pub struct ProfileMetadata {
pub icon: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon_url: Option<String>,
#[serde(default)]
pub groups: Vec<String>,
pub game_version: String,
#[serde(default)]
pub loader: ModLoader,
#[serde(skip_serializing_if = "Option::is_none")]
pub loader_version: Option<LoaderVersion>,
pub format_version: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub linked_data: Option<LinkedData>,
#[serde(default)]
pub date_created: DateTime<Utc>,
#[serde(default)]
pub date_modified: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_played: Option<DateTime<Utc>>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -157,11 +166,14 @@ impl Profile {
name,
icon: None,
icon_url: None,
groups: vec![],
game_version: version,
loader: ModLoader::Vanilla,
loader_version: None,
format_version: CURRENT_FORMAT_VERSION,
linked_data: None,
date_created: Utc::now(),
date_modified: Utc::now(),
last_played: None,
},
projects: HashMap::new(),
java: None,
@@ -182,6 +194,7 @@ impl Profile {
let file =
write_cached_icon(file_name, cache_dir, icon, semaphore).await?;
self.metadata.icon = Some(file);
self.metadata.date_modified = Utc::now();
Ok(())
}
@@ -400,6 +413,7 @@ impl Profile {
file_name: file_name.to_string(),
},
);
profile.metadata.date_modified = Utc::now();
}
}
@@ -446,6 +460,7 @@ impl Profile {
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(&self.path) {
profile.projects.insert(new_path.clone(), project);
profile.metadata.date_modified = Utc::now();
}
Ok(new_path)
@@ -471,6 +486,7 @@ impl Profile {
if let Some(profile) = profiles.0.get_mut(&self.path) {
profile.projects.remove(path);
profile.metadata.date_modified = Utc::now();
}
}
} else {

View File

@@ -26,6 +26,8 @@ pub struct Settings {
pub max_concurrent_writes: usize,
pub version: u32,
pub collapsed_navigation: bool,
#[serde(default)]
pub developer_mode: bool,
}
impl Default for Settings {
@@ -43,6 +45,7 @@ impl Default for Settings {
max_concurrent_writes: 10,
version: CURRENT_FORMAT_VERSION,
collapsed_navigation: false,
developer_mode: false,
}
}
}