Make the update checker work for non-mods (#3088)

* Fix https://github.com/modrinth/code/issues/1057

* Make sure mods use the installed loader

* Switch &PathBuf to &Path

* Clippy fix

* Deduplicate some code

---------

Co-authored-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Josiah Glosson
2024-12-28 21:23:27 -06:00
committed by GitHub
parent 8b7547ae38
commit 01fe08f079
4 changed files with 80 additions and 41 deletions

View File

@@ -1,5 +1,7 @@
use super::settings::{Hooks, MemorySettings, WindowSize};
use crate::state::{cache_file_hash, CacheBehaviour, CachedEntry};
use crate::state::{
cache_file_hash, CacheBehaviour, CachedEntry, CachedFileHash,
};
use crate::util;
use crate::util::fetch::{write_cached_icon, FetchSemaphore, IoSemaphore};
use crate::util::io::{self};
@@ -9,7 +11,7 @@ use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::path::{Path, PathBuf};
use std::path::Path;
// Represent a Minecraft instance.
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -146,7 +148,7 @@ pub struct FileMetadata {
pub version_id: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, Copy)]
#[derive(Serialize, Deserialize, Clone, Debug, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ProjectType {
Mod,
@@ -176,7 +178,7 @@ impl ProjectType {
}
}
pub fn get_from_parent_folder(path: PathBuf) -> Option<Self> {
pub fn get_from_parent_folder(path: &Path) -> Option<Self> {
// Get parent folder
let path = path.parent()?.file_name()?;
match path.to_str()? {
@@ -206,6 +208,15 @@ impl ProjectType {
}
}
pub fn get_loaders(&self) -> &'static [&'static str] {
match self {
ProjectType::Mod => &["fabric", "forge", "quilt", "neoforge"],
ProjectType::DataPack => &["datapack"],
ProjectType::ResourcePack => &["vanilla", "canvas", "minecraft"],
ProjectType::ShaderPack => &["iris", "optifine"],
}
}
pub fn iterator() -> impl Iterator<Item = ProjectType> {
[
ProjectType::Mod,
@@ -587,17 +598,10 @@ impl Profile {
let file_updates = file_hashes
.iter()
.filter_map(|x| {
all.iter().find(|prof| x.path.contains(&prof.path)).map(
|profile| {
format!(
"{}-{}-{}",
x.hash,
profile.loader.as_str(),
profile.game_version
)
},
)
.filter_map(|file| {
all.iter()
.find(|prof| file.path.contains(&prof.path))
.map(|profile| Self::get_cache_key(file, profile))
})
.collect::<Vec<_>>();
@@ -690,14 +694,7 @@ impl Profile {
let file_updates = file_hashes
.iter()
.map(|x| {
format!(
"{}-{}-{}",
x.hash,
self.loader.as_str(),
self.game_version
)
})
.map(|x| Self::get_cache_key(x, self))
.collect::<Vec<_>>();
let file_hashes_ref =
@@ -773,6 +770,18 @@ impl Profile {
Ok(files)
}
fn get_cache_key(file: &CachedFileHash, profile: &Profile) -> String {
format!(
"{}-{}-{}",
file.hash,
file.project_type
.filter(|x| *x != ProjectType::Mod)
.map(|x| x.get_loaders().join("+"))
.unwrap_or_else(|| profile.loader.as_str().to_string()),
profile.game_version
)
}
#[tracing::instrument(skip(pool))]
pub async fn add_project_version(
profile_path: &str,
@@ -873,8 +882,15 @@ impl Profile {
let project_path =
format!("{}/{}", project_type.get_folder(), file_name);
cache_file_hash(bytes.clone(), profile_path, &project_path, hash, exec)
.await?;
cache_file_hash(
bytes.clone(),
profile_path,
&project_path,
hash,
Some(project_type),
exec,
)
.await?;
util::fetch::write(&path.join(&project_path), &bytes, io_semaphore)
.await?;