diff options
| author | 2025-02-01 23:06:37 -0600 | |
|---|---|---|
| committer | 2025-02-01 23:06:37 -0600 | |
| commit | c19a1077e85334a3e5ba885a60b03d76409a2b2e (patch) | |
| tree | 5e726e8180770ac9c2f6c415a0437d6d2c29c226 /src/launcher/jre | |
| parent | random changes (diff) | |
restructure project
Diffstat (limited to 'src/launcher/jre')
| -rw-r--r-- | src/launcher/jre/arch.rs | 45 | ||||
| -rw-r--r-- | src/launcher/jre/download.rs | 195 | ||||
| -rw-r--r-- | src/launcher/jre/manifest.rs | 65 |
3 files changed, 0 insertions, 305 deletions
diff --git a/src/launcher/jre/arch.rs b/src/launcher/jre/arch.rs deleted file mode 100644 index e984171..0000000 --- a/src/launcher/jre/arch.rs +++ /dev/null @@ -1,45 +0,0 @@ -use cfg_if::cfg_if; - -macro_rules! define_arch { - ($arch:expr) => { - pub const JRE_ARCH: &str = $arch; - } -} - -cfg_if! { - if #[cfg(target_os = "windows")] { - cfg_if! { - if #[cfg(target_arch = "x86_64")] { - define_arch!("windows-x64"); - } else if #[cfg(target_arch = "x86")] { - define_arch!("windows-x86"); - } else if #[cfg(target_arch = "aarch64")] { - define_arch!("windows-arm64"); - } else { - define_arch!("gamecore"); - } - } - } else if #[cfg(target_os = "linux")] { - cfg_if! { - if #[cfg(target_arch = "x86_64")] { - define_arch!("linux"); - } else if #[cfg(target_arch = "x86")] { - define_arch!("linux-i386"); - } else { - define_arch!("gamecore"); - } - } - } else if #[cfg(target_os = "macos")] { - cfg_if! { - if #[cfg(target_arch = "aarch64")] { - define_arch!("mac-os-arm64"); - } else if #[cfg(target_arch = "x86_64")] { - define_arch!("mac-os"); - } else { - define_arch!("gamecore"); - } - } - } else { - define_arch!("gamecore"); - } -} diff --git a/src/launcher/jre/download.rs b/src/launcher/jre/download.rs deleted file mode 100644 index ddf1ff6..0000000 --- a/src/launcher/jre/download.rs +++ /dev/null @@ -1,195 +0,0 @@ -use std::error::Error; -use std::fmt::{Debug, Display, Formatter}; -use std::io::Write; -use std::path::{PathBuf}; -use log::debug; -use lzma_rs::decompress; -use reqwest::{Client, RequestBuilder}; -use sha1_smol::{Digest, Sha1}; -use tokio::io::AsyncWriteExt; -use tokio::fs::File; -use crate::launcher::download::Download; -use crate::launcher::jre::manifest::JavaRuntimeFile; -use crate::util; -use crate::util::IntegrityError; -use crate::version::DownloadInfo; - -pub enum LzmaDownloadError { - NotAFile, - MissingURL -} - -pub struct LzmaDownloadJob { - url: String, - path: PathBuf, - inflate: bool, - executable: bool, - - raw_size: Option<usize>, - raw_sha1: Option<Digest>, - - raw_sha1_st: Sha1, - raw_tally: usize, - - stream: Option<decompress::Stream<Vec<u8>>>, - out_file: Option<File> -} - -impl LzmaDownloadJob { - fn new_inflate(raw: &DownloadInfo, lzma: &DownloadInfo, exe: bool, path: PathBuf) -> Result<Self, LzmaDownloadError> { - Ok(LzmaDownloadJob { - url: lzma.url.as_ref().map_or_else(|| Err(LzmaDownloadError::MissingURL), |u| Ok(u.to_owned()))?, - path, - inflate: true, - executable: exe, - - raw_size: raw.size, - raw_sha1: raw.sha1, - - raw_sha1_st: Sha1::new(), - raw_tally: 0, - - stream: Some(decompress::Stream::new(Vec::new())), - out_file: None - }) - } - - fn new_raw(raw: &DownloadInfo, exe: bool, path: PathBuf) -> Result<Self, LzmaDownloadError> { - Ok(LzmaDownloadJob { - url: raw.url.as_ref().map_or_else(|| Err(LzmaDownloadError::MissingURL), |u| Ok(u.to_owned()))?, - path, - inflate: false, - executable: exe, - - raw_size: raw.size, - raw_sha1: raw.sha1, - - raw_sha1_st: Sha1::new(), - raw_tally: 0, - - stream: None, - out_file: None - }) - } -} - -impl TryFrom<(&JavaRuntimeFile, PathBuf)> for LzmaDownloadJob { - type Error = LzmaDownloadError; - - fn try_from((file, path): (&JavaRuntimeFile, PathBuf)) -> Result<Self, Self::Error> { - if !file.is_file() { - return Err(LzmaDownloadError::NotAFile); - } - - let JavaRuntimeFile::File { executable, downloads } = file else { - unreachable!("we just made sure this was a file"); - }; - - match downloads.lzma.as_ref() { - Some(lzma) => LzmaDownloadJob::new_inflate(&downloads.raw, lzma, *executable, path), - None => LzmaDownloadJob::new_raw(&downloads.raw, *executable, path) - } - } -} - -impl Debug for LzmaDownloadJob { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("LzmaDownloadJob") - .field("url", &self.url) - .field("path", &self.path) - .field("inflate", &self.inflate) - .finish() - } -} - -impl Display for LzmaDownloadJob { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - if self.inflate { - write!(f, "download and inflate {} to {}", &self.url, self.path.display()) - } else { - write!(f, "download {} to {}", &self.url, self.path.display()) - } - } -} - -impl Download for LzmaDownloadJob { - async fn prepare(&mut self, client: &Client) -> Result<Option<RequestBuilder>, Box<dyn Error>> { - if !util::should_download(&self.path, self.raw_size, self.raw_sha1).await? { - return Ok(None) - } - - let mut options = File::options(); - - #[cfg(unix)] - { - options.mode(match self.executable { - true => 0o775, - _ => 0o664 - }); - } - - let file = options.create(true).write(true).truncate(true).open(&self.path).await?; - self.out_file = Some(file); - - Ok(Some(client.get(&self.url))) - } - - async fn handle_chunk(&mut self, chunk: &[u8]) -> Result<(), Box<dyn Error>> { - let out_file = self.out_file.as_mut().expect("output file gone"); - - if let Some(ref mut stream) = self.stream { - stream.write_all(chunk)?; - let buf = stream.get_output_mut().expect("stream output missing before finish()"); - - out_file.write_all(buf.as_slice()).await?; - - self.raw_sha1_st.update(buf.as_slice()); - self.raw_tally += buf.len(); - - buf.truncate(0); - } else { - out_file.write_all(chunk).await?; - - self.raw_sha1_st.update(chunk); - self.raw_tally += chunk.len(); - } - - Ok(()) - } - - async fn finish(&mut self) -> Result<(), Box<dyn Error>> { - let mut out_file = self.out_file.take().expect("output file gone"); - - if let Some(stream) = self.stream.take() { - let buf = stream.finish()?; - - out_file.write_all(buf.as_slice()).await?; - - self.raw_sha1_st.update(buf.as_slice()); - self.raw_tally += buf.len(); - } - - let inf_digest = self.raw_sha1_st.digest(); - if let Some(sha1) = self.raw_sha1 { - if inf_digest != sha1 { - debug!("Could not download {}: sha1 mismatch (exp {}, got {}).", self.path.display(), sha1, inf_digest); - return Err(IntegrityError::Sha1Mismatch { - expect: sha1, - actual: inf_digest - }.into()); - } - } - - if let Some(size) = self.raw_size { - if self.raw_tally != size { - debug!("Could not download {}: size mismatch (exp {}, got {}).", self.path.display(), size, self.raw_tally); - return Err(IntegrityError::SizeMismatch { - expect: size, - actual: self.raw_tally - }.into()); - } - } - - Ok(()) - } -} diff --git a/src/launcher/jre/manifest.rs b/src/launcher/jre/manifest.rs deleted file mode 100644 index 3fd6484..0000000 --- a/src/launcher/jre/manifest.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::collections::HashMap; -use indexmap::IndexMap; -use serde::Deserialize; -use crate::version::DownloadInfo; - -#[derive(Debug, Deserialize)] -pub struct Availability { - pub group: u32, // unknown meaning - pub progress: u32 // unknown meaning -} - -#[derive(Debug, Deserialize)] -pub struct Version { - pub name: String, - pub version: String -} - -#[derive(Debug, Deserialize)] -pub struct JavaRuntimeInfo { - // I don't see how half of this information is useful with how the JRE system currently functions -figboot - pub availability: Availability, - pub manifest: DownloadInfo, - //pub version: Version -} - -pub type JavaRuntimesManifest = HashMap<String, HashMap<String, Vec<JavaRuntimeInfo>>>; - -#[derive(Debug, Deserialize)] -pub struct FileDownloads { - pub lzma: Option<DownloadInfo>, - pub raw: DownloadInfo -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "lowercase", tag = "type")] -pub enum JavaRuntimeFile { - File { - #[serde(default)] - executable: bool, - downloads: Box<FileDownloads> - }, - Directory, - Link { - target: String - } -} - -impl JavaRuntimeFile { - pub fn is_file(&self) -> bool { - matches!(*self, JavaRuntimeFile::File { .. }) - } - - pub fn is_directory(&self) -> bool { - matches!(*self, JavaRuntimeFile::Directory) - } - - pub fn is_link(&self) -> bool { - matches!(*self, JavaRuntimeFile::Link { .. }) - } -} - -#[derive(Debug, Deserialize)] -pub struct JavaRuntimeManifest { - pub files: IndexMap<String, JavaRuntimeFile> -} |
