Skip to content

Commit

Permalink
Merge pull request #16 from dark0dave/feature/printer
Browse files Browse the repository at this point in the history
feat(print): Print json rather than rust struct
  • Loading branch information
dark0dave authored Mar 9, 2024
2 parents 1e117a5 + d69f278 commit b37d9da
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
with:
fetch-depth: 0
- name: Generate a changelog
uses: orhun/git-cliff-action@v2
uses: orhun/git-cliff-action@v3
id: git-cliff
with:
config: cliff.toml
Expand Down
2 changes: 1 addition & 1 deletion cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Args {
/// Flag to tlk file
#[clap(long, short, action=ArgAction::SetFalse)]
pub process_tlk: bool,
/// Flag to tlk file
/// Json Output
#[clap(long, short, action=ArgAction::SetTrue)]
pub json: bool,
/// The path to the file to read
Expand Down
20 changes: 12 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod args;
use std::{
fs::{self, File},
io::{BufReader, Read, Write},
io::{self, BufReader, Read, Write},
path::Path,
process::exit,
str,
Expand All @@ -13,7 +13,7 @@ use models::{
from_buffer,
key::Key,
model::Model,
resources::types::{extention_to_resource_type, ResourceType},
resources::types::{extension_to_resource_type, ResourceType},
save::Save,
spell::Spell,
tlk::Lookup,
Expand All @@ -39,8 +39,10 @@ fn json_back_to_ie_type(path: &Path) {
}
}

fn write_model(path: &Path, model: std::rc::Rc<dyn Model>) {
let file_name = Path::new(path.file_stem().unwrap_or_default()).with_extension("json");
fn write_model(path: &Path, model: std::rc::Rc<dyn Model>, resource_type: ResourceType) {
let file_name = Path::new(path.file_stem().unwrap_or_default())
.with_extension(format!("{}.json", resource_type));
println!("{:?}", file_name);
if let Ok(file) = File::create(file_name) {
let mut json = serde_json::Serializer::new(file);
let mut format = <dyn Serializer>::erase(&mut json);
Expand Down Expand Up @@ -86,7 +88,7 @@ fn get_model_from_file(path: &Path, json: bool) -> Vec<Biff> {
.to_str()
.unwrap_or_default()
.to_ascii_lowercase();
let resource_type = extention_to_resource_type(&extention);
let resource_type = extension_to_resource_type(&extention);

// Non resource types
if resource_type == ResourceType::NotFound {
Expand All @@ -100,7 +102,7 @@ fn get_model_from_file(path: &Path, json: bool) -> Vec<Biff> {
exit(0)
}
"json" => {
json_back_to_ie_type(&path);
json_back_to_ie_type(path);
exit(0)
}
_ => panic!("Unprocessable file type: {:?}", path.as_os_str()),
Expand All @@ -109,9 +111,11 @@ fn get_model_from_file(path: &Path, json: bool) -> Vec<Biff> {

let model = from_buffer(&buffer, resource_type).expect("Could not parse file");
if json {
write_model(path, model);
write_model(path, model, resource_type);
} else {
println!("{:?}", model);
let print = &mut serde_json::Serializer::new(io::stdout());
let mut format = <dyn Serializer>::erase(print);
model.erased_serialize(&mut format).unwrap();
}
exit(0)
}
Expand Down
16 changes: 5 additions & 11 deletions models/src/common/fixed_char_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<const N: usize> Serialize for FixedCharSlice<N> {
where
S: Serializer,
{
serializer.collect_seq(self.0)
serializer.collect_str(self)
}
}

Expand All @@ -68,17 +68,11 @@ impl<'de, const N: usize> Visitor<'de> for FixedCharSliceVisitor<N> {
write!(formatter, "struct FixedCharSlice")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
A: serde::de::SeqAccess<'de>,
E: serde::de::Error,
{
let mut destination = [0; N];
let mut counter = 0;
while let Ok(Some(item)) = seq.next_element::<u8>() {
destination[counter] = item;
counter += 1;
}
Ok(FixedCharSlice(destination))
Ok(FixedCharSlice::from(v))
}
}

Expand All @@ -87,7 +81,7 @@ impl<'de, const N: usize> Deserialize<'de> for FixedCharSlice<N> {
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_seq(FixedCharSliceVisitor)
deserializer.deserialize_str(FixedCharSliceVisitor)
}
}

Expand Down
17 changes: 5 additions & 12 deletions models/src/common/signed_fixed_char_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<const N: usize> Serialize for SignedFixedCharSlice<N> {
where
S: Serializer,
{
serializer.collect_seq(self.0)
serializer.collect_str(self)
}
}

Expand All @@ -88,18 +88,11 @@ impl<'de, const N: usize> Visitor<'de> for SignedFixedCharSliceVisitor<N> {
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "struct SignedFixedCharSlice")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
A: serde::de::SeqAccess<'de>,
E: serde::de::Error,
{
let mut destination = [0; N];
let mut counter = 0;
while let Ok(Some(item)) = seq.next_element::<i8>() {
destination[counter] = item;
counter += 1;
}
Ok(SignedFixedCharSlice(destination))
Ok(SignedFixedCharSlice::from(v))
}
}

Expand All @@ -108,7 +101,7 @@ impl<'de, const N: usize> Deserialize<'de> for SignedFixedCharSlice<N> {
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_seq(SignedFixedCharSliceVisitor)
deserializer.deserialize_str(SignedFixedCharSliceVisitor)
}
}

Expand Down
2 changes: 1 addition & 1 deletion models/src/effect_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Model for EffectV2 {
// There is one weird file in BG1, to do with Opcode 67
let tmp = if buffer.len() < 272 {
let mut temp = buffer.to_vec();
temp.extend([0 as u8]);
temp.extend([0_u8]);
temp
} else {
buffer.to_vec()
Expand Down
59 changes: 57 additions & 2 deletions models/src/resources/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ pub enum ResourceType {
FileTypeSrc = 0x0803,
}

pub fn extention_to_resource_type(extention: &str) -> ResourceType {
match extention.to_ascii_lowercase().as_str() {
impl std::fmt::Display for ResourceType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", resource_type_to_string(*self))
}
}

pub fn extension_to_resource_type(extension: &str) -> ResourceType {
match extension.to_ascii_lowercase().as_str() {
"bmp" => ResourceType::FileTypeBmp,
"mve" => ResourceType::FileTypeMve,
"wav" => ResourceType::FileTypeWav,
Expand Down Expand Up @@ -95,3 +101,52 @@ pub fn extention_to_resource_type(extention: &str) -> ResourceType {
_ => ResourceType::NotFound,
}
}

pub fn resource_type_to_string(resource_type: ResourceType) -> String {
match resource_type {
ResourceType::FileTypeBmp => "bmp",
ResourceType::FileTypeMve => "mve",
ResourceType::FileTypeWav => "wav",
ResourceType::FileTypeWfx => "wfx",
ResourceType::FileTypePlt => "plt",
ResourceType::FileTypeBam => "bam",
ResourceType::FileTypeWed => "wed",
ResourceType::FileTypeChu => "chu",
ResourceType::FileTypeTi => "ti",
ResourceType::FileTypeMos => "mos",
ResourceType::FileTypeItm => "itm",
ResourceType::FileTypeSpl => "spl",
ResourceType::FileTypeBcs => "bcs",
ResourceType::FileTypeIds => "ids",
ResourceType::FileTypeCre => "cre",
ResourceType::FileTypeAre => "are",
ResourceType::FileTypeDlg => "dlg",
ResourceType::FileType2da => "2da",
ResourceType::FileTypeGam => "gam",
ResourceType::FileTypeSto => "sto",
ResourceType::FileTypeWmap => "wmap",
ResourceType::FileTypeEff => "eff",
ResourceType::FileTypeBs => "ebs",
ResourceType::FileTypeChr => "chr",
ResourceType::FileTypeVvc => "vvc",
ResourceType::FileTypeVef => "vef",
ResourceType::FileTypePro => "pro",
ResourceType::FileTypeBio => "bio",
ResourceType::FileTypeWbm => "wbm",
ResourceType::FileTypeFnt => "fnt",
ResourceType::FileTypeGui => "gui",
ResourceType::FileTypeSql => "sql",
ResourceType::FileTypePvrz => "pvrz",
ResourceType::FileTypeGlsl => "glsl",
ResourceType::FileTypeTlk => "tlk",
ResourceType::FileTypeMenu => "enu",
ResourceType::FileTypeMenu2 => "nu2",
ResourceType::FileTypeTtf => "ttf",
ResourceType::FileTypePng => "png",
ResourceType::FileTypeBah => "bah",
ResourceType::FileTypeIni => "ini",
ResourceType::FileTypeSrc => "src",
_ => "",
}
.to_string()
}
6 changes: 3 additions & 3 deletions models/src/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
common::{header::Header, variable_char_array::VariableCharArray},
from_buffer,
model::Model,
resources::{types::extention_to_resource_type, utils::copy_buff_to_struct},
resources::{types::extension_to_resource_type, utils::copy_buff_to_struct},
};

// https://gibberlings3.github.io/iesdp/file_formats/ie_formats/sav_v1.htm
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Save {
let file_name = file.filename.clone().to_string();
let uncompresseed_buffer = file.decompress();
let file_extension = file_name[file_name.len() - 3..].to_string();
let file_type = extention_to_resource_type(&file_extension);
let file_type = extension_to_resource_type(&file_extension);
if let Some(model) = from_buffer(&uncompresseed_buffer, file_type) {
uncompressed_files.push(model);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ impl File {
Ok(_) => buff,
Err(err) => {
println!("{}", err);
return vec![];
vec![]
}
}
}
Expand Down

0 comments on commit b37d9da

Please sign in to comment.