goldsrc_rs/lib.rs
1#![feature(array_try_from_fn)]
2
3#[cfg(test)]
4#[macro_use]
5extern crate static_assertions;
6
7use std::{
8 collections::HashMap,
9 io::{self, Read, Seek},
10};
11
12pub use repr::*;
13
14mod parser;
15mod repr;
16
17/// Parse all entries from a WAD file.
18///
19/// # Arguments
20/// - `reader` — Any type implementing [`Read`] + [`Seek`] + [`Send`] + `'static`.
21/// Typically a file handle or in-memory buffer containing the WAD data.
22/// - `remap_name_to_lower` — If `true`, forces all entries names to lowercase
23/// (useful because WAD entries are case-insensitive in practice).
24///
25/// # Returns
26/// A [`HashMap`] mapping lump names ([`CStr16`]) to their corresponding [`wad::Entry`].
27///
28/// # Errors
29/// Returns an [`io::Error`] if the stream cannot be read or the WAD is invalid.
30///
31/// # Example
32/// ```no_run
33/// use std::fs::File;
34///
35/// let file = File::open("halflife.wad").expect("file open");
36/// let entries = goldsrc_rs::wad_entries(file, true).expect("parsing wad header");
37/// ```
38pub fn wad_entries<R>(
39 reader: R,
40 remap_name_to_lower: bool,
41) -> io::Result<HashMap<CStr16, wad::Entry>>
42where
43 R: Read + Seek + Send + 'static,
44{
45 parser::wad::entries(reader, remap_name_to_lower)
46}
47
48/// Parse a Half-Life BSP level file.
49///
50/// # Arguments
51/// - `reader` — Any type implementing [`Read`] + [`Seek`].
52///
53/// # Returns
54/// A fully parsed [`bsp::Level`] struct with geometry, entities, textures, and more.
55///
56/// # Errors
57/// Returns an [`io::Error`] if the BSP is malformed or the stream cannot be read.
58///
59/// # Example
60/// ```no_run
61/// use std::fs::File;
62///
63/// let file = File::open("c1a0.bsp").expect("file opening");
64/// let level = goldsrc_rs::bsp(file).expect("map parsing");
65/// ```
66pub fn bsp<R: Read + Seek>(reader: R) -> io::Result<bsp::Level> {
67 parser::bsp::level(reader)
68}
69
70/// Parse a Quake-style `.pic` image (qpic).
71///
72/// # Arguments
73/// - `reader` — Any type implementing [`Read`].
74///
75/// # Returns
76/// A [`texture::Picture`] representing the decoded image.
77///
78/// # Errors
79/// Returns an [`io::Error`] if the stream cannot be read or is not a valid qpic.
80pub fn pic<R: Read>(reader: R) -> io::Result<texture::Picture> {
81 parser::texture::qpic(reader)
82}
83
84/// Parse a Half-Life `miptex` texture lump.
85///
86/// # Arguments
87/// - `reader` — Any type implementing [`Read`].
88///
89/// # Returns
90/// A [`texture::MipTexture`] containing all mipmap levels and optional palette data.
91///
92/// # Errors
93/// Returns an [`io::Error`] if parsing fails.
94pub fn miptex<R: Read>(reader: R) -> io::Result<texture::MipTexture> {
95 parser::texture::miptex(reader)
96}
97
98/// Parse a Half-Life font resource (e.g. used for HUD text).
99///
100/// # Arguments
101/// - `reader` — Any type implementing [`Read`].
102///
103/// # Returns
104/// A [`texture::Font`] object with character bitmaps and metadata.
105///
106/// # Errors
107/// Returns an [`io::Error`] if parsing fails.
108pub fn font<R: Read>(reader: R) -> io::Result<texture::Font> {
109 parser::texture::font(reader)
110}