use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Seek};
use std::path::Path;
use anyhow::Context;
use serde::de::DeserializeOwned;
use serde::Serialize;
use zip::ZipArchive;
pub mod files;
pub mod java;
pub mod minecraft;
pub mod persistent;
pub mod update;
pub fn json_from_file<D: DeserializeOwned>(path: impl AsRef<Path>) -> anyhow::Result<D> {
let file = BufReader::new(File::open(path).context("Failed to open file")?);
Ok(simd_json::from_reader(file)?)
}
pub fn json_to_file<S: Serialize>(path: impl AsRef<Path>, data: &S) -> anyhow::Result<()> {
let file = BufWriter::new(File::create(path).context("Failed to open file")?);
simd_json::to_writer(file, data).context("Failed to serialize data to file")?;
Ok(())
}
pub fn json_to_file_pretty<S: Serialize>(path: impl AsRef<Path>, data: &S) -> anyhow::Result<()> {
let file = BufWriter::new(File::create(path).context("Failed to open file")?);
serde_json::to_writer_pretty(file, data).context("Failed to serialize data to file")?;
Ok(())
}
pub fn json_to_file_pretty_fast<S: Serialize>(
path: impl AsRef<Path>,
data: &S,
) -> anyhow::Result<()> {
let file = BufWriter::new(File::create(path).context("Failed to open file")?);
simd_json::to_writer_pretty(file, data).context("Failed to serialize data to file")?;
Ok(())
}
pub fn extract_zip_dir<R: Read + Seek>(
zip: &mut ZipArchive<R>,
zip_dir: &str,
target_dir: impl AsRef<Path>,
) -> anyhow::Result<()> {
let _ = std::fs::create_dir_all(target_dir.as_ref());
for index in 0..zip.len() {
let mut file = zip.by_index(index)?;
if file.is_dir() {
continue;
}
let Some(filename) = file.enclosed_name() else {
continue;
};
let Ok(filename) = filename.strip_prefix(zip_dir) else {
continue;
};
let out_path = target_dir.as_ref().join(filename);
let _ = files::create_leading_dirs(&out_path);
let mut out_file = File::create(out_path).context("Failed to create output file")?;
std::io::copy(&mut file, &mut out_file).context("Failed to copy file from zip")?;
}
Ok(())
}