Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder names #318

Merged
merged 12 commits into from
Jul 22, 2023
2 changes: 1 addition & 1 deletion theseus/src/api/jre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub async fn auto_install_java(java_version: u32) -> crate::Result<PathBuf> {
)
.await?;

let path = state.directories.java_versions_dir();
let path = state.directories.java_versions_dir().await;

if path.exists() {
io::remove_dir_all(&path).await?;
Expand Down
64 changes: 54 additions & 10 deletions theseus/src/api/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
util::io::{self, IOError},
State,
{state::ProfilePathId, State},
};
use serde::{Deserialize, Serialize};

Expand All @@ -11,7 +11,7 @@ pub struct Logs {
}
impl Logs {
async fn build(
profile_uuid: uuid::Uuid,
profile_subpath: &ProfilePathId,
datetime_string: String,
clear_contents: Option<bool>,
) -> crate::Result<Self> {
Expand All @@ -20,7 +20,7 @@ impl Logs {
None
} else {
Some(
get_output_by_datetime(profile_uuid, &datetime_string)
get_output_by_datetime(profile_subpath, &datetime_string)
.await?,
)
},
Expand All @@ -35,7 +35,18 @@ pub async fn get_logs(
clear_contents: Option<bool>,
) -> crate::Result<Vec<Logs>> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};

let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
let mut logs = Vec::new();
if logs_folder.exists() {
for entry in std::fs::read_dir(&logs_folder)
Expand All @@ -48,7 +59,7 @@ pub async fn get_logs(
if let Some(datetime_string) = path.file_name() {
logs.push(
Logs::build(
profile_uuid,
&profile_path,
datetime_string.to_string_lossy().to_string(),
clear_contents,
)
Expand All @@ -69,29 +80,51 @@ pub async fn get_logs_by_datetime(
profile_uuid: uuid::Uuid,
datetime_string: String,
) -> crate::Result<Logs> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};
Ok(Logs {
output: Some(
get_output_by_datetime(profile_uuid, &datetime_string).await?,
get_output_by_datetime(&profile_path, &datetime_string).await?,
),
datetime_string,
})
}

#[tracing::instrument]
pub async fn get_output_by_datetime(
profile_uuid: uuid::Uuid,
profile_subpath: &ProfilePathId,
datetime_string: &str,
) -> crate::Result<String> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder =
state.directories.profile_logs_dir(profile_subpath).await?;
let path = logs_folder.join(datetime_string).join("stdout.log");
Ok(io::read_to_string(&path).await?)
}

#[tracing::instrument]
pub async fn delete_logs(profile_uuid: uuid::Uuid) -> crate::Result<()> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};

let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
{
Expand All @@ -109,8 +142,19 @@ pub async fn delete_logs_by_datetime(
profile_uuid: uuid::Uuid,
datetime_string: &str,
) -> crate::Result<()> {
let profile_path = if let Some(p) =
crate::profile::get_by_uuid(profile_uuid, None).await?
{
p.profile_id()
} else {
return Err(crate::ErrorKind::UnmanagedProfileError(
profile_uuid.to_string(),
)
.into());
};

let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let logs_folder = state.directories.profile_logs_dir(&profile_path).await?;
let path = logs_folder.join(datetime_string);
io::remove_dir_all(&path).await?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions theseus/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod prelude {
profile::{self, Profile},
profile_create, settings,
state::JavaGlobals,
state::{ProfilePathId, ProjectPathId},
util::{
io::{canonicalize, IOError},
jre::JavaVersion,
Expand Down
45 changes: 29 additions & 16 deletions theseus/src/api/pack/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::event::emit::{
};
use crate::event::LoadingBarType;
use crate::pack::install_from::{EnvType, PackFile, PackFileHash};
use crate::state::{LinkedData, ProfileInstallStage, SideType};
use crate::state::{LinkedData, ProfileInstallStage, ProfilePathId, SideType};
use crate::util::fetch::{fetch_mirrors, write};
use crate::State;
use async_zip::tokio::read::seek::ZipFileReader;
Expand All @@ -20,8 +20,8 @@ use super::install_from::{
#[theseus_macros::debug_pin]
pub async fn install_pack(
location: CreatePackLocation,
profile: PathBuf,
) -> crate::Result<PathBuf> {
profile_path: ProfilePathId,
) -> crate::Result<ProfilePathId> {
// Get file from description
let description: CreatePackDescription = match location {
CreatePackLocation::FromVersionId {
Expand All @@ -31,12 +31,16 @@ pub async fn install_pack(
icon_url,
} => {
generate_pack_from_version_id(
project_id, version_id, title, icon_url, profile,
project_id,
version_id,
title,
icon_url,
profile_path,
)
.await?
}
CreatePackLocation::FromFile { path } => {
generate_pack_from_file(path, profile).await?
generate_pack_from_file(path, profile_path).await?
}
};

Expand All @@ -46,7 +50,7 @@ pub async fn install_pack(
let project_id = description.project_id;
let version_id = description.version_id;
let existing_loading_bar = description.existing_loading_bar;
let profile = description.profile;
let profile_path = description.profile_path;

let state = &State::get().await?;

Expand Down Expand Up @@ -125,7 +129,7 @@ pub async fn install_pack(
loader_version.cloned(),
)
.await?;
crate::api::profile::edit(&profile, |prof| {
crate::api::profile::edit(&profile_path, |prof| {
prof.metadata.name =
override_title.clone().unwrap_or_else(|| pack.name.clone());
prof.install_stage = ProfileInstallStage::PackInstalling;
Expand All @@ -142,12 +146,15 @@ pub async fn install_pack(
})
.await?;

let profile = profile.clone();
let profile_path = profile_path.clone();
let result = async {
let loading_bar = init_or_edit_loading(
existing_loading_bar,
LoadingBarType::PackDownload {
profile_path: profile.clone(),
profile_path: profile_path
.get_full_path()
.await?
.clone(),
pack_name: pack.name.clone(),
icon,
pack_id: project_id,
Expand All @@ -169,7 +176,7 @@ pub async fn install_pack(
num_files,
None,
|project| {
let profile = profile.clone();
let profile_path = profile_path.clone();
async move {
//TODO: Future update: prompt user for optional files in a modpack
if let Some(env) = project.env {
Expand Down Expand Up @@ -203,7 +210,10 @@ pub async fn install_pack(
match path {
Component::CurDir
| Component::Normal(_) => {
let path = profile.join(project.path);
let path = profile_path
.get_full_path()
.await?
.join(project.path);
write(
&path,
&file,
Expand Down Expand Up @@ -265,7 +275,10 @@ pub async fn install_pack(

if new_path.file_name().is_some() {
write(
&profile.join(new_path),
&profile_path
.get_full_path()
.await?
.join(new_path),
&content,
&state.io_semaphore,
)
Expand All @@ -285,7 +298,7 @@ pub async fn install_pack(
}

if let Some(profile_val) =
crate::api::profile::get(&profile, None).await?
crate::api::profile::get(&profile_path, None).await?
{
crate::launcher::install_minecraft(
&profile_val,
Expand All @@ -296,14 +309,14 @@ pub async fn install_pack(
State::sync().await?;
}

Ok::<PathBuf, crate::Error>(profile.clone())
Ok::<ProfilePathId, crate::Error>(profile_path.clone())
}
.await;

match result {
Ok(profile) => Ok(profile),
Err(err) => {
let _ = crate::api::profile::remove(&profile).await;
let _ = crate::api::profile::remove(&profile_path).await;

Err(err)
}
Expand All @@ -319,7 +332,7 @@ pub async fn install_pack(
match result {
Ok(profile) => Ok(profile),
Err(err) => {
let _ = crate::api::profile::remove(&profile).await;
let _ = crate::api::profile::remove(&profile_path).await;

Err(err)
}
Expand Down
19 changes: 11 additions & 8 deletions theseus/src/api/pack/install_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::config::MODRINTH_API_URL;
use crate::data::ModLoader;
use crate::event::emit::{emit_loading, init_loading};
use crate::event::{LoadingBarId, LoadingBarType};
use crate::state::{LinkedData, ModrinthProject, ModrinthVersion, SideType};
use crate::state::{
LinkedData, ModrinthProject, ModrinthVersion, ProfilePathId, SideType,
};
use crate::util::fetch::{
fetch, fetch_advanced, fetch_json, write_cached_icon,
};
Expand Down Expand Up @@ -71,7 +73,7 @@ pub enum PackDependency {
Minecraft,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum CreatePackLocation {
FromVersionId {
Expand All @@ -98,14 +100,15 @@ pub struct CreatePackProfile {
pub skip_install_profile: Option<bool>,
}

#[derive(Debug)]
pub struct CreatePackDescription {
pub file: bytes::Bytes,
pub icon: Option<PathBuf>,
pub override_title: Option<String>,
pub project_id: Option<String>,
pub version_id: Option<String>,
pub existing_loading_bar: Option<LoadingBarId>,
pub profile: PathBuf,
pub profile_path: ProfilePathId,
}

pub fn get_profile_from_pack(
Expand Down Expand Up @@ -158,13 +161,13 @@ pub async fn generate_pack_from_version_id(
version_id: String,
title: String,
icon_url: Option<String>,
profile: PathBuf,
profile_path: ProfilePathId,
) -> crate::Result<CreatePackDescription> {
let state = State::get().await?;

let loading_bar = init_loading(
LoadingBarType::PackFileDownload {
profile_path: profile.clone(),
profile_path: profile_path.get_full_path().await?,
pack_name: title,
icon: icon_url,
pack_version: version_id.clone(),
Expand Down Expand Up @@ -253,15 +256,15 @@ pub async fn generate_pack_from_version_id(
project_id: Some(project_id),
version_id: Some(version_id),
existing_loading_bar: Some(loading_bar),
profile,
profile_path,
})
}

#[tracing::instrument]
#[theseus_macros::debug_pin]
pub async fn generate_pack_from_file(
path: PathBuf,
profile: PathBuf,
profile_path: ProfilePathId,
) -> crate::Result<CreatePackDescription> {
let file = io::read(&path).await?;
Ok(CreatePackDescription {
Expand All @@ -271,6 +274,6 @@ pub async fn generate_pack_from_file(
project_id: None,
version_id: None,
existing_loading_bar: None,
profile,
profile_path,
})
}
Loading
Loading