1mod detect;
2mod installer;
3mod profiled;
4
5use async_trait::async_trait;
6use elemental_core::{minecraft::MinecraftVersionId, runtime::RuntimeResolver};
7use elemental_infra::downloader::DownloadSession;
8use elemental_schema::forge::ForgeInstallerProfile;
9
10use crate::error::{Error, Result};
11use crate::families::{
12 installer::{
13 InstallerArtifact, installer_has_client_processors, run_installer_client_processors,
14 submit_installer_profile_libraries,
15 },
16 version_json::{
17 InstanceStorage, PreparedVersionJsonInstance, ResolvedVersionJsonInstance,
18 VersionJsonDownloadStatus, VersionJsonRemoteResolver,
19 },
20};
21use crate::launch::{RuntimeOptions, resolve_prepared_version_runtime};
22use crate::loader_version::LoaderVersionId;
23
24pub use crate::descriptors::DriverDescriptor;
25pub use crate::inspect::InstalledDriver;
26pub use detect::{InstalledInstance, InstanceDetectionFailure, InstanceProbe, inspect_instances};
27pub use installer::{InstallerFamilyDriver, ResolvedInstallerFamilyVersion};
28pub use profiled::{
29 ProfiledVersionJsonDriver, ProfiledVersionJsonFamily, ProfiledVersionJsonFamilyExt,
30};
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum PrepareStage {
34 ResolvingMetadata,
35 Downloading,
36 ExtractingNatives,
37 RunningProcessor {
38 family_name: String,
39 index: usize,
40 total: usize,
41 jar: String,
42 },
43 Ready,
44}
45
46#[derive(Debug, Clone)]
47pub struct InstallerExecutionPlan {
48 pub family_name: &'static str,
49 pub installer_artifact: InstallerArtifact,
50 pub install_profile: ForgeInstallerProfile,
51}
52
53#[derive(Debug)]
54pub struct ResolvedDriverPlan {
55 pub instance: ResolvedVersionJsonInstance,
56 pub remote_resolver: Box<dyn VersionJsonRemoteResolver>,
57 pub download_status: Option<VersionJsonDownloadStatus>,
58 pub installer: Option<InstallerExecutionPlan>,
59}
60
61pub struct PrepareRequest<'a> {
62 pub session: &'a DownloadSession,
63 pub instance: &'a InstanceStorage,
64 pub game_version: &'a MinecraftVersionId,
65 pub loader_version: Option<&'a LoaderVersionId>,
66 pub runtime_options: &'a RuntimeOptions,
67 pub runtime_resolver: &'a RuntimeResolver,
68 pub on_stage: Option<&'a (dyn Fn(PrepareStage) + Send + Sync)>,
69}
70
71impl<'a> PrepareRequest<'a> {
72 pub fn required_loader_version(&self, driver: DriverDescriptor) -> Result<&'a LoaderVersionId> {
73 self.loader_version
74 .ok_or_else(|| Error::LoaderVersionRequired {
75 driver: driver.id.to_owned(),
76 })
77 }
78
79 pub fn emit(&self, stage: PrepareStage) {
80 if let Some(observer) = self.on_stage {
81 observer(stage);
82 }
83 }
84}
85
86#[async_trait]
87pub trait Driver: Send + Sync {
88 fn descriptor(&self) -> DriverDescriptor;
89
90 async fn inspect(&self, probe: &InstanceProbe) -> Result<Option<InstalledDriver>>;
91
92 async fn resolve(&self, request: &PrepareRequest<'_>) -> Result<ResolvedDriverPlan>;
94
95 async fn verify_prepared(
97 &self,
98 _instance: &InstanceStorage,
99 _prepared: &PreparedVersionJsonInstance,
100 ) -> Result<()> {
101 Ok(())
102 }
103
104 async fn prepare(&self, request: &PrepareRequest<'_>) -> Result<PreparedVersionJsonInstance> {
109 request.emit(PrepareStage::ResolvingMetadata);
110 let plan = self.resolve(request).await?;
111 let resolver = plan.remote_resolver.as_ref();
112
113 let download_status = match plan.download_status {
114 Some(status) if plan.installer.is_none() && status.is_downloaded() => status,
115 _ => {
116 request.emit(PrepareStage::Downloading);
117 plan.instance
118 .submit_install_batches(resolver, request.session)
119 .await?;
120 if let Some(installer) = &plan.installer {
121 submit_installer_profile_libraries(
122 request.session,
123 &request.instance.parent,
124 &installer.install_profile,
125 installer.family_name,
126 &|raw_url, artifact_path| resolver.artifact_url(raw_url, artifact_path),
127 )
128 .await?;
129 }
130 request.session.success().await?;
131 plan.instance.download_status()?
132 }
133 };
134
135 request.emit(PrepareStage::ExtractingNatives);
136 let prepared = plan
137 .instance
138 .finish_preparation_with_download_status(download_status)
139 .await?;
140
141 if let Some(installer) = &plan.installer
142 && installer_has_client_processors(&installer.install_profile)
143 {
144 let runtime = resolve_prepared_version_runtime(
145 &prepared,
146 request.runtime_options,
147 request.runtime_resolver,
148 )
149 .await?;
150 let on_processor = |index: usize, total: usize, jar: &str| {
151 request.emit(PrepareStage::RunningProcessor {
152 family_name: installer.family_name.to_owned(),
153 index,
154 total,
155 jar: jar.to_owned(),
156 });
157 };
158 run_installer_client_processors(
159 &runtime,
160 request.instance,
161 &installer.installer_artifact,
162 &installer.install_profile,
163 installer.family_name,
164 request
165 .on_stage
166 .map(|_| &on_processor as &(dyn Fn(usize, usize, &str) + Send + Sync)),
167 )
168 .await?;
169 }
170
171 request.emit(PrepareStage::Ready);
172 Ok(prepared)
173 }
174
175 async fn load_prepared(
180 &self,
181 instance: &InstanceStorage,
182 ) -> Result<PreparedVersionJsonInstance> {
183 let prepared = ResolvedVersionJsonInstance::load(instance.clone())?
184 .finish_preparation()
185 .await?;
186 self.verify_prepared(instance, &prepared).await?;
187 Ok(prepared)
188 }
189}