elemental-addon 0.3.0

Composable, host-aware addon system for Elemental Minecraft instances.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::{borrow::Cow, path::PathBuf, sync::Arc};

use async_trait::async_trait;
use elemental_core::{
    auth::credential::Credential, minecraft::MinecraftVersionId, runtime::RuntimeResolver,
};
use elemental_driver::{
    catalog::GameVersion,
    driver::{DriverDescriptor, InstanceProbe},
    families::version_json::{InstanceStorage, MetadataPatch, PatchMode},
    launch::RuntimeOptions,
};
use elemental_infra::downloader::DownloadSession;
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};

/// Identifies an addon type by stable machine ID and human-readable display name.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AddonDescriptor {
    /// Stable machine-readable identifier (for example, `"liteloader"`).
    pub id: Cow<'static, str>,
    /// Human-readable display name.
    pub name: Cow<'static, str>,
}

impl AddonDescriptor {
    /// Creates a new addon descriptor from a machine ID and display name.
    pub fn new(id: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
        }
    }
}

/// Which patch modes an [`Addon`] is capable of supporting.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PatchModeSupport {
    /// Only supports launch-time virtual overlays (`vpatch`) and/or runtime hooks.
    VirtualOnly,
    /// Requires physical writes into the instance's `version.json`.
    PhysicalOnly,
    /// Supports both virtual overlays (`vpatch`) and physical `version.json` patching.
    #[default]
    Both,
}

/// Per-addon patch mode preference, monotonically constrained by the instance-level [`PatchMode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AddonPatchMode {
    /// Follow the instance's [`PatchMode`] policy (default).
    #[default]
    Inherit,
    /// Force virtual patching (`vpatch`) regardless of a `Physical` instance policy.
    Virtual,
    /// Request physical patching into `version.json` (rejected if instance policy is `Virtual`).
    Physical,
}

/// Resolves the effective [`PatchMode`] for an addon given the instance-level policy,
/// the addon's per-addon preference, and the addon's declared capability.
///
/// Enforces monotonic tightening: an addon can tighten a `Physical` instance policy down
/// to `Virtual`, but can never elevate a `Virtual` instance policy to `Physical`.
pub fn resolve_effective_patch_mode(
    addon_id: &str,
    instance_policy: PatchMode,
    addon_mode: AddonPatchMode,
    support: PatchModeSupport,
) -> Result<PatchMode> {
    if instance_policy == PatchMode::Virtual
        && (addon_mode == AddonPatchMode::Physical || support == PatchModeSupport::PhysicalOnly)
    {
        return Err(Error::PatchModeDisallowedByInstance {
            id: addon_id.to_owned(),
            instance_policy,
        });
    }

    match (addon_mode, support) {
        (AddonPatchMode::Physical, PatchModeSupport::VirtualOnly) => {
            Err(Error::PatchModeUnsupported {
                id: addon_id.to_owned(),
                requested: PatchMode::Physical,
                supported: "VirtualOnly".to_owned(),
            })
        }
        (AddonPatchMode::Virtual, PatchModeSupport::PhysicalOnly) => {
            Err(Error::PatchModeUnsupported {
                id: addon_id.to_owned(),
                requested: PatchMode::Virtual,
                supported: "PhysicalOnly".to_owned(),
            })
        }
        (AddonPatchMode::Virtual, _) => Ok(PatchMode::Virtual),
        (AddonPatchMode::Physical, _) => Ok(PatchMode::Physical),
        (AddonPatchMode::Inherit, PatchModeSupport::VirtualOnly) => Ok(PatchMode::Virtual),
        (AddonPatchMode::Inherit, PatchModeSupport::PhysicalOnly) => Ok(PatchMode::Physical),
        (AddonPatchMode::Inherit, PatchModeSupport::Both) => Ok(instance_policy),
    }
}

/// Whether an installed addon is managed by Elemental or detected from an external launcher.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AddonOrigin {
    /// Installed and tracked by Elemental in `.elemental/addons.json`.
    Elemental,
    /// Detected from an existing instance's metadata or files without an Elemental state record.
    External,
}

/// Result of checking whether an addon can be applied to a host driver and game version.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AddonCompatibility {
    /// The addon can be applied to the target host instance.
    Compatible,
    /// The addon cannot be applied to the target host instance.
    Incompatible {
        /// Human-readable explanation of why the addon is incompatible.
        reason: String,
    },
}

/// Read-only context passed to [`Addon::check_compatibility`].
pub struct AddonContext<'a> {
    /// Host driver installed on the target instance.
    pub driver: DriverDescriptor,
    /// Target Minecraft version, when known.
    pub game_version: Option<&'a MinecraftVersionId>,
    /// Storage handle for the target instance directory.
    pub instance: &'a InstanceStorage,
}

/// Inputs passed to [`Addon::prepare`].
pub struct AddonPrepareRequest<'a> {
    /// Active download session used to download addon artifacts.
    pub session: &'a DownloadSession,
    /// Storage handle for the target instance directory.
    pub instance: &'a InstanceStorage,
    /// Host driver installed on the target instance.
    pub driver: DriverDescriptor,
    /// Minecraft version of the target instance.
    pub game_version: &'a MinecraftVersionId,
    /// Requested addon version, if specified by the caller.
    pub addon_version: Option<&'a str>,
    /// Runtime options configured for installation/launch preparation.
    pub runtime_options: &'a RuntimeOptions,
    /// Runtime resolver for locating Java installations when needed.
    pub runtime_resolver: &'a RuntimeResolver,
}

/// Output produced by [`Addon::prepare`].
#[derive(Debug, Clone, Default)]
pub struct AddonPrepareOutput {
    /// Resolved addon version string to persist in instance state.
    pub version: Option<String>,
    /// Declarative metadata patch layered onto the instance's `PistonMetaData`.
    pub metadata_patch: Option<MetadataPatch>,
    /// Additional files created/managed by this addon (tracked for cleanup on removal).
    pub managed_files: Vec<PathBuf>,
}

/// Launch-time argument injections produced by [`Addon::augment_launch`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AddonLaunchArgs {
    /// Additional JVM flags appended before the main class.
    pub extra_jvm_arguments: Vec<String>,
    /// Additional game arguments appended after the host driver's game arguments.
    pub extra_game_arguments: Vec<String>,
}

/// Context passed to [`Addon::augment_launch`].
pub struct AddonLaunchContext<'a> {
    /// Storage handle for the instance being launched.
    pub instance: &'a InstanceStorage,
    /// Host driver of the instance being launched.
    pub driver: DriverDescriptor,
    /// Authenticated player credential for this launch.
    pub credential: &'a Credential,
    /// Persisted state record for this addon on the instance.
    pub record: &'a InstalledAddonRecord,
}

/// Summary of an addon installed or detected on an instance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InstalledAddon {
    /// Machine-readable addon identifier.
    pub id: String,
    /// Human-readable addon display name.
    pub name: String,
    /// Installed addon version, when known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Whether this addon is Elemental-managed or externally detected.
    pub origin: AddonOrigin,
    /// Effective patch mode resolved from instance policy and per-addon preference.
    #[serde(default)]
    pub effective_patch_mode: PatchMode,
    /// Whether launching requires executing [`Addon::augment_launch`] in code.
    #[serde(default)]
    pub requires_runtime_hook: bool,
}

/// Persisted state record for a single addon installed on an instance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledAddonRecord {
    /// Machine-readable addon identifier.
    pub id: String,
    /// Human-readable addon display name.
    pub name: String,
    /// Installed addon version, when recorded.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Origin indicating whether Elemental or an external launcher installed the addon.
    pub origin: AddonOrigin,
    /// Per-addon patch mode preference.
    #[serde(default)]
    pub patch_mode: AddonPatchMode,
    /// Patch mode capabilities declared by the addon when installed.
    #[serde(default)]
    pub patch_mode_support: PatchModeSupport,
    /// Whether this addon requires a launch-time hook via [`Addon::augment_launch`].
    #[serde(default)]
    pub requires_runtime_hook: bool,
    /// Instance-relative paths of files managed by this addon.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub managed_files: Vec<String>,
    /// Stored metadata patch applied in [`PatchMode::Virtual`] or [`PatchMode::Physical`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata_patch: Option<MetadataPatch>,
}

impl InstalledAddonRecord {
    /// Computes the effective [`PatchMode`] constrained by `instance_policy`.
    pub fn effective_patch_mode(&self, instance_policy: PatchMode) -> Result<PatchMode> {
        resolve_effective_patch_mode(
            &self.id,
            instance_policy,
            self.patch_mode,
            self.patch_mode_support,
        )
    }

    /// Converts this persisted record into a public [`InstalledAddon`] summary.
    pub fn to_installed(&self, instance_policy: PatchMode) -> InstalledAddon {
        let effective_patch_mode = self
            .effective_patch_mode(instance_policy)
            .unwrap_or(PatchMode::Virtual);
        InstalledAddon {
            id: self.id.clone(),
            name: self.name.clone(),
            version: self.version.clone(),
            origin: self.origin,
            effective_patch_mode,
            requires_runtime_hook: self.requires_runtime_hook,
        }
    }
}

/// A catalog entry describing an available version of an [`Addon`].
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AddonRelease {
    /// Addon release version string.
    pub version: String,
    /// Optional release channel (for example, `"release"` or `"snapshot"`).
    pub channel: Option<String>,
    /// Optional publication timestamp string from the upstream source.
    pub published_at: Option<String>,
    /// Whether the upstream catalog marks this release as a prerelease.
    pub prerelease: Option<bool>,
}

impl AddonRelease {
    /// Creates a release entry for the given addon `version`.
    pub fn new(version: impl Into<String>) -> Self {
        Self {
            version: version.into(),
            channel: None,
            published_at: None,
            prerelease: None,
        }
    }

    /// Sets the release channel label.
    pub fn with_channel(mut self, channel: Option<String>) -> Self {
        self.channel = channel;
        self
    }

    /// Sets the publication timestamp string.
    pub fn with_published_at(mut self, published_at: Option<String>) -> Self {
        self.published_at = published_at;
        self
    }

    /// Sets whether this release is a prerelease.
    pub fn with_prerelease(mut self, prerelease: Option<bool>) -> Self {
        self.prerelease = prerelease;
        self
    }
}

/// Optional host driver and game version filters for querying an addon's version catalog.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AddonCatalogQuery<'a> {
    /// Optional host driver filter to query compatible addon releases for.
    pub driver: Option<DriverDescriptor>,
    /// Optional Minecraft version filter to query compatible addon releases for.
    pub game_version: Option<&'a MinecraftVersionId>,
}

impl<'a> AddonCatalogQuery<'a> {
    /// Creates an unfiltered catalog query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters releases by host `driver`.
    pub fn with_driver(mut self, driver: DriverDescriptor) -> Self {
        self.driver = Some(driver);
        self
    }

    /// Filters releases by Minecraft `game_version`.
    pub fn with_game_version(mut self, game_version: &'a MinecraftVersionId) -> Self {
        self.game_version = Some(game_version);
        self
    }
}

/// Composable, host-aware extension layered onto a Minecraft instance.
#[async_trait]
pub trait Addon: Send + Sync {
    /// Returns the addon's unique identifier and display name.
    fn descriptor(&self) -> AddonDescriptor;

    /// Declares which patch modes this addon supports.
    fn patch_mode_support(&self) -> PatchModeSupport {
        PatchModeSupport::Both
    }

    /// Returns `true` when this addon requires code execution during launch
    /// command construction (`augment_launch`) and cannot degrade to data-only.
    fn requires_runtime_hook(&self) -> bool {
        false
    }

    /// Returns supported game versions when this addon exposes a remote catalog.
    async fn supported_game_versions(&self) -> Result<Option<Vec<GameVersion>>> {
        Ok(None)
    }

    /// Returns available addon releases for a host driver and game version, or
    /// `None` when this addon does not provide a version catalog.
    async fn available_versions(
        &self,
        _query: &AddonCatalogQuery<'_>,
    ) -> Result<Option<Vec<AddonRelease>>> {
        Ok(None)
    }

    /// Checks whether this addon is compatible with the host driver and game version.
    fn check_compatibility(&self, _ctx: &AddonContext<'_>) -> AddonCompatibility {
        AddonCompatibility::Compatible
    }

    /// Detects whether an unmanaged/external copy of this addon is already present in `probe`.
    async fn inspect(&self, _probe: &InstanceProbe) -> Result<Option<InstalledAddon>> {
        Ok(None)
    }

    /// Downloads any required artifacts and produces the metadata patch and managed files
    /// to persist for this addon.
    async fn prepare(&self, _request: &AddonPrepareRequest<'_>) -> Result<AddonPrepareOutput> {
        Ok(AddonPrepareOutput::default())
    }

    /// Augments the launch command with dynamic JVM or game arguments before spawning.
    async fn augment_launch(
        &self,
        _args: &mut AddonLaunchArgs,
        _ctx: &AddonLaunchContext<'_>,
    ) -> Result<()> {
        Ok(())
    }
}

#[async_trait]
impl<T: Addon + ?Sized> Addon for Arc<T> {
    fn descriptor(&self) -> AddonDescriptor {
        (**self).descriptor()
    }

    fn patch_mode_support(&self) -> PatchModeSupport {
        (**self).patch_mode_support()
    }

    fn requires_runtime_hook(&self) -> bool {
        (**self).requires_runtime_hook()
    }

    async fn supported_game_versions(&self) -> Result<Option<Vec<GameVersion>>> {
        (**self).supported_game_versions().await
    }

    async fn available_versions(
        &self,
        query: &AddonCatalogQuery<'_>,
    ) -> Result<Option<Vec<AddonRelease>>> {
        (**self).available_versions(query).await
    }

    fn check_compatibility(&self, ctx: &AddonContext<'_>) -> AddonCompatibility {
        (**self).check_compatibility(ctx)
    }

    async fn inspect(&self, probe: &InstanceProbe) -> Result<Option<InstalledAddon>> {
        (**self).inspect(probe).await
    }

    async fn prepare(&self, request: &AddonPrepareRequest<'_>) -> Result<AddonPrepareOutput> {
        (**self).prepare(request).await
    }

    async fn augment_launch(
        &self,
        args: &mut AddonLaunchArgs,
        ctx: &AddonLaunchContext<'_>,
    ) -> Result<()> {
        (**self).augment_launch(args, ctx).await
    }
}