Skip to content

Commit

Permalink
Exports (#135)
Browse files Browse the repository at this point in the history
* Initial bug fixes

* fix compile error on non-mac

* Fix even more bugs

* Fix more

* fix more

* fix build

* fix build

* working basic

* removed zip

* working functions

* merge fixes

* fixed loadintg bar bug

* changed to one layer deep

* forge version numbers

* overrides dont include mrpack

* merge

* fixes

* fixes

* fixed deletion

* merge errors

* force sync before export

* removed testing

* missed line

* removed console log

* mac error reverted

---------

Co-authored-by: Jai A <[email protected]>
  • Loading branch information
thesuzerain and Geometrically authored Jun 26, 2023
1 parent 1c3441a commit 47970d9
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 59 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions theseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ futures = "0.3"
reqwest = { version = "0.11", features = ["json", "stream"] }
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["fs"] }
async-recursion = "1.0.4"

notify = { version = "5.1.0", default-features = false }
notify-debouncer-mini = { version = "0.2.1", default-features = false }
Expand Down
14 changes: 8 additions & 6 deletions theseus/src/api/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use tokio::fs;

#[derive(Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
struct PackFormat {
pub struct PackFormat {
pub game: String,
pub format_version: i32,
pub version_id: String,
Expand All @@ -34,7 +34,7 @@ struct PackFormat {

#[derive(Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
struct PackFile {
pub struct PackFile {
pub path: String,
pub hashes: HashMap<PackFileHash, String>,
pub env: Option<HashMap<EnvType, SideType>>,
Expand All @@ -44,7 +44,7 @@ struct PackFile {

#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase", from = "String")]
enum PackFileHash {
pub enum PackFileHash {
Sha1,
Sha512,
Unknown(String),
Expand All @@ -62,14 +62,14 @@ impl From<String> for PackFileHash {

#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase")]
enum EnvType {
pub enum EnvType {
Client,
Server,
}

#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
enum PackDependency {
pub enum PackDependency {
Forge,
FabricLoader,
QuiltLoader,
Expand Down Expand Up @@ -299,7 +299,9 @@ async fn install_pack(
mod_loader = Some(ModLoader::Quilt);
loader_version = Some(value);
}
PackDependency::Minecraft => game_version = Some(value),
PackDependency::Minecraft => {
game_version = Some(value.clone())
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions theseus/src/api/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::event::emit::{init_loading, loading_try_for_each_concurrent};
use crate::event::LoadingBarType;
use crate::prelude::JavaVersion;
use crate::state::ProjectMetadata;
use crate::util::export;
use crate::{
auth::{self, refresh},
event::{emit::emit_profile, ProfilePayloadType},
Expand Down Expand Up @@ -490,6 +491,50 @@ pub async fn remove_project(
}
}

/// Exports the profile to a Modrinth-formatted .mrpack file
// Version ID of uploaded version (ie 1.1.5), not the unique identifying ID of the version (nvrqJg44)
#[tracing::instrument(skip_all)]
pub async fn export_mrpack(
profile_path: &Path,
export_path: PathBuf,
included_overrides: Vec<String>, // which folders to include in the overrides
version_id: Option<String>,
) -> crate::Result<()> {
let state = State::get().await?;
let io_semaphore = state.io_semaphore.0.read().await;
let permit: tokio::sync::SemaphorePermit = io_semaphore.acquire().await?;
let profile = get(profile_path, None).await?.ok_or_else(|| {
crate::ErrorKind::OtherError(format!(
"Tried to export a nonexistent or unloaded profile at path {}!",
profile_path.display()
))
})?;
export::export_mrpack(
&profile,
&export_path,
version_id.unwrap_or("1.0.0".to_string()),
included_overrides,
true,
&permit,
)
.await?;
Ok(())
}

// Given a folder path, populate a Vec of all the subfolders
// Intended to be used for finding potential override folders
// profile
// -- folder1
// -- folder2
// -- file1
// => [folder1, folder2]
#[tracing::instrument]
pub async fn get_potential_override_folders(
profile_path: PathBuf,
) -> crate::Result<Vec<PathBuf>> {
export::get_potential_override_folders(profile_path).await
}

/// Run Minecraft using a profile and the default credentials, logged in credentials,
/// failing with an error if no credentials are available
#[tracing::instrument]
Expand Down
9 changes: 6 additions & 3 deletions theseus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ pub enum ErrorKind {
#[error("Zip error: {0}")]
ZipError(#[from] async_zip::error::ZipError),

#[error("Error: {0}")]
OtherError(String),

#[error("File watching error: {0}")]
NotifyError(#[from] notify::Error),

#[error("Error stripping prefix: {0}")]
StripPrefixError(#[from] std::path::StripPrefixError),

#[error("Error: {0}")]
OtherError(String),
}

#[derive(Debug)]
Expand Down
6 changes: 5 additions & 1 deletion theseus/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Drop for LoadingBarId {
loader_uuid,
},
);
tracing::debug!(
tracing::trace!(
"Exited at {fraction} for loading bar: {:?}",
loader_uuid
);
Expand Down Expand Up @@ -165,6 +165,10 @@ pub enum LoadingBarType {
profile_path: PathBuf,
profile_name: String,
},
ZipExtract {
profile_path: PathBuf,
profile_name: String,
},
}

#[derive(Serialize, Clone)]
Expand Down
75 changes: 37 additions & 38 deletions theseus/src/state/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,44 +224,7 @@ impl Profile {

pub fn sync_projects_task(path: PathBuf) {
tokio::task::spawn(async move {
let res = async {
let state = State::get().await?;
let profile = crate::api::profile::get(&path, None).await?;

if let Some(profile) = profile {
let paths = profile.get_profile_project_paths()?;

let projects = crate::state::infer_data_from_files(
profile.clone(),
paths,
state.directories.caches_dir(),
&state.io_semaphore,
&state.fetch_semaphore,
)
.await?;

let mut new_profiles = state.profiles.write().await;
if let Some(profile) = new_profiles.0.get_mut(&path) {
profile.projects = projects;
}

emit_profile(
profile.uuid,
profile.path,
&profile.metadata.name,
ProfilePayloadType::Synced,
)
.await?;
} else {
tracing::warn!(
"Unable to fetch single profile projects: path {path:?} invalid",
);
}

Ok::<(), crate::Error>(())
}
.await;

let res = Self::sync_projects_inner(path).await;
match res {
Ok(()) => {}
Err(err) => {
Expand All @@ -273,6 +236,42 @@ impl Profile {
});
}

pub async fn sync_projects_inner(path: PathBuf) -> crate::Result<()> {
let state = State::get().await?;
let profile = crate::api::profile::get(&path, None).await?;

if let Some(profile) = profile {
let paths = profile.get_profile_project_paths()?;

let projects = crate::state::infer_data_from_files(
profile.clone(),
paths,
state.directories.caches_dir(),
&state.io_semaphore,
&state.fetch_semaphore,
)
.await?;

let mut new_profiles = state.profiles.write().await;
if let Some(profile) = new_profiles.0.get_mut(&path) {
profile.projects = projects;
}

emit_profile(
profile.uuid,
profile.path,
&profile.metadata.name,
ProfilePayloadType::Synced,
)
.await?;
} else {
tracing::warn!(
"Unable to fetch single profile projects: path {path:?} invalid",
);
}
Ok::<(), crate::Error>(())
}

pub fn get_profile_project_paths(&self) -> crate::Result<Vec<PathBuf>> {
let mut files = Vec::new();
let mut read_paths = |path: &str| {
Expand Down
Loading

0 comments on commit 47970d9

Please sign in to comment.