diff --git a/assets/maps/huge.tar b/assets/maps/huge.tar index ee5bec394..6b77ec4d0 100644 --- a/assets/maps/huge.tar +++ b/assets/maps/huge.tar @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:14f5a19d005cc9a8685df17219bf61ff4d08f14ff0d5a7af329b6aacb27f51d0 +oid sha256:f51a62c5cbd7d6a299a5d13916950b199e635acfa4b18bc6cab39e774336bcf0 size 522240 diff --git a/crates/map/src/io.rs b/crates/map/src/io.rs index 06eb62e7c..6f7d61d58 100644 --- a/crates/map/src/io.rs +++ b/crates/map/src/io.rs @@ -183,7 +183,7 @@ mod test { #[test] fn test_store_load() { let bounds = MapBounds::new(Vec2::new(1000., 2000.)); - let mut map = Map::empty(MapMetadata::new(bounds, Player::Player4)); + let mut map = Map::empty(MapMetadata::new("Test Map".into(), bounds, Player::Player4)); let bases = [ (Vec2::new(-400., -900.), Player::Player1), diff --git a/crates/map/src/map.rs b/crates/map/src/map.rs index bede21bcf..b26f310dd 100644 --- a/crates/map/src/map.rs +++ b/crates/map/src/map.rs @@ -102,6 +102,7 @@ mod test { #[test] fn test_map() { let mut map = Map::empty(MapMetadata::new( + "Test Map".into(), MapBounds::new(Vec2::new(1000., 1000.)), Player::Player3, )); @@ -141,7 +142,11 @@ mod test { )); let map = Map::new( - MapMetadata::new(MapBounds::new(Vec2::new(5., 5.)), Player::Player4), + MapMetadata::new( + "Test Map".into(), + MapBounds::new(Vec2::new(5., 5.)), + Player::Player4, + ), content, ); diff --git a/crates/map/src/meta.rs b/crates/map/src/meta.rs index 5eaea4d5c..f8b6c6a86 100644 --- a/crates/map/src/meta.rs +++ b/crates/map/src/meta.rs @@ -4,10 +4,13 @@ use thiserror::Error; use crate::size::{MapBounds, MapBoundsValidationError}; +pub const MAX_MAP_NAME_LEN: usize = 16; + /// General information about a map. It does not hold full content of the map /// (i.e. location of objects on the map). #[derive(Serialize, Deserialize)] pub struct MapMetadata { + name: String, bounds: MapBounds, max_player: Player, } @@ -17,6 +20,8 @@ impl MapMetadata { /// /// # Arguments /// + /// * `name` - name of the map. + /// /// * `bounds` - bounds of the map. /// /// * `max_player` - maximum number of players which can play on the map. @@ -26,8 +31,12 @@ impl MapMetadata { /// # Panics /// /// Panics if any of the map parameters is invalid. - pub fn new(bounds: MapBounds, max_player: Player) -> Self { - let map = Self { bounds, max_player }; + pub fn new(name: String, bounds: MapBounds, max_player: Player) -> Self { + let map = Self { + name, + bounds, + max_player, + }; map.validate().unwrap(); map } @@ -41,6 +50,19 @@ impl MapMetadata { } pub(crate) fn validate(&self) -> Result<(), MapMetadataValidationError> { + if self.name.is_empty() { + return Err(MapMetadataValidationError::MapName( + "map name is empty".into(), + )); + } + if self.name.len() > MAX_MAP_NAME_LEN { + return Err(MapMetadataValidationError::MapName(format!( + "map name too long: {} > {}", + self.name.len(), + MAX_MAP_NAME_LEN + ))); + } + if let Err(error) = self.bounds.validate() { return Err(MapMetadataValidationError::MapBounds { source: error }); } @@ -55,6 +77,8 @@ impl MapMetadata { #[derive(Error, Debug)] pub enum MapMetadataValidationError { + #[error("invalid map name: {0}")] + MapName(String), #[error("invalid map bounds")] MapBounds { source: MapBoundsValidationError }, #[error("map has to have at least 2 players, got {0}")]