mirror of
https://github.com/billsonnn/nitro-renderer.git
synced 2025-01-19 06:46:28 +01:00
Update asset loader
This commit is contained in:
parent
29c2ee2282
commit
03ef597a06
@ -1,12 +1,13 @@
|
|||||||
import { BaseTexture, Resource, Texture } from '@pixi/core';
|
import { BaseTexture, Resource, Texture } from '@pixi/core';
|
||||||
import { Loader, LoaderResource } from '@pixi/loaders';
|
import { Loader, LoaderResource } from '@pixi/loaders';
|
||||||
import { Spritesheet } from '@pixi/spritesheet';
|
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 { GraphicAssetCollection } from '../../room/object/visualization/utils/GraphicAssetCollection';
|
||||||
import { IGraphicAssetCollection } from '../../room/object/visualization/utils/IGraphicAssetCollection';
|
import { IGraphicAssetCollection } from '../../room/object/visualization/utils/IGraphicAssetCollection';
|
||||||
import { Disposable } from '../common/disposable/Disposable';
|
import { Disposable } from '../common/disposable/Disposable';
|
||||||
import { INitroLogger } from '../common/logger/INitroLogger';
|
import { INitroLogger } from '../common/logger/INitroLogger';
|
||||||
import { NitroLogger } from '../common/logger/NitroLogger';
|
import { NitroLogger } from '../common/logger/NitroLogger';
|
||||||
|
import { ArrayBufferToBase64 } from '../utils';
|
||||||
import { IAssetManager } from './IAssetManager';
|
import { IAssetManager } from './IAssetManager';
|
||||||
import { IAssetData } from './interfaces';
|
import { IAssetData } from './interfaces';
|
||||||
import { NitroBundle } from './NitroBundle';
|
import { NitroBundle } from './NitroBundle';
|
||||||
@ -118,7 +119,8 @@ export class AssetManager extends Disposable implements IAssetManager
|
|||||||
.add({
|
.add({
|
||||||
url,
|
url,
|
||||||
crossOrigin: 'anonymous',
|
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];
|
const resource = resources[key];
|
||||||
|
|
||||||
if(!resource || resource.error)
|
if(!resource || resource.error || !resource.xhr)
|
||||||
{
|
{
|
||||||
onDownloaded(false, resource.url);
|
onDownloaded(false, resource.url);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(resource.extension === 'nitro')
|
const resourceType = resource.xhr.getResponseHeader('Content-Type');
|
||||||
|
|
||||||
|
if(resourceType === 'application/octet-stream')
|
||||||
{
|
{
|
||||||
const nitroBundle = new NitroBundle(resource.data);
|
const nitroBundle = new NitroBundle(resource.data);
|
||||||
|
|
||||||
@ -174,14 +178,19 @@ export class AssetManager extends Disposable implements IAssetManager
|
|||||||
continue;
|
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);
|
onDownloaded(true, resource.url);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onDownloaded(false, resource.url);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { BaseTexture } from '@pixi/core';
|
import { BaseTexture } from '@pixi/core';
|
||||||
import { Data, inflate } from 'pako';
|
import { Data, inflate } from 'pako';
|
||||||
import { BinaryReader } from '../communication/codec/BinaryReader';
|
import { BinaryReader } from '../communication/codec/BinaryReader';
|
||||||
|
import { ArrayBufferToBase64 } from '../utils';
|
||||||
|
|
||||||
export class NitroBundle
|
export class NitroBundle
|
||||||
{
|
{
|
||||||
@ -16,18 +17,6 @@ export class NitroBundle
|
|||||||
this.parse(arrayBuffer);
|
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
|
public parse(arrayBuffer: ArrayBuffer): void
|
||||||
{
|
{
|
||||||
const binaryReader = new BinaryReader(arrayBuffer);
|
const binaryReader = new BinaryReader(arrayBuffer);
|
||||||
@ -50,7 +39,7 @@ export class NitroBundle
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
const decompressed = inflate((buffer.toArrayBuffer() as Data));
|
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);
|
this._baseTexture = new BaseTexture('data:image/png;base64,' + base64);
|
||||||
}
|
}
|
||||||
|
11
src/core/utils/ArrayBufferToBase64.ts
Normal file
11
src/core/utils/ArrayBufferToBase64.ts
Normal 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);
|
||||||
|
};
|
@ -1,3 +1,4 @@
|
|||||||
export * from './AdvancedMap';
|
export * from './AdvancedMap';
|
||||||
|
export * from './ArrayBufferToBase64';
|
||||||
export * from './NitroTimer';
|
export * from './NitroTimer';
|
||||||
export * from './proxy';
|
export * from './proxy';
|
||||||
|
Loading…
Reference in New Issue
Block a user