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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use serde::{Deserialize, Serialize};
use crate::{basename, file_exists, list_all_objects};
lazy_static::lazy_static! {
static ref ROOT_DIR: String = {
if let Some(dir) = option_env!("ABST_DATA_DIR") {
dir.trim_end_matches('/').to_string()
} else if cfg!(target_arch = "wasm32") {
"../data".to_string()
} else if file_exists("data/".to_string()) {
"data".to_string()
} else if file_exists("../data/".to_string()) {
"../data".to_string()
} else if file_exists("../../data/".to_string()) {
"../../data".to_string()
} else {
panic!("Can't find the data/ directory");
}
};
static ref ROOT_PLAYER_DIR: String = {
if option_env!("ABST_PLAYER_HOME_DIR").is_some() {
match std::env::var("HOME") {
Ok(dir) => format!("{}/.abstreet", dir.trim_end_matches('/')),
Err(err) => panic!("This build of A/B Street stores player data in $HOME/.abstreet, but $HOME isn't set: {}", err),
}
} else if cfg!(target_arch = "wasm32") {
"../data".to_string()
} else if file_exists("data/".to_string()) {
"data".to_string()
} else if file_exists("../data/".to_string()) {
"../data".to_string()
} else if file_exists("../../data/".to_string()) {
"../../data".to_string()
} else {
panic!("Can't find the data/ directory");
}
};
}
pub fn path<I: Into<String>>(p: I) -> String {
let p = p.into();
if p.starts_with("player/") {
format!("{}/{}", *ROOT_PLAYER_DIR, p)
} else {
format!("{}/{}", *ROOT_DIR, p)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MapName {
pub city: String,
pub map: String,
}
impl MapName {
pub fn new(city: &str, map: &str) -> MapName {
MapName {
city: city.to_string(),
map: map.to_string(),
}
}
pub fn seattle(map: &str) -> MapName {
MapName::new("seattle", map)
}
pub fn describe(&self) -> String {
format!("{} (in {})", self.map, self.city)
}
pub fn as_filename(&self) -> String {
format!("{}_{}", self.city, self.map)
}
pub fn from_path(path: &str) -> MapName {
let parts = path.split("/").collect::<Vec<_>>();
let city = parts[parts.len() - 3];
let map = basename(parts[parts.len() - 1]);
MapName::new(city, &map)
}
pub fn path(&self) -> String {
path(format!("system/{}/maps/{}.bin", self.city, self.map))
}
pub fn list_all_maps() -> Vec<MapName> {
let mut names = Vec::new();
for city in list_all_objects(path("system")) {
if city == "assets" || city == "fonts" || city == "proposals" {
continue;
}
for map in list_all_objects(path(format!("system/{}/maps", city))) {
names.push(MapName::new(&city, &map));
}
}
names
}
}
pub fn path_prebaked_results(name: &MapName, scenario_name: &str) -> String {
path(format!(
"system/{}/prebaked_results/{}/{}.bin",
name.city, name.map, scenario_name
))
}
pub fn path_scenario(name: &MapName, scenario_name: &str) -> String {
let bin = path(format!(
"system/{}/scenarios/{}/{}.bin",
name.city, name.map, scenario_name
));
let json = path(format!(
"system/{}/scenarios/{}/{}.json",
name.city, name.map, scenario_name
));
if file_exists(&bin) {
return bin;
}
if file_exists(&json) {
return json;
}
bin
}
pub fn path_all_scenarios(name: &MapName) -> String {
path(format!("system/{}/scenarios/{}", name.city, name.map))
}
pub fn parse_scenario_path(path: &str) -> (MapName, String) {
let parts = path.split("/").collect::<Vec<_>>();
let city = parts[parts.len() - 4];
let map = parts[parts.len() - 2];
let scenario = basename(parts[parts.len() - 1]);
let map_name = MapName::new(city, map);
(map_name, scenario)
}
pub fn path_camera_state(name: &MapName) -> String {
path(format!("player/camera_state/{}.json", name.map))
}
pub fn path_edits(name: &MapName, edits_name: &str) -> String {
path(format!("player/edits/{}/{}.json", name.map, edits_name))
}
pub fn path_all_edits(name: &MapName) -> String {
path(format!("player/edits/{}", name.map))
}
pub fn path_save(name: &MapName, edits_name: &str, run_name: &str, time: String) -> String {
path(format!(
"player/saves/{}/{}_{}/{}.bin",
name.map, edits_name, run_name, time
))
}
pub fn path_all_saves(name: &MapName, edits_name: &str, run_name: &str) -> String {
path(format!(
"player/saves/{}/{}_{}",
name.map, edits_name, run_name
))
}
pub fn path_popdat() -> String {
path("input/seattle/popdat.bin")
}
pub fn path_raw_map(name: &MapName) -> String {
path(format!("input/{}/raw_maps/{}.bin", name.city, name.map))
}