refactor: 移除遥测并修复启动器流程

This commit is contained in:
Disy committed 2026-09-13 20:32:57 +08:00
1 parent 1fa56add19
commit 1593ac7a7c
175 files changed
+547 -12210

No files matched your search

@@ -18,8 +18,8 @@ use crate::state::{
Version, cache_file_hash, cache_file_hash_metadata,
};
use crate::util::fetch::{
self, ContentValidation, DownloadMeta, DownloadReason, DownloadRequest,
Integrity, ResourceClass, download_to_path,
self, ContentValidation, DownloadRequest, Integrity, ResourceClass,
download_to_path,
};
use crate::util::io;
use crate::util::io::io_error_with_lock_info;
@@ -307,7 +307,6 @@ pub(crate) async fn install_resolved_content_plan_with_reporter(
add_resolved_content_with_progress(
instance_id,
&plan.primary,
DownloadReason::Standalone,
false,
primary_progress,
state,
@@ -332,7 +331,6 @@ pub(crate) async fn install_resolved_content_plan_with_reporter(
add_resolved_content_with_progress(
instance_id,
dependency,
DownloadReason::Dependency,
true,
dependency_progress,
state,
@@ -424,8 +422,6 @@ pub(crate) async fn switch_project_version_with_dependencies(
let mut new_path = add_project_from_version(
instance_id,
&plan.primary.version_id,
DownloadReason::Update,
None,
ContentSourceKind::Local,
ownership_kind,
state,
@@ -442,14 +438,7 @@ pub(crate) async fn switch_project_version_with_dependencies(
installed_paths.push(new_path.clone());
for dependency in &plan.dependencies {
installed_paths.push(
add_resolved_content(
instance_id,
dependency,
DownloadReason::Dependency,
true,
state,
)
.await?,
add_resolved_content(instance_id, dependency, true, state).await?,
);
}
persist_resolved_plan_dependency_edges(
@@ -474,14 +463,12 @@ pub(crate) async fn switch_project_version_with_dependencies(
pub(crate) async fn add_resolved_content(
instance_id: &str,
content: &ResolvedContent,
reason: DownloadReason,
auto_dependency: bool,
state: &State,
) -> crate::Result<String> {
add_resolved_content_with_progress(
instance_id,
content,
reason,
auto_dependency,
None,
state,
@@ -492,7 +479,6 @@ pub(crate) async fn add_resolved_content(
async fn add_resolved_content_with_progress(
instance_id: &str,
content: &ResolvedContent,
reason: DownloadReason,
auto_dependency: bool,
progress: Option<ResolvedContentDownloadProgress>,
state: &State,
@@ -500,8 +486,6 @@ async fn add_resolved_content_with_progress(
let path = add_project_from_version_with_progress(
instance_id,
&content.version_id,
reason,
content.dependent_on_version_id.clone(),
ContentSourceKind::Local,
ContentOwnershipKind::UserAdded,
progress,
@@ -536,15 +520,8 @@ pub(crate) async fn install_resolved_dependency(
content: &ResolvedContent,
state: &State,
) -> crate::Result<String> {
add_resolved_content_with_progress(
instance_id,
content,
DownloadReason::Dependency,
true,
None,
state,
)
.await
add_resolved_content_with_progress(instance_id, content, true, None, state)
.await
}
pub(crate) async fn persist_resolved_plan_dependency_edges(
@@ -728,8 +705,6 @@ pub(crate) async fn resolve_content_scope(
pub(crate) async fn add_project_from_version(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
source_kind: ContentSourceKind,
ownership_kind: ContentOwnershipKind,
state: &State,
@@ -737,8 +712,6 @@ pub(crate) async fn add_project_from_version(
add_project_from_version_with_progress(
instance_id,
version_id,
reason,
dependent_on_version_id,
source_kind,
ownership_kind,
None,
@@ -751,22 +724,14 @@ pub(crate) async fn add_project_from_version(
pub(crate) async fn add_project_from_version_with_progress(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
source_kind: ContentSourceKind,
ownership_kind: ContentOwnershipKind,
progress: Option<ResolvedContentDownloadProgress>,
state: &State,
) -> crate::Result<String> {
let downloaded = download_project_version_with_progress(
instance_id,
version_id,
reason,
dependent_on_version_id,
progress,
state,
)
.await?;
let downloaded =
download_project_version_with_progress(version_id, progress, state)
.await?;
add_downloaded_project_version(
instance_id,
@@ -779,21 +744,10 @@ pub(crate) async fn add_project_from_version_with_progress(
}
pub(crate) async fn download_project_version(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
state: &State,
) -> crate::Result<DownloadedProjectVersion> {
download_project_version_with_progress(
instance_id,
version_id,
reason,
dependent_on_version_id,
None,
state,
)
.await
download_project_version_with_progress(version_id, None, state).await
}
/// Progress context for one file inside a multi-file content install.
@@ -807,38 +761,21 @@ pub(crate) struct ResolvedContentDownloadProgress {
}
pub(crate) async fn download_project_version_with_progress(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
progress: Option<ResolvedContentDownloadProgress>,
state: &State,
) -> crate::Result<DownloadedProjectVersion> {
download_project_version_with_reporting(
instance_id,
version_id,
reason,
dependent_on_version_id,
progress,
None,
state,
)
.await
download_project_version_with_reporting(version_id, progress, None, state)
.await
}
pub(crate) async fn download_project_version_with_reporter(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
reporter: crate::install::InstallProgressReporter,
state: &State,
) -> crate::Result<DownloadedProjectVersion> {
download_project_version_with_reporting(
instance_id,
version_id,
reason,
dependent_on_version_id,
None,
Some(reporter),
state,
@@ -847,26 +784,15 @@ pub(crate) async fn download_project_version_with_reporter(
}
async fn download_project_version_with_reporting(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
progress: Option<ResolvedContentDownloadProgress>,
reporter: Option<crate::install::InstallProgressReporter>,
state: &State,
) -> crate::Result<DownloadedProjectVersion> {
let prepared = prepare_version_download(
instance_id,
version_id,
reason,
dependent_on_version_id,
state,
)
.await?;
let prepared = prepare_version_download(version_id, state).await?;
let mut request =
DownloadRequest::new(&prepared.url, ResourceClass::Modrinth)
.with_integrity(prepared.integrity)
.with_download_meta(prepared.download_meta);
.with_integrity(prepared.integrity);
let tracking_reporter = progress
.as_ref()
.map(|progress| progress.reporter.clone())
@@ -961,7 +887,6 @@ async fn download_project_version_with_reporting(
struct PreparedVersionDownload {
url: String,
path: PathBuf,
download_meta: DownloadMeta,
integrity: Integrity,
file_name: String,
sha1: Option<String>,
@@ -973,22 +898,9 @@ struct PreparedVersionDownload {
/// Resolves the content scope and version metadata for a download and
/// validates the target path, without touching the network.
async fn prepare_version_download(
instance_id: &str,
version_id: &str,
reason: DownloadReason,
dependent_on_version_id: Option<String>,
state: &State,
) -> crate::Result<PreparedVersionDownload> {
let scope = resolve_content_scope(instance_id, None, state).await?;
let content_set =
content_rows::get_content_set(&scope.content_set_id, &state.pool)
.await?
.ok_or_else(|| {
crate::ErrorKind::InputError(format!(
"Unknown content set {}",
scope.content_set_id
))
})?;
let version = CachedEntry::get_version(
&ModrinthVersionId::new(version_id.to_string())?,
None,
@@ -1011,12 +923,6 @@ async fn prepare_version_download(
"No files for input version present!".to_string(),
)
})?;
let download_meta = DownloadMeta {
reason,
game_version: content_set.game_version,
loader: content_set.loader.as_str().to_string(),
dependent_on: dependent_on_version_id,
};
let file_name_path = Path::new(&file.filename);
if file_name_path.as_os_str().is_empty()
|| file_name_path.is_absolute()
@@ -1057,7 +963,6 @@ async fn prepare_version_download(
Ok(PreparedVersionDownload {
url: file.url.clone(),
path,
download_meta,
integrity,
file_name: file.filename.clone(),
sha1: file.hashes.get("sha1").cloned(),
@@ -6,7 +6,6 @@ use crate::state::{
CacheBehaviour, CachedEntry, ContentProviderRef, Dependency,
DependencyType, ModrinthVersionId, ProjectType, State, Version,
};
use crate::util::fetch::DownloadReason;
use futures::stream::{FuturesUnordered, StreamExt};
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
@@ -31,7 +30,6 @@ struct BulkUpdatePlan {
struct PlannedProjectUpdate {
relative_path: String,
project_id: String,
current_version_id: String,
update_version_id: String,
}
@@ -107,7 +105,6 @@ async fn apply_content_update(
let mut new_path = match update {
ContentUpdate::Modrinth {
project_id,
current_version_id,
update_version_id,
..
} => {
@@ -149,8 +146,6 @@ async fn apply_content_update(
add_project_from_version(
instance_id,
&plan.primary.version_id,
DownloadReason::Update,
Some(current_version_id.to_string()),
ContentSourceKind::Local,
ownership_kind,
state,
@@ -159,14 +154,8 @@ async fn apply_content_update(
);
for dependency in &plan.dependencies {
paths.push(
add_resolved_content(
instance_id,
dependency,
DownloadReason::Dependency,
true,
state,
)
.await?,
add_resolved_content(instance_id, dependency, true, state)
.await?,
);
}
persist_resolved_plan_dependency_edges(
@@ -449,10 +438,7 @@ async fn download_planned_projects(
match download {
PlannedDownload::ProjectUpdate(update) => {
let downloaded = download_project_version(
instance_id,
&update.update_version_id,
DownloadReason::Update,
Some(update.current_version_id.clone()),
state,
)
.await?;
@@ -462,14 +448,9 @@ async fn download_planned_projects(
))
}
PlannedDownload::DependencyAddition(dependency) => {
let downloaded = download_project_version(
instance_id,
&dependency.version_id,
DownloadReason::Dependency,
Some(dependency.parent_version_id.clone()),
state,
)
.await?;
let downloaded =
download_project_version(&dependency.version_id, state)
.await?;
Ok::<_, crate::Error>(
DownloadedBulkProject::DependencyAddition(
@@ -636,11 +617,10 @@ async fn plan_bulk_update(
let project_updates = updates
.into_iter()
.filter_map(|update| {
let (project_id, current, target) = update.modrinth_ids()?;
let (project_id, _, target) = update.modrinth_ids()?;
Some(PlannedProjectUpdate {
relative_path: update.relative_path().to_string(),
project_id: project_id.to_string(),
current_version_id: current.to_string(),
update_version_id: target.to_string(),
})
})
@@ -294,15 +294,8 @@ async fn fetch_icon_bytes(
return Ok(response.bytes().await?);
}
fetch::fetch(
icon_url,
None,
None,
None,
&state.fetch_semaphore,
&state.pool,
)
.await
fetch::fetch(icon_url, None, None, &state.fetch_semaphore, &state.pool)
.await
}
fn is_direct_cdn_icon_url(url: &str) -> bool {
@@ -507,47 +507,6 @@ pub(crate) async fn add_instance_recent_playtime(
Ok(())
}
pub(crate) async fn mark_instance_playtime_submitted(
instance_id: &str,
recent_time_played: u64,
pool: &SqlitePool,
) -> crate::Result<()> {
if recent_time_played == 0 {
return Ok(());
}
let recent_time_played =
playtime_to_storage(recent_time_played, "recent_time_played")?;
let max_playtime = i64::MAX;
let max_playtime_before_increment = max_playtime - recent_time_played;
let modified = Utc::now().timestamp();
sqlx::query!(
"
UPDATE instances
SET
submitted_time_played = CASE
WHEN submitted_time_played < 0 THEN ?
WHEN submitted_time_played > ? THEN ?
ELSE submitted_time_played + ?
END,
recent_time_played = 0,
modified = ?
WHERE id = ?
",
recent_time_played,
max_playtime_before_increment,
max_playtime,
recent_time_played,
modified,
instance_id,
)
.execute(pool)
.await?;
Ok(())
}
#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
@@ -20,8 +20,8 @@ use crate::state::{
ReleaseChannel, TeamMember, Version, VersionV3,
};
use crate::util::fetch::{
ContentValidation, DownloadMeta, DownloadReason, DownloadRequest,
FetchSemaphore, Integrity, ResourceClass, download_to_path, sha1_async,
ContentValidation, DownloadRequest, FetchSemaphore, Integrity,
ResourceClass, download_to_path, sha1_async,
};
use async_zip::tokio::read::fs::ZipFileReader;
use dashmap::DashMap;
@@ -507,7 +507,6 @@ pub(crate) async fn list_linked_modpack_content(
};
let ids = match get_modpack_identifiers(
version_id,
&resolved.content_set,
&state.pool,
&state.api_semaphore,
)
@@ -2520,7 +2519,6 @@ async fn get_cached_modpack_identifiers(
async fn get_modpack_identifiers(
version_id: &str,
content_set: &ContentSet,
pool: &SqlitePool,
fetch_semaphore: &FetchSemaphore,
) -> crate::Result<ModpackIdentifiers> {
@@ -2585,12 +2583,6 @@ async fn get_modpack_identifiers(
"No files found for modpack version {version_id}"
))
})?;
let download_meta = DownloadMeta {
reason: DownloadReason::Modpack,
game_version: content_set.game_version.clone(),
loader: content_set.loader.as_str().to_string(),
dependent_on: Some(version_id.to_string()),
};
let state = State::get().await?;
let file_name = Path::new(&primary_file.filename);
if file_name.components().count() != 1
@@ -2619,8 +2611,7 @@ async fn get_modpack_identifiers(
sha512: primary_file.hashes.get("sha512").cloned(),
content: ContentValidation::Jar,
..Integrity::default()
})
.with_download_meta(download_meta),
}),
&pack_path,
&state.download_semaphore,
pool,