Kevin Lee hai 1 ano
pai
achega
44b8392a99
Modificáronse 5 ficheiros con 76 adicións e 35 borrados
  1. 1 1
      src/ezcad/array_of.rs
  2. 20 8
      src/ezcad/basic_field.rs
  3. 43 8
      src/ezcad/file.rs
  4. 7 3
      src/main.rs
  5. 5 15
      tests/basic_field.rs

+ 1 - 1
src/ezcad/array_of.rs

@@ -16,7 +16,7 @@ where
     #[bw(try_calc(u32::try_from(value.len())))]
     size: u32,
 
-    #[br(count = size)]
+    #[br(count = size / core::mem::size_of::<T>() as u32)]
     pub value: Vec<T>,
 }
 

+ 20 - 8
src/ezcad/basic_field.rs

@@ -4,17 +4,29 @@ use binrw::{BinRead, BinWrite};
 
 use crate::{array_of::ArrayOf, field_of::FieldOf};
 
-/// Wrapper for basic types determined by initial u32 size
-#[derive(BinRead, BinWrite, PartialEq, Debug)]
-pub enum BasicField {
-    U16(FieldOf<u16>),
-    U32(FieldOf<u32>),
-    Double(FieldOf<f64>),
-}
-
 /// Generic field with structure of length + data
 pub type Field = ArrayOf<u8>;
 
+pub type U32 = FieldOf<u32>;
+pub type U16 = FieldOf<u16>;
+pub type Double = FieldOf<f64>;
+pub type Color = FieldOf<Rgba>;
+
+pub type WString = ArrayOf<u16>;
+
+impl From<&WString> for String {
+    fn from(value: &WString) -> Self {
+        String::from_utf16_lossy(value)
+    }
+}
+
+impl From<String> for WString {
+    fn from(value: String) -> Self {
+        let value: Vec<u16> = value.encode_utf16().collect();
+        Self { value }
+    }
+}
+
 #[derive(BinRead, BinWrite, PartialEq, Debug)]
 pub struct Rgba {
     red: u8,

+ 43 - 8
src/ezcad/file.rs

@@ -2,7 +2,7 @@ use binrw::{binread, BinRead, BinWrite, FilePtr64};
 
 use crate::{
     array_of::ArrayOf,
-    basic_field::{Field, Rgba},
+    basic_field::{Color, Double, Field, Rgba, WString, U32},
     field_of::FieldOf,
 };
 
@@ -20,37 +20,72 @@ pub struct Header {
 #[derive(BinRead, BinWrite, Debug)]
 pub struct Thumbnail {
     _zeros_1: u32,
-    dimension_x: u32,
-    dimension_y: u32,
+    pub dimension_x: u32,
+    pub dimension_y: u32,
     _unknown_1: u32, // 0x320?
     _unknown_2: u32, // 0x200001?
     _zeros_2: [u32; 3],
 
     #[br(count = dimension_x * dimension_y)]
-    pixels: Vec<Rgba>,
+    pub pixels: Vec<Rgba>,
 }
 
 #[derive(BinRead, Debug)]
 pub struct PenHeader {
-    pen_count: u32,
+    pub pen_count: u32,
     #[br(args {
         inner: binrw::args! {
             pen_count
         }
     })]
-    start_offset: FilePtr64<Pens>,
+    pub start_offset: FilePtr64<Pens>,
 }
 
 #[derive(BinRead, BinWrite, Debug)]
 #[br(import {pen_count: u32})]
 pub struct Pens {
+    // #[br(count = pen_count)]
+    // pens: Vec<ArrayOf<Field>>,
     #[br(count = pen_count)]
-    pens: Vec<Pen>
+    pub pens: Vec<Pen>,
 }
 
 #[derive(BinRead, BinWrite, Debug)]
 pub struct Pen {
-
+    #[br(assert(field_count == 236))]
+    pub field_count: u32,
+    pub color: Color,
+    pub name: WString,
+    pub disabled: U32,
+    pub use_default: U32,
+    pub loop_count: U32,
+    pub speed: Double,
+    pub power: Double,
+    pub frequency: U32,
+    _unknown_1: Field,
+    pub start_tc: U32,
+    pub end_tc: U32,
+    pub polygon_tc: U32,
+    pub jump_speed: Double,
+    _unknown_2: [Field; 10],
+    pub laser_off_tc: U32,
+    pub wave: U32,
+    _unknown_3: Field,
+    pub wobble_enable: U32,
+    pub wobble_diameter: Double,
+    pub wobble_distance: Double,
+    _unknown_4: [Field; 8],
+    pub min_jump_tc: U32,
+    pub max_jump_tc: U32,
+    pub jump_limit: Double,
+    _unknown_5: [Field; 2],
+    pub frequency_2: Double,
+    _unknown_6: [Field; 152],
+    pub wobble_type: U32,
+    pub continue_mode: U32,
+    _unknown_7: [Field; 12],
+    pub wobble_diameter_2: Double,
+    _unknown_8: [Field; 26],
 }
 
 #[derive(BinRead, BinWrite, Debug)]

+ 7 - 3
src/main.rs

@@ -1,8 +1,12 @@
-use std::{fs::{self}, path::PathBuf};
+use std::{
+    fs::{self},
+    path::PathBuf,
+};
 
 use binrw::BinRead;
 use clap::Parser;
 use ezcad::file::Header;
+use log::debug;
 
 #[derive(Debug, Parser)]
 struct Cli {
@@ -17,7 +21,6 @@ struct Cli {
     // /// Configuration file
     // #[arg(short, long)]
     // config: PathBuf,
-
     #[command(flatten)]
     verbose: clap_verbosity_flag::Verbosity,
 }
@@ -34,5 +37,6 @@ fn main() {
     // let _config = read(args.config).unwrap();
 
     let file = Header::read_le(&mut input).unwrap();
-    dbg!(file);
+    dbg!(&file.pens_offset.start_offset.pens[255].color);
+    dbg!(String::from(&file.pens_offset.start_offset.pens[255].name));
 }

+ 5 - 15
tests/basic_field.rs

@@ -1,7 +1,7 @@
 use std::io::Cursor;
 
 use binrw::{BinRead, BinWrite};
-use ezcad::basic_field::BasicField;
+use ezcad::basic_field::U32;
 
 #[test]
 fn field_u32_be() {
@@ -9,15 +9,10 @@ fn field_u32_be() {
     let decoded: u32 = 0x0A0B0C0D;
 
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-    assert_eq!(
-        BasicField::read_be(&mut stream).unwrap(),
-        BasicField::U32(decoded.into())
-    );
+    assert_eq!(U32::read_be(&mut stream).unwrap(), decoded.into());
 
     let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-    BasicField::U32(decoded.into())
-        .write_be(&mut buffer)
-        .unwrap();
+    U32::from(decoded).write_be(&mut buffer).unwrap();
     assert_eq!(buffer.into_inner(), encoded);
 }
 
@@ -27,14 +22,9 @@ fn field_u32_le() {
     let decoded: u32 = 0x0A0B0C0D;
 
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-    assert_eq!(
-        BasicField::read_le(&mut stream).unwrap(),
-        BasicField::U32(decoded.into())
-    );
+    assert_eq!(U32::read_le(&mut stream).unwrap(), decoded.into());
 
     let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
-    BasicField::U32(decoded.into())
-        .write_le(&mut buffer)
-        .unwrap();
+    U32::from(decoded).write_le(&mut buffer).unwrap();
     assert_eq!(buffer.into_inner(), encoded);
 }