cargo fmt

This commit is contained in:
lifning 2021-01-16 23:43:39 -08:00
parent 2658e076d5
commit 3e4fd54402
6 changed files with 113 additions and 62 deletions

View File

@ -1,12 +1,13 @@
#[macro_use] extern crate argh;
#[macro_use]
extern crate argh;
extern crate binread;
extern crate one_rust;
use one_rust::prelude::*;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use one_rust::prelude::*;
use std::error::Error;
use std::str::FromStr;
/// Unpack .one archives from NiGHTS: Journey of Dreams
@ -75,10 +76,10 @@ struct DescribeFile {
#[argh(subcommand, name = "c")]
struct CreateFile {
/// (D|R|S|H) game of the .one file to create
#[argh(option, short='g')]
#[argh(option, short = 'g')]
game: GameType,
/// path of the .one file to create
#[argh(option, short='f')]
#[argh(option, short = 'f')]
file: PathBuf,
/// paths of the files to pack into the archive
#[argh(positional)]

View File

@ -1,16 +1,18 @@
//#![feature(fixed_size_array)]
#[macro_use] extern crate binread;
#[macro_use]
extern crate binread;
extern crate binwrite;
#[macro_use] extern crate log;
#[macro_use]
extern crate log;
extern crate prs_rust;
mod one;
pub mod prelude {
pub use one::OneArchive;
pub use one::dreams::*;
pub use one::rings::*;
pub use one::dreams::{JodOne, JodOneEntry};
pub use one::read_one_archive;
pub use one::rings::{SsrOne, SsrOneEntry};
pub use one::{OneArchive, OneArchiveEntry};
}
use binread::NullString;

View File

@ -1,10 +1,10 @@
use binread::FilePtr;
use binwrite::BinWrite;
use one::{OneArchive, OneArchiveEntry, WriteSeek};
use prs_rust::prs;
use one::{OneArchive, WriteSeek, OneArchiveEntry};
use std::fmt::Formatter;
use std::path::PathBuf;
use std::mem::size_of;
use std::path::PathBuf;
use crate::string_binread_helper;
use crate::string_binwrite_helper;
@ -23,8 +23,7 @@ const JOD_ONE_MAGIC: &'static [u8; 16] = b"ThisIsOneFile\0\0\0";
const SIZE_OF_JOD_ONE_HEADER: usize = JOD_ONE_MAGIC.len() + 2 * size_of::<u32>() + 32 + 132;
const SIZE_OF_JOD_ONE_ENTRY: usize = 6 * size_of::<u32>() + 192;
#[derive(BinRead)]
#[derive(BinWrite)]
#[derive(BinRead, BinWrite)]
#[br(little, magic = b"ThisIsOneFile\0\0\0")]
#[binwrite(little)]
pub struct JodOne {
@ -41,11 +40,10 @@ pub struct JodOne {
#[binwrite(preprocessor(string_binwrite_helper(132)))]
pub comment: String,
#[br(count = count)]
pub entries: Vec<JodOneEntry>
pub entries: Vec<JodOneEntry>,
}
#[derive(BinRead)]
#[derive(BinWrite)]
#[derive(BinRead, BinWrite)]
#[br(little, assert(id_again == id))]
#[binwrite(little)]
pub struct JodOneEntry {
@ -64,7 +62,11 @@ pub struct JodOneEntry {
impl std::fmt::Debug for JodOne {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "JodOne{{version: 0x{:x}, comment: {:?}, category: {}, count: {}, ..}}", self.version, self.comment, self.category, self.count)
write!(
f,
"JodOne{{version: 0x{:x}, comment: {:?}, category: {}, count: {}, ..}}",
self.version, self.comment, self.category, self.count
)
}
}
@ -73,7 +75,11 @@ impl std::fmt::Debug for JodOneEntry {
write!(
f,
"JodOneEntry{{id: {}, is_compressed: {}, name: {:?}, size_cmp: {}, size_dec: {}, ..}}",
self.id, self.is_compressed != 0, self.name, self.size_cmp, self.size_dec,
self.id,
self.is_compressed != 0,
self.name,
self.size_cmp,
self.size_dec,
)
}
}
@ -93,11 +99,16 @@ impl OneArchive for JodOne {
Ok(())
}
fn pack(contents: impl IntoIterator<Item=(PathBuf, Vec<u8>)>) -> Self where Self: Sized {
fn pack(contents: impl IntoIterator<Item = (PathBuf, Vec<u8>)>) -> Self
where
Self: Sized,
{
let mut entries = Vec::new();
for (id, (path, data)) in contents.into_iter().enumerate() {
let cmp = prs::Compressor::new(&data, None).compress();
let name = path.to_string_lossy().replace(std::path::MAIN_SEPARATOR, "\\");
let name = path
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "\\");
if cmp.len() < data.len() {
entries.push(JodOneEntry {
id: id as u32,
@ -105,7 +116,10 @@ impl OneArchive for JodOne {
is_compressed: 1,
size_dec: data.len() as u32,
id_again: id as u32,
data: FilePtr { ptr: 0, value: Some(cmp) },
data: FilePtr {
ptr: 0,
value: Some(cmp),
},
name,
});
} else {
@ -115,7 +129,10 @@ impl OneArchive for JodOne {
is_compressed: 0,
size_dec: 0,
id_again: id as u32,
data: FilePtr { ptr: 0, value: Some(data) },
data: FilePtr {
ptr: 0,
value: Some(data),
},
name,
});
}
@ -138,22 +155,31 @@ impl OneArchive for JodOne {
}
}
fn unpack(self) -> Box<dyn Iterator<Item=(PathBuf, Vec<u8>)>> {
fn unpack(self) -> Box<dyn Iterator<Item = (PathBuf, Vec<u8>)>> {
Box::new(self.entries.into_iter().map(|entry| {
let key = PathBuf::from(entry.name.replace('\\', std::path::MAIN_SEPARATOR.encode_utf8(&mut [0u8; 4])));
let key = PathBuf::from(
entry
.name
.replace('\\', std::path::MAIN_SEPARATOR.encode_utf8(&mut [0u8; 4])),
);
if entry.is_compressed == 0 || entry.size_dec == 0 {
(key, entry.data.into_inner())
} else {
let dec_buf = prs::Decompressor::new(entry.data.into_inner(), Some(entry.size_dec as usize)).decompress();
let dec_buf =
prs::Decompressor::new(entry.data.into_inner(), Some(entry.size_dec as usize))
.decompress();
(key, dec_buf)
}
}))
}
fn entries(&self) -> Vec<&dyn OneArchiveEntry> {
self.entries.iter().map(|x| {
let y: &dyn OneArchiveEntry = x;
y
}).collect()
self.entries
.iter()
.map(|x| {
let y: &dyn OneArchiveEntry = x;
y
})
.collect()
}
}

View File

@ -1,11 +1,11 @@
use std::path::PathBuf;
use std::io::{Seek, Write, Read};
use std::error::Error;
use std::fmt::{Debug, Formatter};
use binread::BinReaderExt;
use binread::io::SeekFrom;
use binread::BinReaderExt;
use one::dreams::JodOne;
use one::rings::SsrOne;
use std::error::Error;
use std::fmt::{Debug, Formatter};
use std::io::{Read, Seek, Write};
use std::path::PathBuf;
pub(crate) mod dreams;
pub(crate) mod rings;
@ -15,11 +15,14 @@ impl<T> WriteSeek for T where T: Write + Seek {}
pub trait OneArchive: Debug {
fn write(&self, writer: &mut dyn WriteSeek) -> std::io::Result<()>;
fn pack(contents: impl IntoIterator<Item=(PathBuf, Vec<u8>)>) -> Self where Self: Sized;
fn unpack(self) -> Box<dyn Iterator<Item=(PathBuf, Vec<u8>)>>;
fn pack(contents: impl IntoIterator<Item = (PathBuf, Vec<u8>)>) -> Self
where
Self: Sized;
fn unpack(self) -> Box<dyn Iterator<Item = (PathBuf, Vec<u8>)>>;
fn entries(&self) -> Vec<&dyn OneArchiveEntry>;
}
// TODO: find out the least-common-denominator of things we know about these files
pub trait OneArchiveEntry: Debug {}
// hack to work around inability to consume self via boxed trait object
@ -45,11 +48,14 @@ impl OneArchive for DynOneArchive {
}
}
fn pack(_: impl IntoIterator<Item=(PathBuf, Vec<u8>)>) -> Self where Self: Sized {
fn pack(_: impl IntoIterator<Item = (PathBuf, Vec<u8>)>) -> Self
where
Self: Sized,
{
unimplemented!()
}
fn unpack(self) -> Box<dyn Iterator<Item=(PathBuf, Vec<u8>)>> {
fn unpack(self) -> Box<dyn Iterator<Item = (PathBuf, Vec<u8>)>> {
match self {
DynOneArchive::JodOne(one) => one.unpack(),
DynOneArchive::SsrOne(one) => one.unpack(),

View File

@ -1,14 +1,14 @@
use binread::FilePtr;
use binwrite::BinWrite;
use one::{OneArchive, OneArchiveEntry, WriteSeek};
use prs_rust::prs;
use std::fmt::Formatter;
use std::io::SeekFrom;
use std::mem::size_of;
use std::path::PathBuf;
use crate::string_binread_helper;
use crate::string_binwrite_helper;
use one::{OneArchive, WriteSeek, OneArchiveEntry};
use prs_rust::prs;
use std::path::PathBuf;
use std::fmt::Formatter;
use std::mem::size_of;
const SIZE_OF_SSR_ONE_HEADER: usize = 4 * size_of::<u32>(); // 0x10
const SIZE_OF_SSR_ONE_ENTRY: usize = 4 * size_of::<u32>() + 32; // 0x30
@ -23,8 +23,7 @@ const SIZE_OF_SSR_ONE_ENTRY: usize = 4 * size_of::<u32>() + 32; // 0x30
// 36 SsrOneEntry{size_cmp: 1+, size_dec: 0, ..}
// 5105 SsrOneEntry{size_cmp: 1+, size_dec: 1+, ..}
#[derive(BinRead)]
#[derive(BinWrite)]
#[derive(BinRead, BinWrite)]
#[br(big, assert(entries.ptr == 0x10 && data_offset == entries.ptr + count * 0x30))]
#[binwrite(big)]
pub struct SsrOne {
@ -40,8 +39,7 @@ pub struct SsrOne {
pub _padding: u32,
}
#[derive(BinRead)]
#[derive(BinWrite)]
#[derive(BinRead, BinWrite)]
#[br(big)]
#[binwrite(big)]
pub struct SsrOneEntry {
@ -62,7 +60,11 @@ pub struct SsrOneEntry {
impl std::fmt::Debug for SsrOne {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "SsrOne{{count: {}, _unknown: 0x{:x}, ..}}", self.count, self._padding)
write!(
f,
"SsrOne{{count: {}, _unknown: 0x{:x}, ..}}",
self.count, self._padding
)
}
}
@ -92,7 +94,10 @@ impl OneArchive for SsrOne {
Ok(())
}
fn pack(contents: impl IntoIterator<Item=(PathBuf, Vec<u8>)>) -> Self where Self: Sized {
fn pack(contents: impl IntoIterator<Item = (PathBuf, Vec<u8>)>) -> Self
where
Self: Sized,
{
let mut entries = Vec::new();
for (id, (path, data)) in contents.into_iter().enumerate() {
let cmp = prs::Compressor::new(&data, None).compress();
@ -102,18 +107,24 @@ impl OneArchive for SsrOne {
id: id as u32,
size_cmp: cmp.len() as u32,
size_dec: data.len() as u32,
data: FilePtr { ptr: 0, value: Some(cmp) },
data: FilePtr {
ptr: 0,
value: Some(cmp),
},
name,
_size_cmp: 0
_size_cmp: 0,
});
} else {
entries.push(SsrOneEntry {
id: id as u32,
size_cmp: data.len() as u32,
size_dec: 0,
data: FilePtr { ptr: 0, value: Some(data) },
data: FilePtr {
ptr: 0,
value: Some(data),
},
name,
_size_cmp: 0
_size_cmp: 0,
});
}
}
@ -134,26 +145,31 @@ impl OneArchive for SsrOne {
value: Some(entries),
},
data_offset,
_padding: 0
_padding: 0,
}
}
fn unpack(self) -> Box<dyn Iterator<Item=(PathBuf, Vec<u8>)>> {
fn unpack(self) -> Box<dyn Iterator<Item = (PathBuf, Vec<u8>)>> {
Box::new(self.entries.value.unwrap().into_iter().map(|entry| {
let key = PathBuf::from(entry.name);
if entry.size_dec == 0 {
(key, entry.data.into_inner())
} else {
let dec_buf = prs::Decompressor::new(entry.data.into_inner(), Some(entry.size_dec as usize)).decompress();
let dec_buf =
prs::Decompressor::new(entry.data.into_inner(), Some(entry.size_dec as usize))
.decompress();
(key, dec_buf)
}
}))
}
fn entries(&self) -> Vec<&dyn OneArchiveEntry> {
self.entries.iter().map(|x| {
let y: &dyn OneArchiveEntry = x;
y
}).collect()
self.entries
.iter()
.map(|x| {
let y: &dyn OneArchiveEntry = x;
y
})
.collect()
}
}

@ -1 +1 @@
Subproject commit c2757ca1ba88ea51b386d1f48af0b9fa01829cc7
Subproject commit 8ee6b274c0180d5d58ffa00f815a84cc0780603a