UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

84 lines (83 loc) 3.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ProjectInfoItem_1 = __importDefault(require("../ProjectInfoItem")); const Pack_1 = require("../../minecraft/Pack"); const IInfoItemData_1 = require("../IInfoItemData"); const ImageUtilites_1 = require("../../storage/ImageUtilites"); var CheckPacksIconsGeneratorTest; (function (CheckPacksIconsGeneratorTest) { CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["NoIconFound"] = 101] = "NoIconFound"; CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["MultipleIconsFound"] = 102] = "MultipleIconsFound"; CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["IconNotValidImage"] = 103] = "IconNotValidImage"; CheckPacksIconsGeneratorTest[CheckPacksIconsGeneratorTest["IconNotValidSize"] = 104] = "IconNotValidSize"; })(CheckPacksIconsGeneratorTest || (CheckPacksIconsGeneratorTest = {})); const IconMaxWidth = 256; const IconMinWidth = 2; /*********** * Generator for validating Pack Icons * * Will ensure: * * exactly 1 icon per pack * * icon is a valid .png * * icon is square * * icon is within size limits * * @see {@link ../../../public/data/forms/mctoolsval/cpackicon.form.json} for topic definitions */ class CheckPackIconsGenerator { id = "CPACKICON"; title = "Pack Icon"; canAlwaysProcess = true; severity = IInfoItemData_1.InfoItemType.error; async generate(project) { const results = []; if (project.isVanillaEditSession) { return results; } for (const pack of project.packs) { if (requiresPackIcon(pack)) { const result = await this.getResultForPack(pack); if (result) { results.push(result); } } } return results; } summarize() { } async getResultForPack(pack) { const icons = await pack.getFiles(ImageUtilites_1.isPackIcon); if (icons.length === 0) { const message = `pack_icon image file not found for ${pack.name}. It must use the .png extension.`; return this.createResult(CheckPacksIconsGeneratorTest.NoIconFound, message); } else if (icons.length > 1) { const message = `Found multiple pack icon files for ${pack.name}, there should be only one.`; return this.createResult(CheckPacksIconsGeneratorTest.MultipleIconsFound, message); } const file = icons[0]; const image = await (0, ImageUtilites_1.parseImageMetadata)(file); if (!image?.ImageWidth || !image?.ImageHeight) { const message = `Pack Image (${file.name}) is not valid`; return this.createResult(CheckPacksIconsGeneratorTest.IconNotValidImage, message); } if (this.isValidSize(image.ImageWidth, image.ImageHeight)) { const message = `pack_icon must be square with size 2, 4, 8, 16, 32, 64, 128, or 256. Found [${image.ImageWidth} x ${image.ImageHeight}] on ${file.name}`; return this.createResult(CheckPacksIconsGeneratorTest.IconNotValidSize, message); } return null; } createResult(test, message) { return new ProjectInfoItem_1.default(this.severity, this.id, test, message); } isValidSize(width, height) { return width !== height || width < IconMinWidth || width > IconMaxWidth || (width & (width - 1)) !== 0; } } exports.default = CheckPackIconsGenerator; function requiresPackIcon(pack) { return pack.packType !== Pack_1.PackType.skin && pack.packType !== Pack_1.PackType.design; }