elemental-driver 0.1.0

Driver library for Elemental, providing implementations for interacting with different modloaders and game versions.
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
437
438
439
440
441
442
443
444
445
446
447
448
use std::{
    fs::File,
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use elemental_core::storage::{Storage, layout::Layoutable};
use elemental_infra::jar::JarFile;
use elemental_schema::mojang::piston::{PistonMetaAssetIndexObjects, PistonMetaData};
use tokio::fs::create_dir_all;

use super::{
    layout::{VersionJsonInstanceLayout, VersionJsonRootLayout},
    resource::{VersionJsonInstanceResource, VersionJsonRootResource},
};
use crate::{
    driver::Driver,
    families::version_json::{
        extensions::PistonMetaLibrariesExt,
        rules::VersionJsonRuleContext,
        state::{NativesState, natives_state_store},
    },
    inspect::InstalledInstance,
};

#[async_trait(?Send)]
pub trait VersionJsonGameStorageExt {
    type Layout: VersionJsonRootLayout;

    async fn ensure_instance<VL>(
        &self,
        name: String,
        version_layout: VL,
    ) -> Result<Storage<VL, Storage<Self::Layout>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout + Send;
    fn instance_exists<VL>(&self, name: impl AsRef<str>, version_layout: &VL) -> Result<bool>
    where
        VL: VersionJsonInstanceLayout;
    fn instance<VL>(
        &self,
        name: impl Into<String>,
        version_layout: VL,
    ) -> Result<Storage<VL, Storage<Self::Layout>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout;
    fn instance_names<VL>(&self, version_layout: &VL) -> Result<Vec<String>>
    where
        VL: VersionJsonInstanceLayout;
    fn instances<VL>(&self, version_layout: VL) -> Result<Vec<Storage<VL, Storage<Self::Layout>>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout + Clone;
    async fn write_asset_index(
        &self,
        id: String,
        objects: &PistonMetaAssetIndexObjects,
    ) -> Result<()>;
    fn asset_index_objects(&self, id: impl AsRef<str>) -> Result<PistonMetaAssetIndexObjects>;
}

#[async_trait(?Send)]
impl<L> VersionJsonGameStorageExt for Storage<L>
where
    L: VersionJsonRootLayout,
{
    type Layout = L;

    async fn ensure_instance<VL>(
        &self,
        name: String,
        version_layout: VL,
    ) -> Result<Storage<VL, Storage<Self::Layout>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout + Send,
    {
        let instance_root = self.try_get_resource(VersionJsonRootResource::Versions(Some(name)))?;
        create_dir_all(&instance_root).await?;

        Ok(Storage::with_parent(
            instance_root,
            self.clone(),
            version_layout,
        ))
    }

    fn instance_exists<VL>(&self, name: impl AsRef<str>, version_layout: &VL) -> Result<bool>
    where
        VL: VersionJsonInstanceLayout,
    {
        let name = name.as_ref();
        let instance_root =
            self.try_get_resource(VersionJsonRootResource::Versions(Some(name.to_owned())))?;
        let metadata_path = version_layout
            .try_get_resource(&instance_root, VersionJsonInstanceResource::Metadata)?;
        let jar_path =
            version_layout.try_get_resource(&instance_root, VersionJsonInstanceResource::Jar)?;

        Ok(metadata_path.exists() && jar_path.exists())
    }

    fn instance<VL>(
        &self,
        name: impl Into<String>,
        version_layout: VL,
    ) -> Result<Storage<VL, Storage<Self::Layout>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout,
    {
        let name = name.into();
        if !self.instance_exists(&name, &version_layout)? {
            return Err(anyhow!("can't find a valid instance named '{name}'"));
        }
        let instance_root = self.try_get_resource(VersionJsonRootResource::Versions(Some(name)))?;

        Ok(Storage::with_parent(
            instance_root,
            self.clone(),
            version_layout,
        ))
    }

    fn instance_names<VL>(&self, version_layout: &VL) -> Result<Vec<String>>
    where
        VL: VersionJsonInstanceLayout,
    {
        let instances_root = self.try_get_resource(VersionJsonRootResource::Versions(None))?;
        if !instances_root.exists() {
            return Ok(Vec::new());
        }

        let mut instances = Vec::new();
        for entry in instances_root.read_dir()? {
            let entry = entry?;
            if !entry.file_type()?.is_dir() {
                continue;
            }

            let name = entry.file_name().to_string_lossy().to_string();
            if self.instance_exists(&name, version_layout)? {
                instances.push(name);
            }
        }

        Ok(instances)
    }

    fn instances<VL>(&self, version_layout: VL) -> Result<Vec<Storage<VL, Storage<Self::Layout>>>>
    where
        Self::Layout: Clone,
        VL: VersionJsonInstanceLayout + Clone,
    {
        self.instance_names(&version_layout)?
            .into_iter()
            .map(|name| self.instance(name, version_layout.clone()))
            .collect()
    }

    async fn write_asset_index(
        &self,
        id: String,
        objects: &PistonMetaAssetIndexObjects,
    ) -> Result<()> {
        let path = self.try_get_resource(VersionJsonRootResource::AssetIndexes(Some(id)))?;
        let parent = path
            .parent()
            .context("asset index path has no parent directory")?;
        create_dir_all(parent).await?;
        tokio::fs::write(path, serde_json::to_vec(objects)?).await?;
        Ok(())
    }

    fn asset_index_objects(&self, id: impl AsRef<str>) -> Result<PistonMetaAssetIndexObjects> {
        let path = self.try_get_resource(VersionJsonRootResource::AssetIndexes(Some(
            id.as_ref().to_owned(),
        )))?;
        Ok(serde_json::from_reader(File::open(path)?)?)
    }
}

#[async_trait(?Send)]
pub trait VersionJsonVersionStorageExt {
    type GameLayout: VersionJsonRootLayout;
    type VersionLayout: VersionJsonInstanceLayout;

    fn metadata(&self) -> Result<PistonMetaData>;
    async fn write_metadata(&self, metadata: &PistonMetaData) -> Result<()>;
    async fn natives_are_extracted(&self) -> bool;
    async fn ensure_platform_natives_path(&self) -> Result<PathBuf>;
    async fn extract_natives(&self) -> Result<()>;
}

#[async_trait(?Send)]
impl<L, VL> VersionJsonVersionStorageExt for Storage<VL, Storage<L>>
where
    L: VersionJsonRootLayout,
    VL: VersionJsonInstanceLayout,
{
    type GameLayout = L;
    type VersionLayout = VL;

    fn metadata(&self) -> Result<PistonMetaData> {
        let path = self.try_get_resource(VersionJsonInstanceResource::Metadata)?;
        Ok(serde_json::from_reader(File::open(path)?)?)
    }

    async fn write_metadata(&self, metadata: &PistonMetaData) -> Result<()> {
        self.ensure_root().await?;
        let path = self.try_get_resource(VersionJsonInstanceResource::Metadata)?;
        tokio::fs::write(path, serde_json::to_vec(metadata)?).await?;
        Ok(())
    }

    async fn natives_are_extracted(&self) -> bool {
        let metadata = match self.metadata() {
            Ok(metadata) => metadata,
            Err(_) => return false,
        };
        let rule_context = VersionJsonRuleContext::current();
        let natives_root = match self.try_get_resource(VersionJsonInstanceResource::Natives) {
            Ok(path) => path,
            Err(_) => return false,
        };
        let store = match natives_state_store(&self.path).await {
            Ok(store) => store,
            Err(_) => return false,
        };
        let state = store.cloned().await;
        if state.value.path.is_empty() {
            return false;
        }

        let expected_path = natives_root.to_string_lossy().to_string();
        if state.value.path != expected_path {
            return false;
        }

        let expected_artifacts = collect_native_artifact_paths(&metadata, &rule_context);
        if state.value.native_artifacts != expected_artifacts {
            return false;
        }

        if expected_artifacts.is_empty() {
            return true;
        }

        if state.value.extracted_files.is_empty() {
            return false;
        }

        state
            .value
            .extracted_files
            .iter()
            .all(|file| natives_root.join(file).exists())
    }

    async fn ensure_platform_natives_path(&self) -> Result<PathBuf> {
        let path = self.try_get_resource(VersionJsonInstanceResource::Natives)?;
        create_dir_all(&path).await?;
        Ok(path)
    }

    async fn extract_natives(&self) -> Result<()> {
        let metadata = self.metadata()?;
        let destination = self.try_get_resource(VersionJsonInstanceResource::Natives)?;
        let rule_context = VersionJsonRuleContext::current();
        std::fs::create_dir_all(&destination)?;

        tokio::task::block_in_place(|| -> Result<()> {
            let natives_directory = NativesDirectory::new(&destination);

            for library in &metadata.libraries {
                if !library.is_allowed(&rule_context) {
                    continue;
                }

                for artifact in library
                    .native_source_artifacts(rule_context.platform())
                    .into_iter()
                    .flatten()
                {
                    let source =
                        self.parent
                            .try_get_resource(VersionJsonRootResource::Libraries(Some(
                                PathBuf::from(artifact.path.as_str()),
                            )))?;
                    JarFile::new(source).extract_blocking(&destination)?;
                }
            }

            natives_directory.flatten_binaries()
        })?;
        let extracted_files = NativesDirectory::new(&destination)
            .root_binaries()?
            .into_iter()
            .filter_map(|path| {
                path.file_name()
                    .map(|name| name.to_string_lossy().to_string())
            })
            .collect::<Vec<String>>();
        let checked_at_unix_ms = current_unix_ms()?;
        let store = natives_state_store(&self.path).await?;
        store
            .set(|state| {
                state.value = NativesState {
                    path: destination.to_string_lossy().to_string(),
                    native_artifacts: collect_native_artifact_paths(&metadata, &rule_context),
                    extracted_files,
                    checked_at_unix_ms,
                };
            })
            .await?;

        Ok(())
    }
}

pub async fn inspect_instances<L, VL>(
    storage: &Storage<L>,
    version_layout: VL,
    drivers: &[&dyn Driver<L, VL>],
) -> Result<Vec<InstalledInstance<L, VL>>>
where
    L: VersionJsonRootLayout + Clone,
    VL: VersionJsonInstanceLayout + Clone,
{
    InstalledInstance::detect_all(storage, version_layout, drivers).await
}

fn current_unix_ms() -> Result<u64> {
    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system time is before unix epoch")?;
    Ok(duration.as_millis() as u64)
}

fn collect_native_artifact_paths(
    metadata: &PistonMetaData,
    rule_context: &VersionJsonRuleContext,
) -> Vec<String> {
    let mut paths = Vec::new();

    for library in &metadata.libraries {
        if !library.is_allowed(rule_context) {
            continue;
        }

        for artifact in library
            .native_source_artifacts(rule_context.platform())
            .into_iter()
            .flatten()
        {
            paths.push(artifact.path.clone());
        }
    }

    paths.sort();
    paths.dedup();
    paths
}

#[derive(Debug, Clone, Copy)]
struct NativesDirectory<'a> {
    root: &'a Path,
}

impl<'a> NativesDirectory<'a> {
    fn new(root: &'a Path) -> Self {
        Self { root }
    }

    fn flatten_binaries(self) -> Result<()> {
        for path in self.binaries()? {
            let Some(file_name) = path.file_name() else {
                continue;
            };
            let target = self.root.join(file_name);
            if target == path {
                continue;
            }

            std::fs::copy(&path, &target).with_context(|| {
                format!(
                    "copy native binary '{}' to '{}' failed",
                    path.display(),
                    target.display()
                )
            })?;
        }

        Ok(())
    }

    fn root_binaries(self) -> Result<Vec<PathBuf>> {
        let mut files = Vec::new();

        for entry in std::fs::read_dir(self.root)? {
            let entry = entry?;
            let path = entry.path();
            if entry.file_type()?.is_file() && Self::is_binary(&path) {
                files.push(path);
            }
        }

        Ok(files)
    }

    fn binaries(self) -> Result<Vec<PathBuf>> {
        let mut files = Vec::new();
        self.collect_binaries_into(self.root, &mut files)?;
        Ok(files)
    }

    fn collect_binaries_into(self, current: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
        for entry in std::fs::read_dir(current)? {
            let entry = entry?;
            let path = entry.path();
            if entry.file_type()?.is_dir() {
                self.collect_binaries_into(&path, files)?;
                continue;
            }

            if Self::is_binary(&path) {
                files.push(path);
            }
        }

        Ok(())
    }

    fn is_binary(path: &Path) -> bool {
        path.extension()
            .and_then(|extension| extension.to_str())
            .is_some_and(|extension| {
                matches!(
                    extension.to_ascii_lowercase().as_str(),
                    "dll" | "so" | "dylib" | "jnilib"
                )
            })
    }
}