cobble_core/minecraft/models/
asset_index.rs

1use crate::consts;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::path::{Path, PathBuf};
5
6/// Map of all the assets for a Minecraft version.
7#[derive(Clone, Debug, Deserialize, Serialize)]
8pub struct AssetIndex {
9    /// Wether to install the assets as resources or not.
10    ///
11    /// Used primarily on older versions (pre1.6).
12    #[serde(default)]
13    pub map_to_resources: bool,
14    /// Asset objects.
15    ///
16    /// Key is the name of the file.
17    pub objects: HashMap<String, AssetInfo>,
18}
19
20/// Information of a single asset.
21#[derive(Clone, Debug, Deserialize, Serialize)]
22pub struct AssetInfo {
23    /// SHA1 of the asset file.
24    pub hash: String,
25    /// Size of the asset file.
26    pub size: usize,
27}
28
29impl AssetInfo {
30    /// Builds the complete path for the asset file.
31    pub fn asset_path(&self, assets_path: impl AsRef<Path>) -> PathBuf {
32        let mut asset_path = PathBuf::from(assets_path.as_ref());
33
34        asset_path.push(self.relative_asset_path());
35
36        asset_path
37    }
38
39    /// Builds the path for the asset file relative to the assets folder.
40    pub fn relative_asset_path(&self) -> PathBuf {
41        let mut asset_path = PathBuf::new();
42
43        asset_path.push("objects");
44        asset_path.push(self.hash.chars().take(2).collect::<String>().as_str());
45        asset_path.push(&self.hash);
46
47        asset_path
48    }
49
50    /// Builds the complete path for the asset file mapped as a resource.
51    pub fn resource_path(key: &str, minecraft_path: impl AsRef<Path>) -> PathBuf {
52        let mut resource_path = PathBuf::from(minecraft_path.as_ref());
53
54        resource_path.push("resources");
55        resource_path.push(key);
56
57        resource_path
58    }
59
60    /// Builds the download URL for the asset.
61    pub fn download_url(&self) -> String {
62        let mut url = vec![consts::MC_ASSETS_BASE_URL];
63
64        let part = self.hash.chars().take(2).collect::<String>();
65
66        url.push(part.as_str());
67        url.push(&self.hash);
68
69        url.join("/")
70    }
71}