Skip to content

Commit bd66778

Browse files
committed
feat: cli
1 parent ad22ddb commit bd66778

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

Cargo.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ bzip2 = { version = "0.4", features = [] }
2424
toml = "0.8.19"
2525
sha1 = "0.10.6"
2626
hex = "0.4.3"
27+
clap = { version = "4.5.21", features = ["derive"] }

src/main.rs

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,42 @@ use bin_patch_gen::version::{fetch_spigot_version_meta, fetch_versions};
99
use bin_patch_gen::{
1010
config, jar, prepare_extraction_path, write_patch, MinecraftVersion, JAR_VERSIONS_PATH,
1111
};
12+
use bzip2::read::BzDecoder;
13+
use clap::{command, Parser, Subcommand};
1214
use regex::Regex;
1315
use std::env::current_dir;
14-
use std::fs;
16+
use std::fs::{self, File};
17+
use std::io::{Read, Write};
1518
use std::path::{Path, PathBuf};
1619
use tracing::info;
1720
use tracing_subscriber::fmt::format;
1821

22+
#[derive(Parser)]
23+
#[command(about, long_about = None)]
24+
/// The binary patch generator for Sploon.
25+
struct Cli {
26+
/// The version to generate for. If not specified, it will generate patches for
27+
/// all versions of Spigot.
28+
#[arg(short, long, value_name = "version")]
29+
pub version: Option<String>,
30+
31+
#[command(subcommand)]
32+
pub command: Option<Commands>,
33+
}
34+
35+
#[derive(Subcommand)]
36+
enum Commands {
37+
/// Patches a file.
38+
Patch {
39+
/// The old file.
40+
old: PathBuf,
41+
/// The new file, patched.
42+
new: PathBuf,
43+
/// The bsdiff patch file, compressed with bzip2.
44+
patch: PathBuf,
45+
},
46+
}
47+
1948
#[tokio::main]
2049
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2150
let fmt = format()
@@ -25,6 +54,39 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2554

2655
tracing_subscriber::fmt().event_format(fmt).init();
2756

57+
let cli = Cli::parse();
58+
59+
if let Some(Commands::Patch { old, new, patch }) = cli.command {
60+
let patch_file = File::open(patch)?;
61+
let mut old_file = File::open(old)?;
62+
let mut new_file = File::create(new)?;
63+
64+
let mut old_buf = vec![];
65+
let mut new_buf = vec![];
66+
67+
old_file.read_to_end(&mut old_buf)?;
68+
69+
let mut decompressor = BzDecoder::new(patch_file);
70+
71+
info!("Patching...");
72+
bsdiff::patch(&old_buf, &mut decompressor, &mut new_buf)?;
73+
74+
new_file.write_all(&new_buf)?;
75+
info!("Patched!");
76+
77+
return Ok(());
78+
}
79+
80+
let versions = if let Some(version) = cli.version {
81+
vec![version]
82+
} else {
83+
fetch_versions().await
84+
};
85+
86+
run(versions).await
87+
}
88+
89+
async fn run(versions: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
2890
let config_file = PathBuf::from("config.toml");
2991
if !fs::exists(&config_file)? {
3092
fs::write(config_file, toml::to_string_pretty(&Config::default())?)?;
@@ -33,9 +95,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3395

3496
let config = config::read_config("config.toml")?;
3597

36-
info!("Fetching versions...");
37-
let versions = fetch_versions().await;
38-
3998
info!("Releases found: {versions:?}");
4099

41100
info!("Downloading BuildTools...");
@@ -146,7 +205,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
146205
info!("Diff generated!");
147206

148207
let patched_meta = PatchedVersionMeta {
149-
patch_file: patch_file.file_name().unwrap().to_string_lossy().into_owned(),
208+
patch_file: patch_file
209+
.file_name()
210+
.unwrap()
211+
.to_string_lossy()
212+
.into_owned(),
150213
commit_hashes: remote_meta.refs,
151214
patch_hash: sha1(patch_file)?,
152215
patched_jar_hash: sha1(spigot_jar)?,

0 commit comments

Comments
 (0)