1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
mod json_0_7_0;
use std::{fs::File, path::Path};
pub use json_0_7_0::*;
impl Project {
pub fn new(f: String) -> Self {
let mut o = Project::load_project(f.clone());
if o.external_levels {
o.load_external_levels(f);
}
o
}
pub fn load_project(f: String) -> Self {
let json_file_path = Path::new(&f);
let file = File::open(json_file_path).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(&mut self, f: String) {
if self.external_levels {
let mut all_level_files: Vec<String> = 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.to_string());
}
self.clear_levels();
for file in all_level_files.iter() {
let mut full_path: String = String::new();
let parent = Path::new(&f).parent().unwrap().to_str().unwrap();
full_path.push_str(parent);
full_path.push_str("/");
full_path.push_str(&file.to_string());
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(f: String) -> Self {
let json_file_path = Path::new(&f);
let file = File::open(json_file_path).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)
}
}