UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

206 lines (205 loc) 8.09 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 MinecraftUtilities_1 = __importDefault(require("./MinecraftUtilities")); const IProjectItemData_1 = require("../app/IProjectItemData"); const Database_1 = __importDefault(require("./Database")); const Utilities_1 = __importDefault(require("../core/Utilities")); const TextureDefinition_1 = __importDefault(require("./TextureDefinition")); class ParticleEffectResourceDefinition { _data; _file; _isLoaded = false; _loadedWithComments = false; _onLoaded = new ste_events_1.EventDispatcher(); get data() { return this._data; } get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } get id() { if (!this._data || !this._data.particle_effect || !this._data.particle_effect.description) { return undefined; } return this._data.particle_effect.description.identifier; } get description() { if (!this._data || !this._data.particle_effect || !this._data.particle_effect.description) { return undefined; } return this._data.particle_effect.description; } getCanonicalizedTexturesList() { if (!this.description || !this.description.basic_render_parameters || !this.description.basic_render_parameters.texture) { return undefined; } const result = TextureDefinition_1.default.canonicalizeTexturePath(this.description.basic_render_parameters.texture); return result ? [result] : []; } async getFormatVersionIsCurrent() { const fv = this.getFormatVersion(); if (fv === undefined || fv.length !== 3) { return false; } return fv[0] > 1 || fv[1] >= 10; } getFormatVersion() { if (!this._data) { return undefined; } return MinecraftUtilities_1.default.getVersionArrayFrom(this._data.format_version); } get formatVersion() { if (!this._data || !this._data.format_version) { return undefined; } return this._data.format_version; } static async ensureOnFile(file, loadHandler) { let et; if (file.manager === undefined) { et = new ParticleEffectResourceDefinition(); et.file = file; file.manager = et; } if (file.manager !== undefined && file.manager instanceof ParticleEffectResourceDefinition) { et = file.manager; if (!et.isLoaded) { if (loadHandler) { et.onLoaded.subscribe(loadHandler); } await et.load(); } } return et; } persist() { if (this._file === undefined) { return false; } if (!this._data) { 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("PERPF"); 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); } async deleteLinkToChild(rel) { let packRootFolder = this.getPackRootFolder(); if (this._data === undefined) { await this.load(); } if (!this.description || !this.description.basic_render_parameters || !this.description.basic_render_parameters.texture) { return; } const basicTexture = this.description.basic_render_parameters.texture; if (rel.childItem.itemType === IProjectItemData_1.ProjectItemType.texture) { if (!rel.childItem.isContentLoaded) { await rel.childItem.loadContent(); } if (rel.childItem.primaryFile && packRootFolder) { let relativePath = StorageUtilities_1.default.getBaseRelativePath(rel.childItem.primaryFile, packRootFolder); if (relativePath) { if (basicTexture === relativePath) { this.description.basic_render_parameters.texture = undefined; } } } } this.persist(); } getPackRootFolder() { let packRootFolder = undefined; if (this.file && this.file.parentFolder) { let parentFolder = this.file.parentFolder; packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("particles", parentFolder); } return packRootFolder; } async addChildItems(project, item, index) { let packRootFolder = this.getPackRootFolder(); let textureList = this.getCanonicalizedTexturesList(); // Textures still need path-based matching (no ID-based index possible) if (packRootFolder && textureList && textureList.length > 0) { const textureItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.texture); for (const candItem of textureItems) { 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) { const isVanillaToken = await Database_1.default.isVanillaToken(texturePath); item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, isVanillaToken); } } } } exports.default = ParticleEffectResourceDefinition;