UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

186 lines (185 loc) 7.34 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Log_1 = __importDefault(require("../core/Log")); const ste_events_1 = require("ste-events"); const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities")); const IProjectItemData_1 = require("../app/IProjectItemData"); const Utilities_1 = __importDefault(require("../core/Utilities")); const Database_1 = __importDefault(require("./Database")); class TextureSetDefinition { _data; _file; _isLoaded = false; _loadedWithComments = false; _onLoaded = new ste_events_1.EventDispatcher(); id; get data() { return this._data; } get texturesList() { if (!this._data || !this._data["minecraft:texture_set"]) { return undefined; } const textureList = []; const textureSet = this._data["minecraft:texture_set"]; if (textureSet.metalness_emissive_roughness && typeof textureSet.metalness_emissive_roughness === "string") { textureList.push(this.adaptTexturePath(textureSet.metalness_emissive_roughness)); } if (textureSet.metalness_emissive_roughness_subsurface && typeof textureSet.metalness_emissive_roughness_subsurface === "string") { textureList.push(this.adaptTexturePath(textureSet.metalness_emissive_roughness_subsurface)); } if (textureSet.heightmap && typeof textureSet.heightmap === "string") { textureList.push(this.adaptTexturePath(textureSet.heightmap)); } if (textureSet.normal && typeof textureSet.normal === "string") { textureList.push(this.adaptTexturePath(textureSet.normal)); } return textureList; } get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } adaptTexturePath(path) { path = path.toLowerCase(); if (path.indexOf("/") >= 0) { return path; } if (this._file) { return this._file.parentFolder.fullPath + "/" + path; } return path; } static async ensureOnFile(file, loadHandler) { let tsd; if (file.manager === undefined) { tsd = new TextureSetDefinition(); tsd.file = file; file.manager = tsd; } if (file.manager !== undefined && file.manager instanceof TextureSetDefinition) { tsd = file.manager; if (!tsd.isLoaded) { if (loadHandler) { tsd.onLoaded.subscribe(loadHandler); } await tsd.load(); } } return tsd; } persist() { if (this._file === undefined) { return false; } if (!this._data) { Log_1.default.unexpectedUndefined("TSTDF"); return false; } return this._file.setObjectContentIfSemanticallyDifferent(this._data); } /** * Loads the definition from the file. * @param preserveComments If true, uses comment-preserving JSON parsing for edit/save cycles. * If false (default), uses efficient standard JSON parsing. * Can be called again with true to "upgrade" a read-only load to read/write. */ async load(preserveComments = false) { // If already loaded with comments, we have the "best" version - nothing more to do if (this._isLoaded && this._loadedWithComments) { return; } // If already loaded without comments and caller doesn't need comments, we're done if (this._isLoaded && !preserveComments) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("TSTCDF"); return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (!this._file.content || this._file.content instanceof Uint8Array) { this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); return; } let data = []; // Use comment-preserving parser only when needed for editing let result = preserveComments ? StorageUtilities_1.default.getJsonObjectWithComments(this._file) : StorageUtilities_1.default.getJsonObject(this._file); if (result) { data = result; } this._data = data; this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); } getPackRootFolder() { let packRootFolder = undefined; if (this.file && this.file.parentFolder) { let parentFolder = this.file.parentFolder; packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("textures", parentFolder); } return packRootFolder; } async addChildItems(project, item) { const textureItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.texture); let packRootFolder = this.getPackRootFolder(); let textureListInitial = this.texturesList; let textureList = []; if (!packRootFolder || !textureListInitial) { return; } for (let texturePath of textureListInitial) { texturePath = StorageUtilities_1.default.canonicalizePath(texturePath).toLowerCase(); const basePath = StorageUtilities_1.default.canonicalizePath(packRootFolder.fullPath).toLowerCase(); if (texturePath.startsWith(basePath)) { textureList.push(texturePath.substring(basePath.length + 1).toLowerCase()); } else { textureList.push(texturePath.toLowerCase()); } } for (const candItem of textureItems) { if (packRootFolder && textureList) { if (!candItem.isContentLoaded) { await candItem.loadContent(); } if (candItem.primaryFile) { let relativePath = StorageUtilities_1.default.getBaseRelativePath(candItem.primaryFile, packRootFolder); if (relativePath) { if (textureList && textureList.includes(relativePath)) { item.addChildItem(candItem); textureList = Utilities_1.default.removeItemInArray(relativePath, textureList); } } } } } if (textureList) { for (const texturePath of textureList) { item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, await Database_1.default.matchesVanillaPath(texturePath)); } } } } exports.default = TextureSetDefinition;