diff --git a/packages/api/src/asset/IAssetManager.ts b/packages/api/src/asset/IAssetManager.ts index 5005341c..6457e286 100644 --- a/packages/api/src/asset/IAssetManager.ts +++ b/packages/api/src/asset/IAssetManager.ts @@ -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 downloadAssets(urls: string[]): Promise; downloadAsset(url: string): Promise; readonly collections: Map; diff --git a/packages/assets/package.json b/packages/assets/package.json index e2238c50..9f0dac28 100644 --- a/packages/assets/package.json +++ b/packages/assets/package.json @@ -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" diff --git a/packages/assets/src/AssetManager.ts b/packages/assets/src/AssetManager.ts index 8f683ff1..b2425eb1 100644 --- a/packages/assets/src/AssetManager.ts +++ b/packages/assets/src/AssetManager.ts @@ -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 - { - 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(url); - - if(!texture) return null; - - this.setTexture(name ?? url, texture); - - return texture; - } - - catch (err) - { - NitroLogger.error(err); - } - } - public async downloadAssets(urls: string[]): Promise { 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(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 buffer = await response.arrayBuffer(); - const nitroBundle = await NitroBundle.from(buffer); + const contentType = response.headers.get('Content-Type'); - await this.processAsset(nitroBundle.texture, nitroBundle.jsonFile as IAssetData); + 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(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; } diff --git a/packages/assets/src/GetAssetManager.ts b/packages/assets/src/GetAssetManager.ts index 257ceea1..7c36aa1d 100644 --- a/packages/assets/src/GetAssetManager.ts +++ b/packages/assets/src/GetAssetManager.ts @@ -1,5 +1,8 @@ +import { Assets } from 'pixi.js'; import { AssetManager } from './AssetManager'; +Assets.init(); + const assetManager = new AssetManager(); export const GetAssetManager = () => assetManager; diff --git a/packages/room/src/GetRoomPreviewerInstance.ts b/packages/room/src/GetRoomPreviewerInstance.ts new file mode 100644 index 00000000..8f0739d9 --- /dev/null +++ b/packages/room/src/GetRoomPreviewerInstance.ts @@ -0,0 +1,4 @@ +import { GetRoomEngine } from './GetRoomEngine'; +import { RoomPreviewer } from './RoomPreviewer'; + +export const GetRoomPreviewerInstance = () => new RoomPreviewer(GetRoomEngine(), ++RoomPreviewer.PREVIEW_COUNTER); diff --git a/packages/session/src/badge/BadgeImageManager.ts b/packages/session/src/badge/BadgeImageManager.ts index 0151ed59..3aa32891 100644 --- a/packages/session/src/badge/BadgeImageManager.ts +++ b/packages/session/src/badge/BadgeImageManager.ts @@ -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 = new Map(); private _readyToGenerateGroupBadges: boolean = false; - public init(): void + public async init(): Promise { 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); + const texture = GetAssetManager().getTexture(url); - if(texture) GetEventDispatcher().dispatchEvent(new BadgeImageReadyEvent(badgeName, texture)); + if(texture) GetEventDispatcher().dispatchEvent(new BadgeImageReadyEvent(badgeName, texture)); + } + + catch (err) + { + NitroLogger.error(err); + } }; loadBadge(); diff --git a/yarn.lock b/yarn.lock index 06764d79..5aa656c4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"