From 03ef597a0636f973aec41473f275f91bbcd7dd64 Mon Sep 17 00:00:00 2001 From: Bill Date: Mon, 11 Apr 2022 02:17:08 -0400 Subject: [PATCH] Update asset loader --- src/core/asset/AssetManager.ts | 21 +++++++++++++++------ src/core/asset/NitroBundle.ts | 15 ++------------- src/core/utils/ArrayBufferToBase64.ts | 11 +++++++++++ src/core/utils/index.ts | 1 + 4 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 src/core/utils/ArrayBufferToBase64.ts diff --git a/src/core/asset/AssetManager.ts b/src/core/asset/AssetManager.ts index f7bbc9a0..7ebbab2b 100644 --- a/src/core/asset/AssetManager.ts +++ b/src/core/asset/AssetManager.ts @@ -1,12 +1,13 @@ import { BaseTexture, Resource, Texture } from '@pixi/core'; import { Loader, LoaderResource } from '@pixi/loaders'; import { Spritesheet } from '@pixi/spritesheet'; -import { IGraphicAsset } from '../../room'; +import { IGraphicAsset } from '../../room/object/visualization/utils'; import { GraphicAssetCollection } from '../../room/object/visualization/utils/GraphicAssetCollection'; import { IGraphicAssetCollection } from '../../room/object/visualization/utils/IGraphicAssetCollection'; import { Disposable } from '../common/disposable/Disposable'; import { INitroLogger } from '../common/logger/INitroLogger'; import { NitroLogger } from '../common/logger/NitroLogger'; +import { ArrayBufferToBase64 } from '../utils'; import { IAssetManager } from './IAssetManager'; import { IAssetData } from './interfaces'; import { NitroBundle } from './NitroBundle'; @@ -118,7 +119,8 @@ export class AssetManager extends Disposable implements IAssetManager .add({ url, crossOrigin: 'anonymous', - xhrType: url.endsWith('.nitro') ? LoaderResource.XHR_RESPONSE_TYPE.BUFFER : LoaderResource.XHR_RESPONSE_TYPE.JSON + loadType: LoaderResource.LOAD_TYPE.XHR, + xhrType: LoaderResource.XHR_RESPONSE_TYPE.BUFFER }); } @@ -155,14 +157,16 @@ export class AssetManager extends Disposable implements IAssetManager { const resource = resources[key]; - if(!resource || resource.error) + if(!resource || resource.error || !resource.xhr) { onDownloaded(false, resource.url); return; } - if(resource.extension === 'nitro') + const resourceType = resource.xhr.getResponseHeader('Content-Type'); + + if(resourceType === 'application/octet-stream') { const nitroBundle = new NitroBundle(resource.data); @@ -174,14 +178,19 @@ export class AssetManager extends Disposable implements IAssetManager continue; } - if(resource.type === LoaderResource.TYPE.IMAGE) + if(resourceType === 'image/png') { - if(resource.texture) this.setTexture(resource.name, resource.texture); + const base64 = ArrayBufferToBase64(resource.data); + const texture = new Texture(new BaseTexture('data:image/png;base64,' + base64)); + + this.setTexture(resource.name, texture); onDownloaded(true, resource.url); continue; } + + onDownloaded(false, resource.url); } }); } diff --git a/src/core/asset/NitroBundle.ts b/src/core/asset/NitroBundle.ts index f7866c17..4c31075e 100644 --- a/src/core/asset/NitroBundle.ts +++ b/src/core/asset/NitroBundle.ts @@ -1,6 +1,7 @@ import { BaseTexture } from '@pixi/core'; import { Data, inflate } from 'pako'; import { BinaryReader } from '../communication/codec/BinaryReader'; +import { ArrayBufferToBase64 } from '../utils'; export class NitroBundle { @@ -16,18 +17,6 @@ export class NitroBundle this.parse(arrayBuffer); } - private static arrayBufferToBase64(buffer: ArrayBuffer): string - { - let binary = ''; - - const bytes = new Uint8Array(buffer); - const len = bytes.byteLength; - - for(let i = 0; i < len; i++) (binary += String.fromCharCode(bytes[i])); - - return window.btoa(binary); - } - public parse(arrayBuffer: ArrayBuffer): void { const binaryReader = new BinaryReader(arrayBuffer); @@ -50,7 +39,7 @@ export class NitroBundle else { const decompressed = inflate((buffer.toArrayBuffer() as Data)); - const base64 = NitroBundle.arrayBufferToBase64(decompressed); + const base64 = ArrayBufferToBase64(decompressed); this._baseTexture = new BaseTexture('data:image/png;base64,' + base64); } diff --git a/src/core/utils/ArrayBufferToBase64.ts b/src/core/utils/ArrayBufferToBase64.ts new file mode 100644 index 00000000..aa8eb743 --- /dev/null +++ b/src/core/utils/ArrayBufferToBase64.ts @@ -0,0 +1,11 @@ +export const ArrayBufferToBase64 = (buffer: ArrayBuffer) => +{ + let binary = ''; + + const bytes = new Uint8Array(buffer); + const len = bytes.byteLength; + + for(let i = 0; i < len; i++) (binary += String.fromCharCode(bytes[i])); + + return window.btoa(binary); +}; diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts index f5fae8bc..d3ee33eb 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -1,3 +1,4 @@ export * from './AdvancedMap'; +export * from './ArrayBufferToBase64'; export * from './NitroTimer'; export * from './proxy';