132 lines
5.5 KiB
Rust
132 lines
5.5 KiB
Rust
//! `horizon-prepare`: prepares Horizon's resources from your own Minecraft Java Edition.
|
|
//! Its flags are the ones tools/prepare_native_client.py took.
|
|
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::ExitCode;
|
|
|
|
const HELP: &str = "\
|
|
horizon-prepare: prepare Horizon's resources from your own Minecraft Java Edition
|
|
|
|
Usage: horizon-prepare [--prepare] [--server-jar PATH] [--minecraft-dir PATH] [--root PATH]
|
|
horizon-prepare sounds [--minecraft-dir PATH] [--root PATH]
|
|
horizon-prepare panorama [--minecraft-dir PATH] [--root PATH]
|
|
|
|
--prepare Import the pinned game files from your own copy first
|
|
--server-jar PATH The pinned server jar you downloaded yourself
|
|
(default: HORIZON_SERVER_JAR, or the copy imported before)
|
|
--minecraft-dir PATH The official launcher's game directory with your installed copy
|
|
(default: HORIZON_MINECRAFT_DIR, or the launcher's own)
|
|
--root PATH The Horizon folder to prepare (default: the folder this program is
|
|
in, else the nearest folder above the current one with research-lock.json)
|
|
|
|
Needs Minecraft played once from the official launcher, and Java (a JDK) of the version
|
|
research-lock.json pins: JAVA_HOME, else javac and java on the PATH.
|
|
The resources go to <root>/.local/horizon-client/<version>; pass that to horizon-client --resources.
|
|
|
|
horizon-prepare panorama repairs the classic menu background in existing resources without Java.
|
|
Full preparation also imports these textures after extracting the client JAR's placeholders.
|
|
|
|
horizon-prepare sounds copies the pinned version's sounds (about 380 MB) from the official
|
|
launcher's asset store into the same folder, checking every file. It is optional: without it the
|
|
game is silent. Nothing is downloaded; it needs the version played once from the official launcher.
|
|
";
|
|
|
|
/// The folder with research-lock.json: beside the executable in a release folder, else the
|
|
/// current folder or one above it (a checkout, with the binary in target/).
|
|
fn default_root() -> Option<PathBuf> {
|
|
let beside = std::env::current_exe()
|
|
.ok()
|
|
.and_then(|exe| exe.parent().map(Path::to_path_buf));
|
|
if let Some(dir) = beside.filter(|d| d.join("research-lock.json").is_file()) {
|
|
return Some(dir);
|
|
}
|
|
let cwd = std::env::current_dir().ok()?;
|
|
cwd.ancestors()
|
|
.find(|d| d.join("research-lock.json").is_file())
|
|
.map(Path::to_path_buf)
|
|
}
|
|
|
|
fn main() -> ExitCode {
|
|
let mut options = horizon_prepare::Options {
|
|
root: PathBuf::new(),
|
|
import: false,
|
|
minecraft_dir: None,
|
|
server_jar: None,
|
|
};
|
|
let mut root = None;
|
|
let mut args = std::env::args_os().skip(1).peekable();
|
|
let assets = args.next_if(|a| a == "sounds" || a == "panorama");
|
|
while let Some(arg) = args.next() {
|
|
let text = arg.to_string_lossy().into_owned();
|
|
let (flag, inline) = match text.split_once('=') {
|
|
Some((flag, value)) if flag.starts_with("--") => {
|
|
(flag.to_string(), Some(value.to_string()))
|
|
}
|
|
_ => (text.clone(), None),
|
|
};
|
|
let mut value = |name: &str| -> Result<PathBuf, String> {
|
|
match &inline {
|
|
Some(v) => Ok(PathBuf::from(v)),
|
|
None => args
|
|
.next()
|
|
.map(PathBuf::from)
|
|
.ok_or_else(|| format!("{name} needs a path")),
|
|
}
|
|
};
|
|
let parsed = match flag.as_str() {
|
|
"--prepare" | "--server-jar" if assets.is_some() => {
|
|
Err(format!("{flag} is for preparing, not for asset copying"))
|
|
}
|
|
"--prepare" if inline.is_none() => {
|
|
options.import = true;
|
|
Ok(())
|
|
}
|
|
"--server-jar" => value("--server-jar").map(|v| options.server_jar = Some(v)),
|
|
"--minecraft-dir" => value("--minecraft-dir").map(|v| options.minecraft_dir = Some(v)),
|
|
"--root" => value("--root").map(|v| root = Some(v)),
|
|
"-h" | "--help" => {
|
|
print!("{HELP}");
|
|
return ExitCode::SUCCESS;
|
|
}
|
|
"-V" | "--version" => {
|
|
println!("horizon-prepare {}", env!("CARGO_PKG_VERSION"));
|
|
return ExitCode::SUCCESS;
|
|
}
|
|
_ => Err(format!("unrecognized argument: {text}")),
|
|
};
|
|
if let Err(e) = parsed {
|
|
eprintln!("horizon-prepare: error: {e}\n\n{HELP}");
|
|
return ExitCode::from(2);
|
|
}
|
|
}
|
|
let Some(root) = root.or_else(default_root) else {
|
|
eprintln!(
|
|
"horizon-prepare: error: no research-lock.json beside this program or above the current folder; pass --root"
|
|
);
|
|
return ExitCode::from(2);
|
|
};
|
|
options.root = root;
|
|
let log = |line: &str| {
|
|
let mut out = std::io::stdout().lock();
|
|
let _ = writeln!(out, "{line}");
|
|
let _ = out.flush();
|
|
};
|
|
let result = match assets.as_deref().and_then(|a| a.to_str()) {
|
|
Some("sounds") => {
|
|
horizon_prepare::sounds(&options.root, options.minecraft_dir.as_deref(), &log)
|
|
}
|
|
Some("panorama") => {
|
|
horizon_prepare::panorama(&options.root, options.minecraft_dir.as_deref(), &log)
|
|
}
|
|
_ => horizon_prepare::prepare(&options, &log),
|
|
};
|
|
match result {
|
|
Ok(_) => ExitCode::SUCCESS,
|
|
Err(e) => {
|
|
eprintln!("error: {e}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|