Files
bworld/server/game/skins.ts
T
2026-10-12 15:09:22 -03:00

36 lines
1.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// player skins set with /skin: 64x64 pngs in minecraft 1.8's layout, see client/rendering/player_renderer.ts
// minecraft skins are a few KB, this leaves room for ones with extra metadata
export const MAX_SKIN_BYTES = 64 * 1024;
export const SKIN_SIZE = 64;
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
// why the bytes can't be a skin, for the player. only the header is checked, clients decode the rest and fall back
// to the default skin if they can't
export function skin_problem(bytes: Uint8Array): string | undefined {
const is_png = bytes.length >= 24 && PNG_SIGNATURE.every((byte, i) => bytes[i] === byte) &&
String.fromCharCode(...bytes.subarray(12, 16)) === "IHDR";
if (!is_png) {
return "That isn't a png";
}
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const width = view.getUint32(16);
const height = view.getUint32(20);
if (width !== SKIN_SIZE || height !== SKIN_SIZE) {
return `Skins have to be ${SKIN_SIZE}x${SKIN_SIZE} (it's ${width}x${height})`;
}
return undefined;
}
export function encode_skin(bytes: Uint8Array): string {
let binary = "";
for (const byte of bytes) {
binary += String.fromCharCode(byte);
}
return btoa(binary);
}