Skip to content

Commit

Permalink
Export patrol points/links in separate files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Feb 27, 2024
1 parent 62a4c9f commit 038838e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/xray-db/src/data/alife/alife_object_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl AlifeObjectGeneric for AlifeObjectShape {
fn export(&self, section: &String, ini: &mut Ini) {
self.base.export(section, ini);

ini.with_section(Some(section)).set("shape", "todo"); // todo: Write shape.
Shape::export_shapes(&self.shape, section, ini);
}
}

Expand Down
13 changes: 11 additions & 2 deletions crates/xray-db/src/data/graph/graph_vertex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chunk::chunk::Chunk;
use crate::chunk::writer::ChunkWriter;
use crate::data::vector_3d::Vector3d;
use crate::export::file_export::export_vector_to_string;
use crate::types::U32Bytes;
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use ini::Ini;
Expand Down Expand Up @@ -68,11 +69,19 @@ impl GraphVertex {
.set("game_point", self.game_point.to_string())
.set("level_id", self.level_id.to_string())
.set("level_vertex_id", self.level_vertex_id.to_string())
.set("vertex_type", self.vertex_type.0.to_string()) // todo: Write bytes.
.set("edge_offset", self.edge_offset.to_string())
.set("level_point_offset", self.level_point_offset.to_string())
.set("edge_count", self.edge_count.to_string())
.set("level_point_count", self.level_point_count.to_string());
.set("level_point_count", self.level_point_count.to_string())
.set(
"vertex_type",
export_vector_to_string(&vec![
self.vertex_type.0,
self.vertex_type.1,
self.vertex_type.2,
self.vertex_type.3,
]),
);
}
}

Expand Down
18 changes: 17 additions & 1 deletion crates/xray-db/src/data/patrol/patrol_link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chunk::chunk::Chunk;
use crate::chunk::writer::ChunkWriter;
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use ini::Ini;
use std::io;

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -38,7 +39,7 @@ impl PatrolLink {
let mut vertices: Vec<(u32, f32)> = Vec::new();

for _ in 0..count {
let to: u32 = chunk.read_u32::<T>()?;
let to: u32 = chunk.read_u32::<T>()?; // from->to in u16.
let weight: f32 = chunk.read_f32::<T>()?;

vertices.push((to, weight));
Expand Down Expand Up @@ -76,6 +77,21 @@ impl PatrolLink {

Ok(())
}

/// Export patrol link data into ini.
pub fn export(&self, section: &String, ini: &mut Ini) {
ini
.with_section(Some(section))
.set("index", self.index.to_string())
.set("count", self.links.len().to_string());

for (index, (from, weight)) in self.links.iter().enumerate() {
ini
.with_section(Some(section))
.set(format!("from.{index}"), from.to_string())
.set(format!("weight.{index}"), weight.to_string());
}
}
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions crates/xray-db/src/data/patrol/patrol_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::chunk::iterator::ChunkIterator;
use crate::chunk::writer::ChunkWriter;
use crate::data::vector_3d::Vector3d;
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use ini::Ini;
use std::io;
use std::io::Write;

Expand Down Expand Up @@ -96,6 +97,17 @@ impl PatrolPoint {

Ok(())
}

/// Export patrol point data into ini.
pub fn export(&self, section: &String, ini: &mut Ini) {
ini
.with_section(Some(section))
.set("name", &self.name)
.set("flags", self.flags.to_string())
.set("position", self.position.to_string())
.set("level_vertex_id", self.level_vertex_id.to_string())
.set("game_vertex_id", self.game_vertex_id.to_string());
}
}

#[cfg(test)]
Expand Down
63 changes: 37 additions & 26 deletions crates/xray-db/src/file/patrols_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::data::patrol::patrol::Patrol;
use crate::export::file_export::{create_export_file, export_ini_to_file};
use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt};
use ini::Ini;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::{fmt, io};

/// `CPatrolPathStorage::load` in xray engine.
Expand Down Expand Up @@ -52,42 +51,54 @@ impl PatrolsChunk {

/// Export patrols data into provided path.
pub fn export<T: ByteOrder>(&self, path: &Path) -> io::Result<()> {
let patrols_path: PathBuf = path.join("patrols.ltx");

let mut file: File = create_export_file(&patrols_path)?;
let mut config: Ini = Ini::new();
let mut patrols_config: Ini = Ini::new();
let mut patrol_points_config: Ini = Ini::new();
let mut patrol_links_config: Ini = Ini::new();

for patrol in &self.patrols {
config
patrols_config
.with_section(Some(&patrol.name))
.set("type", "patrol")
.set("name", &patrol.name)
.set("points_count", patrol.points.len().to_string())
.set(
"points",
patrol
.points
.iter()
.map(|it| it.name.clone())
.collect::<Vec<_>>()
.join(","),
)
.set("links_count", patrol.links.len().to_string());

// todo: Create linking section.
for (index, point) in patrol.points.iter().enumerate() {
config
.with_section(Some(format!("{}_point_{}", patrol.name, index)))
.set("type", "point")
.set("name", &point.name)
.set("flags", point.flags.to_string())
.set("position", point.position.to_string())
.set("level_vertex_id", point.level_vertex_id.to_string())
.set("game_vertex_id", point.game_vertex_id.to_string());
for point in &patrol.points {
point.export(
&format!("{}.{}", patrol.name, point.name),
&mut patrol_points_config,
);
}

// todo: Create linking section.
for (index, link) in patrol.links.iter().enumerate() {
config
.with_section(Some(format!("{}_link_{}", patrol.name, index)))
.set("type", "link")
.set("index", link.index.to_string())
.set("count", link.links.len().to_string()); // todo: Links list.
link.export(
&format!("{}.{}", patrol.name, index),
&mut patrol_links_config,
);
}
}

export_ini_to_file(&config, &mut file)?;
export_ini_to_file(
&patrols_config,
&mut create_export_file(&path.join("patrols.ltx"))?,
)?;

export_ini_to_file(
&patrol_points_config,
&mut create_export_file(&path.join("patrol_points.ltx"))?,
)?;

export_ini_to_file(
&patrol_links_config,
&mut create_export_file(&path.join("patrol_links.ltx"))?,
)?;

log::info!("Exported patrols chunk");

Expand Down

0 comments on commit 038838e

Please sign in to comment.