twmap/lib.rs
1//! This crate provides a library for safe parsing, editing and saving of [Teeworlds](https://www.teeworlds.com/) and [DDNet](https://ddnet.org/) maps.
2//! Goals of this library are:
3//!
4//! - performance
5//! - reasonable map standards
6//! - compatibility with all Teeworlds [maps](https://github.com/teeworlds/teeworlds-maps) (0.7) and DDNet (0.6) [maps](https://github.com/ddnet/ddnet-maps)
7//!
8//! In the very center of this library is the [`TwMap`](struct.TwMap.html) struct.
9//! For more information and the complete list of its available methods, check out its documentation page.
10//!
11//! Note that library is a aware of the origin of the map (Teeworlds 0.7 or DDNet 0.6).
12//! On parsing a map it will store its [`Version`](enum.Version.html) in the `version` field, which in turn is used to perform the appropriate checks and save in the correct version.
13//!
14//!
15//! # Examples:
16//!
17//! Note that for better readability, some lines of code are left out in the examples, most notably `use twmap::*;` at the top.
18//!
19//! Loading a binary map, accessing its version and image names and saving it again in the binary format:
20//! ```
21//! # use std::path::PathBuf;
22//! # use twmap::*;
23//! # use std::fs;
24//! #
25//! # let dm1_path = PathBuf::from("tests/dm1.map");
26//! let map_data: Vec<u8> = fs::read(dm1_path)?;
27//! let mut map: TwMap = TwMap::parse(&map_data)?;
28//!
29//! assert_eq!(map.version, Version::Teeworlds07);
30//! assert_eq!(map.images[0].name(), "bg_cloud1");
31//! assert_eq!(map.images[6].name(), "sun");
32//!
33//! # let mut tmp_dir = tempfile::tempdir().unwrap();
34//! # let output_path = tmp_dir.path().join("dm1_new").with_extension("map");
35//! map.save_file(output_path)?;
36//! # Ok::<(), Error>(())
37//! ```
38//!
39//! Loading a MapDir **or** binary map, removing unused envelopes, layers, groups, images, sounds and saving it again in the MapDir format:
40//! ```
41//! # use std::path::PathBuf;
42//! # use twmap::*;
43//! #
44//! # let dm1_path = PathBuf::from("tests/dm1.map");
45//! # let mut dm1 = TwMap::parse_path(dm1_path)?;
46//! # let tmp_dir = tempfile::tempdir().unwrap();
47//! # let map_path = tmp_dir.path().join("map").with_extension("map");
48//! # dm1.save_dir(&map_path);
49//! let mut map = TwMap::parse_path(map_path)?;
50//!
51//! map.remove_everything_unused();
52//!
53//! # let tmp_dir = tempfile::tempdir().unwrap();
54//! # let output_path = tmp_dir.path().join("map_new");
55//! map.save_dir(output_path)?;
56//! # Ok::<(), Error>(())
57//! ```
58//!
59//! Loading a MapDir map, flipping it upside down and saving the map data into a vector:
60//!
61//! ```
62//! # use std::path::PathBuf;
63//! # use twmap::*;
64//! #
65//! # let dm1_path = PathBuf::from("tests/dm1.map");
66//! # let mut dm1 = TwMap::parse_path(dm1_path)?;
67//! # let tmp_dir = tempfile::tempdir().unwrap();
68//! # let map_path = tmp_dir.path().join("map").with_extension("map");
69//! # dm1.save_dir(&map_path);
70//! let mut map = TwMap::parse_dir(map_path)?;
71//! # // TODO: Fix this as soon as NoneError is stable
72//! // Usually you would match the Option<TwMap> return values
73//! // -> or wrap it into a function that also returns an Option
74//! // To take up less space, we insert an error that we return in place of the None value
75//! # let e = std::io::Error::from_raw_os_error(21);
76//! map = map.rotate_right().ok_or(e)?;
77//! # let e = std::io::Error::from_raw_os_error(21);
78//! map = map.rotate_right().ok_or(e)?;
79//! # let e = std::io::Error::from_raw_os_error(21);
80//! map = map.mirror().ok_or(e)?;
81//!
82//! let mut map_data = Vec::new();
83//! map.save(&mut map_data)?;
84//! # Ok::<(), Box<dyn std::error::Error>>(())
85//! ```
86
87pub mod datafile;
88mod map;
89pub use map::*;
90
91pub mod automapper;
92pub mod constants;
93
94pub mod compression; // safe zlib (libz-sys) wrapper
95pub mod convert;
96
97pub use fixed;
98pub use image;
99pub use ndarray;
100pub use vek;