1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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/>.

#[macro_use]
extern crate bitflags;
extern crate bit_vec;
extern crate nalgebra as na;

#[macro_use]
mod macros;
pub mod directory;
pub mod lumps;
pub mod types;

use lumps::*;
use directory::Header;
use types::{Error, Result};

/// Represents a parsed BSP file.
#[derive(Debug, Clone)]
pub struct BSPFile {
    pub directory: Header,
    pub entities: EntitiesLump,
    pub textures: TexturesLump,
    pub planes: PlanesLump,
    pub light_vols: LightVolsLump,
    pub brushes: BrushesLump,
    pub vertices: VerticesLump,
    pub meshverts: MeshVertsLump,
    pub light_maps: LightMapsLump,
    pub effects: EffectsLump,
    pub faces: FaceLump,
    pub tree: BSPTree,
    pub visdata: VisDataLump,
    pub models: ModelsLump,

    /// Only present for Quake live maps (IBSP47)
    pub advertisements: Option<AdvertisementsLump>
}

impl BSPFile {
    /// Try to parse the given buffer as a BSP file
    pub fn from_buffer(buf: Box<[u8]>) -> Result<BSPFile> {
        let header = Header::from(&buf)?;

        match header.version {
            // Quake 3 or Quake LIVE (IBSP47)
            0x2e | 0x2f => {
                let entities = EntitiesLump::from_lump(header.get_lump(&buf, 0))?;
                let textures = TexturesLump::from_lump(header.get_lump(&buf, 1))?;
                let planes = PlanesLump::from_lump(header.get_lump(&buf, 2))?;
                let vertices = VerticesLump::from_lump(header.get_lump(&buf, 10))?;
                let meshverts = MeshVertsLump::from_lump(header.get_lump(&buf, 11))?;
                let light_maps = LightMapsLump::from_lump(header.get_lump(&buf, 14))?;
                let light_vols = LightVolsLump::from_lump(header.get_lump(&buf, 15))?;
                let visdata = VisDataLump::from_lump(header.get_lump(&buf, 16))?;
                let brushes = BrushesLump::from_lump(
                    header.get_lump(&buf, 8),
                    header.get_lump(&buf, 9),
                    &textures,
                    &planes
                )?;
                let effects = EffectsLump::from_lump(header.get_lump(&buf, 12), &brushes)?;
                let faces = FaceLump::from_lump(
                    header.get_lump(&buf, 13),
                    &textures,
                    &effects,
                    &vertices,
                    &meshverts,
                    &light_maps,
                )?;
                let tree = BSPTree::from_lumps(
                    header.get_lump(&buf, 3),
                    header.get_lump(&buf, 4),
                    header.get_lump(&buf, 5),
                    header.get_lump(&buf, 6),
                    &faces,
                    &brushes,
                )?;

                let models = ModelsLump::from_lump(header.get_lump(&buf, 7), &faces, &brushes)?;

                // Quake Live has an advertisements lump
                let advertisements = if header.version == 0x2f {
                    Some(AdvertisementsLump::from_lump(header.get_lump(&buf, 17))?)
                } else {
                    None
                };

                Ok(BSPFile {
                    directory: header,
                    entities,
                    textures,
                    planes,
                    light_vols,
                    light_maps,
                    vertices,
                    meshverts,
                    visdata,
                    advertisements,
                    brushes,
                    effects,
                    faces,
                    tree,
                    models
                })
            }
            _ => Err(Error::Unsupported {
                version: header.version,
            }),
        }
    }
}