Skip to content

Commit b5ccddf

Browse files
authored
Change vanilla JAR download timeout and add warnings (#474)
1 parent df3a3a4 commit b5ccddf

File tree

1 file changed

+38
-3
lines changed
  • feather/datapacks/vanilla_assets/src

1 file changed

+38
-3
lines changed

feather/datapacks/vanilla_assets/src/lib.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use anyhow::Context;
2+
use std::sync::atomic::{AtomicBool, Ordering};
3+
use std::sync::Arc;
4+
use std::time::SystemTime;
25
use std::{
36
fs::{self, File},
47
io,
@@ -11,6 +14,8 @@ use zip::ZipArchive;
1114
const JAR_URL: &str =
1215
"https://launcher.mojang.com/v1/objects/c5f6fb23c3876461d46ec380421e42b289789530/server.jar";
1316
const JAR_NAME: &str = "server-1.16.2.jar";
17+
const DOWNLOAD_WARNING_MINUTES: f64 = 2.0;
18+
const DOWNLOAD_TIMEOUT_MINUTES: u64 = 30;
1419

1520
/// Downloads vanilla server/client JARs and assets, used
1621
/// for loot tables, recipes, etc.
@@ -19,7 +24,10 @@ const JAR_NAME: &str = "server-1.16.2.jar";
1924
/// The vanilla datapack will be extracted to `$base/datapacks`.
2025
pub fn download_vanilla_assets(base: &Path) -> anyhow::Result<()> {
2126
let jar = download_jar(base)
22-
.context("failed to download vanilla server JAR")
27+
.context(format!(
28+
"failed to download vanilla server JAR in {} minutes",
29+
DOWNLOAD_TIMEOUT_MINUTES
30+
))
2331
.context("please make sure you have an Internet connection.")?;
2432
// NB: JAR files are just glorified ZIP files, so we can use the zip crate
2533
// to process the data.
@@ -32,10 +40,37 @@ pub fn download_vanilla_assets(base: &Path) -> anyhow::Result<()> {
3240

3341
fn download_jar(base: &Path) -> anyhow::Result<File> {
3442
log::info!("Downloading vanilla server JAR from {}", JAR_URL);
43+
44+
let downloaded = Arc::new(AtomicBool::new(false));
45+
let download_start_time = SystemTime::now();
46+
let mut download_time = 1.0;
47+
let mut warning = 1;
48+
let downloaded1 = downloaded.clone();
49+
std::thread::spawn(move || {
50+
while !downloaded1.load(Ordering::Relaxed) {
51+
let elapsed = download_start_time.elapsed().unwrap().as_secs_f64();
52+
if elapsed / 60.0 >= warning as f64 * DOWNLOAD_WARNING_MINUTES
53+
&& download_time / 60.0 < warning as f64 * DOWNLOAD_WARNING_MINUTES
54+
{
55+
log::warn!("Looks like you have a slow internet connection! Downloading vanilla server JAR for {} minutes", warning as f64 * DOWNLOAD_WARNING_MINUTES);
56+
warning += 1;
57+
}
58+
download_time = elapsed;
59+
std::thread::sleep(Duration::from_secs(1));
60+
}
61+
});
62+
3563
let mut data = ureq::get(JAR_URL)
36-
.timeout(Duration::from_secs(10))
37-
.call()?
64+
.timeout(Duration::from_secs(60 * DOWNLOAD_TIMEOUT_MINUTES))
65+
.call()
66+
.map_err(|err| {
67+
// stop download time counter
68+
downloaded.store(true, Ordering::Relaxed);
69+
err
70+
})?
3871
.into_reader();
72+
downloaded.store(true, Ordering::Relaxed);
73+
log::info!("Downloaded vanilla server JAR successfully");
3974

4075
let downloaded_dir = base.join("downloaded");
4176
fs::create_dir_all(&downloaded_dir)?;

0 commit comments

Comments
 (0)