-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathbuild.rs
More file actions
154 lines (132 loc) · 4.74 KB
/
build.rs
File metadata and controls
154 lines (132 loc) · 4.74 KB
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
use anyhow::Context;
use std::env;
use std::fs;
use std::fs::File;
use std::io::{copy, Write};
use std::path::Path;
use std::process::Command;
use zip::ZipArchive;
fn main() {
match run() {
Ok(_) => (),
Err(e) => panic!("{:?}", e),
}
}
fn run() -> anyhow::Result<()> {
let path = format!("{}/minecraft", env::var("OUT_DIR")?);
let path_1_15 = format!("{}/minecraft-1.15", env::var("OUT_DIR")?);
download_version("https://launcher.mojang.com/v1/objects/3737db93722a9e39eeada7c27e7aca28b144ffa7/server.jar", &path, true).context("failed to download 1.13 data")?;
download_version("https://launcher.mojang.com/v1/objects/bb2b6b1aefcd70dfd1892149ac3a215f6c636b07/server.jar", &path_1_15, false).context("failed to download 1.15 data")?;
clone_minecraft_data().context("failed to clone PrismarineJS/minecraft-data")?;
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/build.rs")
);
Ok(())
}
fn download_version(url: &str, path: &str, do_generate: bool) -> anyhow::Result<()> {
let path = Path::new(&path);
let path_server = path.join("server.jar");
if data_exists(path).unwrap_or(false) {
println!("cargo:rerun-if-changed={}", &path.display());
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/build.rs")
);
return Ok(());
}
let _ = fs::remove_dir_all(path);
fs::create_dir_all(path).context("failed to create target directory for downloaded data")?;
download(url, &path_server).context("failed to download vanilla server JAR")?;
println!(
"after download: {:?}",
std::fs::read_dir(path)?.collect::<Vec<_>>()
);
if do_generate {
generate(path).context("failed to generate vanilla server reports.")?;
}
extract(path).context("failed to extract vanilla assets.")?;
println!(
"after extract: {:?}",
std::fs::read_dir(path)?.collect::<Vec<_>>()
);
Ok(())
}
fn data_exists(path: &Path) -> anyhow::Result<bool> {
Ok(File::open(path.join("server.jar")).is_ok()
&& File::open(path.join("assets")).is_ok()
&& File::open(path.join("data")).is_ok()
&& File::open(path.join("generated")).is_ok())
}
fn download<P: AsRef<Path>>(url: &str, server: P) -> anyhow::Result<()> {
let mut response = reqwest::blocking::get(url)?;
let mut dest = File::create(server)
.context("failed to create destination file for server JAR download")?;
copy(&mut response, &mut dest)?;
dest.flush()?;
Ok(())
}
fn generate<P: AsRef<Path>>(working: P) -> anyhow::Result<()> {
let status = Command::new("java")
.current_dir(working.as_ref())
.args(&["-cp", "server.jar", "net.minecraft.data.Main", "--reports"])
.status()?;
if !status.success() {
anyhow::bail!(
"process to generate server reports was not successful (exit status {}, JAR path {})",
status,
working.as_ref().display(),
)
}
Ok(())
}
fn extract<P: AsRef<Path>>(working: P) -> anyhow::Result<()> {
println!(
"{:?}",
std::fs::read_dir(working.as_ref())?.collect::<Vec<_>>()
);
let server_jar = working.as_ref().join("server.jar");
let mut archive = ZipArchive::new(std::fs::File::open(server_jar)?)?;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
if !(file.name().starts_with("assets/") || file.name().starts_with("data/")) {
continue;
}
let outpath_name = file.name().replace("..", ".");
let outpath = working.as_ref().join(outpath_name);
if file.is_dir() {
println!("Directory \"{}\" was created", outpath.display());
fs::create_dir_all(&outpath).unwrap();
} else {
println!("Writing to \"{}\"", outpath.display(),);
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
}
}
let mut outfile = fs::File::create(&outpath).unwrap();
std::io::copy(&mut file, &mut outfile).unwrap();
}
}
Ok(())
}
fn clone_minecraft_data() -> anyhow::Result<()> {
let path = format!("{}/minecraft-data", env::var("OUT_DIR")?);
if Path::new(&path).exists() {
// Already cloned - no need to do so again
return Ok(());
}
if !Command::new("git")
.arg("clone")
.arg("https://github.com/PrismarineJS/minecraft-data.git")
.arg(&path)
.status()?
.success()
{
Err(anyhow::anyhow!(
"failed to clone minecraft-data repository: please ensure git is installed"
))
} else {
Ok(())
}
}