Merge pull request #2313 from socksprox/fix/player-head-textures
🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed at this head (checked against the head by contrib-gate-20). Re-review was asked about 07:30Z with no new objection. Proven in wave 5's union run (main + 54 PRs, 332/332 selected harnesses; lan_e2e 18/18 in-frame and on the server thread). Thank you!
This commit is contained in:
@@ -179,7 +179,7 @@ fn agent() -> ureq::Agent {
|
||||
}
|
||||
|
||||
/// The discovery document, fetched once per game (a failure is retried next time).
|
||||
fn discovery() -> Result<Arc<serde_json::Value>, JoinError> {
|
||||
pub(crate) fn discovery() -> Result<Arc<serde_json::Value>, JoinError> {
|
||||
static CACHE: Mutex<Option<Arc<serde_json::Value>>> = Mutex::new(None);
|
||||
if let Some(d) = CACHE.lock().unwrap().clone() {
|
||||
return Ok(d);
|
||||
|
||||
@@ -736,7 +736,7 @@ pub struct Scene<'a> {
|
||||
/// The skin sprite a `player_head` shows at a position (`SkullBlockRenderer`
|
||||
/// profile → skin): a downloaded `skins/<sha1>` slot or the profile's
|
||||
/// `DefaultPlayerSkin` fallback; None keeps `SKIN_BY_TYPE` (`DefaultPlayerSkin.getDefaultTexture`, slim steve).
|
||||
pub skull: &'a dyn Fn([i32; 3]) -> Option<&'a str>,
|
||||
pub skull: &'a dyn Fn([i32; 3]) -> Option<String>,
|
||||
/// Font glyph cell (none for blank glyphs such as the space) and advance.
|
||||
pub glyph: &'a dyn Fn(char) -> (Option<Rect>, f32),
|
||||
}
|
||||
@@ -1288,11 +1288,12 @@ impl BlockEntityRenderer {
|
||||
};
|
||||
// SkullBlockRenderer.getRenderType: the profile's skin when it
|
||||
// resolves, `SKIN_BY_TYPE` (steve) otherwise.
|
||||
let texture = if *kind == SkullType::Player {
|
||||
(scene.skull)(pos).unwrap_or(texture)
|
||||
let skin = if *kind == SkullType::Player {
|
||||
(scene.skull)(pos)
|
||||
} else {
|
||||
texture
|
||||
None
|
||||
};
|
||||
let texture = skin.as_deref().unwrap_or(texture);
|
||||
let (Some(model), Some(paint)) =
|
||||
(self.models.get(model), paint(texture, 0xFFFFFF, packed))
|
||||
else {
|
||||
|
||||
@@ -190,6 +190,9 @@ impl Skull {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SkullSkin {
|
||||
pub download: Option<(String, String)>,
|
||||
/// `ResolvableProfile.nameOrId` when the profile is `Dynamic` (a name or
|
||||
/// an id only): the key `skins::Resolver` resolves it by.
|
||||
pub dynamic: Option<crate::skins::NameOrId>,
|
||||
pub fallback: &'static str,
|
||||
}
|
||||
impl SkullSkin {
|
||||
@@ -198,10 +201,21 @@ impl SkullSkin {
|
||||
let location = crate::skins::skin_location(&crate::skins::texture_hash(&url)?);
|
||||
Some((url, location))
|
||||
});
|
||||
// A `Dynamic` profile (a name or id only) resolves through the
|
||||
// services when the skull is drawn.
|
||||
let dynamic = crate::skins::dynamic_nbt(profile);
|
||||
// DefaultPlayerSkin.get(id), or getDefaultSkin (slim steve) without an id.
|
||||
let fallback = crate::skins::profile_uuid_nbt(profile)
|
||||
.or_else(|| {
|
||||
crate::skins::nbt_text(profile.get("name"))
|
||||
.map(|name| horizon_protocol::crypto::offline_uuid(&name))
|
||||
})
|
||||
.map_or("entity/player/slim/steve", crate::tab_list::default_skin);
|
||||
Self { download, fallback }
|
||||
Self {
|
||||
download,
|
||||
dynamic,
|
||||
fallback,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The skull and head blocks (`SkullBlock`, `WallSkullBlock`): every
|
||||
|
||||
@@ -1609,6 +1609,9 @@ struct Game<'a> {
|
||||
/// Player skin downloads, and which skin each atlas skin slot holds
|
||||
/// (least recently shown first).
|
||||
skin_downloads: RefCell<horizon_client::skins::Downloader>,
|
||||
/// `ProfileResolver.Cached`: `Dynamic` profiles (name- or id-only heads
|
||||
/// and mannequins) resolve through the services in the background.
|
||||
skin_profiles: RefCell<horizon_client::skins::Resolver>,
|
||||
skin_slots: Vec<(String, usize)>,
|
||||
/// Atlas trim slots in use: (sprite name, slot), least recently drawn first.
|
||||
trim_slots: Vec<(String, usize)>,
|
||||
@@ -13333,6 +13336,7 @@ impl<'a> Game<'a> {
|
||||
.unwrap_or_default()
|
||||
.join("assets/skins"),
|
||||
)),
|
||||
skin_profiles: RefCell::new(horizon_client::skins::Resolver::new()),
|
||||
skin_slots: Vec::new(),
|
||||
trim_slots: Vec::new(),
|
||||
cart_models: prepared.cart_models,
|
||||
|
||||
@@ -725,21 +725,38 @@ impl Game<'_> {
|
||||
/// the player head at `pos`. The skin download is started here; while it
|
||||
/// is in flight (or when it cannot be got) the profile's default skin
|
||||
/// stands in. A head with no profile answers None: `SKIN_BY_TYPE`.
|
||||
pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option<&str> {
|
||||
pub(super) fn skull_skin_at(&self, pos: [i32; 3]) -> Option<String> {
|
||||
let Some(BlockEntity::Skull(skull)) =
|
||||
self.world.block_entities.get(&(pos[0], pos[1], pos[2]))
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let skin = skull.skin.as_ref()?;
|
||||
if let Some((url, location)) = &skin.download {
|
||||
self.skin_downloads
|
||||
.borrow_mut()
|
||||
.state(skins::Kind::Skin, url);
|
||||
if self.gui.atlas.sprites.contains_key(location) {
|
||||
// The texture a packed or resolved `textures` property fetches: the
|
||||
// download of a static profile, or the one the services answer for a
|
||||
// `Dynamic` (name- or id-only) profile.
|
||||
let download = skin.download.clone().or_else(|| {
|
||||
let key = skin.dynamic.as_ref()?;
|
||||
let skins::Resolve::Done(Some(profile)) = self.skin_profiles.borrow_mut().state(key)
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let (value, signature) = profile.textures()?;
|
||||
let url = skins::unpack(&value, signature, &skins::TEXTURE_URIS).skin?;
|
||||
let location = skins::skin_location(&skins::texture_hash(&url)?);
|
||||
Some((url, location))
|
||||
});
|
||||
if let Some((url, location)) = download {
|
||||
let mut downloads = self.skin_downloads.borrow_mut();
|
||||
if self.gui.atlas.sprites.contains_key(&location) {
|
||||
let _ = downloads.state(skins::Kind::Skin, &url);
|
||||
return Some(location);
|
||||
}
|
||||
// Its slot was evicted: offer the image again.
|
||||
if downloads.state(skins::Kind::Skin, &url) == skins::Download::Ready {
|
||||
downloads.again(&location);
|
||||
}
|
||||
}
|
||||
Some(skin.fallback)
|
||||
Some(skin.fallback.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,7 @@ pub(super) struct Host<'a> {
|
||||
banners: &'a horizon_client::banner::Banners,
|
||||
sprites: &'a HashMap<String, Rect>,
|
||||
skins: &'a RefCell<horizon_client::skins::Downloader>,
|
||||
profiles: &'a RefCell<horizon_client::skins::Resolver>,
|
||||
}
|
||||
impl SpecialHost for Host<'_> {
|
||||
fn banner_texture(&self, pattern: &str) -> Option<String> {
|
||||
@@ -144,14 +145,14 @@ impl SpecialHost for Host<'_> {
|
||||
fn player_skin(&self, profile: &Value) -> Option<String> {
|
||||
// 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)
|
||||
// A `Dynamic` profile (a name or id only) resolves through the
|
||||
// services (`PlayerSkinRenderCache.getOrDefault`).
|
||||
Some(horizon_client::skins::head_skin(
|
||||
profile,
|
||||
&mut self.profiles.borrow_mut(),
|
||||
&mut self.skins.borrow_mut(),
|
||||
|location| self.sprites.contains_key(location),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +195,7 @@ impl Game<'_> {
|
||||
banners: &self.res.stations().banners,
|
||||
sprites: &self.gui.atlas.sprites,
|
||||
skins: &self.skin_downloads,
|
||||
profiles: &self.skin_profiles,
|
||||
}
|
||||
}
|
||||
/// The client level as item models read it: worked out once a frame (a
|
||||
|
||||
@@ -2906,7 +2906,7 @@ impl Game<'_> {
|
||||
resolved.push((uuid, skin));
|
||||
}
|
||||
}
|
||||
resolved.retain(|(_, skin)| !skin.starts_with("skins/") || self.show_skin(skin));
|
||||
resolved.retain(|(_, skin)| !skin.starts_with("skins/") || self.again_skin(skin));
|
||||
let Some((level, skins, own)) = self.remote.as_mut().map(|r| {
|
||||
r.skins = resolved.into_iter().collect();
|
||||
(
|
||||
@@ -2945,11 +2945,9 @@ impl Game<'_> {
|
||||
.iter()
|
||||
.filter(|m| &*m.kind.id == "minecraft:mannequin" && m.profile.is_some())
|
||||
.map(|m| {
|
||||
let t = m
|
||||
.profile
|
||||
.as_ref()
|
||||
.and_then(|p| p.properties.iter().find(|p| p.name == "textures"));
|
||||
let textures = t.map_or_else(Default::default, |t| {
|
||||
let profile = m.profile.as_ref();
|
||||
let t = profile.and_then(|p| p.properties.iter().find(|p| p.name == "textures"));
|
||||
let textures = if let Some(t) = t {
|
||||
let signature = if t.signature.is_some() {
|
||||
horizon_client::skins::Signature::Signed
|
||||
} else {
|
||||
@@ -2960,7 +2958,30 @@ impl Game<'_> {
|
||||
signature,
|
||||
&horizon_client::skins::TEXTURE_URIS,
|
||||
)
|
||||
});
|
||||
} else {
|
||||
// `ResolvableProfile.resolveProfile`: no `textures` and
|
||||
// only a name or an id resolves through the services.
|
||||
let key = profile.and_then(|p| match (&p.name, p.uuid) {
|
||||
(Some(name), None) => {
|
||||
Some(horizon_client::skins::NameOrId::Name(name.clone()))
|
||||
}
|
||||
(None, Some(id)) => Some(horizon_client::skins::NameOrId::Id(id)),
|
||||
_ => None,
|
||||
});
|
||||
key.and_then(|key| match self.skin_profiles.borrow_mut().state(&key) {
|
||||
horizon_client::skins::Resolve::Done(Some(profile)) => {
|
||||
profile.textures().map(|(value, signature)| {
|
||||
horizon_client::skins::unpack(
|
||||
&value,
|
||||
signature,
|
||||
&horizon_client::skins::TEXTURE_URIS,
|
||||
)
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_default()
|
||||
};
|
||||
(m.id, textures)
|
||||
})
|
||||
.collect();
|
||||
@@ -2975,7 +2996,7 @@ impl Game<'_> {
|
||||
resolved.push((id, skin));
|
||||
}
|
||||
for (id, skin) in resolved {
|
||||
let skin = skin.filter(|s| !s.starts_with("skins/") || self.show_skin(s));
|
||||
let skin = skin.filter(|s| !s.starts_with("skins/") || self.again_skin(s));
|
||||
if let Some(mob) = self
|
||||
.mobs
|
||||
.iter_mut()
|
||||
@@ -3025,6 +3046,10 @@ impl Game<'_> {
|
||||
self.skin_slots.push(e);
|
||||
true
|
||||
}
|
||||
/// `show_skin`, or — its slot evicted — the image queued for upload again.
|
||||
fn again_skin(&mut self, location: &str) -> bool {
|
||||
self.show_skin(location) || self.skin_downloads.borrow_mut().again(location)
|
||||
}
|
||||
/// ChatType decoration: `chat.translation_key` with sender/content parameters.
|
||||
/// ChatType.Bound.decorate: the chat type's translation, with the
|
||||
/// sender, the target and the message as its parameters, in its style.
|
||||
|
||||
@@ -499,7 +499,7 @@ pub(super) fn submit(
|
||||
let skin = (!argument.is_null())
|
||||
.then(|| host.player_skin(argument))
|
||||
.flatten()
|
||||
.unwrap_or_else(|| "entity/player/wide/steve".to_owned());
|
||||
.unwrap_or_else(|| "entity/player/slim/steve".to_owned());
|
||||
part("player", &none, &skin, white, Foil::None);
|
||||
}
|
||||
SpecialKind::Trident => {
|
||||
|
||||
@@ -229,6 +229,17 @@ fn profile_tag(p: &Profile) -> Tag {
|
||||
if let Some(name) = &p.name {
|
||||
entries.push(("name", Tag::String(name.as_str().into())));
|
||||
}
|
||||
if let Some(id) = p.id {
|
||||
entries.push((
|
||||
"id",
|
||||
Tag::IntArray(vec![
|
||||
(id >> 96) as i32,
|
||||
(id >> 64) as i32,
|
||||
(id >> 32) as i32,
|
||||
id as i32,
|
||||
]),
|
||||
));
|
||||
}
|
||||
entries.push(("properties", Tag::List(properties)));
|
||||
Tag::compound(entries)
|
||||
}
|
||||
|
||||
@@ -116,20 +116,9 @@ pub fn unpack(value: &str, signature: Signature, valid_uris: &[&str]) -> Texture
|
||||
/// 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 = 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
|
||||
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.
|
||||
@@ -164,13 +153,329 @@ pub fn profile_uuid_nbt(profile: &horizon_world::nbt::Tag) -> Option<u128> {
|
||||
}
|
||||
}
|
||||
|
||||
fn nbt_text(tag: Option<&horizon_world::nbt::Tag>) -> Option<String> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// `Either<String, UUID>`: the key a `Dynamic` `ResolvableProfile` resolves
|
||||
/// by (`ResolvableProfile.nameOrId`).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum NameOrId {
|
||||
Name(String),
|
||||
Id(u128),
|
||||
}
|
||||
|
||||
/// `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;
|
||||
@@ -313,7 +618,16 @@ pub struct Downloader {
|
||||
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();
|
||||
@@ -323,6 +637,9 @@ impl Downloader {
|
||||
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.
|
||||
@@ -331,7 +648,8 @@ impl Downloader {
|
||||
let state = match image {
|
||||
Some(rgba) => {
|
||||
if key.0 == Kind::Skin {
|
||||
self.ready.push((skin_location(&key.1), rgba));
|
||||
let location = skin_location(&key.1);
|
||||
self.queue(location, rgba);
|
||||
}
|
||||
Download::Ready
|
||||
}
|
||||
@@ -370,8 +688,53 @@ impl Downloader {
|
||||
});
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -452,6 +815,134 @@ pub fn sha1(data: &[u8]) -> [u8; 20] {
|
||||
|
||||
#[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=";
|
||||
|
||||
Reference in New Issue
Block a user