Skip to content

Commit

Permalink
Make default skins to go through official textures server
Browse files Browse the repository at this point in the history
  • Loading branch information
NickAcPT committed Aug 3, 2024
1 parent e621000 commit e621000
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
3 changes: 2 additions & 1 deletion nmsr-aas/src/model/request/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ impl CacheHandler<RenderRequestEntry, ResolvedRenderEntryTextures, ModelCacheCon
RenderRequestEntry::MojangPlayerUuid(u)
| RenderRequestEntry::MojangOfflinePlayerUuid(u)
| RenderRequestEntry::GeyserPlayerUuid(u) => Some(u.to_string()),
RenderRequestEntry::TextureHash(hash) => Some(hash.clone()),
RenderRequestEntry::TextureHash(hash)
| RenderRequestEntry::DefaultSkinTextureHash(hash) => Some(hash.clone()),
RenderRequestEntry::MojangPlayerName(_) | RenderRequestEntry::PlayerSkin(_, _) => None,
})
}
Expand Down
8 changes: 6 additions & 2 deletions nmsr-aas/src/model/request/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum RenderRequestEntry {
MojangOfflinePlayerUuid(Uuid),
GeyserPlayerUuid(Uuid),
TextureHash(String),
DefaultSkinTextureHash(String),
PlayerSkin(#[debug(skip)] Vec<u8>, #[debug(skip)] Option<Vec<u8>>),
}

Expand All @@ -27,7 +28,9 @@ impl RenderRequestEntry {

pub(crate) fn default_skin_hash(skin: DefaultSkin, is_slim: bool) -> RenderRequestEntry {
// TODO: Move to CoW
Self::TextureHash(DefaultSkinResolver::resolve_default_skin(skin, is_slim).to_string())
Self::DefaultSkinTextureHash(
DefaultSkinResolver::resolve_default_skin(skin, is_slim).to_string(),
)
}
}

Expand Down Expand Up @@ -98,7 +101,8 @@ impl TryFrom<RenderRequestEntry> for String {
RenderRequestEntry::MojangPlayerUuid(uuid)
| RenderRequestEntry::MojangOfflinePlayerUuid(uuid)
| RenderRequestEntry::GeyserPlayerUuid(uuid) => Ok(uuid.to_string()),
RenderRequestEntry::TextureHash(hash) => Ok(hash),
RenderRequestEntry::TextureHash(hash)
| RenderRequestEntry::DefaultSkinTextureHash(hash) => Ok(hash),
RenderRequestEntry::PlayerSkin(_, _) => Err(RenderRequestError::InvalidPlayerRequest(
"Unable to convert PlayerSkin to String".to_string(),
)),
Expand Down
17 changes: 13 additions & 4 deletions nmsr-aas/src/model/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ impl RenderRequestResolver {
cape_texture = None;
model = None;
}
RenderRequestEntry::DefaultSkinTextureHash(skin_hash) => {
// Handle default skin textures. These have to go straight to Mojang, whether or not the user changed the config.
skin_texture = Some(
self.fetch_texture_from_mojang(skin_hash, None, MojangTextureRequestType::DefaultSkin)
.await?,
);
cape_texture = None;
model = None;
}
RenderRequestEntry::PlayerSkin(skin_bytes, cape_bytes) => {
skin_texture = Some(MojangTexture::new_unnamed(skin_bytes.clone()));
cape_texture = cape_bytes.to_owned().map(|b| MojangTexture::new_unnamed(b));
Expand Down Expand Up @@ -414,9 +423,9 @@ impl RenderRequestResolver {
RenderRequestEntry::GeyserPlayerUuid(u)
| RenderRequestEntry::MojangOfflinePlayerUuid(u)
| RenderRequestEntry::MojangPlayerUuid(u) => Some(*u),
RenderRequestEntry::TextureHash(_) | RenderRequestEntry::PlayerSkin(_, _) => {
None
}
RenderRequestEntry::TextureHash(_)
| RenderRequestEntry::DefaultSkinTextureHash(_)
| RenderRequestEntry::PlayerSkin(_, _) => None,
RenderRequestEntry::MojangPlayerName(_) => Some(Uuid::new_v4()),
};

Expand Down Expand Up @@ -453,7 +462,7 @@ impl RenderRequestResolver {
resolved
}

pub async fn resolve_raw(&self, request: &RenderRequest) -> Result<ResolvedRenderRequest> {
async fn resolve_raw(&self, request: &RenderRequest) -> Result<ResolvedRenderRequest> {
// First, we need to resolve the skin and cape textures.
let resolved_textures = self
.resolve_entry_textures(&request.entry)
Expand Down
25 changes: 20 additions & 5 deletions nmsr-aas/src/model/resolver/mojang/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::model::GameProfile;
use crate::{
config::MojankConfiguration,
config::{
MojankConfiguration, DEFAULT_TEXTURES_SERVER, DEFAULT_TEXTURES_SERVER_SKIN_URL_TEMPLATE,
},
error::{MojangRequestError, MojangRequestResult},
model::resolver::mojang::model::UsernameToUuidResponse,
utils::http_client::NmsrHttpClient,
Expand All @@ -24,6 +26,7 @@ pub enum MojangClientKind {

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MojangTextureRequestType {
DefaultSkin,
Skin,
Cape,
}
Expand Down Expand Up @@ -150,12 +153,24 @@ impl MojangClient {
fn build_request_url(&self, req_type: MojangTextureRequestType, texture_id: &str) -> String {
let mojank = self.mojank_config();

let url = match req_type {
MojangTextureRequestType::Skin => &mojank.textures_server_skin_url_template,
MojangTextureRequestType::Cape => &mojank.textures_server_cape_url_template,
let req_type_default_skin = (req_type, mojank.default_skins_use_official_textures_server);

let url = match req_type_default_skin {
(MojangTextureRequestType::DefaultSkin, true) => {
DEFAULT_TEXTURES_SERVER_SKIN_URL_TEMPLATE
}
(MojangTextureRequestType::Skin, _) | (MojangTextureRequestType::DefaultSkin, _) => {
&mojank.textures_server_skin_url_template
}
(MojangTextureRequestType::Cape, _) => &mojank.textures_server_cape_url_template,
};

url.replace("{textures_server}", &mojank.textures_server)
let target_server = match req_type_default_skin {
(MojangTextureRequestType::DefaultSkin, true) => DEFAULT_TEXTURES_SERVER,
_ => &mojank.textures_server,
};

url.replace("{textures_server}", target_server)
.replace("{texture_id}", texture_id)
}
}
14 changes: 11 additions & 3 deletions nmsr-aas/src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ pub struct MojankConfiguration {
/// By enabling this, the UUIDs will be sent to the Mojang API without dashes.
pub use_dashless_uuids: bool,

/// Whether to redirect default skin requests to the official Mojang textures server.
/// Change this value if you want default skins to be fetched from the configured textures server.
pub default_skins_use_official_textures_server: bool,

/// The template to use for resolving player skin textures.
pub textures_server_skin_url_template: String,

/// The template to use for resolving player cape textures.
pub textures_server_cape_url_template: String,
}

pub const DEFAULT_TEXTURES_SERVER_SKIN_URL_TEMPLATE: &str = "{textures_server}/texture/{texture_id}";
pub const DEFAULT_TEXTURES_SERVER: &str = "https://textures.minecraft.net";

impl Default for MojankConfiguration {
fn default() -> Self {
Self {
session_server: "https://sessionserver.mojang.com/".to_string(),
textures_server: "https://textures.minecraft.net".to_string(),
textures_server: DEFAULT_TEXTURES_SERVER.to_string(),
mojang_api_server: "https://api.mojang.com/".to_string(),
geysermc_api_server: "https://api.geysermc.org/".to_string(),

Expand All @@ -126,11 +133,12 @@ impl Default for MojankConfiguration {
session_server_timeout: 5 * 60 /* 5 minutes */,

use_default_skins_when_missing: true,
default_skins_use_official_textures_server: true,

allow_offline_mode_uuids: false,
use_dashless_uuids: false,
textures_server_skin_url_template: "{textures_server}/texture/{texture_id}".to_string(),
textures_server_cape_url_template: "{textures_server}/texture/{texture_id}".to_string(),
textures_server_skin_url_template: DEFAULT_TEXTURES_SERVER_SKIN_URL_TEMPLATE.to_string(),
textures_server_cape_url_template: DEFAULT_TEXTURES_SERVER_SKIN_URL_TEMPLATE.to_string(),
}
}
}
Expand Down

0 comments on commit e621000

Please sign in to comment.