Files
horizon-airdef/crates/horizon-client/src/skins.rs
T
Ludovic Marie 21ac444f28 refactor: block entity data moves down beside the chunk
Step A of the horizon-client crate split. `Chunk.block_entities` held
`containers::BlockEntity`, whose payloads were defined across containers,
brewing, signs, sculk, mobs::spawner, mobs::trial_spawner, vault, beehive
and command_block, so `chunk` reached most of the gameplay code.

The enum and every payload type now live in `chunk/block_entity.rs` as
plain data (definitions and derives). Every impl block stays in its
feature module, and each module re-exports its types under the old path,
so callers don't change. Also:

- `mobs::Who` moves to `horizon_client_core::who` (the vibration `Info`
  names it); mobs re-exports it.
- `skins::NameOrId` moves beside `SkullSkin`, which holds it; skins
  re-exports it.
- `Spawner.next`, the `Next` enum and `Selector.current` become
  `pub(crate)`, since their struct now lives in another module.
- The vault, spawner and trial_spawner harnesses record
  `chunk/block_entity.rs`.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:16:27 +01:00

1036 lines
39 KiB
Rust

//! Player skins from profile `textures` properties: authlib's
//! `MinecraftServicesSessionService.unpackTextures`, vanilla's
//! `SkinManager` lookup (which texture a player shows, and when the default
//! one) and `SkinTextureDownloader.processLegacySkin`.
/// `Property.hasSignature` and the services key check, as authlib's
/// `SignatureState`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Signature {
Unsigned,
Signed,
Invalid,
}
/// `MinecraftProfileTextures`: the texture URLs a profile names. Empty when
/// the payload doesn't decode or names a URL outside the allowed domains.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Textures {
pub skin: Option<String>,
pub cape: Option<String>,
pub elytra: Option<String>,
pub signed: bool,
}
/// The `getTexture` endpoint's valid URIs from Minecraft services discovery.
pub const TEXTURE_URIS: [&str; 2] = [
"https://textures.minecraft.net/texture/{textureId}",
"http://textures.minecraft.net/texture/{textureId}",
];
/// `java.util.Base64.getDecoder().decode`.
pub fn base64_decode(text: &str) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(text.len() / 4 * 3);
let (mut bits, mut shift) = (0u32, 18i32);
let bytes: Vec<char> = text.chars().collect();
let mut i = 0;
while i < bytes.len() {
let c = bytes[i];
i += 1;
let b = match c {
'A'..='Z' => c as u32 - 'A' as u32,
'a'..='z' => c as u32 - 'a' as u32 + 26,
'0'..='9' => c as u32 - '0' as u32 + 52,
'+' => 62,
'/' => 63,
'=' => {
if shift == 6 && (i == bytes.len() || bytes[i] != '=') || shift == 18 {
return None;
}
if shift == 6 {
i += 1;
}
break;
}
_ => return None,
};
bits |= b << shift;
shift -= 6;
if shift < 0 {
out.extend([(bits >> 16) as u8, (bits >> 8) as u8, bits as u8]);
shift = 18;
bits = 0;
}
}
match shift {
6 => out.push((bits >> 16) as u8),
0 => out.extend([(bits >> 16) as u8, (bits >> 8) as u8]),
12 => return None,
_ => {}
}
(i >= bytes.len()).then_some(out)
}
/// `unpackTextures(property)`: the textures of a `textures` property's
/// base64 JSON payload.
pub fn unpack(value: &str, signature: Signature, valid_uris: &[&str]) -> Textures {
let empty = Textures::default();
let Some(json) = base64_decode(value)
.map(|b| String::from_utf8_lossy(&b).into_owned())
.and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
else {
return empty;
};
let Some(textures) = json.get("textures").and_then(|t| t.as_object()) else {
return empty;
};
if textures.is_empty() {
return empty;
}
let mut out = Textures {
signed: signature == Signature::Signed,
..empty.clone()
};
for (kind, texture) in textures {
let Some(url) = texture.get("url").and_then(|u| u.as_str()) else {
return empty;
};
let allowed = valid_uris
.iter()
.any(|u| url.starts_with(&u.replace("{textureId}", "")));
if !allowed {
return empty;
}
let slot = match kind.as_str() {
"SKIN" => &mut out.skin,
"CAPE" => &mut out.cape,
"ELYTRA" => &mut out.elytra,
_ => continue,
};
*slot = Some(url.to_owned());
}
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<String> {
let (value, signature) = parse_profile(profile).textures?;
unpack(&value, signature, &TEXTURE_URIS).skin
}
/// `profile_skin_url` on a block entity's `profile` NBT (`SkullBlockEntity`):
/// the same `properties` list, held as a compound. A name-only profile (a
/// string tag) has no properties and no skin URL.
pub fn profile_skin_url_nbt(profile: &horizon_world::nbt::Tag) -> Option<String> {
let properties = match profile.get("properties")? {
horizon_world::nbt::Tag::List(list) => list,
_ => return None,
};
let value = properties.iter().find_map(|p| {
let name = nbt_text(p.get("name"))?;
(name == "textures")
.then(|| nbt_text(p.get("value")))
.flatten()
})?;
unpack(&value, Signature::Unsigned, &TEXTURE_URIS).skin
}
/// The profile's uuid as u128: `id` as an int array (block entity NBT) or a
/// uuid string (older and component forms). Used for `DefaultPlayerSkin.get`
/// when a head's skin is not downloaded yet.
pub fn profile_uuid_nbt(profile: &horizon_world::nbt::Tag) -> Option<u128> {
match profile.get("id")? {
horizon_world::nbt::Tag::IntArray(v) if v.len() == 4 => Some(
v.iter()
.fold(0u128, |a, &w| (a << 32) | u128::from(w as u32)),
),
horizon_world::nbt::Tag::String(s) => {
let digits: String = s.to_string_lossy().chars().filter(|c| *c != '-').collect();
u128::from_str_radix(&digits, 16).ok()
}
_ => None,
}
}
pub(crate) fn nbt_text(tag: Option<&horizon_world::nbt::Tag>) -> Option<String> {
match tag {
Some(horizon_world::nbt::Tag::String(s)) => Some(s.to_string_lossy()),
_ => None,
}
}
pub use crate::chunk::block_entity::NameOrId;
/// `GameProfile` as `fetchProfile` returns it: the account's id, name and
/// property map (its `textures` property is the skin).
#[derive(Clone, Debug)]
pub struct ResolvedProfile {
pub id: u128,
pub name: String,
/// `(name, value, signature)` properties.
pub properties: Vec<(String, String, Option<String>)>,
}
impl ResolvedProfile {
/// `SessionService.getPackedTextures`: the `textures` property.
pub fn textures(&self) -> Option<(String, Signature)> {
self.properties
.iter()
.find(|(n, _, _)| n == "textures")
.map(|(_, v, s)| {
(
v.clone(),
if s.is_some() {
Signature::Signed
} else {
Signature::Unsigned
},
)
})
}
}
/// Where a `Dynamic` profile's resolution stands (`resolveProfile`'s future).
#[derive(Clone, Debug)]
pub enum Resolve {
Pending,
/// The services' answer: `None` for no profile or no answer, which the
/// cache holds like any result (`Optional.empty`).
Done(Option<ResolvedProfile>),
}
/// `ProfileResolver.Cached`: resolves a `Dynamic` profile through the
/// services — `profileCacheByName` asks `profiles`/`getByName` for the id,
/// `profileCacheById` asks `session`/`getProfileById` for the profile —
/// at most 256 entries kept 10 minutes after their last use, fetched on
/// background threads.
pub struct Resolver {
entries: std::collections::HashMap<NameOrId, Entry>,
done: std::sync::mpsc::Receiver<(NameOrId, Option<ResolvedProfile>)>,
send: std::sync::mpsc::Sender<(NameOrId, Option<ResolvedProfile>)>,
last_sweep: std::time::Instant,
}
struct Entry {
touched: std::time::Instant,
/// `None` while the fetch is in flight.
resolved: Option<Option<ResolvedProfile>>,
}
/// `ProfileResolver.Cached`'s cache bounds.
const MAX_PROFILES: usize = 256;
const PROFILE_TTL: std::time::Duration = std::time::Duration::from_secs(10 * 60);
/// `StringUtil.isValidPlayerName` (the check `fetchByName` makes first).
fn valid_player_name(name: &str) -> bool {
name.encode_utf16().count() <= 16 && name.encode_utf16().all(|c| c > 32 && c < 127)
}
/// `fetchByNameOrId`: `getByName` answers `{id, name}`; `getProfileById`
/// (`fetchProfile(id, requireSecure)`) answers `{id, name, properties}` —
/// asked `unsigned=false` so the `textures` property is signed.
fn resolve_profile(key: &NameOrId, doc: &serde_json::Value) -> Option<ResolvedProfile> {
let id = match key {
NameOrId::Id(id) => *id,
NameOrId::Name(name) => {
let url = crate::auth::endpoint(doc, "profiles", "getByName")?
.replace("{name}", &name.to_lowercase());
let body = http_get(&url)?;
let v: serde_json::Value = serde_json::from_slice(&body).ok()?;
crate::auth::parse_uuid(v.get("id")?.as_str()?)?
}
};
let url = crate::auth::endpoint(doc, "session", "getProfileById")?
.replace("{profileId}", &format!("{id:032x}"));
let separator = if url.contains('?') { '&' } else { '?' };
let body = http_get(&format!("{url}{separator}unsigned=false"))?;
let v: serde_json::Value = serde_json::from_slice(&body).ok()?;
Some(ResolvedProfile {
id: crate::auth::parse_uuid(v.get("id")?.as_str()?)?,
name: v.get("name")?.as_str()?.to_owned(),
properties: v
.get("properties")?
.as_array()?
.iter()
.map(|p| {
Some((
p.get("name")?.as_str()?.to_owned(),
p.get("value")?.as_str()?.to_owned(),
p.get("signature")
.and_then(|s| s.as_str())
.map(str::to_owned),
))
})
.collect::<Option<Vec<_>>>()?,
})
}
impl Default for Resolver {
fn default() -> Self {
Self::new()
}
}
impl Resolver {
pub fn new() -> Self {
let (send, done) = std::sync::mpsc::channel();
Self {
entries: Default::default(),
done,
send,
last_sweep: std::time::Instant::now(),
}
}
/// `CacheBuilder.expireAfterAccess` with `maximumSize`.
fn sweep(&mut self) {
if self.last_sweep.elapsed() < std::time::Duration::from_secs(30) {
return;
}
self.last_sweep = std::time::Instant::now();
self.entries
.retain(|_, e| e.touched.elapsed() < PROFILE_TTL);
while self.entries.len() > MAX_PROFILES
&& let Some(oldest) = self
.entries
.iter()
.min_by_key(|(_, e)| e.touched)
.map(|(k, _)| k.clone())
{
self.entries.remove(&oldest);
}
}
/// How the resolution of `key` stands, starting it the first time.
pub fn state(&mut self, key: &NameOrId) -> Resolve {
while let Ok((key, resolved)) = self.done.try_recv() {
self.entries.insert(
key,
Entry {
touched: std::time::Instant::now(),
resolved: Some(resolved),
},
);
}
self.sweep();
if let Some(entry) = self.entries.get_mut(key) {
entry.touched = std::time::Instant::now();
return match &entry.resolved {
Some(resolved) => Resolve::Done(resolved.clone()),
None => Resolve::Pending,
};
}
// `fetchByName` refuses an invalid name without a fetch; like every
// other empty answer that is the cached result.
let resolved = matches!(key, NameOrId::Name(name) if !valid_player_name(name));
self.entries.insert(
key.clone(),
Entry {
touched: std::time::Instant::now(),
resolved: resolved.then_some(None),
},
);
if resolved {
return Resolve::Done(None);
}
let (key, send) = (key.clone(), self.send.clone());
std::thread::spawn(move || {
let resolved = crate::auth::discovery()
.ok()
.and_then(|doc| resolve_profile(&key, &doc));
let _ = send.send((key, resolved));
});
Resolve::Pending
}
}
/// `ResolvableProfile` (an item's `minecraft:profile` component, a skull's
/// `profile` tag): the `textures` property it carries, the key a `Dynamic`
/// profile resolves by, and `partialProfile.id` — the fallback skin's key.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ProfileInfo {
/// `GameProfile.id` (`DefaultPlayerSkin.get`).
pub id: u128,
/// The `textures` property's value and signature state, when present.
pub textures: Option<(String, Signature)>,
/// `Dynamic` profiles resolve by name or id.
pub dynamic: Option<NameOrId>,
}
/// A profile's `id`: `UUIDUtil.CODEC` (an int array of four, or a UUID
/// string).
fn profile_id(v: &serde_json::Value) -> Option<u128> {
match v {
serde_json::Value::Array(a) => {
let ints: Option<Vec<i64>> = a.iter().map(|x| x.as_i64()).collect();
let i = ints?;
(i.len() == 4).then(|| {
((i[0] as u32 as u128) << 96)
| ((i[1] as u32 as u128) << 64)
| ((i[2] as u32 as u128) << 32)
| i[3] as u32 as u128
})
}
serde_json::Value::String(s) => crate::auth::parse_uuid(s),
_ => None,
}
}
/// `ResolvableProfile`'s read of a `profile` value: a bare name is
/// `createUnresolved` (the `PLAYER_NAME` codec's alternative), a compound
/// `name` + `id` + `properties` (`Partial.MAP_CODEC` or a `GameProfile`).
pub fn parse_profile(v: &serde_json::Value) -> ProfileInfo {
if let serde_json::Value::String(name) = v {
return ProfileInfo {
id: horizon_protocol::crypto::offline_uuid(name),
textures: None,
dynamic: valid_player_name(name).then(|| NameOrId::Name(name.clone())),
};
}
let name = v.get("name").and_then(|n| n.as_str()).map(str::to_owned);
let id = v.get("id").and_then(profile_id);
let properties = v.get("properties").and_then(|p| p.as_array());
let textures = properties
.into_iter()
.flatten()
.find(|p| p.get("name").and_then(|n| n.as_str()) == Some("textures"))
.and_then(|p| {
Some((
p.get("value")?.as_str()?.to_owned(),
if p.get("signature").is_some() {
Signature::Signed
} else {
Signature::Unsigned
},
))
});
// `ResolvableProfile.create`: a Partial is Dynamic with no properties
// and exactly one of a name or an id.
let dynamic =
(properties.is_none_or(|p| p.is_empty()) && name.is_some() != id.is_some()).then(|| {
name.as_deref().map_or_else(
|| NameOrId::Id(id.unwrap()),
|n| NameOrId::Name(n.to_owned()),
)
});
ProfileInfo {
// `createPartialProfile`: the id, else the name's offline UUID, else nil.
id: id
.or_else(|| name.as_deref().map(horizon_protocol::crypto::offline_uuid))
.unwrap_or(0),
textures,
dynamic,
}
}
/// `ResolvableProfile.create`'s `Dynamic` key from a skull's `profile` tag:
/// a bare name string is `createUnresolved`, and a compound without
/// properties and with exactly one of a name or an id is `Dynamic`.
pub fn dynamic_nbt(profile: &horizon_world::nbt::Tag) -> Option<NameOrId> {
if let horizon_world::nbt::Tag::String(s) = profile {
let name = s.to_string_lossy();
return valid_player_name(&name).then_some(NameOrId::Name(name));
}
if matches!(profile.get("properties"), Some(horizon_world::nbt::Tag::List(l)) if !l.is_empty())
{
return None;
}
match (nbt_text(profile.get("name")), profile_uuid_nbt(profile)) {
(Some(name), None) => Some(NameOrId::Name(name)),
(None, Some(id)) => Some(NameOrId::Id(id)),
_ => None,
}
}
/// `PlayerSkinRenderCache.getOrDefault` for a head: the resolved skin once
/// its texture is in the atlas (`atlas`), else `partialProfile`'s default.
pub fn head_skin(
profile: &serde_json::Value,
resolver: &mut Resolver,
downloads: &mut Downloader,
atlas: impl Fn(&str) -> bool,
) -> String {
let p = parse_profile(profile);
let property = p.textures.or_else(|| {
let key = p.dynamic.as_ref()?;
match resolver.state(key) {
Resolve::Done(Some(profile)) => profile.textures(),
_ => None,
}
});
if let Some((value, signature)) = property
&& let Some(url) = unpack(&value, signature, &TEXTURE_URIS).skin
&& let Some(hash) = texture_hash(&url)
{
let location = skin_location(&hash);
match downloads.state(Kind::Skin, &url) {
Download::Ready if atlas(&location) => return location,
// Its slot was evicted: offer the image again.
Download::Ready => {
downloads.again(&location);
}
_ => {}
}
}
crate::tab_list::default_skin(p.id).to_owned()
}
/// `MinecraftProfileTexture.getHash`: the base name of the URL's path.
pub fn texture_hash(url: &str) -> Option<String> {
let rest = url.split_once("://")?.1;
let path = &rest[rest.find('/').unwrap_or(rest.len())..];
let path = &path[..path.find(['?', '#']).unwrap_or(path.len())];
let name = &path[path.rfind(['/', '\\']).map_or(0, |i| i + 1)..];
Some(name[..name.rfind('.').unwrap_or(name.len())].to_owned())
}
/// `SkinManager.TextureCache.registerTexture`: the texture id of a skin,
/// `skins/` and the SHA-1 of its hash's UTF-16 code units.
pub fn skin_location(hash: &str) -> String {
let units: Vec<u8> = hash.encode_utf16().flat_map(u16::to_le_bytes).collect();
let digest = sha1(&units);
let mut out = String::from("skins/");
for b in digest {
out.push_str(&format!("{b:02x}"));
}
out
}
/// Where a texture download stands.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Download {
Ready,
Pending,
Failed,
}
/// `MinecraftProfileTexture.Type`: which texture cache a download is in.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Kind {
Skin,
Cape,
Elytra,
}
/// `SkinManager.createLookup(profile, requireSecure).get()`: the skin
/// texture a player shows, or `None` for their default skin. `download`
/// tells how the skin, cape and elytra texture downloads stand.
pub fn lookup(
textures: &Textures,
require_secure: bool,
mut download: impl FnMut(Kind, &str) -> Download,
) -> Option<String> {
let mut skin = None;
let mut state = Download::Ready;
let urls = [
(Kind::Skin, &textures.skin),
(Kind::Cape, &textures.cape),
(Kind::Elytra, &textures.elytra),
];
for (kind, url) in urls {
let Some(url) = url else { continue };
// registerTexture computes the hash first; a bad URL fails the lookup.
let hash = texture_hash(url)?;
if kind == Kind::Skin {
skin = Some(skin_location(&hash));
}
match download(kind, url) {
Download::Failed => return None,
Download::Pending => state = Download::Pending,
Download::Ready => {}
}
}
if state != Download::Ready || require_secure && !textures.signed {
return None;
}
skin
}
/// `SkinTextureDownloader.processLegacySkin` on RGBA pixels: 64x32 skins
/// get the 64x64 layout, and the opaque parts lose their alpha. `None` for
/// any other size (the download fails).
pub fn process_legacy_skin(width: u32, height: u32, rgba: &[u8]) -> Option<Vec<u8>> {
if width != 64 || height != 32 && height != 64 {
return None;
}
let legacy = height == 32;
let mut img = vec![0u8; 64 * 64 * 4];
let n = rgba.len().min(img.len());
img[..n].copy_from_slice(&rgba[..n]);
let at = |x: i32, y: i32| ((y * 64 + x) * 4) as usize;
if legacy {
let rects = [
(4, 16, 16, 32, 4, 4),
(8, 16, 16, 32, 4, 4),
(0, 20, 24, 32, 4, 12),
(4, 20, 16, 32, 4, 12),
(8, 20, 8, 32, 4, 12),
(12, 20, 16, 32, 4, 12),
(44, 16, -8, 32, 4, 4),
(48, 16, -8, 32, 4, 4),
(40, 20, 0, 32, 4, 12),
(44, 20, -8, 32, 4, 12),
(48, 20, -16, 32, 4, 12),
(52, 20, -8, 32, 4, 12),
];
for (sx, sy, ox, oy, w, h) in rects {
for y in 0..h {
for x in 0..w {
let (s, d) = (at(sx + x, sy + y), at(sx + ox + w - 1 - x, sy + oy + y));
let p: [u8; 4] = img[s..s + 4].try_into().unwrap();
img[d..d + 4].copy_from_slice(&p);
}
}
}
}
let no_alpha = |img: &mut Vec<u8>, x0: i32, y0: i32, x1: i32, y1: i32| {
for x in x0..x1 {
for y in y0..y1 {
img[at(x, y) + 3] = 255;
}
}
};
no_alpha(&mut img, 0, 0, 32, 16);
if legacy {
// doNotchTransparencyHack: a fully opaque hat layer is cleared.
let opaque = (32..64).all(|x| (0..32).all(|y| img[at(x, y) + 3] >= 128));
if opaque {
for x in 32..64 {
for y in 0..32 {
img[at(x, y) + 3] = 0;
}
}
}
}
no_alpha(&mut img, 0, 16, 64, 32);
no_alpha(&mut img, 16, 48, 48, 64);
Some(img)
}
/// `SkinTextureDownloader`: fetches textures on background threads, from
/// the local cache (`<root>/<2 hex>/<sha1>`) or over HTTP, keeping each one
/// by kind and hash as vanilla's `TextureCache`s do. A texture that can't
/// be fetched fails, and the player keeps their default skin.
pub struct Downloader {
root: std::path::PathBuf,
states: std::collections::HashMap<(Kind, String), Download>,
done: std::sync::mpsc::Receiver<((Kind, String), Option<Vec<u8>>)>,
send: std::sync::mpsc::Sender<((Kind, String), Option<Vec<u8>>)>,
ready: Vec<(String, Vec<u8>)>,
/// Locations already in `ready`, so `again` does not double-queue.
queued: std::collections::HashSet<String>,
/// Decoded skin images by location, kept so a skin evicted from the
/// atlas uploads again (`again`); the disk cache backs anything dropped.
images: std::collections::HashMap<String, Vec<u8>>,
image_order: std::collections::VecDeque<String>,
}
/// `images` is bounded only so a long session on a busy server does not grow
/// without end; the disk cache holds everything it drops.
const MAX_SKIN_IMAGES: usize = 1024;
impl Downloader {
pub fn new(root: std::path::PathBuf) -> Self {
let (send, done) = std::sync::mpsc::channel();
Self {
root,
states: Default::default(),
done,
send,
ready: Vec::new(),
queued: Default::default(),
images: Default::default(),
image_order: Default::default(),
}
}
/// How the download of `url` stands, starting it the first time.
pub fn state(&mut self, kind: Kind, url: &str) -> Download {
while let Ok((key, image)) = self.done.try_recv() {
let state = match image {
Some(rgba) => {
if key.0 == Kind::Skin {
let location = skin_location(&key.1);
self.queue(location, rgba);
}
Download::Ready
}
None => Download::Failed,
};
self.states.insert(key, state);
}
let Some(hash) = texture_hash(url) else {
return Download::Failed;
};
let key = (kind, hash);
if let Some(state) = self.states.get(&key) {
return *state;
}
self.states.insert(key.clone(), Download::Pending);
let digest: String = skin_location(&key.1)["skins/".len()..].to_owned();
let file = self
.root
.join(if digest.len() > 2 { &digest[..2] } else { "xx" })
.join(&digest);
let (url, send) = (url.to_owned(), self.send.clone());
std::thread::spawn(move || {
let bytes = std::fs::read(&file).ok().or_else(|| {
let body = http_get(&url)?;
let _ = std::fs::create_dir_all(file.parent()?);
let _ = std::fs::write(&file, &body);
Some(body)
});
let image = bytes
.and_then(|b| crate::hud::decode_bytes(&b).ok())
.and_then(|(w, h, rgba)| match kind {
Kind::Skin => process_legacy_skin(w, h, &rgba),
_ => Some(rgba),
});
let _ = send.send((key, image));
});
Download::Pending
}
/// Keeps a decoded skin for `again`, dropping the oldest past the bound.
fn remember(&mut self, location: String, rgba: Vec<u8>) {
if !self.images.contains_key(&location) {
self.image_order.push_back(location.clone());
while self.image_order.len() > MAX_SKIN_IMAGES
&& let Some(old) = self.image_order.pop_front()
{
self.images.remove(&old);
}
}
self.images.insert(location, rgba);
}
/// An image off to the atlas: remembered for `again` and marked queued
/// so nothing uploads the same location twice.
fn queue(&mut self, location: String, rgba: Vec<u8>) {
self.remember(location.clone(), rgba.clone());
self.queued.insert(location.clone());
self.ready.push((location, rgba));
}
/// A ready skin whose atlas slot was evicted (`show_skin` answered no):
/// its image for upload again, from memory or the disk cache.
pub fn again(&mut self, location: &str) -> bool {
if self.queued.contains(location) {
return true;
}
let Some(digest) = location.strip_prefix("skins/") else {
return false;
};
let image = self.images.get(location).cloned().or_else(|| {
let file = self
.root
.join(if digest.len() > 2 { &digest[..2] } else { "xx" })
.join(digest);
let (w, h, rgba) = crate::hud::decode_bytes(&std::fs::read(&file).ok()?).ok()?;
process_legacy_skin(w, h, &rgba)
});
let Some(image) = image else {
return false;
};
self.queue(location.to_owned(), image);
true
}
/// Skins that finished since the last call: texture id and 64x64 RGBA.
pub fn take_ready(&mut self) -> Vec<(String, Vec<u8>)> {
for (location, _) in &self.ready {
self.queued.remove(location);
}
std::mem::take(&mut self.ready)
}
}
/// A GET over HTTP or HTTPS (textures.minecraft.net serves both; vanilla
/// uses https): the body of a 2xx answer, 16 MiB at most, after redirects.
fn http_get(url: &str) -> Option<Vec<u8>> {
use std::io::Read;
let response = ureq::AgentBuilder::new()
.timeout(std::time::Duration::from_secs(15))
.user_agent("Java")
.build()
.get(url)
.call()
.ok()?;
let mut body = Vec::new();
response
.into_reader()
.take(16 << 20)
.read_to_end(&mut body)
.ok()?;
Some(body)
}
/// SHA-1 (FIPS 180-4).
pub fn sha1(data: &[u8]) -> [u8; 20] {
let mut h: [u32; 5] = [
0x6745_2301,
0xEFCD_AB89,
0x98BA_DCFE,
0x1032_5476,
0xC3D2_E1F0,
];
let mut msg = data.to_vec();
msg.push(0x80);
while msg.len() % 64 != 56 {
msg.push(0);
}
msg.extend((data.len() as u64 * 8).to_be_bytes());
for block in msg.as_chunks::<64>().0 {
let mut w = [0u32; 80];
for (i, word) in block.as_chunks::<4>().0.iter().enumerate() {
w[i] = u32::from_be_bytes(*word);
}
for i in 16..80 {
w[i] = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]).rotate_left(1);
}
let [mut a, mut b, mut c, mut d, mut e] = h;
for (i, wi) in w.iter().enumerate() {
let (f, k) = match i {
0..=19 => ((b & c) | (!b & d), 0x5A82_7999),
20..=39 => (b ^ c ^ d, 0x6ED9_EBA1),
40..=59 => ((b & c) | (b & d) | (c & d), 0x8F1B_BCDC),
_ => (b ^ c ^ d, 0xCA62_C1D6),
};
let t = a
.rotate_left(5)
.wrapping_add(f)
.wrapping_add(e)
.wrapping_add(k)
.wrapping_add(*wi);
e = d;
d = c;
c = b.rotate_left(30);
b = a;
a = t;
}
for (x, y) in h.iter_mut().zip([a, b, c, d, e]) {
*x = x.wrapping_add(y);
}
}
let mut out = [0u8; 20];
for (i, x) in h.iter().enumerate() {
out[i * 4..i * 4 + 4].copy_from_slice(&x.to_be_bytes());
}
out
}
#[cfg(test)]
mod tests {
#[test]
fn profiles_parse_like_resolvable_profiles() {
use super::{NameOrId, Signature, parse_profile};
// A bare name is `createUnresolved`: dynamic by name, and its partial
// id is the name's offline UUID.
let p = parse_profile(&serde_json::json!("Notch"));
assert_eq!(p.dynamic, Some(NameOrId::Name("Notch".into())));
assert_eq!(p.id, horizon_protocol::crypto::offline_uuid("Notch"));
assert!(p.textures.is_none());
// A name-only compound resolves the same way.
let p = parse_profile(&serde_json::json!({"name": "Notch"}));
assert_eq!(p.dynamic, Some(NameOrId::Name("Notch".into())));
// An id-only compound resolves by id (int array or UUID string).
let id = 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef_u128;
let ints: Vec<i64> = [
(id >> 96) as i32 as i64,
(id >> 64) as i32 as i64,
(id >> 32) as i32 as i64,
id as i32 as i64,
]
.into();
let p = parse_profile(&serde_json::json!({"id": ints}));
assert_eq!(p.dynamic, Some(NameOrId::Id(id)));
let p = parse_profile(&serde_json::json!({"id": "01234567-89ab-cdef-0123-4567abcdef00"}));
assert_eq!(
p.dynamic,
Some(NameOrId::Id(0x0123_4567_89ab_cdef_0123_4567_abcd_ef00))
);
// Name and id but no properties is Static: it never resolves, and its
// default skin keys off the id.
let p = parse_profile(&serde_json::json!({"name": "Notch", "id": ints}));
assert!(p.dynamic.is_none());
assert_eq!(p.id, id);
// A `textures` property makes it Static (unsigned and signed).
let p = parse_profile(&serde_json::json!({
"properties": [{"name": "textures", "value": "v"}]
}));
assert!(p.dynamic.is_none());
assert_eq!(p.textures, Some(("v".into(), Signature::Unsigned)));
let p = parse_profile(&serde_json::json!({
"properties": [{"name": "textures", "value": "v", "signature": "s"}]
}));
assert_eq!(p.textures, Some(("v".into(), Signature::Signed)));
// Empty properties leave it dynamic.
let p = parse_profile(&serde_json::json!({"name": "Notch", "properties": []}));
assert_eq!(p.dynamic, Some(NameOrId::Name("Notch".into())));
}
/// `getByName` and `getProfileById` answer from the services the
/// discovery document names; a profile resolves through both.
#[test]
fn profiles_resolve_through_the_services() {
use std::io::{BufRead, BufReader, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
std::thread::spawn(move || {
let reply = |status: &str, body: &str| {
format!(
"HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
)
};
for stream in listener.incoming().take(3) {
let mut stream = stream.unwrap();
let mut line = String::new();
let mut reader = BufReader::new(stream.try_clone().unwrap());
reader.read_line(&mut line).unwrap();
while reader.read_line(&mut String::new()).unwrap() > 2 {}
let body = if line.contains("/users/profiles/minecraft/") {
r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch"}"#
} else if line.contains("/session/minecraft/profile/")
&& line.contains("unsigned=false")
{
r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch","properties":[{"name":"textures","value":"dmFsdWU=","signature":"c2ln"}]}"#
} else {
"{}"
};
stream.write_all(reply("200 OK", body).as_bytes()).unwrap();
}
});
let doc = serde_json::json!({"discovery": {
"profiles": {"endpoints": {"getByName": {"uri": format!("{base}/users/profiles/minecraft/{{name}}")}}},
"session": {"endpoints": {"getProfileById": {"uri": format!("{base}/session/minecraft/profile/{{profileId}}")}}},
}});
let resolved =
super::resolve_profile(&super::NameOrId::Name("Notch".into()), &doc).unwrap();
assert_eq!(resolved.id, 0x069a_79f4_44e9_4726_a5be_fca9_0e38_aaf5);
assert_eq!(resolved.name, "Notch");
assert_eq!(
resolved.properties,
vec![(
"textures".to_owned(),
"dmFsdWU=".to_owned(),
Some("c2ln".to_owned())
)]
);
let by_id = super::resolve_profile(&super::NameOrId::Id(resolved.id), &doc).unwrap();
assert_eq!(by_id.name, "Notch");
// A discovery without `profiles`/`getByName` resolves no name.
let doc = serde_json::json!({"discovery": {
"session": {"endpoints": {"getProfileById": {"uri": format!("{base}/session/minecraft/profile/{{profileId}}")}}},
}});
assert!(super::resolve_profile(&super::NameOrId::Name("Notch".into()), &doc).is_none());
}
/// A skin evicted from the atlas re-uploads (`again`), once per ask.
#[test]
fn evicted_skins_upload_again() {
let mut d = super::Downloader::new(std::env::temp_dir().join("horizon-test-skins"));
d.remember("skins/abc".into(), vec![1, 2, 3]);
assert!(d.again("skins/abc"));
assert!(!d.again("skins/missing"));
// Once queued, more asks do not pile up uploads.
assert!(d.again("skins/abc"));
assert_eq!(
d.take_ready(),
vec![("skins/abc".to_owned(), vec![1, 2, 3])]
);
assert!(d.take_ready().is_empty());
assert!(d.again("skins/abc"));
assert_eq!(d.take_ready().len(), 1);
// The first upload marks the location queued too: an `again` before
// `take_ready` does not put the same skin in the atlas twice.
d.queue("skins/def".into(), vec![4]);
assert!(d.again("skins/def"));
assert_eq!(d.take_ready(), vec![("skins/def".to_owned(), vec![4])]);
}
#[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]
fn http_get_reads_2xx_bodies() {
use std::io::{BufRead, BufReader, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
std::thread::spawn(move || {
for stream in listener.incoming().take(2) {
let mut stream = stream.unwrap();
let mut line = String::new();
let mut reader = BufReader::new(stream.try_clone().unwrap());
reader.read_line(&mut line).unwrap();
while reader.read_line(&mut String::new()).unwrap() > 2 {}
let reply: &[u8] = if line.contains("/texture/abc") {
b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\nPNG!"
} else {
b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
};
stream.write_all(reply).unwrap();
}
});
assert_eq!(
http_get(&format!("{base}/texture/abc")),
Some(b"PNG!".to_vec())
);
assert_eq!(http_get(&format!("{base}/texture/missing")), None);
}
use super::*;
#[test]
fn skull_block_entity_profiles_supply_head_skins() {
let value = "eyJ0ZXh0dXJlcyI6IHsiU0tJTiI6IHsidXJsIjogImh0dHBzOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2FiYzEyMyJ9fX0=";
let profile = horizon_world::nbt::parse_snbt(&format!(
r#"{{name:"T",id:[I;-158327916,1572358581,-2125825222,-487948715],properties:[{{name:"textures",value:"{value}"}}]}}"#
))
.unwrap()
.0;
assert_eq!(
super::profile_skin_url_nbt(&profile).as_deref(),
Some("https://textures.minecraft.net/texture/abc123")
);
assert_eq!(
super::profile_uuid_nbt(&profile),
Some(
[-158327916, 1572358581, -2125825222, -487948715]
.iter()
.fold(0u128, |a, &w: &i32| (a << 32) | u128::from(w as u32))
)
);
// A name-only profile (a string tag) has no properties: no url, no id.
let named = horizon_world::nbt::Tag::String("MHF_Blaze".into());
assert_eq!(super::profile_skin_url_nbt(&named), None);
assert_eq!(super::profile_uuid_nbt(&named), None);
// A uuid in string form decodes too.
let text_id = horizon_world::nbt::parse_snbt(
r#"{name:"T",id:"e843a1f4-0000-0001-0000-000200000003"}"#,
)
.unwrap()
.0;
assert_eq!(
super::profile_uuid_nbt(&text_id),
Some(u128::from_str_radix("e843a1f4000000010000000200000003", 16).unwrap())
);
}
#[test]
fn sha1_and_base64() {
let hex: String = sha1(b"abc").iter().map(|b| format!("{b:02x}")).collect();
assert_eq!(hex, "a9993e364706816aba3e25717850c26c9cd0d89d");
assert_eq!(base64_decode("aGk=").unwrap(), b"hi");
assert_eq!(base64_decode("aGk").unwrap(), b"hi");
assert!(base64_decode("a").is_none());
assert!(base64_decode("aGk=x").is_none());
assert_eq!(
texture_hash("http://textures.minecraft.net/texture/abc.png?x=1").unwrap(),
"abc"
);
}
}