aboutsummaryrefslogtreecommitdiff
path: root/src/map_data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/map_data.rs')
-rw-r--r--src/map_data.rs38
1 files changed, 34 insertions, 4 deletions
diff --git a/src/map_data.rs b/src/map_data.rs
index d958736..62229c6 100644
--- a/src/map_data.rs
+++ b/src/map_data.rs
@@ -1,6 +1,9 @@
use crate::math::{Rect, Vec2};
+use ron::de::from_reader;
+use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
-use std::io;
+use std::fs::File;
+use std::io::{self, Write};
use std::path::Path;
/// All the data of the currently opened map. This does not include things that are currently being
@@ -20,12 +23,39 @@ impl MapData {
}
}
- pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<MapData> {
- todo!()
+ pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
+ let file = File::open(&path)?;
+ let mut data: Self = match from_reader(file) {
+ Ok(data) => data,
+ Err(err) => {
+ return Err(io::Error::new(io::ErrorKind::InvalidData, err));
+ }
+ };
+
+ /* Append map data to the currently loaded map. (Default behaviour might change in the
+ * future)
+ */
+ self.rooms.append(&mut data.rooms);
+ self.walls.append(&mut data.walls);
+
+ Ok(())
}
pub fn write_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
- todo!()
+ let mut file = File::create(&path)?;
+
+ let pretty_conf = PrettyConfig::new()
+ .with_depth_limit(4)
+ .with_separate_tuple_members(true)
+ .with_indentor("\t".to_owned());
+ let string = match to_string_pretty(&self, pretty_conf) {
+ Ok(string) => string,
+ Err(err) => {
+ return Err(io::Error::new(io::ErrorKind::InvalidInput, err));
+ }
+ };
+
+ file.write_all(&string.as_bytes())
}
pub fn rooms(&self) -> &Vec<Rect<f32>> {