Skip to content

Commit

Permalink
Bug fixes round 3 (#298)
Browse files Browse the repository at this point in the history
* fixed bugs

* title case

* bugs; ioerror

* reset breadcrumbs

* more fixes

* more fixes

* scrolling bug

* more fixes

* more fixes

* clippy

* canonicalize fix

* fixed requested changes

* removed debouncer update
  • Loading branch information
thesuzerain authored Jul 19, 2023
1 parent 6650cc9 commit 1f478ec
Show file tree
Hide file tree
Showing 34 changed files with 929 additions and 599 deletions.
876 changes: 425 additions & 451 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion theseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ thiserror = "1.0"

tracing = "0.1.37"
tracing-subscriber = {version = "0.2", features = ["chrono"]}
tracing-error = "0.1"
tracing-error = "0.1.0"
tracing-appender = "0.1"

paste = { version = "1.0"}
Expand Down
11 changes: 7 additions & 4 deletions theseus/src/api/handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::path::PathBuf;

use crate::event::{
emit::{emit_command, emit_warning},
CommandPayload,
use crate::{
event::{
emit::{emit_command, emit_warning},
CommandPayload,
},
util::io,
};

/// Handles external functions (such as through URL deep linkage)
Expand Down Expand Up @@ -46,7 +49,7 @@ pub async fn parse_command(
} else {
// We assume anything else is a filepath to an .mrpack file
let path = PathBuf::from(command_string);
let path = path.canonicalize()?;
let path = io::canonicalize(path)?;
if let Some(ext) = path.extension() {
if ext == "mrpack" {
return Ok(CommandPayload::RunMRPack { path });
Expand Down
3 changes: 2 additions & 1 deletion theseus/src/api/jre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::PathBuf;

use crate::event::emit::{emit_loading, init_loading};
use crate::util::fetch::{fetch_advanced, fetch_json};
use crate::util::io;
use crate::util::jre::extract_java_majorminor_version;
use crate::{
state::JavaGlobals,
Expand Down Expand Up @@ -114,7 +115,7 @@ pub async fn auto_install_java(java_version: u32) -> crate::Result<PathBuf> {
let path = state.directories.java_versions_dir();

if path.exists() {
tokio::fs::remove_dir_all(&path).await?;
io::remove_dir_all(&path).await?;
}

let mut archive = zip::ZipArchive::new(std::io::Cursor::new(file))
Expand Down
30 changes: 18 additions & 12 deletions theseus/src/api/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::State;
use crate::{
util::io::{self, IOError},
State,
};
use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;

#[derive(Serialize, Deserialize, Debug)]
pub struct Logs {
Expand Down Expand Up @@ -36,8 +38,11 @@ pub async fn get_logs(
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let mut logs = Vec::new();
if logs_folder.exists() {
for entry in std::fs::read_dir(logs_folder)? {
let entry = entry?;
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
{
let entry =
entry.map_err(|e| IOError::with_path(e, &logs_folder))?;
let path = entry.path();
if path.is_dir() {
if let Some(datetime_string) = path.file_name() {
Expand Down Expand Up @@ -79,21 +84,21 @@ pub async fn get_output_by_datetime(
) -> crate::Result<String> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
Ok(
read_to_string(logs_folder.join(datetime_string).join("stdout.log"))
.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 state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
for entry in std::fs::read_dir(logs_folder)? {
let entry = entry?;
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
{
let entry = entry.map_err(|e| IOError::with_path(e, &logs_folder))?;
let path = entry.path();
if path.is_dir() {
std::fs::remove_dir_all(path)?;
io::remove_dir_all(&path).await?;
}
}
Ok(())
Expand All @@ -106,6 +111,7 @@ pub async fn delete_logs_by_datetime(
) -> crate::Result<()> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
std::fs::remove_dir_all(logs_folder.join(datetime_string))?;
let path = logs_folder.join(datetime_string);
io::remove_dir_all(&path).await?;
Ok(())
}
5 changes: 4 additions & 1 deletion theseus/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ pub mod prelude {
profile::{self, Profile},
profile_create, settings,
state::JavaGlobals,
util::jre::JavaVersion,
util::{
io::{canonicalize, IOError},
jre::JavaVersion,
},
State,
};
}
4 changes: 2 additions & 2 deletions theseus/src/api/pack/install_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::state::{LinkedData, ModrinthProject, ModrinthVersion, SideType};
use crate::util::fetch::{
fetch, fetch_advanced, fetch_json, write_cached_icon,
};
use crate::util::io;
use crate::State;

use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use std::path::PathBuf;
use tokio::fs;

#[derive(Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -263,7 +263,7 @@ pub async fn generate_pack_from_file(
path: PathBuf,
profile: PathBuf,
) -> crate::Result<CreatePackDescription> {
let file = fs::read(&path).await?;
let file = io::read(&path).await?;
Ok(CreatePackDescription {
file: bytes::Bytes::from(file),
icon: None,
Expand Down
10 changes: 8 additions & 2 deletions theseus/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};

use uuid::Uuid;

use crate::state::MinecraftChild;
use crate::{state::MinecraftChild, util::io::IOError};
pub use crate::{
state::{
Hooks, JavaSettings, MemorySettings, Profile, Settings, WindowSize,
Expand Down Expand Up @@ -121,7 +121,13 @@ pub async fn wait_for_by_uuid(uuid: &Uuid) -> crate::Result<()> {
// Kill a running child process directly, and wait for it to be killed
#[tracing::instrument(skip(running))]
pub async fn kill(running: &mut MinecraftChild) -> crate::Result<()> {
running.current_child.write().await.kill().await?;
running
.current_child
.write()
.await
.kill()
.await
.map_err(IOError::from)?;
wait_for(running).await
}

Expand Down
61 changes: 39 additions & 22 deletions theseus/src/api/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::pack::install_from::{
use crate::prelude::JavaVersion;
use crate::state::ProjectMetadata;

use crate::util::io::{self, IOError};
use crate::{
auth::{self, refresh},
event::{emit::emit_profile, ProfilePayloadType},
Expand All @@ -27,11 +28,7 @@ use std::{
sync::Arc,
};
use tokio::io::AsyncReadExt;
use tokio::{
fs::{self, File},
process::Command,
sync::RwLock,
};
use tokio::{fs::File, process::Command, sync::RwLock};

/// Remove a profile
#[tracing::instrument]
Expand Down Expand Up @@ -110,8 +107,8 @@ pub async fn edit_icon(
) -> crate::Result<()> {
let state = State::get().await?;

if let Some(icon) = icon_path {
let bytes = tokio::fs::read(icon).await?;
let res = if let Some(icon) = icon_path {
let bytes = io::read(icon).await?;

let mut profiles = state.profiles.write().await;

Expand All @@ -133,8 +130,6 @@ pub async fn edit_icon(
ProfilePayloadType::Edited,
)
.await?;
State::sync().await?;

Ok(())
}
None => Err(crate::ErrorKind::UnmanagedProfileError(
Expand All @@ -151,7 +146,9 @@ pub async fn edit_icon(
State::sync().await?;

Ok(())
}
};
State::sync().await?;
res
}

// Gets the optimal JRE key for the given profile, using Daedalus
Expand Down Expand Up @@ -416,7 +413,7 @@ pub async fn add_project_from_path(
project_type: Option<String>,
) -> crate::Result<PathBuf> {
if let Some(profile) = get(profile_path, None).await? {
let file = fs::read(path).await?;
let file = io::read(path).await?;
let file_name = path
.file_name()
.unwrap_or_default()
Expand Down Expand Up @@ -525,7 +522,9 @@ pub async fn export_mrpack(

let profile_base_path = &profile.path;

let mut file = File::create(export_path).await?;
let mut file = File::create(&export_path)
.await
.map_err(|e| IOError::with_path(e, &export_path))?;
let mut writer = ZipFileWriter::new(&mut file);

// Create mrpack json configuration file
Expand Down Expand Up @@ -592,9 +591,13 @@ pub async fn export_mrpack(

// File is not in the config file, add it to the .mrpack zip
if path.is_file() {
let mut file = File::open(&path).await?;
let mut file = File::open(&path)
.await
.map_err(|e| IOError::with_path(e, &path))?;
let mut data = Vec::new();
file.read_to_end(&mut data).await?;
file.read_to_end(&mut data)
.await
.map_err(|e| IOError::with_path(e, &path))?;
let builder = ZipEntryBuilder::new(
format!("overrides/{relative_path}"),
Compression::Deflate,
Expand Down Expand Up @@ -639,13 +642,21 @@ pub async fn get_potential_override_folders(
let mrpack_files = get_modrinth_pack_list(&mrpack);

let mut path_list: Vec<PathBuf> = Vec::new();
let mut read_dir = fs::read_dir(&profile_path).await?;
while let Some(entry) = read_dir.next_entry().await? {
let mut read_dir = io::read_dir(&profile_path).await?;
while let Some(entry) = read_dir
.next_entry()
.await
.map_err(|e| IOError::with_path(e, &profile_path))?
{
let path: PathBuf = entry.path();
if path.is_dir() {
// Two layers of files/folders if its a folder
let mut read_dir = fs::read_dir(&path).await?;
while let Some(entry) = read_dir.next_entry().await? {
let mut read_dir = io::read_dir(&path).await?;
while let Some(entry) = read_dir
.next_entry()
.await
.map_err(|e| IOError::with_path(e, &profile_path))?
{
let path: PathBuf = entry.path();
let name = path.strip_prefix(&profile_path)?.to_path_buf();
if !mrpack_files.contains(&name.to_string_lossy().to_string()) {
Expand Down Expand Up @@ -712,9 +723,11 @@ pub async fn run_credentials(
let result = Command::new(command)
.args(&cmd.collect::<Vec<&str>>())
.current_dir(path)
.spawn()?
.spawn()
.map_err(|e| IOError::with_path(e, path))?
.wait()
.await?;
.await
.map_err(IOError::from)?;

if !result.success() {
return Err(crate::ErrorKind::LauncherError(format!(
Expand Down Expand Up @@ -925,8 +938,12 @@ pub async fn build_folder(
path: &Path,
path_list: &mut Vec<PathBuf>,
) -> crate::Result<()> {
let mut read_dir = fs::read_dir(path).await?;
while let Some(entry) = read_dir.next_entry().await? {
let mut read_dir = io::read_dir(path).await?;
while let Some(entry) = read_dir
.next_entry()
.await
.map_err(|e| IOError::with_path(e, path))?
{
let path = entry.path();
if path.is_dir() {
build_folder(&path, path_list).await?;
Expand Down
9 changes: 4 additions & 5 deletions theseus/src/api/profile_create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Theseus profile management interface
use crate::state::LinkedData;
use crate::util::io::{self, canonicalize};
use crate::{
event::{emit::emit_profile, ProfilePayloadType},
prelude::ModLoader,
Expand All @@ -9,11 +10,9 @@ pub use crate::{
State,
};
use daedalus::modded::LoaderVersion;
use dunce::canonicalize;
use futures::prelude::*;

use std::path::PathBuf;
use tokio::fs;
use tokio_stream::wrappers::ReadDirStream;
use tracing::{info, trace};
use uuid::Uuid;
Expand Down Expand Up @@ -48,15 +47,15 @@ pub async fn profile_create(
.into());
}

if ReadDirStream::new(fs::read_dir(&path).await?)
if ReadDirStream::new(io::read_dir(&path).await?)
.next()
.await
.is_some()
{
return Err(ProfileCreationError::NotEmptyFolder.into());
}
} else {
fs::create_dir_all(&path).await?;
io::create_dir_all(&path).await?;
}

info!(
Expand All @@ -80,7 +79,7 @@ pub async fn profile_create(
Profile::new(uuid, name, game_version, path.clone()).await?;
let result = async {
if let Some(ref icon) = icon {
let bytes = tokio::fs::read(icon).await?;
let bytes = io::read(icon).await?;
profile
.set_icon(
&state.directories.caches_dir(),
Expand Down
4 changes: 2 additions & 2 deletions theseus/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Theseus error type
use crate::profile_create;
use crate::{profile_create, util};
use tracing_error::InstrumentError;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -29,7 +29,7 @@ pub enum ErrorKind {
AuthTaskError(#[from] crate::state::AuthTaskError),

#[error("I/O error: {0}")]
IOError(#[from] std::io::Error),
IOError(#[from] util::io::IOError),

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

0 comments on commit 1f478ec

Please sign in to comment.