use std::collections::BTreeMap;
use std::fs;
use std::io::{self, BufWriter, Read, Write};
use std::path::Path;
use crate::error::ErrorExt;
use crate::navmesh::bytes::Reader;
use crate::navmesh::detour::tile::TileData;
pub const SET_MAGIC: i32 = i32::from_le_bytes(*b"TESM");
pub const SET_VERSION: i32 = 9;
pub const SET_VERSION_OLD: i32 = 7;
const SET_HEADER_LEN: usize = 48;
const RECORD_HEADER_LEN: usize = 16;
const EMPTY_SLOT: u32 = 0xFFFF_FFFF;
const READ_CHUNK: usize = 1 << 20;
const SALT_MASK: u32 = 0xFFFF;
const TILE_MASK: u32 = 0x0FFF_FFFF;
const POLY_MASK: u32 = 0x000F_FFFF;
#[must_use]
pub fn encode_poly_ref(salt: u32, tile: u32, poly: u32) -> u64 {
(u64::from(salt & SALT_MASK) << 48)
| (u64::from(tile & TILE_MASK) << 20)
| u64::from(poly & POLY_MASK)
}
#[must_use]
pub fn decode_poly_ref(r: u64) -> (u32, u32, u32) {
let field = |shift: u32, mask: u32| u32::try_from((r >> shift) & u64::from(mask)).unwrap_or(0);
(
field(48, SALT_MASK),
field(20, TILE_MASK),
field(0, POLY_MASK),
)
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct NavMeshParams {
pub orig: [f32; 3],
pub tile_width: f32,
pub tile_height: f32,
pub max_tiles: i32,
pub max_polys: i32,
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct SetTile {
pub tile_ref: u64,
pub padding: u32,
pub data: TileData,
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct NavMeshSet {
pub version: i32,
pub tiles_x: i32,
pub tiles_z: i32,
pub params: NavMeshParams,
pub tiles: Vec<SetTile>,
}
fn file_truncated() -> io::Error {
error!("navmesh file truncated")
}
fn read_chunked(r: &mut impl Read, len: usize) -> io::Result<Option<Vec<u8>>> {
let mut buf = Vec::new();
while buf.len() < len {
let start = buf.len();
let end = start.saturating_add(READ_CHUNK).min(len);
buf.resize(end, 0);
let Some(chunk) = buf.get_mut(start..end) else {
return Ok(None);
};
match r.read_exact(chunk) {
Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e),
}
}
Ok(Some(buf))
}
impl NavMeshSet {
pub fn read(r: &mut impl Read) -> io::Result<Self> {
let mut head = [0u8; SET_HEADER_LEN];
r.read_exact(&mut head).map_err(|e| {
if e.kind() == io::ErrorKind::UnexpectedEof {
file_truncated()
} else {
e
}
})?;
let mut h = Reader::new(&head);
let magic = h.i32()?;
if magic != SET_MAGIC {
return Err(error!("Invalid header format (bad magic)"));
}
let version = h.i32()?;
if version != SET_VERSION && version != SET_VERSION_OLD {
return Err(error!("Invalid version {version}"));
}
let num_tiles = h.i32()?;
let tiles_x = h.i32()?;
let tiles_z = h.i32()?;
let params = NavMeshParams {
orig: h.f32x3()?,
tile_width: h.f32()?,
tile_height: h.f32()?,
max_tiles: h.i32()?,
max_polys: h.i32()?,
};
let max_tiles = u32::try_from(params.max_tiles)
.map_err(|_| error!("negative maxTiles {}", params.max_tiles))?;
let num_tiles =
u32::try_from(num_tiles).map_err(|_| error!("negative numTiles {num_tiles}"))?;
if num_tiles > max_tiles {
return Err(error!("numTiles {num_tiles} exceeds maxTiles {max_tiles}"));
}
let table = u64::from(max_tiles) * 4;
let skipped = io::copy(&mut r.by_ref().take(table), &mut io::sink())?;
if skipped != table {
return Err(file_truncated());
}
let mut tiles = Vec::new();
for i in 0..num_tiles {
let mut rec = [0u8; RECORD_HEADER_LEN];
r.read_exact(&mut rec).map_err(|e| {
if e.kind() == io::ErrorKind::UnexpectedEof {
error!("tile record {i} runs past the end of the file")
} else {
e
}
})?;
let mut rh = Reader::new(&rec);
let tile_ref = rh.u64()?;
let data_size = rh.i32()?;
let padding = rh.u32()?;
if tile_ref == 0 {
return Err(error!("tile record {i} has tileRef 0"));
}
let size = usize::try_from(data_size)
.ok()
.filter(|&n| n > 0)
.ok_or_else(|| error!("tile record {i} has dataSize {data_size}"))?;
let blob = read_chunked(r, size)?
.ok_or_else(|| error!("tile record {i} runs past the end of the file"))?;
let data = TileData::parse(&blob).prepend_error(format!("tile record {i}:"))?;
tiles.push(SetTile {
tile_ref,
padding,
data,
});
}
Ok(Self {
version,
tiles_x,
tiles_z,
params,
tiles,
})
}
pub fn write(&self, w: &mut impl Write) -> io::Result<()> {
let max_tiles = u32::try_from(self.params.max_tiles)
.map_err(|_| error!("negative maxTiles {}", self.params.max_tiles))?;
let too_many = || error!("{} tiles exceed maxTiles {max_tiles}", self.tiles.len());
let num_tiles = u32::try_from(self.tiles.len()).map_err(|_| too_many())?;
if num_tiles > max_tiles {
return Err(too_many());
}
let mut offsets: BTreeMap<u32, u32> = BTreeMap::new();
let mut sizes = Vec::with_capacity(self.tiles.len());
let mut offset: u64 = 0;
for t in &self.tiles {
if t.tile_ref == 0 {
return Err(error!("tile with tileRef 0"));
}
let slot = decode_poly_ref(t.tile_ref).1;
if slot >= max_tiles {
return Err(error!("tile slot {slot} is not below maxTiles {max_tiles}"));
}
let size = t
.data
.blob_len()
.and_then(|n| i32::try_from(n).ok())
.ok_or_else(|| error!("tile in slot {slot} is too large"))?;
let at = u32::try_from(offset)
.ok()
.filter(|&o| o != EMPTY_SLOT)
.ok_or_else(|| error!("tile offset {offset} does not fit the offset table"))?;
if offsets.insert(slot, at).is_some() {
return Err(error!("two tiles in slot {slot}"));
}
sizes.push(size);
offset +=
u64::try_from(RECORD_HEADER_LEN).unwrap_or(16) + u64::from(size.unsigned_abs());
}
let mut head = Vec::with_capacity(SET_HEADER_LEN);
head.extend_from_slice(&SET_MAGIC.to_le_bytes());
head.extend_from_slice(&SET_VERSION.to_le_bytes());
head.extend_from_slice(&num_tiles.to_le_bytes());
head.extend_from_slice(&self.tiles_x.to_le_bytes());
head.extend_from_slice(&self.tiles_z.to_le_bytes());
for f in self.params.orig {
head.extend_from_slice(&f.to_le_bytes());
}
head.extend_from_slice(&self.params.tile_width.to_le_bytes());
head.extend_from_slice(&self.params.tile_height.to_le_bytes());
head.extend_from_slice(&self.params.max_tiles.to_le_bytes());
head.extend_from_slice(&self.params.max_polys.to_le_bytes());
w.write_all(&head)?;
let mut chunk = Vec::with_capacity(16 * 1024);
for slot in 0..max_tiles {
let v = offsets.get(&slot).copied().unwrap_or(EMPTY_SLOT);
chunk.extend_from_slice(&v.to_le_bytes());
if chunk.len() >= 16 * 1024 {
w.write_all(&chunk)?;
chunk.clear();
}
}
w.write_all(&chunk)?;
for (t, size) in self.tiles.iter().zip(sizes) {
let blob = t.data.to_bytes();
if i32::try_from(blob.len()).ok() != Some(size) {
return Err(error!("tile blob size changed while writing"));
}
w.write_all(&t.tile_ref.to_le_bytes())?;
w.write_all(&size.to_le_bytes())?;
w.write_all(&t.padding.to_le_bytes())?;
w.write_all(&blob)?;
}
Ok(())
}
pub fn write_atomic(&self, path: &Path) -> io::Result<()> {
let name = path
.file_name()
.ok_or_else(|| error!("{} has no file name", path.display()))?
.to_string_lossy()
.into_owned();
let dir = match path.parent() {
Some(p) if !p.as_os_str().is_empty() => p,
_ => Path::new("."),
};
let pid = std::process::id();
let mut n = 0u32;
let (tmp, file) = loop {
let tmp = dir.join(format!(".{name}.{pid}.{n}.tmp"));
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)
{
Ok(f) => break (tmp, f),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists && n < 1000 => n += 1,
Err(e) => return Err(e),
}
};
let result = (|| {
let mut w = BufWriter::new(file);
self.write(&mut w)?;
let file = w.into_inner().map_err(io::IntoInnerError::into_error)?;
file.sync_all()?;
fs::rename(&tmp, path)
})();
if result.is_err() {
let _ = fs::remove_file(&tmp);
}
result.prepend_error(format!("writing {}:", path.display()))
}
}