UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

232 lines (230 loc) 8.13 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 IProjectItemData_1 = require("../app/IProjectItemData"); const Utilities_1 = require("../core/Utilities"); const Database_1 = require("./Database"); class TerrainTextureCatalogDefinition { constructor() { this._isLoaded = false; this._onLoaded = new ste_events_1.EventDispatcher(); } get isLoaded() { return this._isLoaded; } get data() { return this._data; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } get textureData() { if (!this._data) { return undefined; } if (this._data.texture_data === undefined) { this._data.texture_data = {}; } return this._data.texture_data; } get texturePathList() { if (!this._data || !this._data.texture_data) { return undefined; } const textureList = []; for (const key in this._data.texture_data) { const texturePathArr = this._data.texture_data[key]; if (texturePathArr && texturePathArr.textures) { if (typeof texturePathArr.textures === "string") { textureList.push(texturePathArr.textures.toLowerCase()); } else if (Array.isArray(texturePathArr)) { for (const texturePath of texturePathArr.textures) { if (typeof texturePath === "string") { textureList.push(texturePath.toLowerCase()); } else if (texturePath.path) { textureList.push(texturePath.path.toLowerCase()); } } } } } return textureList; } get texturesIdList() { if (!this._data || !this._data.texture_data) { return undefined; } const textureIdList = []; for (const key in this._data.texture_data) { textureIdList.push(key); } return textureIdList; } static async ensureOnFile(file, loadHandler) { let et; if (file.manager === undefined) { et = new TerrainTextureCatalogDefinition(); et.file = file; file.manager = et; } if (file.manager !== undefined && file.manager instanceof TerrainTextureCatalogDefinition) { et = file.manager; if (!et.isLoaded && loadHandler) { et.onLoaded.subscribe(loadHandler); } await et.load(); } return et; } getAllTexturePaths(textureId) { if (!this.data || !this.data.texture_data) { return undefined; } const elt = this.data.texture_data[textureId]; if (!elt) { return undefined; } if (typeof elt.textures === "string") { return [elt.textures]; } else if (Array.isArray(elt.textures) && elt.textures.length > 0) { const texturePaths = []; for (const tex of elt.textures) { if (typeof tex === "string") { texturePaths.push(tex); } else if (tex.path) { texturePaths.push(tex.path); } } return texturePaths; } return undefined; } getTexture(textureId) { if (!this.data || !this.data.texture_data) { return undefined; } return this.data.texture_data[textureId]; } getDefaultTexturePath(textureId) { if (!this.data || !this.data.texture_data) { return undefined; } const elt = this.data.texture_data[textureId]; if (!elt) { return undefined; } if (typeof elt.textures === "string") { return elt.textures; } else if (Array.isArray(elt.textures) && elt.textures.length > 0) { if (typeof elt.textures[0] === "string") { return elt.textures[0]; } else if (elt.textures[0].path) { return elt.textures[0].path; } } return undefined; } persist() { if (this._file === undefined) { return; } const defString = JSON.stringify(this._data, null, 2); this._file.setContent(defString); } getTextureReferences() { const textureRefs = []; if (this.data?.texture_data) { for (const resourceId in this.data.texture_data) { const resource = this.data.texture_data[resourceId]; if (resource && resource.textures) { if (!textureRefs.includes(resourceId)) { textureRefs.push(resourceId); } } } } return textureRefs; } getPackRootFolder() { let packRootFolder = undefined; if (this.file && this.file.parentFolder) { let parentFolder = this.file.parentFolder; packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("textures", parentFolder); } return packRootFolder; } getRelativePath(file, packRootFolder) { let relativePath = file.getFolderRelativePath(packRootFolder); if (relativePath) { const lastPeriod = relativePath?.lastIndexOf("."); if (lastPeriod >= 0) { relativePath = relativePath?.substring(0, lastPeriod).toLowerCase(); } relativePath = StorageUtilities_1.default.ensureNotStartsWithDelimiter(relativePath); } return relativePath; } async addChildItems(project, item) { const itemsCopy = project.getItemsCopy(); let packRootFolder = this.getPackRootFolder(); let texturePathList = this.texturePathList; for (const candItem of itemsCopy) { if (candItem.itemType === IProjectItemData_1.ProjectItemType.texture && packRootFolder && texturePathList) { await candItem.ensureStorage(); if (candItem.file) { let relativePath = this.getRelativePath(candItem.file, packRootFolder); if (relativePath) { if (texturePathList && texturePathList.includes(relativePath)) { item.addChildItem(candItem); texturePathList = Utilities_1.default.removeItemInArray(relativePath, texturePathList); } } } } } if (texturePathList) { for (const texturePath of texturePathList) { item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, await Database_1.default.isVanillaToken(texturePath)); } } } async load() { if (this._isLoaded) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("TTCDF"); 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._data = data; this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = TerrainTextureCatalogDefinition; //# sourceMappingURL=../maps/minecraft/TerrainTextureCatalogDefinition.js.map