From a231b309dcf2839a0fdd0651fe84af8694f38c4d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 1 Apr 2025 16:03:53 +0200 Subject: websocket loader --- viewer/resources.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'viewer/resources.ts') diff --git a/viewer/resources.ts b/viewer/resources.ts index 0e9162e..c0eaba3 100644 --- a/viewer/resources.ts +++ b/viewer/resources.ts @@ -1,3 +1,4 @@ +import { Loader } from "./loader.ts"; export class Resource { constructor(public hash: Uint8Array) { } @@ -6,11 +7,7 @@ export class Resource { .map(e => e.toString(16).padStart(2, "0")) .join("") } - async download_raw() { - const res = await fetch(`http://127.0.0.1:28556/${this.toString()}`) - if (!res.ok) throw new Error("aaaa"); - return await res.bytes() - } + } export function read_res_table(buffer: Uint8Array, cb: (key: string, value: Uint8Array) => void) { @@ -54,9 +51,9 @@ async function ob_cached(r: Resource, make: () => Promise): Promise return m } -export async function get_respackentry(r: Resource): Promise { +export async function get_respackentry(l: Loader, r: Resource): Promise { return await ob_cached(r, async () => { - const buf = await r.download_raw(); + const buf = await l.download(r); const o: RespackEntry = {} read_res_table(buf, (key, value) => { switch (key) { @@ -69,9 +66,9 @@ export async function get_respackentry(r: Resource): Promise): Promise { +export async function get_spatialindex(l: Loader, r: Resource): Promise { return await ob_cached(r, async () => { - const buf = await r.download_raw(); + const buf = await l.download(r); const o: SpatialIndex = { child: [] } read_res_table(buf, (key, value) => { switch (key) { @@ -90,9 +87,9 @@ export async function get_spatialindex(r: Resource): Promise): Promise { +export async function get_prefab(l: Loader, r: Resource): Promise { return await ob_cached(r, async () => { - const buf = await r.download_raw(); + const buf = await l.download(r); const o: Prefab = { graphics: [] } read_res_table(buf, (key, value) => { let v, x = 0 @@ -123,9 +120,9 @@ export async function get_prefab(r: Resource): Promise { return o }) } -export async function get_graphics_part(r: Resource): Promise { +export async function get_graphics_part(l: Loader, r: Resource): Promise { return await ob_cached(r, async () => { - return new GraphicsPart(await r.download_raw()) + return new GraphicsPart(await l.download(r)) }) } @@ -192,4 +189,4 @@ export interface GraphicsCommand { stroke?: number stroke_width?: number point?: Vec3 -} \ No newline at end of file +} -- cgit v1.2.3-70-g09d2