Update asset loader

This commit is contained in:
Bill 2022-04-11 02:17:08 -04:00
parent 29c2ee2282
commit 03ef597a06
4 changed files with 29 additions and 19 deletions

View File

@ -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);
}
});
}

View File

@ -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);
}

View File

@ -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);
};

View File

@ -1,3 +1,4 @@
export * from './AdvancedMap';
export * from './ArrayBufferToBase64';
export * from './NitroTimer';
export * from './proxy';