Skip to content
Open
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
91 changes: 91 additions & 0 deletions mod_util/src/mod_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
path::{Path, PathBuf},
};

use zip::read::ZipFile;
use zip::ZipArchive;

use crate::mod_info::{ModInfo, Version};
Expand Down Expand Up @@ -158,12 +159,61 @@
self.internal.get_file(path)
}

// Returns a list of all files/subdirectories that live in `dir`.
pub fn read_dir(&self, dir: &str) -> Result<Vec<ModEntry>> {
self.internal.read_dir(dir)
}

#[must_use]
pub const fn wube_mods() -> [&'static str; 5] {
["core", "base", "elevated-rails", "quality", "space-age"]
}
}

/// An object which represents a file or folder inside of a Mod, like `fs::DirEntry`.
#[derive(Debug)]
pub struct ModEntry {
path: PathBuf,
is_file: bool,
// Can add all sorts of fancy info as we like
}

impl ModEntry {
pub fn path(&self) -> &PathBuf {

Check warning

Code scanning / clippy

this method could have a #[must_use] attribute Warning

this method could have a #[must\_use] attribute
&self.path
}
Comment on lines +182 to +184

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
pub fn is_file(&self) -> bool {

Check warning

Code scanning / clippy

this method could have a #[must_use] attribute Warning

this method could have a #[must\_use] attribute
self.is_file
}
Comment on lines +185 to +187

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
pub fn is_dir(&self) -> bool {

Check warning

Code scanning / clippy

this method could have a #[must_use] attribute Warning

this method could have a #[must\_use] attribute
!self.is_file
}
Comment on lines +188 to +190

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
// I don't think we have to worry about symlinks...
}

impl TryFrom<std::fs::DirEntry> for ModEntry {
type Error = ModError;

fn try_from(item: std::fs::DirEntry) -> Result<ModEntry> {

Check warning

Code scanning / clippy

unnecessary structure name repetition Warning

unnecessary structure name repetition
let metadata = item.metadata()?;
Ok(ModEntry {

Check warning

Code scanning / clippy

unnecessary structure name repetition Warning

unnecessary structure name repetition
path: item.path(),
is_file: metadata.is_file(),
})
}
}

impl TryFrom<&ZipFile<'_, File>> for ModEntry {
type Error = ModError;

fn try_from(item: &ZipFile<'_, File>) -> Result<ModEntry> {

Check warning

Code scanning / clippy

unnecessary structure name repetition Warning

unnecessary structure name repetition
Ok(ModEntry {

Check warning

Code scanning / clippy

unnecessary structure name repetition Warning

unnecessary structure name repetition
path: item.name().into(),
is_file: item.is_file(),
})
}
}

#[derive(Debug)]
enum ModType {
Folder {
Expand Down Expand Up @@ -258,6 +308,47 @@
}
}
}

fn read_dir(&self, dir: &str) -> Result<Vec<ModEntry>> {
match self {
Self::Folder { path } => {
let path = path.join(dir);
if !path.exists() {
return Err(ModError::PathDoesNotExist(path));
}

return std::fs::read_dir(&path)?
.filter_map(|x| x.ok())

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
.map(|x| ModEntry::try_from(x))

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
.collect();
Comment on lines +320 to +323

Check warning

Code scanning / clippy

unneeded return statement Warning

unneeded return statement
}
Self::Zip {
internal_prefix,
zip,
..
} => {
let path = internal_prefix.clone() + dir;
let mut zip = zip.try_borrow_mut()?;

if let Err(_) = &zip.by_name(&path) {

Check warning

Code scanning / clippy

redundant pattern matching, consider using is_err() Warning

redundant pattern matching, consider using is\_err()
return Err(ModError::PathDoesNotExist(path.into()));
}

// We need to copy all of the `&str`s to `String`s so we don't
// keep the immutable ref to `zip` alive and trigger a double borrow
let entries: Vec<String> = zip
.file_names()
.filter(|&x| x.starts_with(&path) && x != path)
.map(|x| x.into())

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
.collect();

return entries
.iter()
.map(|x| ModEntry::try_from(&zip.by_name(x)?))
.collect();
Comment on lines +345 to +348

Check warning

Code scanning / clippy

unneeded return statement Warning

unneeded return statement
}
}
}
}

fn get_zip_internal_folder(path: impl AsRef<Path>, zip: &ZipArchive<File>) -> Result<String> {
Expand Down
Loading