-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ead7267
commit 6b575f6
Showing
28 changed files
with
674 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
use whcore::type_map; | ||
|
||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
#[macro_use] | ||
extern crate serde; | ||
|
||
#[macro_use] | ||
extern crate specta; | ||
|
||
#[macro_use] | ||
extern crate async_trait; | ||
|
||
type_map! {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
use whcore::type_map; | ||
|
||
#[macro_use] | ||
extern crate tracing; | ||
|
||
type_map! {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{collections::HashMap, fs, path::PathBuf}; | ||
use anyhow::Result; | ||
use tokio_stream::StreamExt; | ||
use crate::vanilla::manifest::libraries::Library; | ||
|
||
pub struct LibraryInstaller { | ||
/// A list of listeners. | ||
/// Fn(output_path, downloaded, total_bytes) | ||
listeners: Vec<Box<dyn Fn(&PathBuf, u64, u64)>>, | ||
} | ||
|
||
impl LibraryInstaller { | ||
pub fn new() -> Self { | ||
Self { | ||
listeners: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn add_listener<T>(&mut self, func: T) -> &mut Self where T: Fn(&PathBuf, u64, u64) + 'static { | ||
self.listeners.push(Box::new(func)); | ||
self | ||
} | ||
|
||
/// Download library files into a root directory. | ||
/// | ||
/// Args: | ||
/// - root: The root of the library folder. | ||
/// - libs: The libraries to download. | ||
/// - features: Enabled launcher features. | ||
pub async fn install_libraries(&self, root: PathBuf, libs: Vec<Library>, features: &HashMap<String, bool>) -> Result<()> { | ||
for lib in libs { | ||
let files = lib.get_files(features); | ||
|
||
for file in files { | ||
let path = root.join(file.path()); | ||
|
||
if path.exists() { | ||
self.notify_progress(&path, 1, 1); | ||
continue; | ||
} | ||
|
||
let mut tmp_path = path.clone(); | ||
let parent = path.parent().unwrap(); | ||
|
||
tmp_path.add_extension(".tmp"); | ||
fs::create_dir_all(parent)?; | ||
|
||
let req = reqwest::get(file.url).await?; | ||
let total = req.content_length().unwrap_or_default(); | ||
let mut stream = req.bytes_stream(); | ||
let mut data = Vec::new(); | ||
|
||
while let Some(Ok(chunk)) = stream.next().await { | ||
data.extend(chunk); | ||
self.notify_progress(&path, data.len() as u64, total); | ||
} | ||
|
||
fs::write(&tmp_path, data)?; | ||
fs::rename(tmp_path, path)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn notify_progress(&self, path: &PathBuf, downloaded: u64, total: u64) { | ||
for listener in &self.listeners { | ||
listener(path, downloaded, total); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::collections::HashMap; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::{ | ||
maven::{artifact::Artifact, MavenRepo}, | ||
vanilla::{ | ||
manifest::{ | ||
args::Arguments, | ||
libraries::{Library, LibraryDownloads, LibraryFile}, | ||
VersionManifest, | ||
}, | ||
meta::PistonMetaVersionType, | ||
Vanilla, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct InstallerManifest { | ||
pub id: String, | ||
pub time: String, | ||
pub release_time: Option<String>, | ||
#[serde(rename = "type")] | ||
pub kind: PistonMetaVersionType, | ||
pub main_class: Option<String>, | ||
pub inherits_from: Option<String>, | ||
pub arguments: Arguments, | ||
pub libraries: Vec<InstallerLibraryFile>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Type, Default)] | ||
pub struct InstallerLibraryFile { | ||
pub name: String, | ||
pub sha1: Option<String>, | ||
pub size: Option<u64>, | ||
pub url: Option<String>, | ||
} | ||
|
||
impl InstallerLibraryFile { | ||
pub fn into_lib(self, maven: MavenRepo) -> LibraryFile { | ||
let mvn_url = self | ||
.url | ||
.clone() | ||
.map(MavenRepo::new) | ||
.unwrap_or(maven) | ||
.get_artifact_url(self.name.clone()); | ||
|
||
let url = if let Some(url) = self.url { | ||
if url.starts_with("http") { | ||
url | ||
} else { | ||
mvn_url | ||
} | ||
} else { | ||
mvn_url | ||
}; | ||
|
||
LibraryFile { | ||
path: Artifact::new(self.name).url(), | ||
url, | ||
size: self.size, | ||
sha1: self.sha1, | ||
} | ||
} | ||
} | ||
|
||
impl InstallerManifest { | ||
pub fn merge(&self, mut full: VersionManifest, maven: MavenRepo) -> VersionManifest { | ||
if let Some(main) = self.main_class.clone() { | ||
full.main_class = main; | ||
} | ||
|
||
if let Some(args) = &mut full.arguments { | ||
args.extend(self.arguments.clone()); | ||
} | ||
|
||
for lib in &self.libraries { | ||
full.libraries.push(Library { | ||
name: lib.name.clone(), | ||
rules: Vec::new(), | ||
|
||
downloads: LibraryDownloads { | ||
artifact: Some(lib.clone().into_lib(maven.clone())), | ||
classifiers: HashMap::new(), | ||
}, | ||
}); | ||
} | ||
|
||
full | ||
} | ||
|
||
pub async fn resolve( | ||
&self, | ||
inherits_from: String, | ||
maven: MavenRepo, | ||
) -> Result<VersionManifest> { | ||
Ok(self.merge(Vanilla.get_manifest_for(inherits_from).await?, maven)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod manifest; | ||
pub mod libs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
use anyhow::Result; | ||
use whcore::async_trait::async_trait; | ||
|
||
use crate::maven::artifact::Artifact; | ||
use crate::{maven::artifact::Artifact, vanilla::manifest::VersionManifest}; | ||
|
||
#[async_trait] | ||
pub trait LoaderData { | ||
async fn all_versions(&self) -> Result<Vec<Artifact>>; | ||
async fn versions_for(&self, game_version: impl AsRef<str> + Send) -> Result<Vec<Artifact>>; | ||
async fn version_jar_url(&self, version: impl Into<Artifact> + Send) -> Result<String>; | ||
|
||
async fn get_version_manifest( | ||
&self, | ||
version: impl Into<Artifact> + Send, | ||
game_version: impl AsRef<str> + Send, | ||
) -> Result<VersionManifest>; | ||
} |
Oops, something went wrong.