1use std::{borrow::Cow, path::PathBuf, sync::Arc};
2
3use async_trait::async_trait;
4use elemental_core::{
5 auth::credential::Credential, minecraft::MinecraftVersionId, runtime::RuntimeResolver,
6};
7use elemental_driver::{
8 catalog::GameVersion,
9 driver::{DriverDescriptor, InstanceProbe},
10 families::version_json::{InstanceStorage, MetadataPatch, PatchMode},
11 launch::RuntimeOptions,
12};
13use elemental_infra::downloader::DownloadSession;
14use serde::{Deserialize, Serialize};
15
16use crate::error::{Error, Result};
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20pub struct AddonDescriptor {
21 pub id: Cow<'static, str>,
23 pub name: Cow<'static, str>,
25}
26
27impl AddonDescriptor {
28 pub fn new(id: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
30 Self {
31 id: id.into(),
32 name: name.into(),
33 }
34 }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
39#[serde(rename_all = "snake_case")]
40pub enum PatchModeSupport {
41 VirtualOnly,
43 PhysicalOnly,
45 #[default]
47 Both,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
52#[serde(rename_all = "lowercase")]
53pub enum AddonPatchMode {
54 #[default]
56 Inherit,
57 Virtual,
59 Physical,
61}
62
63pub fn resolve_effective_patch_mode(
69 addon_id: &str,
70 instance_policy: PatchMode,
71 addon_mode: AddonPatchMode,
72 support: PatchModeSupport,
73) -> Result<PatchMode> {
74 if instance_policy == PatchMode::Virtual
75 && (addon_mode == AddonPatchMode::Physical || support == PatchModeSupport::PhysicalOnly)
76 {
77 return Err(Error::PatchModeDisallowedByInstance {
78 id: addon_id.to_owned(),
79 instance_policy,
80 });
81 }
82
83 match (addon_mode, support) {
84 (AddonPatchMode::Physical, PatchModeSupport::VirtualOnly) => {
85 Err(Error::PatchModeUnsupported {
86 id: addon_id.to_owned(),
87 requested: PatchMode::Physical,
88 supported: "VirtualOnly".to_owned(),
89 })
90 }
91 (AddonPatchMode::Virtual, PatchModeSupport::PhysicalOnly) => {
92 Err(Error::PatchModeUnsupported {
93 id: addon_id.to_owned(),
94 requested: PatchMode::Virtual,
95 supported: "PhysicalOnly".to_owned(),
96 })
97 }
98 (AddonPatchMode::Virtual, _) => Ok(PatchMode::Virtual),
99 (AddonPatchMode::Physical, _) => Ok(PatchMode::Physical),
100 (AddonPatchMode::Inherit, PatchModeSupport::VirtualOnly) => Ok(PatchMode::Virtual),
101 (AddonPatchMode::Inherit, PatchModeSupport::PhysicalOnly) => Ok(PatchMode::Physical),
102 (AddonPatchMode::Inherit, PatchModeSupport::Both) => Ok(instance_policy),
103 }
104}
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
108#[serde(rename_all = "lowercase")]
109pub enum AddonOrigin {
110 Elemental,
112 External,
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
118pub enum AddonCompatibility {
119 Compatible,
121 Incompatible {
123 reason: String,
125 },
126}
127
128pub struct AddonContext<'a> {
130 pub driver: DriverDescriptor,
132 pub game_version: Option<&'a MinecraftVersionId>,
134 pub instance: &'a InstanceStorage,
136}
137
138pub struct AddonPrepareRequest<'a> {
140 pub session: &'a DownloadSession,
142 pub instance: &'a InstanceStorage,
144 pub driver: DriverDescriptor,
146 pub game_version: &'a MinecraftVersionId,
148 pub addon_version: Option<&'a str>,
150 pub runtime_options: &'a RuntimeOptions,
152 pub runtime_resolver: &'a RuntimeResolver,
154}
155
156#[derive(Debug, Clone, Default)]
158pub struct AddonPrepareOutput {
159 pub version: Option<String>,
161 pub metadata_patch: Option<MetadataPatch>,
163 pub managed_files: Vec<PathBuf>,
165}
166
167#[derive(Debug, Clone, Default, PartialEq, Eq)]
169pub struct AddonLaunchArgs {
170 pub extra_jvm_arguments: Vec<String>,
172 pub extra_game_arguments: Vec<String>,
174}
175
176pub struct AddonLaunchContext<'a> {
178 pub instance: &'a InstanceStorage,
180 pub driver: DriverDescriptor,
182 pub credential: &'a Credential,
184 pub record: &'a InstalledAddonRecord,
186}
187
188#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
190pub struct InstalledAddon {
191 pub id: String,
193 pub name: String,
195 #[serde(default, skip_serializing_if = "Option::is_none")]
197 pub version: Option<String>,
198 pub origin: AddonOrigin,
200 #[serde(default)]
202 pub effective_patch_mode: PatchMode,
203 #[serde(default)]
205 pub requires_runtime_hook: bool,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct InstalledAddonRecord {
211 pub id: String,
213 pub name: String,
215 #[serde(default, skip_serializing_if = "Option::is_none")]
217 pub version: Option<String>,
218 pub origin: AddonOrigin,
220 #[serde(default)]
222 pub patch_mode: AddonPatchMode,
223 #[serde(default)]
225 pub patch_mode_support: PatchModeSupport,
226 #[serde(default)]
228 pub requires_runtime_hook: bool,
229 #[serde(default, skip_serializing_if = "Vec::is_empty")]
231 pub managed_files: Vec<String>,
232 #[serde(default, skip_serializing_if = "Option::is_none")]
234 pub metadata_patch: Option<MetadataPatch>,
235}
236
237impl InstalledAddonRecord {
238 pub fn effective_patch_mode(&self, instance_policy: PatchMode) -> Result<PatchMode> {
240 resolve_effective_patch_mode(
241 &self.id,
242 instance_policy,
243 self.patch_mode,
244 self.patch_mode_support,
245 )
246 }
247
248 pub fn to_installed(&self, instance_policy: PatchMode) -> InstalledAddon {
250 let effective_patch_mode = self
251 .effective_patch_mode(instance_policy)
252 .unwrap_or(PatchMode::Virtual);
253 InstalledAddon {
254 id: self.id.clone(),
255 name: self.name.clone(),
256 version: self.version.clone(),
257 origin: self.origin,
258 effective_patch_mode,
259 requires_runtime_hook: self.requires_runtime_hook,
260 }
261 }
262}
263
264#[derive(Clone, Debug, PartialEq, Eq, Hash)]
266pub struct AddonRelease {
267 pub version: String,
269 pub channel: Option<String>,
271 pub published_at: Option<String>,
273 pub prerelease: Option<bool>,
275}
276
277impl AddonRelease {
278 pub fn new(version: impl Into<String>) -> Self {
280 Self {
281 version: version.into(),
282 channel: None,
283 published_at: None,
284 prerelease: None,
285 }
286 }
287
288 pub fn with_channel(mut self, channel: Option<String>) -> Self {
290 self.channel = channel;
291 self
292 }
293
294 pub fn with_published_at(mut self, published_at: Option<String>) -> Self {
296 self.published_at = published_at;
297 self
298 }
299
300 pub fn with_prerelease(mut self, prerelease: Option<bool>) -> Self {
302 self.prerelease = prerelease;
303 self
304 }
305}
306
307#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
309pub struct AddonCatalogQuery<'a> {
310 pub driver: Option<DriverDescriptor>,
312 pub game_version: Option<&'a MinecraftVersionId>,
314}
315
316impl<'a> AddonCatalogQuery<'a> {
317 pub fn new() -> Self {
319 Self::default()
320 }
321
322 pub fn with_driver(mut self, driver: DriverDescriptor) -> Self {
324 self.driver = Some(driver);
325 self
326 }
327
328 pub fn with_game_version(mut self, game_version: &'a MinecraftVersionId) -> Self {
330 self.game_version = Some(game_version);
331 self
332 }
333}
334
335#[async_trait]
337pub trait Addon: Send + Sync {
338 fn descriptor(&self) -> AddonDescriptor;
340
341 fn patch_mode_support(&self) -> PatchModeSupport {
343 PatchModeSupport::Both
344 }
345
346 fn requires_runtime_hook(&self) -> bool {
349 false
350 }
351
352 async fn supported_game_versions(&self) -> Result<Option<Vec<GameVersion>>> {
354 Ok(None)
355 }
356
357 async fn available_versions(
360 &self,
361 _query: &AddonCatalogQuery<'_>,
362 ) -> Result<Option<Vec<AddonRelease>>> {
363 Ok(None)
364 }
365
366 fn check_compatibility(&self, _ctx: &AddonContext<'_>) -> AddonCompatibility {
368 AddonCompatibility::Compatible
369 }
370
371 async fn inspect(&self, _probe: &InstanceProbe) -> Result<Option<InstalledAddon>> {
373 Ok(None)
374 }
375
376 async fn prepare(&self, _request: &AddonPrepareRequest<'_>) -> Result<AddonPrepareOutput> {
379 Ok(AddonPrepareOutput::default())
380 }
381
382 async fn augment_launch(
384 &self,
385 _args: &mut AddonLaunchArgs,
386 _ctx: &AddonLaunchContext<'_>,
387 ) -> Result<()> {
388 Ok(())
389 }
390}
391
392#[async_trait]
393impl<T: Addon + ?Sized> Addon for Arc<T> {
394 fn descriptor(&self) -> AddonDescriptor {
395 (**self).descriptor()
396 }
397
398 fn patch_mode_support(&self) -> PatchModeSupport {
399 (**self).patch_mode_support()
400 }
401
402 fn requires_runtime_hook(&self) -> bool {
403 (**self).requires_runtime_hook()
404 }
405
406 async fn supported_game_versions(&self) -> Result<Option<Vec<GameVersion>>> {
407 (**self).supported_game_versions().await
408 }
409
410 async fn available_versions(
411 &self,
412 query: &AddonCatalogQuery<'_>,
413 ) -> Result<Option<Vec<AddonRelease>>> {
414 (**self).available_versions(query).await
415 }
416
417 fn check_compatibility(&self, ctx: &AddonContext<'_>) -> AddonCompatibility {
418 (**self).check_compatibility(ctx)
419 }
420
421 async fn inspect(&self, probe: &InstanceProbe) -> Result<Option<InstalledAddon>> {
422 (**self).inspect(probe).await
423 }
424
425 async fn prepare(&self, request: &AddonPrepareRequest<'_>) -> Result<AddonPrepareOutput> {
426 (**self).prepare(request).await
427 }
428
429 async fn augment_launch(
430 &self,
431 args: &mut AddonLaunchArgs,
432 ctx: &AddonLaunchContext<'_>,
433 ) -> Result<()> {
434 (**self).augment_launch(args, ctx).await
435 }
436}