UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

156 lines (155 loc) 5.18 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 ste_events_1 = require("ste-events"); const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities")); const Log_1 = __importDefault(require("../core/Log")); /** * Manages persistence of structure design data in accessory folders. * Follows the same pattern as ModelDesignDefinition and ImageEditsDefinition. */ class StructureDesignDefinition { _file; _previewFile; _isLoaded = false; data; project; _onLoaded = new ste_events_1.EventDispatcher(); get isLoaded() { return this._isLoaded; } get file() { return this._file; } set file(newFile) { this._file = newFile; } get onLoaded() { return this._onLoaded.asEvent(); } get design() { return this.data?.design; } get wiredTo() { return this.data?.wiredTo; } /** * Updates the design data and persists it. */ async updateDesign(design, options) { this.data = { design, meta: { generatedAt: new Date().toISOString(), generatorModel: options?.generatorModel, prompt: options?.prompt, schemaVersion: "1.0.0", }, wiredTo: options?.wiredTo, }; await this.save(); } /** * Saves a preview image to the accessory folder. */ async savePreview(imageData) { if (!this._file || !this._file.parentFolder) { Log_1.default.debug("StructureDesignDefinition: Cannot save preview - no file set"); return; } this._previewFile = this._file.parentFolder.ensureFile("preview.png"); this._previewFile.setContent(imageData); await this._previewFile.saveContent(false); Log_1.default.debug(`StructureDesignDefinition: Saved preview to ${this._previewFile.storageRelativePath}`); } /** * Gets the preview image data if it exists. */ async getPreview() { if (!this._file || !this._file.parentFolder) { return undefined; } const previewFile = this._file.parentFolder.ensureFile("preview.png"); if (!(await previewFile.exists())) { return undefined; } if (!previewFile.isContentLoaded) { await previewFile.loadContent(); } if (previewFile.content instanceof Uint8Array) { return previewFile.content; } return undefined; } /** * Creates or gets a StructureDesignDefinition for a ProjectItem's accessory folder. * Use this when you have the ProjectItem for the .mcstructure file. */ static async ensureAsAccessoryOnProjectItem(projectItem) { const accessoryFolder = await projectItem.ensureAccessoryFolder(); const designFile = accessoryFolder.ensureFile("structure_design.json"); return await StructureDesignDefinition.ensureOnFile(designFile, projectItem.project); } /** * Creates or gets a StructureDesignDefinition attached to a file. */ static async ensureOnFile(file, project, loadHandler) { let structureDesign; if (file.manager === undefined) { structureDesign = new StructureDesignDefinition(); structureDesign.project = project; structureDesign.file = file; file.manager = structureDesign; } if (file.manager !== undefined && file.manager instanceof StructureDesignDefinition) { structureDesign = file.manager; if (!structureDesign.isLoaded && loadHandler) { structureDesign.onLoaded.subscribe(loadHandler); } await structureDesign.load(); return structureDesign; } return structureDesign; } /** * Persists the design data to file (only if semantically different). */ async persist() { if (this._file === undefined) { return false; } return this._file.setObjectContentIfSemanticallyDifferent(this.data); } /** * Saves the design data to file. */ async save() { if (this._file === undefined) { return; } await this.persist(); await this._file.saveContent(false); } /** * Loads the design data from file. */ async load() { if (this._file === undefined || this._isLoaded) { return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (this._file.content === null || this._file.content instanceof Uint8Array) { return; } this.data = StorageUtilities_1.default.getJsonObject(this._file); this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = StructureDesignDefinition;