All files / player-browser/src/skin/lr2 scene-textures.ts

47.42% Statements 46/97
17.39% Branches 8/46
69.44% Functions 25/36
48.42% Lines 46/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288                        34x 34x 34x             8x       16x 16x       16x     16x 16x               11x 11x                         11x         34x 34x 34x                                                                                             16x     16x 16x 16x                                                                                         1x                               7x       6x                             7x                 14x 14x 51x   14x       14x       14x       7x       8x       1x       1x       1x       1x       1x       1x       1x       1x 1x         50x 19x         20x 17x                         32x     32x    
import { Texture } from 'pixi.js';
import { type LR2Skin, type LR2SpecialGraphic, LR2_SPECIAL_GRAPHIC, isLR2SpecialGraphic } from '@be-music/skin-lr2';
import { destroyTextureAndRevokeBlobURL, loadSkinAssetTexture, loadTextureFromBytes } from './textures.ts';
import { loadAssetBytes, resolveChartImageAsset, resolveChartSource } from '../../collection/collection.ts';
import type { LibraryChart } from '@be-music/player-scene-model';
import type { BrowserAssetSource, BrowserChartCollection } from '../../collection/types.ts';
 
type TextureLoadValidity = () => boolean;
type SkinTexturePathCollector = (paths: Set<string>, skin: LR2Skin) => void;
type SkinTextureSourceElement = { source: { imagePath: string } };
 
export class LR2SkinTextureStore {
  private readonly textures = new Map<string, Texture>();
  private loadSerial = 0;
  private disposed = false;
 
  public get(path: string): Texture | undefined {
    return this.textures.get(path);
  }
 
  public asReadonlyMap(): ReadonlyMap<string, Texture> {
    return this.textures;
  }
 
  public clear(): void {
    this.loadSerial += 1;
    destroyTextureMap(this.textures);
  }
 
  public dispose(): void {
    Iif (this.disposed) {
      return;
    }
    this.disposed = true;
    this.clear();
  }
 
  public async preload(
    skin: LR2Skin,
    paths: ReadonlySet<string>,
    isStillCurrent: TextureLoadValidity,
  ): Promise<boolean> {
    const serial = ++this.loadSerial;
    await Promise.all(
      [...paths].map(async (path) => {
        const texture = await loadSkinAssetTexture(skin, path);
        if (!texture) {
          return;
        }
        if (this.disposed || serial !== this.loadSerial || !isStillCurrent()) {
          destroyTextureAndRevokeBlobURL(texture, true);
          return;
        }
        this.textures.set(path, texture);
      }),
    );
    return !this.disposed && serial === this.loadSerial && isStillCurrent();
  }
}
 
export class LR2ChartGraphicTextureStore {
  private readonly textures = new Map<string, Texture>();
  private readonly pending = new Set<string>();
  private disposed = false;
 
  public resolve(
    collection: BrowserChartCollection,
    chartEntry: LibraryChart,
    path: LR2SpecialGraphic,
    onLoaded: () => void,
  ): Texture | undefined {
    return this.resolveWith(() => resolveChartSource(collection, chartEntry), chartEntry, path, onLoaded);
  }
 
  /**
   * As {@link resolve}, for a chart whose files are `source`: the texture of `path` for `song` once loaded, the solid
   * one for `*BLACK` / `*WHITE`. Without a source nothing loads.
   */
  public resolveFromSource(
    source: BrowserAssetSource | undefined,
    chartEntry: LibraryChart,
    path: LR2SpecialGraphic,
    onLoaded: () => void,
  ): Texture | undefined {
    return this.resolveWith(() => source, chartEntry, path, onLoaded);
  }
 
  private resolveWith(
    resolveSource: () => BrowserAssetSource | undefined,
    chartEntry: LibraryChart,
    path: LR2SpecialGraphic,
    onLoaded: () => void,
  ): Texture | undefined {
    const solidTexture = resolveSolidSpecialGraphicTexture(path);
    if (solidTexture) {
      return solidTexture;
    }
    const cacheKey = chartGraphicCacheKey(chartEntry, path);
    const cached = this.textures.get(cacheKey);
    if (cached) {
      return cached;
    }
    if (!this.pending.has(cacheKey)) {
      this.pending.add(cacheKey);
      void this.load(resolveSource, chartEntry, path, cacheKey, onLoaded);
    }
    return undefined;
  }
 
  public dispose(): void {
    Iif (this.disposed) {
      return;
    }
    this.disposed = true;
    destroyTextureMap(this.textures);
    this.pending.clear();
  }
 
  private async load(
    resolveSource: () => BrowserAssetSource | undefined,
    chartEntry: LibraryChart,
    path: LR2SpecialGraphic,
    cacheKey: string,
    onLoaded: () => void,
  ): Promise<void> {
    try {
      const assetPath = resolveSpecialGraphicAssetPath(chartEntry, path);
      if (!assetPath) {
        return;
      }
      const source = resolveSource();
      if (!source) {
        return;
      }
      // Song-bundle assets are lazy `File` references — read the banner / stagefile / backbmp bytes on demand the
      // moment the user focuses this song.
      const bytes = await loadAssetBytes(resolveChartImageAsset(source, chartEntry.chartPath, assetPath));
      if (!bytes) {
        return;
      }
      if (this.disposed) {
        return;
      }
      const texture = await loadTextureFromBytes(assetPath, bytes);
      if (!texture) {
        return;
      }
      if (this.disposed) {
        destroyTextureAndRevokeBlobURL(texture, true);
        return;
      }
      this.textures.set(cacheKey, texture);
      onLoaded();
    } finally {
      this.pending.delete(cacheKey);
    }
  }
}
 
export function collectSelectSkinTexturePaths(skin: LR2Skin): Set<string> {
  return collectSkinTexturePaths(skin, [
    addImageTexturePaths,
    addNumberTexturePaths,
    addSliderTexturePaths,
    addBarBodyTexturePaths,
    addBarLevelTexturePaths,
    addBarLampTexturePaths,
    addBarRankTexturePaths,
    addButtonTexturePaths,
    addOnMouseTexturePaths,
    addMouseCursorTexturePaths,
    addBarFlashTexturePath,
  ]);
}
 
export function collectResultSkinTexturePaths(skin: LR2Skin): Set<string> {
  return collectBaseSkinTexturePaths(skin);
}
 
export function collectDecideSkinTexturePaths(skin: LR2Skin): Set<string> {
  return collectSkinTexturePaths(skin, [addImageTexturePaths, addNumberTexturePaths]);
}
 
function resolveSpecialGraphicAssetPath(chartEntry: LibraryChart, path: LR2SpecialGraphic): string | undefined {
  const meta = chartEntry.chart.metadata;
  return path === LR2_SPECIAL_GRAPHIC.BACKBMP
    ? meta.backBMP
    : path === LR2_SPECIAL_GRAPHIC.BANNER
      ? meta.banner
      : path === LR2_SPECIAL_GRAPHIC.STAGEFILE
        ? meta.stageFile
        : undefined;
}
 
function collectBaseSkinTexturePaths(skin: LR2Skin): Set<string> {
  return collectSkinTexturePaths(skin, [
    addImageTexturePaths,
    addNumberTexturePaths,
    addBargraphTexturePaths,
    addSliderTexturePaths,
  ]);
}
 
function collectSkinTexturePaths(skin: LR2Skin, collectors: readonly SkinTexturePathCollector[]): Set<string> {
  const paths = new Set<string>();
  for (const collector of collectors) {
    collector(paths, skin);
  }
  return paths;
}
 
function addImageTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.images);
}
 
function addNumberTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.numbers);
}
 
function addBargraphTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.bargraphs);
}
 
function addSliderTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.sliders);
}
 
function addBarBodyTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.barLayout.bodies);
}
 
function addBarLevelTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.barLayout.levels);
}
 
function addBarLampTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.barLayout.lamps);
}
 
function addBarRankTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.barLayout.ranks);
}
 
function addButtonTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.buttons);
}
 
function addOnMouseTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.onMouseElements);
}
 
function addMouseCursorTexturePaths(paths: Set<string>, skin: LR2Skin): void {
  addSourceTexturePaths(paths, skin.mouseCursors);
}
 
function addBarFlashTexturePath(paths: Set<string>, skin: LR2Skin): void {
  Eif (skin.barLayout.flash) {
    addSkinTexturePath(paths, skin.barLayout.flash.source.imagePath);
  }
}
 
function addSourceTexturePaths(paths: Set<string>, elements: readonly SkinTextureSourceElement[]): void {
  for (const element of elements) {
    addSkinTexturePath(paths, element.source.imagePath);
  }
}
 
function addSkinTexturePath(paths: Set<string>, path: string): void {
  if (!isLR2SpecialGraphic(path)) {
    paths.add(path);
  }
}
 
export function resolveSolidSpecialGraphicTexture(path: LR2SpecialGraphic): Texture | undefined {
  return path === LR2_SPECIAL_GRAPHIC.BLACK || path === LR2_SPECIAL_GRAPHIC.WHITE ? Texture.WHITE : undefined;
}
 
function chartGraphicCacheKey(chartEntry: LibraryChart, path: LR2SpecialGraphic): string {
  return `${chartEntry.id}:${path}`;
}
 
function destroyTextureMap(textures: Map<string, Texture>): void {
  for (const texture of textures.values()) {
    destroyTextureAndRevokeBlobURL(texture, true);
  }
  textures.clear();
}