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

chore: code cleanup. #52

Merged
merged 6 commits into from
Oct 15, 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
390 changes: 19 additions & 371 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
resolver = "2"
members = [
"ashen",
"engine",
"cli",
]


[profile.release]
overflow-checks = true


[profile.perf]
inherits = "release"
codegen-units = 1
incremental = false
lto = "fat"
opt-level = 3
panic = "abort"

[profile.release]
debug = true
overflow-checks = true
overflow-checks = false
14 changes: 13 additions & 1 deletion ashen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ version = "0.1.0"
edition = "2021"

[dependencies]
engine = { path = "../engine/" }
bitflags = "2.4.2"
const_format = "0.2.32"
fixed = "1.24.0"
flate2 = "1.0.28"
itertools = "0.12.0"
lewton = "0.10.2"
nom = "7.1.3"
paste = "1.0.14"

[dev-dependencies]
assert_approx_eq = "1.1.0"
eyre = "0.6.8"
image = "0.24.8"
16 changes: 6 additions & 10 deletions engine/src/asset/color_map.rs → ashen/src/asset/color_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Deref;

use super::{extension::*, AssetParser};
use super::Parser;
use crate::{error, utils::nom::*};

const COLORS_COUNT: usize = 256;
Expand All @@ -15,7 +15,7 @@
}

impl Color {
pub fn from_12_bit(color: u16) -> Self {

Check warning on line 18 in ashen/src/asset/color_map.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function which may panic missing `# Panics` section

warning: docs for function which may panic missing `# Panics` section --> ashen/src/asset/color_map.rs:18:5 | 18 | pub fn from_12_bit(color: u16) -> Self { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> ashen/src/asset/color_map.rs:20:9 | 20 | assert!(color <= 0xFFF, "12 bit color is smaller than 0xFFF"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc note: the lint level is defined here --> ashen/src/lib.rs:1:9 | 1 | #![warn(clippy::pedantic)] | ^^^^^^^^^^^^^^^^ = note: `#[warn(clippy::missing_panics_doc)]` implied by `#[warn(clippy::pedantic)]`
// TODO(nenikitov): return result.
assert!(color <= 0xFFF, "12 bit color is smaller than 0xFFF");

Expand All @@ -35,12 +35,10 @@
}
}

impl AssetParser<Wildcard> for Color {
type Output = Self;

impl Parser for Color {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, color) = number::le_u32(input)?;
Ok((input, Self::from_12_bit(color as u16)))
Expand All @@ -55,12 +53,10 @@
pub shades: Box<[[Color; COLORS_COUNT]; SHADES_COUNT]>,
}

impl AssetParser<Pack> for ColorMap {
type Output = Self;

impl Parser for ColorMap {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
error::ensure_bytes_length(
input,
Expand Down Expand Up @@ -155,7 +151,7 @@
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, color_map) = <ColorMap as AssetParser<Pack>>::parser(())(&COLOR_MAP_DATA)?;
let (_, color_map) = ColorMap::parser(())(&COLOR_MAP_DATA)?;

output_file(
parsed_file_path!("color-map/monsters.png"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem;

use super::{extension::*, AssetParser};
use super::Parser;
use crate::{error, utils::nom::*};

const ROWS_COUNT: usize = 256;
Expand All @@ -12,12 +12,10 @@ pub struct GammaTable {
pub lookups: Box<[[u8; ROWS_COUNT]; COLS_COUNT]>,
}

impl AssetParser<Pack> for GammaTable {
type Output = Self;

impl Parser for GammaTable {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
error::ensure_bytes_length(
input,
Expand Down Expand Up @@ -61,7 +59,7 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, gamma_table) = <GammaTable as AssetParser<Pack>>::parser(())(&GAMMA_TABLE_DATA)?;
let (_, gamma_table) = GammaTable::parser(())(&GAMMA_TABLE_DATA)?;

let gamma_table = gamma_table
.lookups
Expand Down
22 changes: 22 additions & 0 deletions ashen/src/asset/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod color_map;
pub mod gamma_table;
pub mod model;
pub mod pack_file;
pub mod pack_info;
pub mod skybox;
pub mod sound;
pub mod string_table;
pub mod texture;

use crate::utils::nom::{Input, Result};

pub trait Parser
where
Self: Sized,
{
/// Extra information passed down to the parser.
type Context<'ctx>;

/// Generates a new parser with the provided context.
fn parser(context: Self::Context<'_>) -> impl Fn(Input) -> Result<Self>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::Parser, utils::nom::*};

// TODO(nenikitov): Should probably be a fancy utility class
// With generics for data type and dimension
Expand All @@ -12,12 +9,10 @@ pub struct Vec3 {
pub z: f32,
}

impl AssetParser<Wildcard> for Vec3 {
type Output = Self;

impl Parser for Vec3 {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, x) = number::le_i16f16(input)?;
let (input, y) = number::le_i16f16(input)?;
Expand Down Expand Up @@ -52,12 +47,10 @@ pub struct VertexTransform {
origin: Vec3,
}

impl AssetParser<Wildcard> for ModelVertex {
type Output = Self;

impl Parser for ModelVertex {
type Context<'ctx> = VertexTransform;

fn parser(transform: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser(transform: Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
macro_rules! transform {
($coordinate: ident) => {
(transform.scale.$coordinate * $coordinate as f32 / -256.0
Expand Down Expand Up @@ -97,12 +90,10 @@ pub struct ModelSpecs {
pub frame_size: u32,
}

impl AssetParser<Wildcard> for ModelFrame {
type Output = Self;

impl Parser for ModelFrame {
type Context<'ctx> = ModelSpecs;

fn parser(model_specs: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser(model_specs: Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, scale) = Vec3::parser(())(input)?;
let (input, origin) = Vec3::parser(())(input)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::Parser, utils::nom::*};

pub struct ModelHeader {
pub triangle_count: u32,
Expand All @@ -18,12 +15,10 @@ pub struct ModelHeader {
pub locator_nodes: [u8; 16],
}

impl AssetParser<Wildcard> for ModelHeader {
type Output = Self;

impl Parser for ModelHeader {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, triangle_count) = number::le_u32(input)?;
let (input, vertex_count) = number::le_u32(input)?;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::Parser, utils::nom::*};

pub struct ModelSequence {
pub frames: Vec<u32>,
}

impl AssetParser<Wildcard> for ModelSequence {
type Output = Self;

impl Parser for ModelSequence {
// TODO(nenikitov): Maybe refactor it to not accept full input.
// In other asset parts, it's parent's responsability to cut input into slices
// for asset parts to parse.
type Context<'ctx> = Input<'ctx>;

fn parser(full_input: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser(full_input: Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, frame_count) = number::le_u32(input)?;
let (input, offset) = number::le_u32(input)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::Parser, utils::nom::*};

pub struct ModelPoint {
pub vertex_index: u16,
Expand All @@ -14,12 +11,10 @@ pub struct TextureDimensions {
pub height: u32,
}

impl AssetParser<Wildcard> for ModelPoint {
type Output = Self;

impl Parser for ModelPoint {
type Context<'ctx> = &'ctx TextureDimensions;

fn parser(texture_dimensions: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser(texture_dimensions: Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, vertex_index) = number::le_u16(input)?;

Expand All @@ -39,12 +34,10 @@ pub struct ModelTriangle {
pub points: [ModelPoint; 3],
}

impl AssetParser<Wildcard> for ModelTriangle {
type Output = Self;

impl Parser for ModelTriangle {
type Context<'ctx> = TextureDimensions;

fn parser(texture_dimensions: Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser(texture_dimensions: Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, points) = multi::count!(ModelPoint::parser(&texture_dimensions))(input)?;

Expand Down
13 changes: 5 additions & 8 deletions engine/src/asset/model/mod.rs → ashen/src/asset/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use dat::{
};

use super::{
extension::*,
texture::{Texture, TextureSize},
AssetParser,
Parser,
};
use crate::utils::nom::*;

Expand All @@ -21,12 +20,10 @@ pub struct Model {
pub frames: Vec<ModelFrame>,
}

impl AssetParser<Pack> for Model {
type Output = Self;

impl Parser for Model {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (_, header) = ModelHeader::parser(())(input)?;

Expand Down Expand Up @@ -95,9 +92,9 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, model) = <Model as AssetParser<Pack>>::parser(())(&MODEL_DATA)?;
let (_, model) = Model::parser(())(&MODEL_DATA)?;
let palette = {
let (_, color_map) = <ColorMap as AssetParser<Pack>>::parser(())(&COLOR_MAP_DATA)?;
let (_, color_map) = ColorMap::parser(())(&COLOR_MAP_DATA)?;
color_map.shades[15]
};

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Index;

use super::{extension::*, AssetParser};
use super::Parser;
use crate::utils::nom::*;

#[derive(Debug)]
Expand All @@ -9,12 +9,10 @@ pub struct PackInfo {
pub size: u32,
}

impl AssetParser<Wildcard> for PackInfo {
type Output = Self;

impl Parser for PackInfo {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, offset) = number::le_u32(input)?;

Expand Down
11 changes: 4 additions & 7 deletions engine/src/asset/skybox.rs → ashen/src/asset/skybox.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{
extension::*,
texture::{Texture, TextureSize},
AssetParser,
Parser,
};
use crate::{asset::color_map::Color, utils::nom::*};

Expand All @@ -12,12 +11,10 @@ pub struct Skybox {
pub texture: Texture,
}

impl AssetParser<Pack> for Skybox {
type Output = Self;

impl Parser for Skybox {
type Context<'ctx> = ();

fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self::Output> {
fn parser((): Self::Context<'_>) -> impl Fn(Input) -> Result<Self> {
move |input| {
let (input, width) = number::le_u32(input)?;

Expand Down Expand Up @@ -51,7 +48,7 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, skybox) = <Skybox as AssetParser<Pack>>::parser(())(&SKYBOX_DATA)?;
let (_, skybox) = Skybox::parser(())(&SKYBOX_DATA)?;

output_file(
parsed_file_path!("skyboxes/level-1.png"),
Expand Down
Loading
Loading