Skip to content

Commit

Permalink
Add capability to write maps
Browse files Browse the repository at this point in the history
  • Loading branch information
maddymakesgames committed Aug 25, 2024
1 parent 43b60da commit 6adc548
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
any::Any,
collections::HashMap,
fmt::{Debug, Display},
io::Read,
io::{Read, Write},
};

pub mod elements;
Expand All @@ -24,6 +24,7 @@ use crate::maps::{
reader::{MapReadError, MapReader},
triggers::{MapTrigger, Trigger},
var_types::EncodedVar,
writer::{MapWriteError, MapWriter},
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -81,6 +82,18 @@ impl RawMap {
})
}

fn to_bytes(&self) -> Result<Vec<u8>, MapWriteError> {
let mut writer = MapWriter::new();

writer.write_string("CELESTE MAP");

writer.write_string(&self.name);
writer.write_lookup_table(&self.lookup_table);
writer.write_element(&self.root_element)?;

Ok(writer.bytes())
}

/// Resolve all the [ResolvableString]s stored in the map
///
/// This should be called directly after reading in the map file,
Expand Down Expand Up @@ -417,7 +430,7 @@ impl MapManager {

/// Gets a reference to the [RawMap] stored in the manager.
///
/// This is initialized either from the constructor or from [encode_map](Self::encode_map)
/// This is initialized in the constructor and modified in [encode_map](Self::encode_map)
pub fn map(&self) -> &RawMap {
&self.map
}
Expand All @@ -428,4 +441,15 @@ impl MapManager {
pub fn add_trigger_parser<T: Trigger>(&mut self) {
self.add_parser::<MapTrigger<T>>();
}

/// Writes the stored map data as binary into the provided writer
///
/// This makes [ResolvableString]s unresolved so you should run [RawMap::resolve_strings]
/// if you intend to further edit the `RawMap` directly.
pub fn write_map(&mut self, writer: &mut impl Write) -> std::io::Result<()> {
// unresolve strings, just in case people are editing the RawMap directly
self.map.unresolve_strings();

writer.write_all(&self.map.to_bytes()?)
}
}

0 comments on commit 6adc548

Please sign in to comment.