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

Tauri errors fix #61

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 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 @@ -18,6 +18,7 @@ sled = { version = "0.34.7", features = ["compression"] }
url = "2.2"
uuid = { version = "1.1", features = ["serde", "v4"] }
zip = "0.5"
async_zip = { version = "0.0.13", features = ["full"] }

chrono = { version = "0.4.19", features = ["serde"] }
daedalus = { version = "0.1.16", features = ["bincode"] }
Expand Down
58 changes: 45 additions & 13 deletions theseus/src/api/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::data::ModLoader;
use crate::state::{ModrinthProject, ModrinthVersion, SideType};
use crate::util::fetch::{fetch, fetch_mirrors, write, write_cached_icon};
use crate::State;
use async_zip::tokio::read::seek::ZipFileReader;
use futures::TryStreamExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::{Cursor, Read};
use std::io::Cursor;
thesuzerain marked this conversation as resolved.
Show resolved Hide resolved
use std::path::{Component, PathBuf};
use tokio::fs;
use zip::ZipArchive;

#[derive(Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -149,16 +149,31 @@ async fn install_pack(
let state = &State::get().await?;

let reader = Cursor::new(&file);
let mut zip = ZipArchive::new(reader).map_err(|_| {

// Create zip reader around file
let mut zip_reader = ZipFileReader::new(reader).await.map_err(|_| {
crate::Error::from(crate::ErrorKind::InputError(
"Failed to read input modpack zip".to_string(),
))
})?;

let index_json = zip.by_name("modrinth.index.json");
if let Ok(mut zip_file) = index_json {
// Extract index of modrinth.index.json
let zip_index_option = zip_reader
.file()
.entries()
.iter()
.position(|f| f.entry().filename() == "modrinth.index.json");
if let Some(zip_index) = zip_index_option {
let mut manifest = String::new();
zip_file.read_to_string(&mut manifest)?;
let entry = zip_reader
.file()
.entries()
.get(zip_index)
.unwrap()
.entry()
.clone();
let mut reader = zip_reader.entry(zip_index).await?;
reader.read_to_string_checked(&mut manifest, &entry).await?;

let pack: PackFormat = serde_json::from_str(&manifest)?;

Expand Down Expand Up @@ -260,18 +275,35 @@ async fn install_pack(

let extract_overrides = |overrides: String| async {
let reader = Cursor::new(&file);
let mut zip = ZipArchive::new(reader).unwrap();

let mut overrides_zip =
ZipFileReader::new(reader).await.map_err(|_| {
crate::Error::from(crate::ErrorKind::InputError(
"Failed extract overrides Zip".to_string(),
))
})?;

let profile = profile.clone();
async move {
for i in 0..zip.len() {
let mut file = zip.by_index(i).unwrap();

let file_path = file.mangled_name();
if file_path.starts_with(overrides.clone())
for index in 0..overrides_zip.file().entries().len() {
let mut file = overrides_zip
.file()
.entries()
.get(index)
.unwrap()
.entry()
.clone();

let file_path = PathBuf::from(file.filename());
if file_path.starts_with(&overrides)
&& !file_path.ends_with("/")
{
// Reads the file into the 'content' variable
let mut content = Vec::new();
file.read_to_end(&mut content)?;
let mut reader = overrides_zip.entry(index).await?;
reader
.read_to_end_checked(&mut content, &mut file)
.await?;

let mut new_path = PathBuf::new();
let components = file_path.components().skip(1);
Expand Down
9 changes: 5 additions & 4 deletions theseus/src/api/profile_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub async fn profile_create(

let uuid = Uuid::new_v4();
let path = state.directories.profiles_dir().join(uuid.to_string());

if path.exists() {
if !path.is_dir() {
return Err(ProfileCreationError::NotFolder.into());
Expand All @@ -66,6 +65,7 @@ pub async fn profile_create(
} else {
fs::create_dir_all(&path).await?;
}

println!(
"Creating profile at path {}",
&canonicalize(&path)?.display()
Expand Down Expand Up @@ -140,9 +140,10 @@ pub async fn profile_create(
}

profile.metadata.linked_project_id = linked_project_id;

let mut profiles = state.profiles.write().await;
profiles.insert(profile)?;
{
let mut profiles = state.profiles.write().await;
profiles.insert(profile)?;
}

State::sync().await?;

Expand Down
3 changes: 3 additions & 0 deletions theseus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub enum ErrorKind {
#[error("Could not create profile: {0}")]
ProfileCreationError(#[from] profile_create::ProfileCreationError),

#[error("Zip error: {0}")]
ZipError(#[from] async_zip::error::ZipError),

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