Player skins !

This commit is contained in:
2026-09-27 12:01:52 -03:00
parent edf4b72dc4
commit 60188ff735
14 changed files with 325 additions and 8 deletions
+62
View File
@@ -9,6 +9,7 @@ const MAX_MESSAGE_SIZE = 4096;
// in build/, see build.ts
export const ENGINE_TEXTURES_INDEX = "assets/textures/index.json";
const SHUTDOWN_TIMEOUT_MS = 5000;
const DOWNLOAD_TIMEOUT_MS = 10_000;
export interface HostOptions {
// what deno task build made: the client and the engine's assets
@@ -70,6 +71,13 @@ export async function start_host(options: HostOptions): Promise<Host> {
on_final_save?.();
}
break;
case "download":
download(message.url, message.max_bytes).then(
(bytes) => post({ type: "downloaded", id: message.id, bytes }),
(e) =>
post({ type: "downloaded", id: message.id, error: e instanceof Error ? e.message : String(e) }),
);
break;
}
};
game.onerror = (event) => {
@@ -157,6 +165,60 @@ export async function start_host(options: HostOptions): Promise<Host> {
};
}
// for the game worker, which has no network access. errors are told to players, so they say what went wrong simply
async function download(url: string, max_bytes: number): Promise<Uint8Array> {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
throw new Error("That isn't a url");
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
throw new Error("Only http and https urls work");
}
let response: Response;
try {
response = await fetch(parsed, { signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS) });
} catch {
throw new Error("Couldn't download it");
}
if (!response.ok || !response.body) {
await response.body?.cancel();
throw new Error(`Couldn't download it (${response.status})`);
}
// read up to the limit instead of trusting content-length
const reader = response.body.getReader();
const chunks: Uint8Array[] = [];
let size = 0;
while (true) {
let chunk: ReadableStreamReadResult<Uint8Array>;
try {
chunk = await reader.read();
} catch {
throw new Error("Couldn't download it");
}
if (chunk.done) {
break;
}
size += chunk.value.length;
if (size > max_bytes) {
await reader.cancel();
throw new Error(`It's bigger than ${Math.floor(max_bytes / 1024)} KB`);
}
chunks.push(chunk.value);
}
const bytes = new Uint8Array(size);
let offset = 0;
for (const chunk of chunks) {
bytes.set(chunk, offset);
offset += chunk.length;
}
return bytes;
}
function read_world(file: string): string | undefined {
try {
return Deno.readTextFileSync(file);