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
use std::collections::BTreeSet;

use fs_err::File;
use futures_channel::mpsc;

use abstio::{DataPacks, Manifest, MapName};
use abstutil::prettyprint_bytes;
use widgetry::tools::{FutureLoader, PopupMsg};
use widgetry::{EventCtx, Key, Transition};

use crate::tools::ChooseSomething;
use crate::AppLike;

// For each city, how many total bytes do the runtime files cost to download?

/// How many bytes to download for a city?
fn size_of_city(map: &MapName) -> u64 {
    let mut data_packs = DataPacks {
        runtime: BTreeSet::new(),
        input: BTreeSet::new(),
    };
    data_packs.runtime.insert(map.to_data_pack_name());
    let mut manifest = Manifest::load().filter(data_packs);
    // Don't download files that already exist
    manifest
        .entries
        .retain(|path, _| !abstio::file_exists(&abstio::path(path.strip_prefix("data/").unwrap())));
    let mut bytes = 0;
    for (_, entry) in manifest.entries {
        bytes += entry.compressed_size_bytes;
    }
    bytes
}

/// Prompt to download a missing city. On either success or failure (maybe the player choosing to
/// not download, maybe a network error), the new map isn't automatically loaded or anything; up to
/// the caller to handle that.
pub fn prompt_to_download_missing_data<A: AppLike + 'static>(
    ctx: &mut EventCtx,
    map_name: MapName,
) -> Transition<A> {
    Transition::Push(ChooseSomething::new_state(
        ctx,
        format!(
            "Missing data. Download {} for {}?",
            prettyprint_bytes(size_of_city(&map_name)),
            map_name.city.describe()
        ),
        vec![
            widgetry::Choice::string("Yes, download"),
            widgetry::Choice::string("Never mind").key(Key::Escape),
        ],
        Box::new(move |resp, ctx, _| {
            if resp == "Never mind" {
                return Transition::Pop;
            }

            let cities = vec![map_name.to_data_pack_name()];

            // Adjust the updater's config, in case the user also runs that.
            let mut packs = DataPacks::load_or_create();
            packs.runtime.insert(cities[0].clone());
            packs.save();

            let (outer_progress_tx, outer_progress_rx) = futures_channel::mpsc::channel(1000);
            let (inner_progress_tx, inner_progress_rx) = futures_channel::mpsc::channel(1000);
            Transition::Replace(FutureLoader::<A, Vec<String>>::new_state(
                ctx,
                Box::pin(async {
                    let result =
                        download_cities(cities, outer_progress_tx, inner_progress_tx).await;
                    let wrap: Box<dyn Send + FnOnce(&A) -> Vec<String>> =
                        Box::new(move |_: &A| result);
                    Ok(wrap)
                }),
                outer_progress_rx,
                inner_progress_rx,
                "Downloading missing files",
                Box::new(|ctx, _, maybe_messages| {
                    let messages = match maybe_messages {
                        Ok(m) => m,
                        Err(err) => vec![format!("Something went very wrong: {}", err)],
                    };
                    Transition::Replace(PopupMsg::new_state(
                        ctx,
                        "Download complete. Try again!",
                        messages,
                    ))
                }),
            ))
        }),
    ))
}

async fn download_cities(
    cities: Vec<String>,
    mut outer_progress: mpsc::Sender<String>,
    mut inner_progress: mpsc::Sender<String>,
) -> Vec<String> {
    let mut data_packs = DataPacks {
        runtime: BTreeSet::new(),
        input: BTreeSet::new(),
    };
    data_packs.runtime.extend(cities);
    let mut manifest = Manifest::load().filter(data_packs);
    // Don't download files that already exist
    manifest
        .entries
        .retain(|path, _| !abstio::file_exists(&abstio::path(path.strip_prefix("data/").unwrap())));

    let num_files = manifest.entries.len();
    let mut messages = Vec::new();
    let mut files_so_far = 0;

    for (path, entry) in manifest.entries {
        files_so_far += 1;
        let local_path = abstio::path(path.strip_prefix("data/").unwrap());
        let url = format!(
            "http://play.abstreet.org/{}/{}.gz",
            crate::tools::version(),
            path
        );
        if let Err(err) = outer_progress.try_send(format!(
            "Downloading file {}/{}: {} ({})",
            files_so_far,
            num_files,
            url,
            prettyprint_bytes(entry.compressed_size_bytes)
        )) {
            warn!("Couldn't send progress: {}", err);
        }

        match abstio::download_bytes(&url, None, &mut inner_progress)
            .await
            .and_then(|bytes| {
                // TODO Instead of holding everything in memory like this, we could also try to
                // stream the gunzipping and output writing
                info!("Decompressing {}", path);
                fs_err::create_dir_all(std::path::Path::new(&local_path).parent().unwrap())
                    .unwrap();
                let mut out = File::create(&local_path).unwrap();
                let mut decoder = flate2::read::GzDecoder::new(&bytes[..]);
                std::io::copy(&mut decoder, &mut out).map_err(|err| err.into())
            }) {
            Ok(_) => {}
            Err(err) => {
                let msg = format!("Problem with {}: {}", url, err);
                error!("{}", msg);
                messages.push(msg);
            }
        }
    }
    messages.insert(0, format!("Downloaded {} files", num_files));
    messages
}