diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f706e9348..8e0cbd373 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -214,6 +214,7 @@ use horizon_world::{ registry::BlockStateId, }; use std::{ + cell::RefCell, collections::{HashMap, HashSet}, path::PathBuf, sync::{ @@ -1387,7 +1388,7 @@ struct Game<'a> { map_patches: Vec, /// Player skin downloads, and which skin each atlas skin slot holds /// (least recently shown first). - skin_downloads: horizon_client::skins::Downloader, + skin_downloads: RefCell, skin_slots: Vec<(String, usize)>, cart_models: minecarts::CartModels, /// The boat the player rides. @@ -11627,11 +11628,11 @@ impl<'a> Game<'a> { breath_clouds: Vec::new(), dragon_model: prepared.dragon_model, map_patches: Vec::new(), - skin_downloads: horizon_client::skins::Downloader::new( + skin_downloads: RefCell::new(horizon_client::skins::Downloader::new( std::env::current_dir() .unwrap_or_default() .join("assets/skins"), - ), + )), skin_slots: Vec::new(), cart_models: prepared.cart_models, riding: None, diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 9f174c5ef..aa55d83df 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -700,9 +700,15 @@ impl Game<'_> { self.dim == super::Dim::Nether, )), }; + let profile = stack + .extra + .as_deref() + .and_then(|d| d.other.iter().find(|(k, _)| k == "minecraft:profile")) + .map(|(_, t)| horizon_client::scoreboard::tag_to_json(t)) + .unwrap_or(serde_json::Value::Null); self.pack.item_renderer.submit_special( &special, - &serde_json::Value::Null, + &profile, false, pose, &self.gui.atlas.sprites, diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs index 8e7176a70..1d79f077d 100644 --- a/crates/horizon-client/src/game/item_draw.rs +++ b/crates/horizon-client/src/game/item_draw.rs @@ -9,12 +9,13 @@ use horizon_assets::item_model::{ }; use horizon_client::{ block_entity_render::{diffuse_lights, vertex_light}, - hud::HudBatch, + hud::{HudBatch, Rect}, item_render::{self, ItemRenderState, SpecialHost, WorldSink}, items::ItemStack, mesher::Vertex, }; use serde_json::Value; +use std::{cell::RefCell, collections::HashMap}; /// The game's answers to what the resolver cannot read from the stack. pub(super) struct Env<'a, 'g> { @@ -126,6 +127,8 @@ pub(super) fn local_time(pattern: &str) -> String { /// Banner and shield pattern textures from the banner pattern registry. pub(super) struct Host<'a> { banners: &'a horizon_client::banner::Banners, + sprites: &'a HashMap, + skins: &'a RefCell, } impl SpecialHost for Host<'_> { fn banner_texture(&self, pattern: &str) -> Option { @@ -138,6 +141,18 @@ impl SpecialHost for Host<'_> { .pattern(pattern) .map(|_| self.banners.shield_texture(pattern)) } + fn player_skin(&self, profile: &Value) -> Option { + // SkullBlockRenderer.getPlayerSkin: SkinManager.getInsecureSkinInformation + // accepts a texture property without a signature, unlike the tab list. + let url = horizon_client::skins::profile_skin_url(profile)?; + let location = + horizon_client::skins::skin_location(&horizon_client::skins::texture_hash(&url)?); + let _ = self + .skins + .borrow_mut() + .state(horizon_client::skins::Kind::Skin, &url); + self.sprites.contains_key(&location).then_some(location) + } } /// `ItemOwner` of an entity holding or showing an item. @@ -177,6 +192,8 @@ impl Game<'_> { pub(super) fn item_host(&self) -> Host<'_> { Host { banners: &self.res.stations().banners, + sprites: &self.gui.atlas.sprites, + skins: &self.skin_downloads, } } /// The client level as item models read it. diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 7c96e7bad..5226377be 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -140,6 +140,8 @@ pub(super) struct Remote { tab_players: HashMap, /// Each listed profile's unpacked `textures` property. skin_textures: HashMap, + /// The skin texture each profile's players show, once its download is ready. + skins: HashMap, listed: std::collections::HashSet, /// `ClientboundLoginPacket.onlineMode`: the player list shows heads. online_mode: bool, @@ -355,6 +357,7 @@ impl Game<'_> { players: HashMap::new(), tab_players: HashMap::new(), skin_textures: HashMap::new(), + skins: HashMap::new(), listed: Default::default(), online_mode: false, level_players: HashMap::new(), @@ -2459,33 +2462,20 @@ impl Game<'_> { pub(super) fn remote_tab_players( &mut self, ) -> Option<(Vec, bool, Vec)> { - let r = self.remote.as_ref()?; - let mut players: Vec<_> = r - .listed - .iter() - .filter_map(|u| r.tab_players.get(u).cloned()) - .collect(); + self.update_remote_skins(); + let mut players: Vec<_> = { + let r = self.remote.as_ref()?; + r.listed + .iter() + .filter_map(|u| r.tab_players.get(u).cloned()) + .collect() + }; for p in &mut players { - let Some(textures) = r.skin_textures.get(&p.uuid) else { - continue; - }; - // PlayerInfo.createSkinLookup: only the local player's own skin - // may be unsigned. - let downloads = &mut self.skin_downloads; - let skin = horizon_client::skins::lookup(textures, p.uuid != r.profile, |kind, url| { - downloads.state(kind, url) - }); - if let Some(skin) = skin { - p.skin = skin; - } - } - for (location, rgba) in self.skin_downloads.take_ready() { - self.store_skin(location, rgba); - } - for p in &mut players { - if p.skin.starts_with("skins/") && !self.show_skin(&p.skin) { - p.skin = horizon_client::tab_list::default_skin(p.uuid).to_owned(); - } + p.skin = self + .remote + .as_ref() + .and_then(|r| r.skins.get(&p.uuid).cloned()) + .unwrap_or_else(|| horizon_client::tab_list::default_skin(p.uuid).to_owned()); } let r = self.remote.as_ref()?; let upside_down = players @@ -2498,9 +2488,68 @@ impl Game<'_> { .collect(); Some((players, r.online_mode, upside_down)) } + /// `PlayerInfo.getSkin` for every player entity, including NPCs that are + /// never in the tab list: resolve each profile's textures and give the + /// level's player models their skin. + fn update_remote_skins(&mut self) { + let Some(r) = self.remote.as_ref() else { + return; + }; + let local = r.profile; + let entries: Vec<(u128, horizon_client::skins::Textures)> = r + .skin_textures + .iter() + .map(|(uuid, textures)| (*uuid, textures.clone())) + .collect(); + let mut resolved: Vec<(u128, String)> = Vec::new(); + for (uuid, textures) in entries { + // PlayerInfo.createSkinLookup: only the local player's own skin + // may be unsigned. + let skin = { + let mut downloads = self.skin_downloads.borrow_mut(); + horizon_client::skins::lookup(&textures, uuid != local, |kind, url| { + downloads.state(kind, url) + }) + }; + if let Some(skin) = skin { + resolved.push((uuid, skin)); + } + } + resolved.retain(|(_, skin)| !skin.starts_with("skins/") || self.show_skin(skin)); + let Some((level, skins, own)) = self.remote.as_mut().map(|r| { + r.skins = resolved.into_iter().collect(); + ( + r.level_players + .iter() + .map(|(id, uuid)| (*id as u32, *uuid)) + .collect::>(), + r.skins.clone(), + r.skins.get(&r.profile).cloned(), + ) + }) else { + return; + }; + for (id, uuid) in level { + let Some(skin) = skins.get(&uuid) else { + continue; + }; + if let Some(mob) = self + .mobs + .iter_mut() + .find(|m| m.id == id && &*m.kind.id == "minecraft:player") + { + mob.skin = Some(skin.clone()); + } + } + if let Some(skin) = own + && let Some(mob) = self.player_model.as_mut() + { + mob.skin = Some(skin); + } + } /// Puts a downloaded skin in an atlas skin slot: a free one, else the /// least recently shown. - fn store_skin(&mut self, location: String, rgba: Vec) { + pub(super) fn store_skin(&mut self, location: String, rgba: Vec) { use horizon_client::hud::SKIN_SLOTS; let slot = if self.skin_slots.len() < SKIN_SLOTS { self.skin_slots.len() diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 99dae39a5..09a21eba0 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -599,6 +599,11 @@ impl Game<'_> { /// objective. pub(super) fn draw_tab_list(&mut self, batch: &mut HudBatch) { use horizon_client::tab_list::{self, TabInputs}; + // Player heads and the player list both draw from the same skin slots. + let ready: Vec<_> = self.skin_downloads.borrow_mut().take_ready(); + for (location, rgba) in ready { + self.store_skin(location, rgba); + } let key = self.screen == super::Screen::Playing && self.keys.contains(&winit::keyboard::KeyCode::Tab); let (players, online_mode, upside_down) = match self.remote_tab_players() { diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index e87032f07..3c2b34178 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -846,6 +846,9 @@ pub struct Mob { /// from the game), handed over with the next tick's events or drained /// by the game. pub outbox: Vec, + /// The skin texture of a player mob (`PlayerInfo.getSkin`), which + /// replaces the type's default when its download is ready. + pub skin: Option, } /// Events a mob tick asks the game to act on. #[derive(Clone, Debug)] @@ -1519,6 +1522,7 @@ impl Mob { anim: anim::AnimState::default(), rider: None, outbox: Vec::new(), + skin: None, attrs: Attrs { speed: kind.speed, attack_damage: kind.attack_damage, diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 4248f22c6..b8098a45a 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -104,7 +104,10 @@ pub fn body(m: &Mob) -> (String, String) { } }; if &*m.kind.id == "minecraft:player" { - return ("main".into(), m.kind.texture.to_string()); + return ( + "main".into(), + m.skin.clone().unwrap_or_else(|| m.kind.texture.clone()), + ); } match m.species() { S::Llama | S::TraderLlama => { diff --git a/crates/horizon-client/src/skins.rs b/crates/horizon-client/src/skins.rs index c47c9d47f..9014032c4 100644 --- a/crates/horizon-client/src/skins.rs +++ b/crates/horizon-client/src/skins.rs @@ -112,6 +112,24 @@ pub fn unpack(value: &str, signature: Signature, valid_uris: &[&str]) -> Texture out } +/// The skin URL of a `minecraft:profile` component's property list. Player +/// heads and mannequins read their profile this way, and `SkinManager` lets +/// those textures be unsigned (`getInsecureSkinInformation`). +pub fn profile_skin_url(profile: &serde_json::Value) -> Option { + let value = profile + .get("properties")? + .as_array()? + .iter() + .find_map(|p| { + if p.get("name")?.as_str()? == "textures" { + p.get("value")?.as_str() + } else { + None + } + })?; + unpack(value, Signature::Unsigned, &TEXTURE_URIS).skin +} + /// `MinecraftProfileTexture.getHash`: the base name of the URL's path. pub fn texture_hash(url: &str) -> Option { let rest = url.split_once("://")?.1; @@ -393,6 +411,21 @@ pub fn sha1(data: &[u8]) -> [u8; 20] { #[cfg(test)] mod tests { + #[test] + fn profile_components_supply_unsigned_head_skins() { + let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0="; + let profile = serde_json::json!({"properties": [{"name": "textures", "value": value}]}); + assert_eq!( + super::profile_skin_url(&profile).as_deref(), + Some("https://textures.minecraft.net/texture/abc123") + ); + let other = serde_json::json!({"properties": [{"name": "other", "value": value}]}); + assert_eq!(super::profile_skin_url(&other), None); + let bad = serde_json::json!({"properties": [{"name": "textures", "value": "not base64"}]}); + assert_eq!(super::profile_skin_url(&bad), None); + assert_eq!(super::profile_skin_url(&serde_json::json!({})), None); + } + /// Textures come over plain HTTP too (a local server here; https is the /// same request through rustls), and only 2xx answers count. #[test]