Type getPlaylistStyle

This commit is contained in:
Jordan Eldredge 2019-04-29 07:30:49 -07:00
parent 4dfc19a527
commit cb0e328ec7
3 changed files with 51 additions and 37 deletions

View file

@ -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),

View file

@ -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<PlaylistStyle> {
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 };
}

View file

@ -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 };