UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

205 lines (204 loc) 7.46 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 DataFormProcessor_1 = __importStar(require("../dataform/DataFormProcessor")); const Database_1 = __importDefault(require("./Database")); class RenderControllerSetDefinition { _file; _isLoaded = false; _loadedWithComments = false; _data; _onLoaded = new ste_events_1.EventDispatcher(); id; get data() { return this._data; } get idList() { if (!this._data || !this._data.render_controllers) { return undefined; } const idList = new Set(); for (const key in this._data.render_controllers) { const rc = this._data.render_controllers[key]; if (key && rc) { idList.add(key); } } return idList; } get renderControllers() { if (!this._data || !this._data.render_controllers) { return undefined; } const rcList = []; for (const key in this._data.render_controllers) { const rc = this._data.render_controllers[key]; if (key && rc) { rcList.push(rc); } } return rcList; } get isLoaded() { return this._isLoaded; } get file() { return this._file; } set file(newFile) { this._file = newFile; } get onLoaded() { return this._onLoaded.asEvent(); } async removeTexture(textureId) { await this.load(); const rcs = this._data?.render_controllers; if (!rcs) { return; } for (const rcKey in rcs) { const rc = rcs[rcKey]; if (rc) { if (rc.arrays.textures) { for (const textureListName in rc.arrays.textures) { const textureList = rc.arrays.textures[textureListName]; let newTextureList = []; if (textureList) { for (const textureStr of textureList) { if (textureStr !== textureId && textureStr !== "Texture." + textureId) { newTextureList.push(textureStr); } } } if (newTextureList.length === 0) { newTextureList = undefined; } rc.arrays.textures[textureListName] = newTextureList; } } } } } static async ensureOnFile(file, loadHandler) { let rc; if (file.manager === undefined) { rc = new RenderControllerSetDefinition(); rc.file = file; file.manager = rc; } if (file.manager !== undefined && file.manager instanceof RenderControllerSetDefinition) { rc = file.manager; if (!rc.isLoaded) { if (loadHandler) { rc.onLoaded.subscribe(loadHandler); } await rc.load(); } } return rc; } async persist() { if (this._file === undefined) { return false; } if (this._data) { const renderControllerSet = await Database_1.default.ensureFormLoaded("resource", "render_controller_set"); if (renderControllerSet) { await DataFormProcessor_1.default.process(this._data, renderControllerSet, DataFormProcessor_1.ProcessorFixupLevel.perField); } } return this._file.setObjectContentIfSemanticallyDifferent(this._data); } ensureDefinition(name, description) { if (!this._data) { this._data = { format_version: "1.12.0", render_controllers: {}, }; } } async save() { if (this._file === undefined) { return; } if (await this.persist()) { await this._file.saveContent(false); } } /** * 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) { return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (this._file.content === null || this._file.content instanceof Uint8Array) { this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); return; } // Use comment-preserving parser only when needed for editing this._data = preserveComments ? StorageUtilities_1.default.getJsonObjectWithComments(this._file) : StorageUtilities_1.default.getJsonObject(this._file); this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); } } exports.default = RenderControllerSetDefinition;