Skip to content

Commit

Permalink
Patrol objects testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Feb 21, 2024
1 parent 382b786 commit 2042a6f
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 102 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 9 additions & 6 deletions crates/xray-spawn/src/chunk/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ impl ChunkWriter {
file: &mut File,
index: u32,
) -> io::Result<usize> {
self.buffer.flush().unwrap();
self.buffer.flush()?;

file.write_u32::<T>(index).unwrap();
file.write_u32::<T>(index)?;
file.write_u32::<T>(self.buffer.len() as u32)?;
file.write(self.buffer.as_slice())
}

/// Flush all the written data as chunk into the file.
pub fn flush_chunk_into_buffer<T: ByteOrder>(&mut self, index: u32) -> io::Result<Vec<u8>> {
self.buffer.flush().unwrap();
pub fn flush_chunk_into_buffer<T: ByteOrder>(&mut self, index: usize) -> io::Result<Vec<u8>> {
self.buffer.flush()?;

let mut buffer: Vec<u8> = Vec::new();

buffer.write_u32::<T>(index).unwrap();
buffer.write_u32::<T>(index as u32)?;
buffer.write_u32::<T>(self.buffer.len() as u32)?;
buffer.write(self.buffer.as_slice())?;

let bytes_written: usize = buffer.write(self.buffer.as_slice())?;

assert_eq!(bytes_written, self.buffer.len());

Ok(buffer)
}
Expand Down
185 changes: 183 additions & 2 deletions crates/xray-spawn/src/data/patrol/patrol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::data::patrol::patrol_link::PatrolLink;
use crate::data::patrol::patrol_point::PatrolPoint;
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use std::io;
use std::io::Write;

/// Patrols list is represented by list of chunks containing patrol chunk.
/// 0...N, where N is chunk.
Expand All @@ -18,7 +19,7 @@ use std::io;
/// 0 - points count
/// 1 - patrol points
/// 2 - patrol points links
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Patrol {
pub name: String,
pub points: Vec<PatrolPoint>,
Expand Down Expand Up @@ -75,6 +76,27 @@ impl Patrol {
})
}

/// Write list of patrols into chunk writer.
pub fn write_list<T: ByteOrder>(
patrols: &Vec<Patrol>,
writer: &mut ChunkWriter,
) -> io::Result<()> {
for (index, patrol) in patrols.iter().enumerate() {
let mut patrol_writer: ChunkWriter = ChunkWriter::new();

patrol.write::<T>(&mut patrol_writer)?;

writer.write_all(
patrol_writer
.flush_chunk_into_buffer::<T>(index)?
.as_slice(),
)?;
}

Ok(())
}

/// Write single patrol entity into chunk writer.
pub fn write<T: ByteOrder>(&self, writer: &mut ChunkWriter) -> io::Result<()> {
let mut meta_writer: ChunkWriter = ChunkWriter::new();
let mut data_writer: ChunkWriter = ChunkWriter::new();
Expand All @@ -90,7 +112,166 @@ impl Patrol {
PatrolPoint::write_list::<T>(&self.points, &mut points_writer)?;
PatrolLink::write_list::<T>(&self.links, &mut links_writer)?;

todo!("Implement writer.");
data_writer.write_all(&point_count_writer.flush_chunk_into_buffer::<T>(0)?)?;
data_writer.write_all(&points_writer.flush_chunk_into_buffer::<T>(1)?)?;
data_writer.write_all(&links_writer.flush_chunk_into_buffer::<T>(2)?)?;

writer.write_all(&meta_writer.flush_chunk_into_buffer::<T>(0)?)?;
writer.write_all(&data_writer.flush_chunk_into_buffer::<T>(1)?)?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::chunk::chunk::Chunk;
use crate::chunk::writer::ChunkWriter;
use crate::data::patrol::patrol::Patrol;
use crate::data::patrol::patrol_link::PatrolLink;
use crate::data::patrol::patrol_point::PatrolPoint;
use crate::test::utils::{
get_test_chunk_file_sub_dir, open_test_resource_as_slice, overwrite_test_resource_as_file,
};
use crate::types::SpawnByteOrder;
use fileslice::FileSlice;
use std::io;

#[test]
fn test_read_write_simple_patrol_point() -> io::Result<()> {
let mut writer: ChunkWriter = ChunkWriter::new();

let patrol: Patrol = Patrol {
name: String::from("patrol-name"),
points: vec![
PatrolPoint {
name: String::from("patrol-point-1"),
position: (7.5, -2.3, -100.0),
flags: 33,
level_vertex_id: 63463634,
game_vertex_id: 555,
},
PatrolPoint {
name: String::from("patrol-point-2"),
position: (2.5, -5.3, 3.0),
flags: 64,
level_vertex_id: 5500,
game_vertex_id: 666,
},
],
links: vec![PatrolLink {
index: 0,
links: vec![(10, 50.5), (15, 60.25)],
}],
};

patrol.write::<SpawnByteOrder>(&mut writer)?;

assert_eq!(writer.bytes_written(), 210);

let bytes_written: usize = writer.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_simple.chunk"),
))?,
0,
)?;

assert_eq!(bytes_written, 210);

let file: FileSlice = open_test_resource_as_slice(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_simple.chunk"),
))?;

assert_eq!(file.bytes_remaining(), 210 + 8);

let mut chunk: Chunk = Chunk::from_file(file)?.read_child_by_index(0)?;
let read_patrol: Patrol = Patrol::read_from_chunk::<SpawnByteOrder>(&mut chunk)?;

assert_eq!(read_patrol, patrol);

Ok(())
}

#[test]
fn test_read_write_simple_patrols_list() -> io::Result<()> {
let mut writer: ChunkWriter = ChunkWriter::new();

let patrols: Vec<Patrol> = vec![
Patrol {
name: String::from("patrol-1"),
points: vec![
PatrolPoint {
name: String::from("patrol-point-1"),
position: (1.5, -2.3, 1.0),
flags: 33,
level_vertex_id: 250,
game_vertex_id: 555,
},
PatrolPoint {
name: String::from("patrol-point-2"),
position: (2.5, -5.3, 3.0),
flags: 64,
level_vertex_id: 5500,
game_vertex_id: 666,
},
],
links: vec![PatrolLink {
index: 0,
links: vec![(10, 50.5), (15, 60.25)],
}],
},
Patrol {
name: String::from("patrol-2"),
points: vec![
PatrolPoint {
name: String::from("patrol-point-1"),
position: (7.5, -4.3, 3.0),
flags: 1,
level_vertex_id: 601,
game_vertex_id: 541,
},
PatrolPoint {
name: String::from("patrol-point-2"),
position: (2.5, -5.3, 3.0),
flags: 0,
level_vertex_id: 600,
game_vertex_id: 542,
},
],
links: vec![PatrolLink {
index: 0,
links: vec![(10, 50.5), (15, 60.25)],
}],
},
];

Patrol::write_list::<SpawnByteOrder>(&patrols, &mut writer)?;

assert_eq!(writer.bytes_written(), 430);

let bytes_written: usize = writer.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_list.chunk"),
))?,
0,
)?;

assert_eq!(bytes_written, 430);

let file: FileSlice = open_test_resource_as_slice(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_list.chunk"),
))?;

assert_eq!(file.bytes_remaining(), 430 + 8);

let mut chunk: Chunk = Chunk::from_file(file)?.read_child_by_index(0)?;
let read_patrols: Vec<Patrol> = Patrol::read_list_from_chunk::<SpawnByteOrder>(&mut chunk, 2)?;

assert_eq!(read_patrols, patrols);

Ok(())
}
Expand Down
88 changes: 35 additions & 53 deletions crates/xray-spawn/src/data/patrol/patrol_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::chunk::writer::ChunkWriter;
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use std::io;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct PatrolLink {
pub index: u32,
pub links: Vec<(u32, f32)>,
Expand Down Expand Up @@ -88,55 +88,53 @@ mod tests {
};
use crate::types::SpawnByteOrder;
use fileslice::FileSlice;
use std::io;

#[test]
fn test_read_write_simple_patrol_link() {
fn test_read_write_simple_patrol_link() -> io::Result<()> {
let mut writer: ChunkWriter = ChunkWriter::new();

PatrolLink {
let link: PatrolLink = PatrolLink {
index: 1000,
links: vec![(10, 1.5), (11, 2.5), (12, 3.5)],
}
.write::<SpawnByteOrder>(&mut writer)
.unwrap();
};

link.write::<SpawnByteOrder>(&mut writer)?;

assert_eq!(writer.bytes_written(), 32);

let bytes_written: usize = writer
.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_simple.chunk"),
))
.unwrap(),
0,
)
.unwrap();
let bytes_written: usize = writer.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_simple.chunk"),
))?,
0,
)?;

assert_eq!(bytes_written, 32);

let file: FileSlice = open_test_resource_as_slice(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_simple.chunk"),
))
.unwrap();
))?;

assert_eq!(file.bytes_remaining(), 32 + 8);

let mut chunk: Chunk = Chunk::from_file(file)
.unwrap()
let mut chunk: Chunk = Chunk::from_file(file)?
.read_child_by_index(0)
.expect("0 index chunk to exist");

let link: PatrolLink = PatrolLink::read_from_chunk::<SpawnByteOrder>(&mut chunk).unwrap();
let read_link: PatrolLink = PatrolLink::read_from_chunk::<SpawnByteOrder>(&mut chunk)?;

assert_eq!(link.index, 1000);
assert_eq!(link.links.len(), 3);
assert_eq!(read_link, link);

Ok(())
}

#[test]
fn test_read_write_list_of_patrol_links() {
fn test_read_write_list_of_patrol_links() -> io::Result<()> {
let mut writer: ChunkWriter = ChunkWriter::new();

let links: Vec<PatrolLink> = vec![
PatrolLink {
index: 1000,
Expand All @@ -148,52 +146,36 @@ mod tests {
},
];

PatrolLink::write_list::<SpawnByteOrder>(&links, &mut writer).unwrap();
PatrolLink::write_list::<SpawnByteOrder>(&links, &mut writer)?;

assert_eq!(writer.bytes_written(), 48);

let bytes_written: usize = writer
.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_list.chunk"),
))
.unwrap(),
0,
)
.unwrap();
let bytes_written: usize = writer.flush_chunk_into_file::<SpawnByteOrder>(
&mut overwrite_test_resource_as_file(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_list.chunk"),
))?,
0,
)?;

assert_eq!(bytes_written, 48);

let file: FileSlice = open_test_resource_as_slice(get_test_chunk_file_sub_dir(
file!(),
String::from("patrol_vertex_list.chunk"),
))
.unwrap();
))?;

assert_eq!(file.bytes_remaining(), 48 + 8);

let mut chunk: Chunk = Chunk::from_file(file)
.unwrap()
let mut chunk: Chunk = Chunk::from_file(file)?
.read_child_by_index(0)
.expect("0 index chunk to exist");

let from_file: Vec<PatrolLink> =
PatrolLink::read_list_from_chunk::<SpawnByteOrder>(&mut chunk).unwrap();

assert_eq!(from_file.len(), 2);
PatrolLink::read_list_from_chunk::<SpawnByteOrder>(&mut chunk)?;

assert_eq!(from_file.get(0).unwrap().index, 1000);
assert_eq!(from_file.get(0).unwrap().links.len(), 3);
assert_eq!(
*from_file.get(0).unwrap().links.get(0).unwrap(),
(10u32, 1.5f32)
);
assert_eq!(*from_file.get(0).unwrap().links.get(1).unwrap(), (11, 2.5));
assert_eq!(*from_file.get(0).unwrap().links.get(2).unwrap(), (12, 3.5));
assert_eq!(from_file, links);

assert_eq!(from_file.get(1).unwrap().index, 1001);
assert_eq!(from_file.get(1).unwrap().links.len(), 1);
assert_eq!(*from_file.get(1).unwrap().links.get(0).unwrap(), (20, 1.5));
Ok(())
}
}
Loading

0 comments on commit 2042a6f

Please sign in to comment.