elemental_driver/
driver.rs1use std::str::FromStr;
2
3use async_trait::async_trait;
4use elemental_core::{
5 minecraft::MinecraftVersionId,
6 runtime::RuntimeResolver,
7 storage::{Storage, layout::Layout},
8};
9use elemental_infra::downloader::DownloadSession;
10use elemental_schema::mojang::piston::PistonMetaData;
11
12use crate::descriptors::from_id;
13use crate::error::{Error, Result};
14use crate::families::version_json::{
15 PistonMetaDataExt, PreparedVersionJsonInstance, VersionJsonInstanceLayout,
16 VersionJsonRootLayout,
17};
18use crate::inspect::InstanceProbe;
19use crate::launch::RuntimeOptions;
20use crate::loader_version::LoaderVersionId;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct DriverDescriptor {
24 pub id: &'static str,
25 pub name: &'static str,
26}
27
28impl TryFrom<&str> for DriverDescriptor {
29 type Error = Error;
30
31 fn try_from(id: &str) -> Result<Self> {
32 from_id(id).ok_or_else(|| Error::UnsupportedDriver { id: id.to_owned() })
33 }
34}
35
36impl TryFrom<String> for DriverDescriptor {
37 type Error = Error;
38
39 fn try_from(id: String) -> Result<Self> {
40 Self::try_from(id.as_str())
41 }
42}
43
44impl FromStr for DriverDescriptor {
45 type Err = Error;
46
47 fn from_str(id: &str) -> Result<Self> {
48 Self::try_from(id)
49 }
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct InstalledDriver {
54 pub driver: DriverDescriptor,
55 pub driver_version: Option<String>,
56 pub game_version: Option<MinecraftVersionId>,
57 pub description: Option<String>,
58}
59
60impl InstalledDriver {
61 pub fn version_json(
62 descriptor: DriverDescriptor,
63 metadata: &PistonMetaData,
64 driver_version: Option<String>,
65 ) -> Self {
66 Self {
67 driver: descriptor,
68 driver_version,
69 game_version: Some(MinecraftVersionId::from(metadata.game_version())),
70 description: Some(metadata.release_type.clone()),
71 }
72 }
73}
74
75pub struct PrepareRequest<'a, L, VL>
76where
77 L: VersionJsonRootLayout,
78 VL: VersionJsonInstanceLayout,
79{
80 pub session: &'a DownloadSession,
81 pub instance: &'a Storage<VL, Storage<L>>,
82 pub game_version: &'a MinecraftVersionId,
83 pub loader_version: Option<&'a LoaderVersionId>,
84 pub runtime_options: &'a RuntimeOptions,
85 pub runtime_resolver: &'a RuntimeResolver,
86}
87
88impl<'a, L, VL> PrepareRequest<'a, L, VL>
89where
90 L: VersionJsonRootLayout,
91 VL: VersionJsonInstanceLayout,
92{
93 pub fn required_loader_version(&self, driver: DriverDescriptor) -> Result<&'a LoaderVersionId> {
94 self.loader_version
95 .ok_or_else(|| Error::LoaderVersionRequired {
96 driver: driver.id.to_owned(),
97 })
98 }
99}
100
101#[async_trait(?Send)]
102pub trait Driver<L: Layout, VL: Layout> {
103 fn descriptor(&self) -> DriverDescriptor;
104
105 async fn inspect(&self, probe: &InstanceProbe) -> Result<Option<InstalledDriver>>;
106
107 async fn prepare(
112 &self,
113 request: &PrepareRequest<'_, L, VL>,
114 ) -> Result<PreparedVersionJsonInstance<L, VL>>
115 where
116 L: VersionJsonRootLayout + Clone,
117 VL: VersionJsonInstanceLayout + Clone;
118
119 async fn load_prepared(
124 &self,
125 instance: &Storage<VL, Storage<L>>,
126 ) -> Result<PreparedVersionJsonInstance<L, VL>>
127 where
128 L: VersionJsonRootLayout + Clone,
129 VL: VersionJsonInstanceLayout + Clone;
130}