From cb0e328ec7d1234543e99e67d7c59f718809d91f Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 29 Apr 2019 07:30:49 -0700 Subject: [PATCH] Type getPlaylistStyle --- js/skinParser.js | 35 ++-------------------------------- js/skinParserUtils.ts | 44 +++++++++++++++++++++++++++++++++++++++++-- js/types.ts | 9 +++++++-- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/js/skinParser.js b/js/skinParser.js index 1c7087ae..ef2f0d32 100644 --- a/js/skinParser.js +++ b/js/skinParser.js @@ -1,7 +1,7 @@ import SKIN_SPRITES from "./skinSprites"; import regionParser from "./regionParser"; import { LETTERS, DEFAULT_SKIN } from "./constants"; -import { parseViscolors, parseIni, objectMap } from "./utils"; +import { parseViscolors, objectMap } from "./utils"; import * as SkinParserUtils from "./skinParserUtils"; const shallowMerge = objs => @@ -51,37 +51,6 @@ const CURSORS = [ */ ]; -async function genPlaylistStyle(zip) { - const pledit = await SkinParserUtils.getFileFromZip( - zip, - "PLEDIT", - "txt", - "text" - ); - const data = pledit && parseIni(pledit.contents).text; - if (!data) { - // Corrupt or missing PLEDIT.txt file. - return DEFAULT_SKIN.playlistStyle; - } - - // Winamp seems to permit colors that contain too many characters. - // For compatibility with existing skins, we normalize them here. - ["normal", "current", "normalbg", "selectedbg", "mbFG", "mbBG"].forEach( - colorKey => { - let color = data[colorKey]; - if (!color) { - return; - } - if (color[0] !== "#") { - color = `#${color}`; - } - data[colorKey] = color.slice(0, 7); - } - ); - - return { ...DEFAULT_SKIN.playlistStyle, ...data }; -} - async function genVizColors(zip) { const viscolor = await SkinParserUtils.getFileFromZip( zip, @@ -252,7 +221,7 @@ async function skinParser(zipFileBuffer, JSZip) { genExColors, ] = await Promise.all([ genVizColors(zip), - genPlaylistStyle(zip), + SkinParserUtils.getPlaylistStyle(zip), genImages(zip), genCursors(zip), genRegion(zip), diff --git a/js/skinParserUtils.ts b/js/skinParserUtils.ts index 4093750f..b1238759 100644 --- a/js/skinParserUtils.ts +++ b/js/skinParserUtils.ts @@ -1,19 +1,25 @@ import JSZip from "jszip"; +import { PlaylistStyle } from "./types"; import SKIN_SPRITES, { Sprite } from "./skinSprites"; +import { DEFAULT_SKIN } from "./constants"; +import * as Utils from "./utils"; export const getFileExtension = (fileName: string): string | null => { const matches = /\.([a-z]{3,4})$/i.exec(fileName); return matches ? matches[1].toLowerCase() : null; }; +function getFilenameRegex(base: string, ext: string): RegExp { + return new RegExp(`^(.*/)?${base}(\.${ext})?$`, "i"); +} + export async function getFileFromZip( zip: JSZip, fileName: string, ext: string, mode: "blob" | "text" | "base64" ) { - const regex = new RegExp(`^(.*/)?${fileName}(\.${ext})?$`, "i"); - const files = zip.file(regex); + const files = zip.file(getFilenameRegex(fileName, ext)); if (!files.length) { return null; } @@ -116,3 +122,37 @@ export async function getCursorFromFilename( ? null : `data:image/x-win-bitmap;base64,${file.contents}`; } + +export async function getPlaylistStyle(zip: JSZip): Promise { + const files = zip.file(getFilenameRegex("PLEDIT", "txt")); + const file = files[0]; + if (file == null) { + return DEFAULT_SKIN.playlistStyle; + } + const ini = await file.async("text"); + if (ini == null) { + return DEFAULT_SKIN.playlistStyle; + } + const data = ini && Utils.parseIni(ini).text; + if (!data) { + // Corrupt or missing PLEDIT.txt file. + return DEFAULT_SKIN.playlistStyle; + } + + // Winamp seems to permit colors that contain too many characters. + // For compatibility with existing skins, we normalize them here. + ["normal", "current", "normalbg", "selectedbg", "mbFG", "mbBG"].forEach( + colorKey => { + let color = data[colorKey]; + if (!color) { + return; + } + if (color[0] !== "#") { + color = `#${color}`; + } + data[colorKey] = color.slice(0, 7); + } + ); + + return { ...DEFAULT_SKIN.playlistStyle, ...data }; +} diff --git a/js/types.ts b/js/types.ts index d3ed5f68..02283829 100644 --- a/js/types.ts +++ b/js/types.ts @@ -49,8 +49,13 @@ export type Cursors = { [cursor: string]: string }; export type GenLetterWidths = { [letter: string]: number }; -// TODO: Use a type to ensure the keys are one of the known values in PLEDIT.txt -export type PlaylistStyle = { [state: string]: string }; +export interface PlaylistStyle { + normal: string; + current: string; + normalbg: string; + selectedbg: string; + font: string; +} // TODO: Type these keys. export type SkinImages = { [sprite: string]: string };