mod json_1_1_3;
pub use json_1_1_3::*;
use std::{
fs::File,
path::{Path, PathBuf},
};
impl Project {
pub fn new<P: AsRef<Path>>(f: P) -> Self {
let mut o = Project::load_project(&f);
if o.external_levels {
o.load_external_levels(f);
}
o
}
pub fn load_project<P: AsRef<Path>>(f: P) -> Self {
let file = File::open(f).expect("project file not found");
let o: Project = serde_json::from_reader(file).expect("error while reading");
o
}
pub fn clear_levels(&mut self) {
self.levels = Vec::new();
}
pub fn load_external_levels<P: AsRef<Path>>(&mut self, f: P) {
if self.external_levels {
let mut all_level_files: Vec<PathBuf> = Vec::new();
for level in self.levels.iter_mut() {
let level_file_path = level.external_rel_path.as_ref().expect("missing level");
all_level_files.push(level_file_path.into());
}
self.clear_levels();
for file in all_level_files.iter() {
let mut full_path = PathBuf::new();
let parent = f.as_ref().parent().unwrap().to_str().unwrap();
full_path.push(parent);
full_path.push("/");
full_path.push(&file);
println!("opening {:#?}", full_path);
let level_ldtk = Level::new(full_path);
self.levels.push(level_ldtk);
}
}
}
pub fn get_level(&self, uid: i64) -> Option<&Level> {
for level in self.levels.iter() {
if level.uid == uid {
return Some(level);
}
}
None
}
}
impl Level {
pub fn new<P: AsRef<Path>>(f: P) -> Self {
let file = File::open(f).expect("level file not found");
let o: Level = serde_json::from_reader(file).expect("error while reading");
o
}
}
#[deprecated = "Use Project instead of LdtkJson to match LDtk documentation."]
pub struct LdtkJson;
#[allow(deprecated)]
impl LdtkJson {
pub fn new(f: String) -> Project {
Project::new(f)
}
}