Browse Source

Refactorign

Kevin Lee 1 year ago
parent
commit
7d7f128b79
8 changed files with 151 additions and 128 deletions
  1. 1 2
      src/ezcad/array_of.rs
  2. 0 42
      src/ezcad/basic_field.rs
  3. 8 81
      src/ezcad/file.rs
  4. 2 1
      src/ezcad/mod.rs
  5. 61 0
      src/ezcad/pen.rs
  6. 77 0
      src/ezcad/types.rs
  7. 1 1
      src/main.rs
  8. 1 1
      tests/basic_field.rs

+ 1 - 2
src/ezcad/array_of.rs

@@ -1,6 +1,5 @@
-use std::ops::{Deref, DerefMut};
-
 use binrw::{binrw, BinRead, BinWrite};
+use std::ops::{Deref, DerefMut};
 
 /// Wrapper to prefix array of single type
 #[binrw]

+ 0 - 42
src/ezcad/basic_field.rs

@@ -1,42 +0,0 @@
-#![allow(dead_code)]
-
-use binrw::{BinRead, BinWrite};
-
-use crate::{array_of::ArrayOf, field_of::FieldOf};
-
-/// 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,
-    green: u8,
-    blue: u8,
-    alpha: u8,
-}
-
-#[derive(BinRead, BinWrite, PartialEq, Debug)]
-pub struct Point {
-    x: f64,
-    y: f64,
-}

+ 8 - 81
src/ezcad/file.rs

@@ -1,10 +1,6 @@
-use binrw::{binread, BinRead, BinWrite, FilePtr64};
+use binrw::{BinRead, BinWrite, FilePtr64};
 
-use crate::{
-    array_of::ArrayOf,
-    basic_field::{Color, Double, Field, Rgba, WString, U32},
-    field_of::FieldOf,
-};
+use crate::{pen::PenHeader, types::Rgba};
 
 #[derive(BinRead, Debug)]
 pub struct Header {
@@ -13,7 +9,7 @@ pub struct Header {
     pub thumbnail_offset: FilePtr64<Thumbnail>,
     pub pens_offset: FilePtr64<PenHeader>,
     _unknown_offset: FilePtr64<()>,
-    pub layers_offset: FilePtr64<Layers>,
+    pub layers_offset: FilePtr64<LayerHeader>,
     _unknown_2: [u8; 0x1A8],
 }
 
@@ -30,81 +26,12 @@ pub struct Thumbnail {
     pub pixels: Vec<Rgba>,
 }
 
-#[derive(BinRead, Debug)]
-pub struct PenHeader {
-    pub pen_count: u32,
-    #[br(args {
-        inner: binrw::args! {
-            pen_count
-        }
-    })]
-    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)]
-    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],
+pub struct LayerHeader {
+    pub layer_count: u32,
+    #[br(count = layer_count)]
+    pub layers: Vec<Layer>,
 }
 
 #[derive(BinRead, BinWrite, Debug)]
-pub struct Layers {}
-
-// #[binread]
-// #[derive(Debug)]
-// pub struct File {
-//     header: Header,
-
-//     #[br(count = 40000)] // 200x200?
-//     thumbnail: Vec<u32>,
-//     _padding: u32,
-
-//     // #[br(temp)]
-//     // #[bw(try_calc(u32::try_from(pens.len())))]
-//     pen_count: u32, // Number of pens
-//     pen_offset: u64, // Offset from start of file of pens (should immediately follow this)
-
-//     // #[br(count = pen_count)]
-//     // pens: Vec<ArrayOf<Field>>,
-// }
+pub struct Layer {}

+ 2 - 1
src/ezcad/mod.rs

@@ -1,4 +1,5 @@
 pub mod array_of;
-pub mod basic_field;
 pub mod field_of;
 pub mod file;
+pub mod pen;
+pub mod types;

+ 61 - 0
src/ezcad/pen.rs

@@ -0,0 +1,61 @@
+use binrw::{BinRead, BinWrite, FilePtr64};
+
+use crate::types::{Color, Double, Field, WString, Wobble, U32};
+
+#[derive(BinRead, Debug)]
+pub struct PenHeader {
+    pub pen_count: u32,
+    #[br(args {
+        inner: binrw::args! {
+            pen_count
+        }
+    })]
+    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)]
+    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, // Changes with wobble relative speed
+    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, // Only available if continue_mode is false
+    _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: Wobble,
+    pub continue_mode: U32,
+    _unknown_7: [Field; 12],
+    pub wobble_diameter_2: Double, // Only with wobble type ellipse
+    _unknown_8: [Field; 26],
+}

+ 77 - 0
src/ezcad/types.rs

@@ -0,0 +1,77 @@
+#![allow(dead_code)]
+
+use binrw::{binrw, BinRead, BinWrite};
+
+use crate::{array_of::ArrayOf, field_of::FieldOf};
+
+/// 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 Wobble = FieldOf<WobbleType>;
+
+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 {
+    pub red: u8,
+    pub green: u8,
+    pub blue: u8,
+    pub alpha: u8,
+}
+
+#[derive(BinRead, BinWrite, PartialEq, Debug)]
+pub struct Point {
+    pub x: f64,
+    pub y: f64,
+}
+
+#[binrw]
+#[derive(PartialEq, Debug)]
+pub struct MultiLine {
+    #[br(temp)]
+    #[bw(try_calc(u32::try_from(points.len())))]
+    point_count: u32,
+    #[br(count = point_count)]
+    points: Vec<Point>,
+}
+
+#[derive(BinRead, BinWrite, PartialEq, Debug)]
+#[brw(repr(u32))]
+#[repr(u32)]
+pub enum WobbleType {
+    Spiral = 0,
+    Sinusoidal = 1,
+    Ellipse = 2,
+    Vert8 = 3,
+    Hori8 = 4,
+}
+
+#[derive(BinRead, BinWrite, PartialEq, Debug)]
+#[brw(repr(u32))]
+#[repr(u32)]
+pub enum ObjectType {
+    Curve = 1,
+    Point = 2,
+    Rectangle = 3,
+    Circle = 4,
+    Ellipse = 5,
+    Polygon = 6,
+    Hatch = 32,
+}

+ 1 - 1
src/main.rs

@@ -6,7 +6,6 @@ use std::{
 use binrw::BinRead;
 use clap::Parser;
 use ezcad::file::Header;
-use log::debug;
 
 #[derive(Debug, Parser)]
 struct Cli {
@@ -39,4 +38,5 @@ fn main() {
     let file = Header::read_le(&mut input).unwrap();
     dbg!(&file.pens_offset.start_offset.pens[255].color);
     dbg!(String::from(&file.pens_offset.start_offset.pens[255].name));
+    dbg!(&file.pens_offset.start_offset.pens[255].wobble_type);
 }

+ 1 - 1
tests/basic_field.rs

@@ -1,7 +1,7 @@
 use std::io::Cursor;
 
 use binrw::{BinRead, BinWrite};
-use ezcad::basic_field::U32;
+use ezcad::types::U32;
 
 #[test]
 fn field_u32_be() {