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
use std::collections::BTreeMap;
use std::error::Error;
use std::fs::File;
use abstutil::{DataPacks, Manifest, Timer};
use widgetry::{Btn, Checkbox, EventCtx, GfxCtx, Line, Outcome, Panel, State, TextExt, Widget};
use crate::app::App;
use crate::game::{PopupMsg, Transition};
const NEXT_RELEASE: &str = "0.2.20";
pub struct Picker {
panel: Panel,
on_load: Option<Box<dyn FnOnce(&mut EventCtx, &mut App) -> Transition>>,
}
impl Picker {
pub fn new(
ctx: &mut EventCtx,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut App) -> Transition>,
) -> Box<dyn State<App>> {
let manifest = Manifest::load();
let data_packs = DataPacks::load_or_create();
let mut col = vec![
Widget::row(vec![
Line("Download more cities").small_heading().draw(ctx),
Btn::close(ctx),
]),
"Select the cities you want to include".draw_text(ctx),
Line("The file sizes shown are uncompressed; the download size will be smaller")
.secondary()
.draw(ctx),
];
for (city, bytes) in size_per_city(&manifest) {
col.push(Widget::row(vec![
Checkbox::checkbox(ctx, &city, None, data_packs.runtime.contains(&city)),
prettyprint_bytes(bytes).draw_text(ctx).centered_vert(),
]));
}
col.push(Btn::text_bg2("Sync files").build_def(ctx, None));
Box::new(Picker {
panel: Panel::new(Widget::col(col)).build(ctx),
on_load: Some(on_load),
})
}
}
impl State<App> for Picker {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
match self.panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"close" => {
return Transition::Pop;
}
"Sync files" => {
let mut data_packs = DataPacks::load_or_create();
data_packs.runtime.clear();
data_packs.runtime.insert("seattle".to_string());
for (city, _) in size_per_city(&Manifest::load()) {
if self.panel.is_checked(&city) {
data_packs.runtime.insert(city);
}
}
abstutil::write_json(abstutil::path("player/data.json"), &data_packs);
let messages = ctx.loading_screen("sync files", |_, timer| sync(timer));
return Transition::Multi(vec![
Transition::Replace(crate::common::CityPicker::new(
ctx,
app,
self.on_load.take().unwrap(),
)),
Transition::Push(PopupMsg::new(ctx, "Download complete", messages)),
]);
}
_ => unreachable!(),
},
_ => {}
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
}
}
fn size_per_city(manifest: &Manifest) -> BTreeMap<String, usize> {
let mut per_city = BTreeMap::new();
for (path, entry) in &manifest.entries {
let parts = path.split("/").collect::<Vec<_>>();
if parts[1] == "system" {
let city = if parts.get(4) == Some(&"huge_seattle") {
"huge_seattle".to_string()
} else {
parts[2].to_string()
};
*per_city.entry(city).or_insert(0) += entry.size_bytes;
}
}
per_city
}
fn prettyprint_bytes(bytes: usize) -> String {
if bytes < 1024 {
return format!("{} bytes", bytes);
}
let kb = (bytes as f64) / 1024.0;
if kb < 1024.0 {
return format!("{} kb", kb as usize);
}
let mb = kb / 1024.0;
format!("{} mb", mb as usize)
}
fn sync(timer: &mut Timer) -> Vec<String> {
let truth = Manifest::load().filter(DataPacks::load_or_create());
let version = if cfg!(feature = "release_s3") {
NEXT_RELEASE
} else {
"dev"
};
let mut files_downloaded = 0;
let mut bytes_downloaded = 0;
let mut messages = Vec::new();
timer.start_iter("sync files", truth.entries.len());
for (path, entry) in truth.entries {
timer.next();
let local_path = abstutil::path(path.strip_prefix("data/").unwrap());
if abstutil::file_exists(&local_path) {
continue;
}
let url = format!(
"http://abstreet.s3-website.us-east-2.amazonaws.com/{}/{}.gz",
version, path
);
timer.note(format!(
"Downloading {} ({} uncompressed)",
url,
prettyprint_bytes(entry.size_bytes)
));
files_downloaded += 1;
std::fs::create_dir_all(std::path::Path::new(&local_path).parent().unwrap()).unwrap();
match download(&url, local_path, timer) {
Ok(bytes) => {
bytes_downloaded += bytes;
}
Err(err) => {
let msg = format!("Problem with {}: {}", url, err);
timer.error(msg.clone());
messages.push(msg);
}
}
}
messages.insert(
0,
format!(
"Downloaded {} files, total {}",
files_downloaded,
prettyprint_bytes(bytes_downloaded)
),
);
messages
}
fn download(url: &str, local_path: String, timer: &mut Timer) -> Result<usize, Box<dyn Error>> {
let mut resp = reqwest::blocking::get(url)?;
if !resp.status().is_success() {
return Err(format!("bad status: {:?}", resp.status()).into());
}
let mut buffer: Vec<u8> = Vec::new();
let bytes = resp.copy_to(&mut buffer)? as usize;
timer.note(format!(
"Decompressing {} ({})",
url,
prettyprint_bytes(bytes)
));
let mut decoder = flate2::read::GzDecoder::new(&buffer[..]);
let mut out = File::create(&local_path).unwrap();
std::io::copy(&mut decoder, &mut out)?;
Ok(bytes)
}