use file/buffreader in docs

This commit is contained in:
Robin Appelman 2025-05-30 18:50:38 +02:00
commit d61a80d83f
6 changed files with 61 additions and 37 deletions

View file

@ -10,11 +10,12 @@ navigation areas are exposed.
```rust
use sourcenav::get_quad_tree;
use std::io::Cursor;
use std::fs::File;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = std::fs::read("data/pl_badwater.nav")?;
let tree = get_quad_tree(&mut Cursor::new(file))?;
let mut file = BufReader::new(File::open("data/pl_badwater.nav")?);
let tree = get_quad_tree(&mut file)?;
assert_eq!(220.83125, tree.find_best_height(320.0, -1030.0, 0.0));

View file

@ -1,7 +1,7 @@
use std::hint::black_box;
use criterion::{criterion_group, criterion_main, Criterion};
use sourcenav::{get_quad_tree, read_areas};
use std::fs::read;
use std::hint::black_box;
use std::io::Cursor;
fn bench_badwater_areas(c: &mut Criterion) {
@ -30,10 +30,19 @@ fn bench_tree_query(c: &mut Criterion) {
c.bench_function("badwater areas", |b| {
b.iter(|| {
let _ = black_box(tree.find_best_height(black_box(320.0), black_box(-1030.0), black_box(0.0)));
let _ = black_box(tree.find_best_height(
black_box(320.0),
black_box(-1030.0),
black_box(0.0),
));
})
});
}
criterion_group!(benches, bench_badwater_areas, bench_badwater_quads, bench_tree_query);
criterion_group!(
benches,
bench_badwater_areas,
bench_badwater_quads,
bench_tree_query
);
criterion_main!(benches);

View file

@ -10,5 +10,8 @@
inputs.flakelight.follows = "flakelight";
};
};
outputs = {mill-scale, ...}: mill-scale ./. {};
outputs = {mill-scale, ...}:
mill-scale ./. {
extraPaths = [./data];
};
}

View file

@ -22,11 +22,12 @@ pub struct NavQuadTree(QuadTree<NavQuad, HammerUnit, [(ItemId, Rect); 4]>);
///
/// ```no_run
/// use sourcenav::get_quad_tree;
/// use std::io::Cursor;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = get_quad_tree(&mut file)?;
/// # Ok(())
/// # }
/// ```
@ -67,11 +68,12 @@ impl NavQuadTree {
///
/// ```no_run
/// use sourcenav::get_quad_tree;
/// use std::io::Cursor;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = get_quad_tree(&mut file)?;
/// let areas = tree.query(150.0, -312.0);
/// # Ok(())
/// # }
@ -90,11 +92,12 @@ impl NavQuadTree {
///
/// ```no_run
/// use sourcenav::get_quad_tree;
/// use std::io::Cursor;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = get_quad_tree(&mut file)?;
/// let heights = tree.find_z_height(150.0, -312.0);
/// # Ok(())
/// # }
@ -125,11 +128,12 @@ impl NavQuadTree {
///
/// ```no_run
/// use sourcenav::get_quad_tree;
/// use std::io::Cursor;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = get_quad_tree(&mut file)?;
/// for quad in tree.quads() {
/// println!("area: {:?}", quad)
/// }
@ -143,11 +147,11 @@ impl NavQuadTree {
#[test]
fn test_tree() {
use std::io::Cursor;
use std::fs::File;
use std::io::BufReader;
let file = std::fs::read("data/pl_badwater.nav").unwrap();
let mut cursor = Cursor::new(&file);
let tree = get_quad_tree(&mut cursor).unwrap();
let mut file = BufReader::new(File::open("data/pl_badwater.nav").unwrap());
let tree = get_quad_tree(&mut file).unwrap();
// single flat plane
let point1 = (1600.0, -1300.0);

View file

@ -3,10 +3,10 @@ use aabb_quadtree::Spatial;
use binrw::io::{Read, Seek};
use binrw::{BinRead, BinReaderExt, BinResult, Endian};
use euclid::{TypedPoint2D, TypedSize2D};
use std::convert::TryFrom;
use std::fmt;
use std::fmt::Debug;
use std::ops::Index;
use std::convert::TryFrom;
/// A 3 dimensional coordinate
#[derive(Debug, BinRead)]
@ -28,7 +28,11 @@ pub struct NavAreaFlags(pub u32);
impl BinRead for NavAreaFlags {
type Args<'a> = (u32,);
fn read_options<R: Read + Seek>(reader: &mut R, endian: Endian, args: Self::Args<'static>) -> BinResult<Self> {
fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
args: Self::Args<'static>,
) -> BinResult<Self> {
let flags = match args.0 {
0..=8 => reader.read_type::<u8>(endian)? as u32,
9..=12 => reader.read_type::<u16>(endian)? as u32,
@ -277,11 +281,12 @@ impl NavQuad {
///
/// ```no_run
/// use sourcenav::get_quad_tree;
/// use std::io::Cursor;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = get_quad_tree(&mut file)?;
/// let area = tree.query(150.0, -312.0).next().unwrap();
///
/// let height = area.get_z_height(150.0, -312.0);

View file

@ -1,8 +1,8 @@
pub use crate::navmesh::NavArea;
use binrw::io::{Read, Seek};
use binrw::{BinRead, BinReaderExt};
use thiserror::Error;
use std::convert::TryFrom;
use thiserror::Error;
/// Errors that can occur when parsing the binary nav file
#[derive(Debug, Error)]
@ -35,16 +35,16 @@ struct FixedString {
///
/// ```no_run
/// use sourcenav::read_areas;
/// use std::fs::File;
/// use std::io::BufReader;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::io::Cursor;
/// let file = std::fs::read("path/to/navfile.nav")?;
/// let tree = read_areas(&mut Cursor::new(file))?;
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
/// let tree = read_areas(&mut file)?;
/// # Ok(())
/// # }
/// ```
pub fn read_areas<R: Read + Seek>(data: &mut R) -> Result<Vec<NavArea>, ParseError> {
let magic = data.read_le()?;
if magic != 0xFEED_FACE {
return Err(ParseError::InvalidMagicNumber(magic));
@ -97,8 +97,10 @@ pub fn read_areas<R: Read + Seek>(data: &mut R) -> Result<Vec<NavArea>, ParseErr
#[test]
fn test() {
use std::io::Cursor;
let file = std::fs::read("data/pl_badwater.nav").unwrap();
let areas = read_areas(&mut Cursor::new(file)).unwrap();
use std::fs::File;
use std::io::BufReader;
let mut file = BufReader::new(File::open("data/pl_badwater.nav").unwrap());
let areas = read_areas(&mut file).unwrap();
assert_eq!(1930, areas.len());
}