UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

96 lines (95 loc) 4.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const IProjectItemData_1 = require("../../app/IProjectItemData"); const ImageUtilites_1 = require("../../storage/ImageUtilites"); const IInfoItemData_1 = require("../IInfoItemData"); const ProjectInfoItem_1 = __importDefault(require("../ProjectInfoItem")); const AsyncUtilities_1 = require("../../core/async/AsyncUtilities"); const Log_1 = __importDefault(require("../../core/Log")); const DefaultEduImageWidth = 480; const DefaultEduImageHeight = 270; const DefaultBedrockImageWidth = 800; const DefaultBedrockImageHeight = 450; var CheckWorldIconsGeneratorTest; (function (CheckWorldIconsGeneratorTest) { CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["NoIconFound"] = 101] = "NoIconFound"; CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["MultipleIconsFound"] = 102] = "MultipleIconsFound"; CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["IconNotValidImage"] = 103] = "IconNotValidImage"; CheckWorldIconsGeneratorTest[CheckWorldIconsGeneratorTest["IconNotValidSize"] = 104] = "IconNotValidSize"; })(CheckWorldIconsGeneratorTest || (CheckWorldIconsGeneratorTest = {})); /* * Generator for validating World Icons * * Will ensure: * * exactly 1 icon per world folder (folders with a world manifest) * * icon is a valid .jpeg * * icon is the correct size * * @see {@link ../../../public/data/forms/mctoolsval/cwi.form.json} for topic definitions */ class CheckWorldIconsGenerator { id = "CWI"; title = "World Icons"; canAlwaysProcess = true; severity = IInfoItemData_1.InfoItemType.error; async generate(project) { const itemsCopy = project.getItemsCopy(); const isEDUOffer = itemsCopy.find((item) => item.itemType === IProjectItemData_1.ProjectItemType.educationJson) !== undefined; const worldFolders = []; for (const projectItem of itemsCopy) { if (projectItem.itemType === IProjectItemData_1.ProjectItemType.worldTemplateManifestJson) { if (!projectItem.isContentLoaded) { await projectItem.loadContent(); } const folder = projectItem.getFolder(); if (folder) { worldFolders.push(folder); } } } const results = []; for (const folder of worldFolders) { Log_1.default.assert(folder !== null, "Manifest file cannot exist without a folder"); const resultForFolder = await this.validateWorldIconForFolder(folder, isEDUOffer); results.push(...resultForFolder); } return results; } async validateWorldIconForFolder(folder, isEDUOffer) { const files = await (0, AsyncUtilities_1.filterAsync)(folder.allFiles, ImageUtilites_1.isWorldIcon); if (files.length === 0) { const message = `No World Icon found for ${folder.getFolderRelativePath}.`; return [this.createResult(CheckWorldIconsGeneratorTest.NoIconFound, message)]; } else if (files.length > 1) { const message = `Too many World Icons found for ${folder.getFolderRelativePath}. ${files.length} icons found, expected: 1`; return [this.createResult(CheckWorldIconsGeneratorTest.MultipleIconsFound, message)]; } const file = files[0]; const image = await (0, ImageUtilites_1.parseImageMetadata)(file); if (!image) { const message = `Image (${file.name}) is not valid.`; return [this.createResult(CheckWorldIconsGeneratorTest.IconNotValidImage, message)]; } const isSizeCorrect = isEDUOffer ? isSizeCorrectForEDU : isSizeCorrectForBedrock; if (!isSizeCorrect({ width: image.ImageWidth, height: image.ImageHeight })) { return [this.createResult(CheckWorldIconsGeneratorTest.IconNotValidSize, `Image (${file.name}) is not valid.`)]; } return []; } createResult(test, message) { return new ProjectInfoItem_1.default(this.severity, this.id, test, message); } summarize() { } } exports.default = CheckWorldIconsGenerator; function isSizeCorrectForBedrock(size) { return size.width === DefaultBedrockImageWidth && size.height === DefaultBedrockImageHeight; } function isSizeCorrectForEDU(size) { const isEDUSize = size.width === DefaultEduImageWidth && size.height === DefaultEduImageHeight; return isEDUSize || isSizeCorrectForBedrock(size); }