diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
commit | d076d3a6fd484e298915cd85609ba9706abacc87 (patch) | |
tree | 0d74395ff54e56fd54cab35ec0f27254e8306822 /stockton-levels/src/q3/effects.rs | |
parent | 5dc6c64394d1e0a09c882b88ecb2b8f04f9e5b22 (diff) |
refactor(all): move stockton-bsp to this repo and start using traits
Diffstat (limited to 'stockton-levels/src/q3/effects.rs')
-rw-r--r-- | stockton-levels/src/q3/effects.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/stockton-levels/src/q3/effects.rs b/stockton-levels/src/q3/effects.rs new file mode 100644 index 0000000..2a76da9 --- /dev/null +++ b/stockton-levels/src/q3/effects.rs @@ -0,0 +1,63 @@ +// Copyright (C) 2019 Oscar Shrimpton +// +// This file is part of stockton-bsp. +// +// stockton-bsp is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// stockton-bsp is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with stockton-bsp. If not, see <http://www.gnu.org/licenses/>. + +use std::str; + +use crate::helpers::slice_to_u32; +use crate::types::{Result, ParseError}; +use crate::traits::effects::*; +use super::Q3BSPFile; + +/// The size of one effect definition +const EFFECT_SIZE: usize = 64 + 4 + 4; + +pub fn from_data(data: &[u8], n_brushes: u32) -> Result<Box<[Effect]>> { + if data.len() % EFFECT_SIZE != 0 { + return Err(ParseError::Invalid); + } + let length = data.len() / EFFECT_SIZE; + + let mut effects = Vec::with_capacity(length); + for n in 0..length { + let raw = &data[n * EFFECT_SIZE..(n + 1) * EFFECT_SIZE]; + + let brush_idx = slice_to_u32(&raw[64..68]); + if brush_idx >= n_brushes { + return Err(ParseError::Invalid); + } + + effects.push(Effect { + name: str::from_utf8(&raw[..64]).map_err(|_| ParseError::Invalid)?.to_owned(), + brush_idx + }); + } + + Ok(effects.into_boxed_slice()) +} + + +impl<'a> HasEffects<'a> for Q3BSPFile { + type EffectsIter = std::slice::Iter<'a, Effect>; + + fn effects_iter(&'a self) -> Self::EffectsIter { + self.effects.iter() + } + + fn get_effect(&'a self, index: u32) -> &'a Effect { + &self.effects[index as usize] + } +} |