feat(discovery): add CurseForge and modpack browsing
This commit is contained in:
1 parent
22d5da9f0d
commit
1742f62cd0
38 files changed
+2384
-135
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::io::Read;
|
||||
use std::io::{Read, Seek};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, LazyLock, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
@@ -227,6 +227,7 @@ pub fn fingerprint(path: &Path) -> Result<FileFingerprint, std::io::Error> {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut sha1 = sha1::Sha1::new();
|
||||
let mut sha512 = sha2::Sha512::new();
|
||||
let mut curseforge_len = 0u32;
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let read = file.read(&mut buffer)?;
|
||||
@@ -235,10 +236,21 @@ pub fn fingerprint(path: &Path) -> Result<FileFingerprint, std::io::Error> {
|
||||
}
|
||||
sha1.update(&buffer[..read]);
|
||||
sha512.update(&buffer[..read]);
|
||||
curseforge_len = curseforge_len.saturating_add(
|
||||
buffer[..read]
|
||||
.iter()
|
||||
.filter(|byte| !matches!(byte, b'\t' | b'\n' | b'\r' | b' '))
|
||||
.count() as u32,
|
||||
);
|
||||
}
|
||||
file.rewind()?;
|
||||
let hashes = BTreeMap::from([
|
||||
("sha1".to_owned(), format!("{:x}", sha1.finalize())),
|
||||
("sha512".to_owned(), format!("{:x}", sha512.finalize())),
|
||||
(
|
||||
"curseforge".to_owned(),
|
||||
curseforge_fingerprint(&mut file, curseforge_len)?.to_string(),
|
||||
),
|
||||
]);
|
||||
Ok(FileFingerprint {
|
||||
size: metadata.len(),
|
||||
@@ -247,6 +259,57 @@ pub fn fingerprint(path: &Path) -> Result<FileFingerprint, std::io::Error> {
|
||||
})
|
||||
}
|
||||
|
||||
fn curseforge_fingerprint(reader: &mut impl Read, length: u32) -> std::io::Result<u32> {
|
||||
const M: u32 = 0x5bd1e995;
|
||||
let mut hash = 1 ^ length;
|
||||
let mut block = [0u8; 4];
|
||||
let mut block_len = 0;
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let read = reader.read(&mut buffer)?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
for &byte in &buffer[..read] {
|
||||
if matches!(byte, b'\t' | b'\n' | b'\r' | b' ') {
|
||||
continue;
|
||||
}
|
||||
block[block_len] = byte;
|
||||
block_len += 1;
|
||||
if block_len == 4 {
|
||||
let mut value = u32::from_le_bytes(block);
|
||||
value = value.wrapping_mul(M);
|
||||
value ^= value >> 24;
|
||||
value = value.wrapping_mul(M);
|
||||
hash = hash.wrapping_mul(M) ^ value;
|
||||
block_len = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
match block_len {
|
||||
3 => {
|
||||
hash ^= u32::from(block[2]) << 16;
|
||||
hash ^= u32::from(block[1]) << 8;
|
||||
hash ^= u32::from(block[0]);
|
||||
hash = hash.wrapping_mul(M);
|
||||
}
|
||||
2 => {
|
||||
hash ^= u32::from(block[1]) << 8;
|
||||
hash ^= u32::from(block[0]);
|
||||
hash = hash.wrapping_mul(M);
|
||||
}
|
||||
1 => {
|
||||
hash ^= u32::from(block[0]);
|
||||
hash = hash.wrapping_mul(M);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
hash ^= hash >> 13;
|
||||
hash = hash.wrapping_mul(M);
|
||||
hash ^= hash >> 15;
|
||||
Ok(hash)
|
||||
}
|
||||
|
||||
pub fn fingerprint_metadata(path: &Path) -> Result<FileFingerprint, std::io::Error> {
|
||||
let metadata = std::fs::metadata(path)?;
|
||||
let modified_ns = metadata
|
||||
|
||||
@@ -182,6 +182,143 @@ impl ContentProvider for ModrinthProvider {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CurseForgeProvider {
|
||||
client: crate::net::HttpClient,
|
||||
api_key: String,
|
||||
}
|
||||
|
||||
impl CurseForgeProvider {
|
||||
pub fn new(client: crate::net::HttpClient, api_key: impl Into<String>) -> Self {
|
||||
Self {
|
||||
client,
|
||||
api_key: api_key.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ContentProvider for CurseForgeProvider {
|
||||
fn id(&self) -> &'static str {
|
||||
"curseforge"
|
||||
}
|
||||
|
||||
async fn search(
|
||||
&self,
|
||||
kind: ContentKind,
|
||||
query: &str,
|
||||
instance: &InstanceConfig,
|
||||
offset: usize,
|
||||
limit: usize,
|
||||
) -> Result<DiscoveryResults, crate::net::NetError> {
|
||||
crate::net::curseforge::search_discovery(
|
||||
&self.client,
|
||||
&self.api_key,
|
||||
kind,
|
||||
query,
|
||||
&instance.game_version,
|
||||
instance.loader,
|
||||
offset,
|
||||
limit,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn resolve_files(
|
||||
&self,
|
||||
files: &[FingerprintQuery],
|
||||
) -> Result<Vec<ResolvedFile>, crate::net::NetError> {
|
||||
let mut by_fingerprint = HashMap::<u32, Vec<String>>::new();
|
||||
for file in files {
|
||||
if let Some(fingerprint) = file
|
||||
.fingerprint
|
||||
.hash("curseforge")
|
||||
.and_then(|value| value.parse().ok())
|
||||
{
|
||||
by_fingerprint
|
||||
.entry(fingerprint)
|
||||
.or_default()
|
||||
.push(file.key.clone());
|
||||
}
|
||||
}
|
||||
let fingerprints = by_fingerprint.keys().copied().collect::<Vec<_>>();
|
||||
let matches = crate::net::curseforge::resolve_fingerprints(
|
||||
&self.client,
|
||||
&self.api_key,
|
||||
&fingerprints,
|
||||
)
|
||||
.await?;
|
||||
Ok(matches
|
||||
.into_iter()
|
||||
.flat_map(|(fingerprint, project_id, version_id)| {
|
||||
by_fingerprint
|
||||
.get(&fingerprint)
|
||||
.cloned()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(move |key| ResolvedFile {
|
||||
key,
|
||||
project: ProviderProject {
|
||||
provider: self.id().to_owned(),
|
||||
project_id: project_id.clone(),
|
||||
version_id: version_id.clone(),
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn project(
|
||||
&self,
|
||||
project_id: &str,
|
||||
) -> Result<crate::net::modrinth::ProjectInfo, crate::net::NetError> {
|
||||
crate::net::curseforge::fetch_project(&self.client, &self.api_key, project_id).await
|
||||
}
|
||||
|
||||
async fn compatible_versions(
|
||||
&self,
|
||||
project_id: &str,
|
||||
kind: ContentKind,
|
||||
game_version: &str,
|
||||
loader: ModLoader,
|
||||
) -> Result<Vec<VersionInfo>, crate::net::NetError> {
|
||||
crate::net::curseforge::fetch_versions(
|
||||
&self.client,
|
||||
&self.api_key,
|
||||
project_id,
|
||||
game_version,
|
||||
(kind == ContentKind::Mod).then_some(loader),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn icon(&self, url: &str) -> Result<Vec<u8>, crate::net::NetError> {
|
||||
self.client.get_bytes(url).await
|
||||
}
|
||||
|
||||
async fn download_version(
|
||||
&self,
|
||||
version: &VersionInfo,
|
||||
destination: &std::path::Path,
|
||||
installed_path: Option<&std::path::Path>,
|
||||
) -> Result<crate::net::modrinth::DownloadOutcome, crate::net::NetError> {
|
||||
let mut version = version.clone();
|
||||
crate::net::curseforge::ensure_download_url(&self.client, &self.api_key, &mut version)
|
||||
.await?;
|
||||
if let Some(installed_path) = installed_path {
|
||||
crate::net::modrinth::download_version_file_for_update(
|
||||
&self.client,
|
||||
&version,
|
||||
destination,
|
||||
installed_path,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
crate::net::modrinth::download_version_file(&self.client, &version, destination).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProviderRegistry {
|
||||
providers: Vec<Box<dyn ContentProvider>>,
|
||||
}
|
||||
@@ -193,6 +330,15 @@ impl ProviderRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn configured(client: crate::net::HttpClient) -> Self {
|
||||
let mut providers: Vec<Box<dyn ContentProvider>> =
|
||||
vec![Box::new(ModrinthProvider::new(client.clone()))];
|
||||
if let Some(api_key) = crate::config::SETTINGS.content.curseforge_api_key() {
|
||||
providers.push(Box::new(CurseForgeProvider::new(client, api_key)));
|
||||
}
|
||||
Self { providers }
|
||||
}
|
||||
|
||||
pub fn providers(&self) -> &[Box<dyn ContentProvider>] {
|
||||
&self.providers
|
||||
}
|
||||
@@ -204,6 +350,13 @@ impl ProviderRegistry {
|
||||
.map(Box::as_ref)
|
||||
.or_else(|| self.providers.first().map(Box::as_ref))
|
||||
}
|
||||
|
||||
pub fn get(&self, id: &str) -> Option<&dyn ContentProvider> {
|
||||
self.providers
|
||||
.iter()
|
||||
.find(|provider| provider.id() == id)
|
||||
.map(Box::as_ref)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -161,7 +161,7 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult {
|
||||
|
||||
match inventory {
|
||||
Ok(Ok((mut inventory, manifest_path))) => {
|
||||
let registry = ProviderRegistry::modrinth(client);
|
||||
let registry = ProviderRegistry::configured(client);
|
||||
task.set_action(format!("Identifying content for {instance_name}"));
|
||||
task.set_sub_action(format!("{} file(s) need matching", inventory.queries.len()));
|
||||
if !inventory.queries.is_empty() {
|
||||
@@ -365,6 +365,17 @@ fn reconcile_inventory(
|
||||
providers: Vec::new(),
|
||||
};
|
||||
}
|
||||
let curseforge_unchecked = crate::config::SETTINGS
|
||||
.content
|
||||
.curseforge_api_key()
|
||||
.is_some()
|
||||
&& provider_was_not_checked(&record.resolution, "curseforge");
|
||||
if !is_directory && !oversized && curseforge_unchecked {
|
||||
if record.fingerprint.hash("curseforge").is_none() {
|
||||
record.fingerprint = fingerprint(&path)?;
|
||||
}
|
||||
record.resolution = Resolution::Pending;
|
||||
}
|
||||
let should_query = !is_directory
|
||||
&& !oversized
|
||||
&& match &record.resolution {
|
||||
@@ -388,6 +399,14 @@ fn reconcile_inventory(
|
||||
Ok(Inventory { manifest, queries })
|
||||
}
|
||||
|
||||
fn provider_was_not_checked(resolution: &Resolution, provider: &str) -> bool {
|
||||
matches!(
|
||||
resolution,
|
||||
Resolution::Unmatched { providers, .. }
|
||||
if !providers.iter().any(|checked| checked == provider)
|
||||
)
|
||||
}
|
||||
|
||||
async fn resolve_queries(
|
||||
registry: &ProviderRegistry,
|
||||
queries: &[FingerprintQuery],
|
||||
@@ -455,10 +474,10 @@ async fn resolve_queries(
|
||||
project: project.clone(),
|
||||
},
|
||||
_ if !crate::config::SETTINGS.content.ask_on_provider_conflict => {
|
||||
let preferred = &crate::config::SETTINGS.content.preferred_provider;
|
||||
let preferred = crate::config::SETTINGS.content.preferred_provider();
|
||||
if let Some(project) = candidates
|
||||
.iter()
|
||||
.find(|project| &project.provider == preferred)
|
||||
.find(|project| project.provider == preferred)
|
||||
{
|
||||
Resolution::Resolved {
|
||||
project: project.clone(),
|
||||
|
||||
Reference in new issue
Block a user