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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Generate paths for different A/B Street files

// TODO There's some repeated code here for listing files locally/from the manifest/merged. Maybe
// have a Source enum and simplify the API. But we would either have to call Manifest::load
// constantly, or plumb it around with a borrow? Or maybe even owned.

use anyhow::Result;
use serde::{Deserialize, Serialize};

use abstutil::basename;

use crate::{file_exists, list_all_objects, Manifest};

lazy_static::lazy_static! {
    static ref ROOT_DIR: String = {
        // If you're packaging for a release and need the data directory to be in some fixed
        // location: ABST_DATA_DIR=/some/path cargo build ...
        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 if file_exists("../../../data/".to_string()) {
            "../../../data".to_string()
        } else {
            panic!("Can't find the data/ directory");
        }
    };

    static ref ROOT_PLAYER_DIR: String = {
        // If you're packaging for a release and want the player's local data directory to be
        // $HOME/.abstreet, set ABST_PLAYER_HOME_DIR=1
        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 if file_exists("../../../data/".to_string()) {
            "../../../data".to_string()
        } else {
            panic!("Can't find the data/ directory");
        }
    };
}

pub fn path<I: AsRef<str>>(p: I) -> String {
    let p = p.as_ref();
    if p.starts_with("player/") {
        format!("{}/{}", *ROOT_PLAYER_DIR, p)
    } else {
        format!("{}/{}", *ROOT_DIR, p)
    }
}

/// A single city is identified using this.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CityName {
    /// A two letter lowercase country code, from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2.
    /// To represent imaginary/test cities, use the code `zz`.
    pub country: String,
    /// The name of the city, in filename-friendly form -- for example, "tel_aviv".
    pub city: String,
}

impl CityName {
    /// Create a CityName from a country code and city.
    pub fn new(country: &str, city: &str) -> CityName {
        if country.len() != 2 {
            panic!(
                "CityName::new({}, {}) has a country code that isn't two letters",
                country, city
            );
        }
        CityName {
            country: country.to_string(),
            city: city.to_string(),
        }
    }

    /// Convenient constructor for the main city of the game.
    pub fn seattle() -> CityName {
        CityName::new("us", "seattle")
    }

    /// Returns all city names available locally.
    fn list_all_cities_locally() -> Vec<CityName> {
        let mut cities = Vec::new();
        for country in list_all_objects(path("system")) {
            if country == "assets"
                || country == "extra_fonts"
                || country == "proposals"
                || country == "study_areas"
            {
                continue;
            }
            for city in list_all_objects(path(format!("system/{}", country))) {
                cities.push(CityName::new(&country, &city));
            }
        }
        cities
    }

    /// Returns all city names based on the manifest of available files.
    fn list_all_cities_from_manifest(manifest: &Manifest) -> Vec<CityName> {
        let mut cities = Vec::new();
        for path in manifest.entries.keys() {
            if let Some(city) = Manifest::path_to_city(path) {
                cities.push(city);
            }
        }
        // The paths in the manifest are ordered, so the same cities will be adjacent.
        cities.dedup();
        cities
    }

    /// Returns all city names either available locally or based on the manifest of available files.
    pub fn list_all_cities_merged(manifest: &Manifest) -> Vec<CityName> {
        let mut all = CityName::list_all_cities_locally();
        all.extend(CityName::list_all_cities_from_manifest(manifest));
        all.sort();
        all.dedup();
        all
    }

    /// Returns all city names based on importer config.
    pub fn list_all_cities_from_importer_config() -> Vec<CityName> {
        let mut cities = Vec::new();
        for country in list_all_objects("importer/config".to_string()) {
            for city in list_all_objects(format!("importer/config/{}", country)) {
                cities.push(CityName::new(&country, &city));
            }
        }
        cities
    }

    /// Returns all maps in a city based on importer config.
    pub fn list_all_maps_in_city_from_importer_config(&self) -> Vec<MapName> {
        crate::list_dir(format!("importer/config/{}/{}", self.country, self.city))
            .into_iter()
            .filter(|path| path.ends_with(".poly"))
            .map(|path| MapName::from_city(self, &basename(path)))
            .collect()
    }

    /// Parses a CityName from something like "gb/london"; the inverse of `to_path`.
    pub fn parse(x: &str) -> Result<CityName> {
        let parts = x.split('/').collect::<Vec<_>>();
        if parts.len() != 2 || parts[0].len() != 2 {
            bail!("Bad CityName {}", x);
        }
        Ok(CityName::new(parts[0], parts[1]))
    }

    /// Expresses the city as a path, like "gb/london"; the inverse of `parse`.
    pub fn to_path(&self) -> String {
        format!("{}/{}", self.country, self.city)
    }

    /// Stringify the city name for debug messages. Don't implement `std::fmt::Display`, to force
    /// callers to explicitly opt into this description, which could change.
    pub fn describe(&self) -> String {
        format!("{} ({})", self.city, self.country)
    }

    /// Constructs the path to some city-scoped data/input.
    pub fn input_path<I: AsRef<str>>(&self, file: I) -> String {
        path(format!(
            "input/{}/{}/{}",
            self.country,
            self.city,
            file.as_ref()
        ))
    }

    /// Should metric units be used by default for this map? (Imperial if false)
    pub fn uses_metric(&self) -> bool {
        // We don't need a full locale lookup or anything. Myanmar and Liberia apparently use both
        // but are leaning metric.
        self.country != "us"
    }
}

/// A single map is identified using this.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct MapName {
    pub city: CityName,
    /// The name of the map within the city, in filename-friendly form -- for example, "downtown"
    pub map: String,
}

impl abstutil::CloneableAny for MapName {}

impl MapName {
    /// Create a MapName from a country code, city, and map name.
    pub fn new(country: &str, city: &str, map: &str) -> MapName {
        MapName {
            city: CityName::new(country, city),
            map: map.to_string(),
        }
    }

    pub fn blank() -> Self {
        Self::new("zz", "blank city", "blank")
    }

    /// Create a MapName from a city and map within that city.
    pub fn from_city(city: &CityName, map: &str) -> MapName {
        MapName::new(&city.country, &city.city, map)
    }

    /// Convenient constructor for the main city of the game.
    pub fn seattle(map: &str) -> MapName {
        MapName::new("us", "seattle", map)
    }

    /// Stringify the map name for debug messages. Don't implement `std::fmt::Display`, to force
    /// callers to explicitly opt into this description, which could change.
    pub fn describe(&self) -> String {
        format!(
            "{} (in {} ({}))",
            self.map, self.city.city, self.city.country
        )
    }

    /// Stringify the map name for filenames.
    pub fn as_filename(&self) -> String {
        format!("{}_{}_{}", self.city.country, self.city.city, self.map)
    }

    /// Transforms a path to a map back to a MapName. Returns `None` if the input is strange.
    pub fn from_path(path: &str) -> Option<MapName> {
        let parts = path.split('/').collect::<Vec<_>>();
        // Expect something ending like system/us/seattle/maps/montlake.bin
        if parts.len() < 5 || parts[parts.len() - 5] != "system" || parts[parts.len() - 2] != "maps"
        {
            return None;
        }
        let country = parts[parts.len() - 4];
        let city = parts[parts.len() - 3];
        let map = basename(parts[parts.len() - 1]);
        Some(MapName::new(country, city, &map))
    }

    /// Returns the filesystem path to this map.
    pub fn path(&self) -> String {
        path(format!(
            "system/{}/{}/maps/{}.bin",
            self.city.country, self.city.city, self.map
        ))
    }

    /// Returns all maps from one city that're available locally.
    fn list_all_maps_in_city_locally(city: &CityName) -> Vec<MapName> {
        let mut names = Vec::new();
        for map in list_all_objects(path(format!("system/{}/{}/maps", city.country, city.city))) {
            names.push(MapName {
                city: city.clone(),
                map,
            });
        }
        names
    }

    /// Returns all maps from all cities available locally.
    pub fn list_all_maps_locally() -> Vec<MapName> {
        let mut names = Vec::new();
        for city in CityName::list_all_cities_locally() {
            names.extend(MapName::list_all_maps_in_city_locally(&city));
        }
        names
    }

    /// Returns all maps from all cities based on the manifest of available files.
    fn list_all_maps_from_manifest(manifest: &Manifest) -> Vec<MapName> {
        let mut names = Vec::new();
        for path in manifest.entries.keys() {
            if let Some(name) = MapName::from_path(path) {
                names.push(name);
            }
        }
        names
    }

    /// Returns all maps from all cities either available locally or based on the manifest of available files.
    pub fn list_all_maps_merged(manifest: &Manifest) -> Vec<MapName> {
        let mut all = MapName::list_all_maps_locally();
        all.extend(MapName::list_all_maps_from_manifest(manifest));
        all.sort();
        all.dedup();
        all
    }

    /// Returns all maps from one city based on the manifest of available files.
    fn list_all_maps_in_city_from_manifest(city: &CityName, manifest: &Manifest) -> Vec<MapName> {
        MapName::list_all_maps_from_manifest(manifest)
            .into_iter()
            .filter(|name| &name.city == city)
            .collect()
    }

    /// Returns all maps from one city that're available either locally or according to the
    /// manifest.
    pub fn list_all_maps_in_city_merged(city: &CityName, manifest: &Manifest) -> Vec<MapName> {
        let mut all = MapName::list_all_maps_in_city_locally(city);
        all.extend(MapName::list_all_maps_in_city_from_manifest(city, manifest));
        all.sort();
        all.dedup();
        all
    }

    /// Returns the string to opt into runtime or input files for DataPacks.
    pub fn to_data_pack_name(&self) -> String {
        if Manifest::is_file_part_of_huge_seattle(&self.path()) {
            return "us/huge_seattle".to_string();
        }
        self.city.to_path()
    }
}

// System data (Players can't edit, needed at runtime)

pub fn path_prebaked_results(name: &MapName, scenario_name: &str) -> String {
    path(format!(
        "system/{}/{}/prebaked_results/{}/{}.bin",
        name.city.country, name.city.city, name.map, scenario_name
    ))
}

pub fn path_scenario(name: &MapName, scenario_name: &str) -> String {
    // TODO Getting complicated. Sometimes we're trying to load, so we should look for .bin, then
    // .json. But when we're writing a custom scenario, we actually want to write a .bin.
    let bin = path(format!(
        "system/{}/{}/scenarios/{}/{}.bin",
        name.city.country, name.city.city, name.map, scenario_name
    ));
    let json = path(format!(
        "system/{}/{}/scenarios/{}/{}.json",
        name.city.country, name.city.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.country, name.city.city, name.map
    ))
}

/// Extract the map and scenario name from a path. Crashes if the input is strange.
pub fn parse_scenario_path(path: &str) -> (MapName, String) {
    // TODO regex
    let parts = path.split('/').collect::<Vec<_>>();
    let country = parts[parts.len() - 5];
    let city = parts[parts.len() - 4];
    let map = parts[parts.len() - 2];
    let scenario = basename(parts[parts.len() - 1]);
    let map_name = MapName::new(country, city, map);
    (map_name, scenario)
}

// Player data (Players edit this)

pub fn path_player<I: AsRef<str>>(p: I) -> String {
    path(format!("player/{}", p.as_ref()))
}

pub fn path_camera_state(name: &MapName) -> String {
    path(format!(
        "player/camera_state/{}/{}/{}.json",
        name.city.country, name.city.city, name.map
    ))
}

pub fn path_edits(name: &MapName, edits_name: &str) -> String {
    path(format!(
        "player/edits/{}/{}/{}/{}.json",
        name.city.country, name.city.city, name.map, edits_name
    ))
}
pub fn path_all_edits(name: &MapName) -> String {
    path(format!(
        "player/edits/{}/{}/{}",
        name.city.country, name.city.city, name.map
    ))
}

pub fn path_ltn_proposals(name: &MapName, proposal_name: &str) -> String {
    path(format!(
        "player/ltn_proposals/{}/{}/{}/{}.json.gz",
        name.city.country, name.city.city, name.map, proposal_name
    ))
}
pub fn path_all_ltn_proposals(name: &MapName) -> String {
    path(format!(
        "player/ltn_proposals/{}/{}/{}",
        name.city.country, name.city.city, name.map
    ))
}

pub fn path_save(name: &MapName, edits_name: &str, run_name: &str, time: String) -> String {
    path(format!(
        "player/saves/{}/{}/{}/{}_{}/{}.bin",
        name.city.country, name.city.city, 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.city.country, name.city.city, name.map, edits_name, run_name
    ))
}

pub fn path_trips(name: &MapName) -> String {
    path(format!(
        "player/routes/{}/{}/{}.json",
        name.city.country, name.city.city, name.map
    ))
}

// Input data (For developers to build maps, not needed at runtime)

pub fn path_popdat() -> String {
    path("input/us/seattle/popdat.bin")
}

pub fn path_raw_map(name: &MapName) -> String {
    path(format!(
        "input/{}/{}/raw_maps/{}.bin",
        name.city.country, name.city.city, name.map
    ))
}

pub fn path_shared_input<I: AsRef<str>>(i: I) -> String {
    path(format!("input/shared/{}", i.as_ref()))
}