cobble_core/minecraft/models/
asset_index.rs1use crate::consts;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::path::{Path, PathBuf};
5
6#[derive(Clone, Debug, Deserialize, Serialize)]
8pub struct AssetIndex {
9 #[serde(default)]
13 pub map_to_resources: bool,
14 pub objects: HashMap<String, AssetInfo>,
18}
19
20#[derive(Clone, Debug, Deserialize, Serialize)]
22pub struct AssetInfo {
23 pub hash: String,
25 pub size: usize,
27}
28
29impl AssetInfo {
30 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 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 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 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}