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

Somewhat working MSB parser impl + viewer code to render the MSB #30

Merged
merged 6 commits into from
Mar 17, 2024
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
20 changes: 10 additions & 10 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 crates/asset-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fstools_dvdbnd.workspace = true
fstools_formats.workspace = true
futures-lite = "2"
memmap2.workspace = true
thiserror.workspace = true
typed-path = "0.8"

[lints]
Expand Down
75 changes: 75 additions & 0 deletions crates/asset-server/src/asset_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::{io, io::Read, path::PathBuf, pin::Pin, sync::Arc, task::Poll};

use bevy::{
app::{App, Plugin},
asset::io::{AssetSource, AssetSourceId},
prelude::{AssetApp, Deref, DerefMut},
};
use fstools_dvdbnd::{ArchiveKeyProvider, DvdBnd};
use futures_lite::AsyncRead;

use crate::asset_source::{
dvdbnd::DvdBndAssetSource,
vfs::{watcher::VfsWatcher, Vfs, VfsAssetSource},
};

pub mod dvdbnd;
pub mod vfs;

pub struct FsAssetSourcePlugin {
dvd_bnd: Arc<DvdBnd>,
}

impl FsAssetSourcePlugin {
pub fn new(
data_archives: &[PathBuf],
key_provider: impl ArchiveKeyProvider,
) -> io::Result<Self> {
let dvd_bnd = Arc::new(DvdBnd::create(data_archives, &key_provider)?);

Ok(Self { dvd_bnd })
}
}

impl Plugin for FsAssetSourcePlugin {
fn build(&self, app: &mut App) {
let dvd_bnd = self.dvd_bnd.clone();

app.register_asset_source(
AssetSourceId::from("dvdbnd"),
AssetSource::build().with_reader(move || Box::new(DvdBndAssetSource(dvd_bnd.clone()))),
);

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

app.insert_resource(vfs.clone());
app.register_asset_source(
AssetSourceId::from("vfs"),
AssetSource::build()
.with_reader(move || Box::new(VfsAssetSource(vfs.clone())))
.with_watcher(move |sender| {
let mut watcher = Box::new(VfsWatcher::new(event_receiver.clone(), sender));
watcher.start();

Some(watcher)
}),
);
}
}

#[derive(Deref, DerefMut)]
struct SimpleReader<R: Read>(R);

impl<R: Read> Unpin for SimpleReader<R> {}

impl<R: Read> AsyncRead for SimpleReader<R> {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let reader = self.get_mut();
Poll::Ready(reader.read(buf))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::asset::{
use fstools_dvdbnd::{DvdBnd, DvdBndEntryError};
use fstools_formats::dcx::DcxHeader;

use crate::SimpleReader;
use crate::asset_source::SimpleReader;

#[derive(Clone)]
pub struct DvdBndAssetSource(pub(crate) Arc<DvdBnd>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crossbeam_channel::Sender;
use memmap2::{Mmap, MmapOptions};
use typed_path::Utf8WindowsPathBuf;

use crate::SimpleReader;
use crate::asset_source::SimpleReader;

pub mod watcher;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{sync::Arc, thread};
use bevy::asset::io::{AssetSourceEvent, AssetWatcher};
use crossbeam_channel::{Receiver, Sender};

use crate::vfs::VfsEvent;
use super::VfsEvent;

impl AssetWatcher for VfsWatcher {}

Expand Down
97 changes: 2 additions & 95 deletions crates/asset-server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,4 @@
use std::{io, io::Read, path::PathBuf, pin::Pin, sync::Arc, task::Poll};
pub use self::{asset_source::FsAssetSourcePlugin, types::FsFormatsPlugin};

use bevy::{
app::{App, Plugin},
asset::{
io::{AssetSource, AssetSourceId},
AssetApp, Handle,
},
prelude::{Deref, DerefMut},
};
use fstools_dvdbnd::{ArchiveKeyProvider, DvdBnd};
use futures_lite::AsyncRead;

use crate::{
dvdbnd::DvdBndAssetSource,
types::{
bnd4::{Archive, ArchiveEntry, Bnd4Loader},
flver::{FlverAsset, FlverLoader},
},
vfs::{watcher::VfsWatcher, Vfs, VfsAssetSource},
};

mod dvdbnd;
pub mod asset_source;
pub mod types;
pub mod vfs;

pub struct FsAssetSourcePlugin {
dvd_bnd: Arc<DvdBnd>,
}

impl FsAssetSourcePlugin {
pub fn new(
data_archives: &[PathBuf],
key_provider: impl ArchiveKeyProvider,
) -> std::io::Result<Self> {
let dvd_bnd = Arc::new(DvdBnd::create(data_archives, &key_provider)?);

Ok(Self { dvd_bnd })
}
}

impl Plugin for FsAssetSourcePlugin {
fn build(&self, app: &mut App) {
let dvd_bnd = self.dvd_bnd.clone();

app.register_asset_source(
AssetSourceId::from("dvdbnd"),
AssetSource::build().with_reader(move || Box::new(DvdBndAssetSource(dvd_bnd.clone()))),
);

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

app.insert_resource(vfs.clone());
app.register_asset_source(
AssetSourceId::from("vfs"),
AssetSource::build()
.with_reader(move || Box::new(VfsAssetSource(vfs.clone())))
.with_watcher(move |sender| {
let mut watcher = Box::new(VfsWatcher::new(event_receiver.clone(), sender));
watcher.start();

Some(watcher)
}),
);
}
}

pub struct FsFormatsPlugin;

impl Plugin for FsFormatsPlugin {
fn build(&self, app: &mut App) {
app.init_asset::<FlverAsset>()
.register_type::<FlverAsset>()
.register_type::<Handle<FlverAsset>>()
.init_asset::<Archive>()
.init_asset::<ArchiveEntry>()
.register_asset_loader(FlverLoader)
.register_asset_loader(Bnd4Loader);
}
}

#[derive(Deref, DerefMut)]
struct SimpleReader<R: Read>(R);

impl<R: Read> Unpin for SimpleReader<R> {}

impl<R: Read> AsyncRead for SimpleReader<R> {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let reader = self.get_mut();
Poll::Ready(reader.read(buf))
}
}
29 changes: 29 additions & 0 deletions crates/asset-server/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
use bevy::prelude::*;

use crate::types::{
bnd4::{Archive, ArchiveEntry, Bnd4Loader},
flver::{FlverAsset, FlverLoader},
msb::{MsbAsset, MsbAssetLoader, MsbPartAsset, MsbPointAsset},
};

pub mod bnd4;
pub mod flver;
pub mod msb;

pub struct FsFormatsPlugin;

impl Plugin for FsFormatsPlugin {
fn build(&self, app: &mut App) {
app.init_asset::<FlverAsset>()
.register_type::<FlverAsset>()
.register_type::<Handle<FlverAsset>>()
.init_asset::<Archive>()
.init_asset::<ArchiveEntry>()
.init_asset::<MsbAsset>()
.register_asset_loader(MsbAssetLoader)
.register_asset_loader(FlverLoader)
.register_asset_loader(Bnd4Loader);
app.init_asset::<MsbAsset>()
.init_asset::<MsbPointAsset>()
.init_asset::<MsbPartAsset>()
.init_asset_loader::<MsbAssetLoader>();
}
}
4 changes: 4 additions & 0 deletions crates/asset-server/src/types/flver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ fn load_mesh(flver: &Flver, flver_mesh: &FlverMesh) -> Mesh {
Mesh::ATTRIBUTE_NORMAL,
VertexAttributeValues::Float32x3(it.collect()),
),
(Normal, VertexAttributeAccessor::SNorm8x4(it)) => (
Mesh::ATTRIBUTE_NORMAL,
VertexAttributeValues::Float32x3(it.map(|f| [f[0], f[1], f[2]]).collect()),
),
(UV, VertexAttributeAccessor::UV(it)) => (
Mesh::ATTRIBUTE_UV_0,
VertexAttributeValues::Float32x2(it.collect()),
Expand Down
Loading