Skip to content

Commit

Permalink
Clean up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
garyttierney committed Mar 16, 2024
1 parent 4a21930 commit 89b8a18
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 337 deletions.
248 changes: 70 additions & 178 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fstools_formats.workspace = true
fstools_dvdbnd.workspace = true

[dev-dependencies]
criterion = "0.3"
criterion = "0.5"
insta = "1"
libtest-mimic = "0.7"

Expand All @@ -19,16 +19,18 @@ harness = false

[[test]]
name = "dcx"
path = "tests/dcx.rs"
harness = false

[workspace]
resolver = "2"
default-members = ["crates/cli", "crates/viewer"]
members = [
"crates/formats",
"crates/asset-server",
"crates/cli",
"crates/dvdbnd",
"crates/formats",
"crates/viewer",
"crates/dvdbnd", "crates/asset-server",
]

[workspace.package]
Expand All @@ -45,6 +47,7 @@ match_same_arms = "warn"
semicolon_if_nothing_returned = "warn"
ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
unwrap_used = "warn"

[workspace.dependencies]
fstools_formats = { path = "crates/formats", version = "0.1.0" }
Expand Down
3 changes: 1 addition & 2 deletions crates/asset-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ crossbeam-channel = "0.5"
fstools_dvdbnd.workspace = true
fstools_formats.workspace = true
futures-lite = "2"
futures-locks = "0.7"
generational-arena = "0.2.9"
memmap2.workspace = true
typed-path = "0.8"

[lints]
Expand Down
23 changes: 12 additions & 11 deletions crates/asset-server/src/dvdbnd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, io::Read, path::Path, sync::Arc};
use std::{io, path::Path, sync::Arc};

use bevy::asset::{
io::{AssetReader, AssetReaderError, PathStream, Reader},
Expand All @@ -23,25 +23,26 @@ impl AssetReader for DvdBndAssetSource {

dvd_bnd
.open(&*path_str)
.map(|r| {
.map_err(|err| match err {
DvdBndEntryError::NotFound => AssetReaderError::NotFound(path.to_path_buf()),
err => AssetReaderError::Io(Arc::new(io::Error::other(err))),
})
.and_then(|r| {
let is_dcx = {
let bytes = r.data();
&bytes[..4] == b"DCX\0"
};

if is_dcx {
let (dcx, dcx_reader) = DcxHeader::read(r).unwrap();
let reader = if is_dcx {
let (_dcx_header, dcx_reader) = DcxHeader::read(r)
.map_err(|err| AssetReaderError::Io(Arc::new(io::Error::other(err))))?;

Box::new(SimpleReader(dcx_reader)) as Box<Reader>
} else {
Box::new(SimpleReader(r)) as Box<Reader>
}
})
.map_err(|e| match e {
DvdBndEntryError::NotFound => AssetReaderError::NotFound(path.to_path_buf()),
_ => AssetReaderError::Io(Arc::new(io::Error::other(
"failed to get data from DVDBND",
))),
};

Ok(reader)
})
})
}
Expand Down
5 changes: 1 addition & 4 deletions crates/asset-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{
types::{
bnd4::{Archive, ArchiveEntry, Bnd4Loader},
flver::{FlverAsset, FlverLoader},
part::{PartsArchiveLoader, PartsAsset},
},
vfs::{watcher::VfsWatcher, Vfs, VfsAssetSource},
};
Expand Down Expand Up @@ -49,7 +48,7 @@ impl Plugin for FsAssetSourcePlugin {
AssetSource::build().with_reader(move || Box::new(DvdBndAssetSource(dvd_bnd.clone()))),
);

let (event_sender, event_receiver) = crossbeam_channel::bounded(100);
let (event_sender, event_receiver) = crossbeam_channel::unbounded();
let vfs = Vfs::new(event_sender);

app.insert_resource(vfs.clone());
Expand All @@ -74,11 +73,9 @@ impl Plugin for FsFormatsPlugin {
app.init_asset::<FlverAsset>()
.register_type::<FlverAsset>()
.register_type::<Handle<FlverAsset>>()
.init_asset::<PartsAsset>()
.init_asset::<Archive>()
.init_asset::<ArchiveEntry>()
.register_asset_loader(FlverLoader)
.register_asset_loader(PartsArchiveLoader)
.register_asset_loader(Bnd4Loader);
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/asset-server/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod bnd4;
pub mod flver;
pub mod part;
4 changes: 2 additions & 2 deletions crates/asset-server/src/types/bnd4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl AssetLoader for Bnd4Loader {
fn load<'a>(
&'a self,
reader: &'a mut Reader,
settings: &'a Self::Settings,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
Expand All @@ -38,7 +38,7 @@ impl AssetLoader for Bnd4Loader {

let bnd = BND4::from_reader(Cursor::new(&data))?;
for file in bnd.files {
let handle = load_context.labeled_asset_scope(file.path.clone(), |ctx| {
let handle = load_context.labeled_asset_scope(file.path.clone(), |_| {
let file_offset = file.data_offset as usize;
let file_end = file_offset + file.compressed_size as usize;
let file_data = data[file_offset..file_end].to_vec();
Expand Down
62 changes: 0 additions & 62 deletions crates/asset-server/src/types/part.rs

This file was deleted.

38 changes: 21 additions & 17 deletions crates/asset-server/src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ use bevy::{
prelude::{Deref, DerefMut, Resource},
};
use crossbeam_channel::Sender;
use memmap2::{Mmap, MmapOptions};
use typed_path::Utf8WindowsPathBuf;

use crate::SimpleReader;

mod bnd4_mount;
pub mod watcher;

pub trait IntoArchive {
fn files(&self) -> impl Iterator<Item = (String, Vec<u8>)>;
}

#[derive(Clone, Resource)]
pub struct Vfs {
inner: Arc<RwLock<VfsInner>>,
Expand All @@ -36,7 +32,7 @@ pub enum VfsEvent {

#[derive(Default)]
pub struct VfsInner {
entries: HashMap<String, Box<[u8]>>,
entries: HashMap<String, Mmap>,
}

impl Vfs {
Expand All @@ -50,20 +46,30 @@ impl Vfs {
pub fn mount_file(&mut self, name: String, data: Vec<u8>) {
let mut inner = self.inner.write().expect("vfs_write_lock");

// TODO: this is specific to Elden Ring
let path = Utf8WindowsPathBuf::from(&name);
let filename = path
.file_name()
.expect("no filename")
.to_string()
.to_ascii_lowercase();
let normalized_path = path
.strip_prefix("N:/GR/data/INTERROOT_win64")
.map(|path| path.with_unix_encoding().into_string())
.expect("path_not_expected");

info!("Mounting {filename} into vfs");
info!("Mounting {normalized_path} into vfs");

let _ = self
.event_sender
.send(VfsEvent::Added(PathBuf::from(filename.clone())));

inner.entries.insert(filename, data.into_boxed_slice());
.send(VfsEvent::Added(PathBuf::from(&normalized_path)));

let mut mmap = MmapOptions::default()
.len(data.len())
.map_anon()
.expect("failed to allocate memory");
mmap.copy_from_slice(&data[..]);

inner.entries.insert(
normalized_path,
mmap.make_read_only()
.expect("failed to make memory read-only"),
);
}

pub fn entry_bytes<P: AsRef<str>>(&self, name: P) -> Option<&[u8]> {
Expand All @@ -80,8 +86,6 @@ impl Vfs {
}
}

impl VfsInner {}

#[derive(Deref, DerefMut)]
pub struct VfsAssetSource(pub(crate) Vfs);

Expand Down
14 changes: 0 additions & 14 deletions crates/asset-server/src/vfs/bnd4_mount.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/cli/src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub fn describe_bnd(dvd_bnd: DvdBnd, name: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}

pub fn describe_matbin(dvd_bnd: DvdBnd, name: &str) -> Result<(), Box<dyn Error>> {
Ok(())
pub fn describe_matbin(_dvd_bnd: DvdBnd, _name: &str) -> Result<(), Box<dyn Error>> {
todo!()
}
6 changes: 3 additions & 3 deletions crates/cli/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ pub fn extract(
.expect("Could not create progress bar style");

lines.par_iter().progress_with_style(style).for_each(|l| {
let path = l.to_str().unwrap();
let path = l.to_string_lossy();

match dvd_bnd.open(path) {
match dvd_bnd.open(path.as_ref()) {
Ok(mut entry) => {
let mut buffer = Vec::new();
entry
.read_to_end(&mut buffer)
.expect("Could not read from dvdbnd to file buffer");

let fs_path = output_path.join(path);
let fs_path = output_path.join(path.as_ref());
if let Some(directory) = fs_path.parent() {
let _ = fs::create_dir_all(directory);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod extract;
use std::{error::Error, path::PathBuf};

use clap::{Parser, Subcommand, ValueEnum};
use describe::describe_matbin;
use fstools_dvdbnd::{DvdBnd, FileKeyProvider};

use crate::{describe::describe_bnd, extract::extract};
Expand Down Expand Up @@ -71,9 +72,9 @@ pub fn main() -> Result<(), Box<dyn Error>> {
}
Command::Describe {
ty: AssetType::Matbin,
..
name,
} => {
// describe_matbin(dvd_bnd, &name)?;
describe_matbin(dvd_bnd, &name)?;
}
Command::Extract {
recursive,
Expand Down
9 changes: 1 addition & 8 deletions crates/dvdbnd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use std::{
collections::HashMap,
fs::File,
io::{Error, Read},
ops::Range,
path::Path,
slice,
};
use std::{collections::HashMap, fs::File, io::Error, ops::Range, path::Path, slice};

use aes::{
cipher::{generic_array::GenericArray, BlockDecrypt, BlockSizeUser, KeyInit},
Expand Down
2 changes: 1 addition & 1 deletion crates/formats/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

fn main() {
let project_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let project_dir = env::var("CARGO_MANIFEST_DIR").expect("cargo_manifest_dir");

println!("cargo:rustc-link-search={}", project_dir); // the "-L" flag
println!("cargo:rustc-link-lib=oo2corelinux64"); // the "-l" flag
Expand Down
4 changes: 2 additions & 2 deletions crates/formats/src/dcx/oodle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{
cmp::min,
io::{Error, Read, Result},
ptr::null_mut,
ptr::{null_mut, NonNull},
};
use std::ptr::NonNull;

use oodle_sys::{
OodleLZDecoder, OodleLZDecoder_Create, OodleLZDecoder_DecodeSome, OodleLZDecoder_Destroy,
Expand Down Expand Up @@ -68,6 +67,7 @@ impl<R: Read> OodleDecoder<R> {
let io_buffer = vec![0u8; OODLELZ_BLOCK_LEN as usize * 2].into_boxed_slice();

Some(Self {
// SAFETY: Pointer is validated to be non-null above.
decoder: unsafe { NonNull::new_unchecked(decoder) },
reader,
decode_buffer,
Expand Down
2 changes: 1 addition & 1 deletion crates/viewer/src/formats/tpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl AssetLoader for TPFAssetLoader {
}),
RenderAssetUsages::MAIN_WORLD,
)
.unwrap()
.expect("invalid_image")
});
}

Expand Down
Loading

0 comments on commit 89b8a18

Please sign in to comment.