use crate::consts;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AssetIndex {
#[serde(default)]
pub map_to_resources: bool,
pub objects: HashMap<String, AssetInfo>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AssetInfo {
pub hash: String,
pub size: usize,
}
impl AssetInfo {
pub fn asset_path(&self, assets_path: impl AsRef<Path>) -> PathBuf {
let mut asset_path = PathBuf::from(assets_path.as_ref());
asset_path.push(self.relative_asset_path());
asset_path
}
pub fn relative_asset_path(&self) -> PathBuf {
let mut asset_path = PathBuf::new();
asset_path.push("objects");
asset_path.push(self.hash.chars().take(2).collect::<String>().as_str());
asset_path.push(&self.hash);
asset_path
}
pub fn resource_path(key: &str, minecraft_path: impl AsRef<Path>) -> PathBuf {
let mut resource_path = PathBuf::from(minecraft_path.as_ref());
resource_path.push("resources");
resource_path.push(key);
resource_path
}
pub fn download_url(&self) -> String {
let mut url = vec![consts::MC_ASSETS_BASE_URL];
let part = self.hash.chars().take(2).collect::<String>();
url.push(part.as_str());
url.push(&self.hash);
url.join("/")
}
}