UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

174 lines (173 loc) 5.61 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 DocumentedCommand_1 = __importDefault(require("./DocumentedCommand")); const Project_1 = require("../../app/Project"); const StorageUtilities_1 = __importDefault(require("../../storage/StorageUtilities")); class DocumentedCommandSet { _file; _id; _name; _version; _isLoaded = false; _docFolder; _docCommands = {}; commandSetDefinition; _onLoaded = new ste_events_1.EventDispatcher(); get isLoaded() { return this._isLoaded; } get docFolder() { return this._docFolder; } get commands() { return this._docCommands; } get file() { return this._file; } set file(newFile) { this._file = newFile; } get onLoaded() { return this._onLoaded.asEvent(); } get name() { if (this.commandSetDefinition) { return this.commandSetDefinition.name; } return this._name; } get id() { return this._id; } set id(newId) { this._id = newId; if (newId) { const underscore = newId.lastIndexOf("_"); if (underscore >= 0 && underscore < newId.length - 2) { this._name = newId.substring(0, underscore); this._version = newId.substring(underscore + 1); } else { this._name = newId; this._version = ""; } } } constructor(docFolder) { this._docFolder = docFolder; } getEnum(name) { const enums = this.commandSetDefinition?.command_enums; if (enums) { name = name.toLowerCase(); for (const enumItem of enums) { if (enumItem.name.toLowerCase() === name) { return enumItem; } } } return undefined; } static async ensureOnFile(file, docFolder, loadHandler) { let dt; if (file.manager === undefined) { dt = new DocumentedCommandSet(docFolder); dt.file = file; file.manager = dt; } if (file.manager !== undefined && file.manager instanceof DocumentedCommandSet) { dt = file.manager; if (!dt.isLoaded) { if (loadHandler) { dt.onLoaded.subscribe(loadHandler); } await dt.load(); } } return dt; } ensureDocFolder(docsFolder) { if (!this.name) { return; } let folderName = this.name; folderName = folderName.toLowerCase(); if (Project_1.remappedMinecraftScriptModules[folderName]) { folderName = Project_1.remappedMinecraftScriptModules[folderName]; } const folders = folderName.split("/"); let curFolder = docsFolder; for (let i = 0; i < folders.length; i++) { curFolder = curFolder.ensureFolder(folders[i]); } return curFolder; } isEnumUsedInMultipleCommands(enumInstance) { let commandCount = 0; for (const commandName in this.commands) { const command = this.commands[commandName]; if (command) { let isInCommand = false; const overloads = command.getOverloads(); for (const overload of overloads) { for (const param of overload.params) { if (param.type && param.type.name.toUpperCase() === enumInstance.name.toUpperCase()) { isInCommand = true; } } } if (isInCommand) { commandCount++; if (commandCount > 1) { return true; } } } } return false; } async persist() { if (this._file === undefined) { return false; } let didPersist = false; for (const docCommandName in this._docCommands) { const docCommand = this._docCommands[docCommandName]; if (await docCommand.persist()) { didPersist = true; } } return didPersist; } 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) { this._isLoaded = true; this._onLoaded.dispatch(this, this); return; } this.id = this._file.name; // Use getJsonObject (not getJsonObjectWithComments) since this is read-only metadata this.commandSetDefinition = StorageUtilities_1.default.getJsonObject(this._file); this._docCommands = {}; if (this.commandSetDefinition) { for (const docClass of this.commandSetDefinition?.commands) { this._docCommands[docClass.name] = new DocumentedCommand_1.default(this, docClass); } } this._isLoaded = true; } } exports.default = DocumentedCommandSet;