UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

207 lines (205 loc) 7.81 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); const Log_1 = require("../core/Log"); const ste_events_1 = require("ste-events"); const StorageUtilities_1 = require("../storage/StorageUtilities"); const ContentIndex_1 = require("../core/ContentIndex"); const Database_1 = require("./Database"); const IProjectItemData_1 = require("../app/IProjectItemData"); const BlockTypeDefinition_1 = require("./BlockTypeDefinition"); class BlocksCatalogDefinition { constructor() { this._isLoaded = false; this._onLoaded = new ste_events_1.EventDispatcher(); } get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } static async ensureOnFile(file, loadHandler) { let et; if (file.manager === undefined) { et = new BlocksCatalogDefinition(); et.file = file; file.manager = et; } if (file.manager !== undefined && file.manager instanceof BlocksCatalogDefinition) { et = file.manager; if (!et.isLoaded && loadHandler) { et.onLoaded.subscribe(loadHandler); } await et.load(); } return et; } getBlockDefinition(id) { if (!this.blocksCatalog) { return undefined; } if (this.blocksCatalog[id]) { return this.blocksCatalog[id]; } const colon = id.indexOf(":"); if (colon >= 0) { id = id.substring(colon + 1); } return this.blocksCatalog[id]; } ensureBlockDefinition(id) { if (!this.blocksCatalog) { this.blocksCatalog = {}; } if (this.blocksCatalog[id]) { return this.blocksCatalog[id]; } const colon = id.indexOf(":"); if (colon >= 0) { let noColonId = id.substring(colon + 1); if (this.blocksCatalog[noColonId]) { return this.blocksCatalog[noColonId]; } } this.blocksCatalog[id] = {}; return this.blocksCatalog[id]; } getDefaultTextureId(id) { const ref = this.getBlockDefinition(id); if (ref && ref.textures) { if (typeof ref.textures === "string") { return ref.textures; } if (ref.textures["side"]) { return ref.textures["side"]; } else if (ref.textures["up"]) { return ref.textures["up"]; } else { for (const key in ref.textures) { return ref.textures[key]; } } } return undefined; } getTextureReferences() { const textureRefs = []; if (this.blocksCatalog) { for (const resourceId in this.blocksCatalog) { const resource = this.blocksCatalog[resourceId]; if (resource && resource.textures) { if (!textureRefs.includes(resourceId)) { textureRefs.push(resourceId); } } } } return textureRefs; } async getDependenciesList(project) { const dependencies = { unused: [], vanillaOverride: [], }; if (this.blocksCatalog) { const myBlockIds = {}; let projectItemsCopy = project.getItemsCopy(); for (const item of projectItemsCopy) { if (item.itemType === IProjectItemData_1.ProjectItemType.blockTypeBehavior) { await item.ensureFileStorage(); if (item.file) { const blockTypeDef = await BlockTypeDefinition_1.default.ensureOnFile(item.file); if (blockTypeDef && blockTypeDef.id) { myBlockIds[blockTypeDef.id] = true; const colon = blockTypeDef.id.indexOf(":"); if (colon >= 0) { const noColonId = blockTypeDef.id.substring(colon + 1); myBlockIds[noColonId] = true; } } } } } for (const resourceId in this.blocksCatalog) { if (resourceId !== "format_version") { const resource = this.blocksCatalog[resourceId]; if (resource && (resource.textures || resource.sound || resource.carried_textures)) { if (!dependencies.unused.includes(resourceId) && !dependencies.vanillaOverride.includes(resourceId)) { let foundMatch = myBlockIds[resourceId] === true; if (!foundMatch) { let resourceColon = resourceId.indexOf(":"); if (resourceColon < 0) { const vanillaTermMatches = await Database_1.default.getVanillaMatches("minecraft:" + resourceId, true, [ ContentIndex_1.AnnotationCategory.blockTypeSource, ]); if (vanillaTermMatches && vanillaTermMatches.length > 0) { dependencies.vanillaOverride.push(resourceId); foundMatch = true; } } else if (resourceId.startsWith("minecraft:")) { const vanillaTermMatches = await Database_1.default.getVanillaMatches(resourceId, true, [ ContentIndex_1.AnnotationCategory.blockTypeSource, ]); if (vanillaTermMatches && vanillaTermMatches.length > 0) { dependencies.vanillaOverride.push(resourceId); foundMatch = true; } } } if (!foundMatch) { dependencies.unused.push(resourceId); } } } } } } return dependencies; } removeId(id) { if (this.blocksCatalog) { this.blocksCatalog[id] = undefined; } } persist() { if (this._file === undefined) { return; } const defString = JSON.stringify(this.blocksCatalog, null, 2); this._file.setContent(defString); } async load() { if (this._isLoaded) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("BCRDF"); return; } await this._file.loadContent(); if (!this._file.content || this._file.content instanceof Uint8Array) { return; } let data = {}; let result = StorageUtilities_1.default.getJsonObject(this._file); if (result) { data = result; } this.blocksCatalog = data; this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = BlocksCatalogDefinition; //# sourceMappingURL=../maps/minecraft/BlocksCatalogDefinition.js.map