This commit is contained in:
Bill 2025-01-18 09:52:27 -05:00
parent 23c0d29688
commit 2185919df7
7 changed files with 54 additions and 52 deletions

View File

@ -12,7 +12,6 @@ export interface IAssetManager
getAsset(name: string): IGraphicAsset;
getCollection(name: string): IGraphicAssetCollection;
createCollection(data: IAssetData, spritesheet: Spritesheet): IGraphicAssetCollection;
loadTextureFromUrl(url: string, name?: string): Promise<Texture>
downloadAssets(urls: string[]): Promise<boolean>;
downloadAsset(url: string): Promise<boolean>;
readonly collections: Map<string, IGraphicAssetCollection>;

View File

@ -14,8 +14,8 @@
"dependencies": {
"@nitrots/api": "1.0.0",
"@nitrots/utils": "1.0.0",
"pixi.js": "^8.5.1",
"@pixi/gif": "^3.0.0"
"@pixi/gif": "^3.0.1",
"pixi.js": "^8.5.1"
},
"devDependencies": {
"typescript": "~5.5.4"

View File

@ -1,5 +1,6 @@
import { IAssetData, IAssetManager, IGraphicAsset, IGraphicAssetCollection } from '@nitrots/api';
import { NitroBundle, NitroLogger } from '@nitrots/utils';
import { AnimatedGIF } from '@pixi/gif';
import { Assets, Spritesheet, SpritesheetData, Texture } from 'pixi.js';
import { GraphicAssetCollection } from './GraphicAssetCollection';
@ -73,33 +74,6 @@ export class AssetManager implements IAssetManager
return collection;
}
public async loadTextureFromUrl(url: string, name: string = null): Promise<Texture>
{
if(!url || !url.length) return null;
let texture = this.getTexture(name);
if(!texture) texture = this.getTexture(url);
if(texture) return texture;
try
{
texture = await Assets.load<Texture>(url);
if(!texture) return null;
this.setTexture(name ?? url, texture);
return texture;
}
catch (err)
{
NitroLogger.error(err);
}
}
public async downloadAssets(urls: string[]): Promise<boolean>
{
if(!urls || !urls.length) return Promise.resolve(true);
@ -125,23 +99,37 @@ export class AssetManager implements IAssetManager
{
if(!url || !url.length) return false;
if(url.endsWith('.png') || url.endsWith('.jpg') || url.endsWith('.jpeg') || url.endsWith('.gif'))
{
const texture = await Assets.load<Texture>(url);
this.setTexture(url, texture);
return true;
}
const response = await fetch(url);
if(response.status !== 200 || !response.headers.has('Content-Type') || response.headers.get('Content-Type') !== 'application/octet-stream') return false;
if(!response || response.status !== 200) return false;
const contentType = response.headers.get('Content-Type');
switch(contentType)
{
case 'application/octet-stream': {
const buffer = await response.arrayBuffer();
const nitroBundle = await NitroBundle.from(buffer);
await this.processAsset(nitroBundle.texture, nitroBundle.jsonFile as IAssetData);
break;
}
case 'image/png':
case 'image/jpeg': {
const texture = await Assets.load<Texture>(url);
if(texture) this.setTexture(url, texture);
break;
}
case 'image/gif': {
const buffer = await response.arrayBuffer();
const animatedGif = AnimatedGIF.fromBuffer(buffer);
const texture = animatedGif.texture;
if(texture) this.setTexture(url, texture);
break;
}
}
return true;
}

View File

@ -1,5 +1,8 @@
import { Assets } from 'pixi.js';
import { AssetManager } from './AssetManager';
Assets.init();
const assetManager = new AssetManager();
export const GetAssetManager = () => assetManager;

View File

@ -0,0 +1,4 @@
import { GetRoomEngine } from './GetRoomEngine';
import { RoomPreviewer } from './RoomPreviewer';
export const GetRoomPreviewerInstance = () => new RoomPreviewer(GetRoomEngine(), ++RoomPreviewer.PREVIEW_COUNTER);

View File

@ -2,7 +2,7 @@ import { GetAssetManager } from '@nitrots/assets';
import { GetCommunication, GroupBadgePartsEvent } from '@nitrots/communication';
import { GetConfiguration } from '@nitrots/configuration';
import { BadgeImageReadyEvent, GetEventDispatcher } from '@nitrots/events';
import { TextureUtils } from '@nitrots/utils';
import { NitroLogger, TextureUtils } from '@nitrots/utils';
import { Container, Sprite, Texture } from 'pixi.js';
import { BadgeInfo } from './BadgeInfo';
import { GroupBadge } from './GroupBadge';
@ -20,7 +20,7 @@ export class BadgeImageManager
private _groupBadgesQueue: Map<string, boolean> = new Map();
private _readyToGenerateGroupBadges: boolean = false;
public init(): void
public async init(): Promise<void>
{
GetCommunication().registerMessageEvent(new GroupBadgePartsEvent(this.onGroupBadgePartsEvent.bind(this)));
}
@ -60,11 +60,19 @@ export class BadgeImageManager
{
const loadBadge = async () =>
{
await GetAssetManager().downloadAsset(url);
try
{
if(!await GetAssetManager().downloadAsset(url)) return;
const texture = GetAssetManager().getTexture(url);
if(texture) GetEventDispatcher().dispatchEvent(new BadgeImageReadyEvent(badgeName, texture));
}
catch (err)
{
NitroLogger.error(err);
}
};
loadBadge();

View File

@ -214,10 +214,10 @@
resolved "https://registry.yarnpkg.com/@pixi/colord/-/colord-2.9.6.tgz#7e4e7851480da6fd3cef4e331f008d60be7e1204"
integrity sha512-nezytU2pw587fQstUu1AsJZDVEynjskwOL+kibwcdxsMBFqPsFFNA7xl0ii/gXuDi6M0xj3mfRJj8pBSc2jCfA==
"@pixi/gif@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@pixi/gif/-/gif-3.0.0.tgz#dbd3e1bfb2a5d83a84965f5b517cc5dcb8407862"
integrity sha512-0am94k1SEwacW6anOiNT9vt3vnefEaiMxkg30nMwvqU3XkIO6sSsFBgQM3UArjDG/quYsujjyZZkyhl7yBF6GQ==
"@pixi/gif@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@pixi/gif/-/gif-3.0.1.tgz#2709d6559d316161cde1821b0f29cc2c05f88794"
integrity sha512-oGl0nkbFAe1vaRLyIvGbJc3fcIrS8vF1E00cwjiV+9f1pYe072D+yijJxHsgYnXs6jdzERh+D0MqSrEag0jRzg==
"@rollup/plugin-typescript@^11.1.6":
version "11.1.6"