UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

184 lines (183 loc) 6.22 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 BehaviorManifestDefinition_1 = __importDefault(require("../minecraft/BehaviorManifestDefinition")); const Log_1 = __importDefault(require("../core/Log")); class VsCodeLaunchDefinition { _file; _id; _isLoaded = false; definition; _onLoaded = new ste_events_1.EventDispatcher(); project = undefined; get isLoaded() { return this._isLoaded; } get file() { return this._file; } set file(newFile) { this._file = newFile; } get onLoaded() { return this._onLoaded.asEvent(); } get id() { return this._id; } set id(newId) { this._id = newId; } static async ensureOnFile(file, loadHandler) { let dt; if (file.manager === undefined) { dt = new VsCodeLaunchDefinition(); dt.file = file; file.manager = dt; } if (file.manager !== undefined && file.manager instanceof VsCodeLaunchDefinition) { dt = file.manager; if (!dt.isLoaded && loadHandler) { dt.onLoaded.subscribe(loadHandler); } await dt.load(); return dt; } return dt; } async hasMinContent(debugSettings) { if (!debugSettings) { debugSettings = { isServer: false }; } await this.load(); if (!this.definition || !this.definition.configurations) { return false; } for (const config of this.definition.configurations) { if (config.type && config.type === "minecraft-js") { if (debugSettings.isServer && config.mode === "listen") { return false; } if (!debugSettings.isServer && config.mode !== "listen") { return false; } if (debugSettings.port && config.port !== debugSettings.port) { return false; } return true; } } return false; } async ensureMinContent(debugSettings) { if (!debugSettings) { debugSettings = { isServer: false }; } const hasDebug = await this.hasMinContent(debugSettings); if (hasDebug) { return true; } if (!this.definition) { this.definition = {}; } if (!this.definition.version || this.definition.version === "0.2.0") { this.definition.version = "0.3.0"; } if (!this.definition.configurations) { this.definition.configurations = []; } let hasMinecraftConfig = false; for (const config of this.definition.configurations) { if (config.type === "minecraft-js") { await this.applyDebugSettingsToConfig(config, debugSettings); hasMinecraftConfig = true; } } if (!hasMinecraftConfig) { const minecraftConfig = { type: "minecraft-js", port: debugSettings.port ? debugSettings.port : 19144, }; await this.applyDebugSettingsToConfig(minecraftConfig, debugSettings); this.definition.configurations.push(minecraftConfig); return true; } return hasMinecraftConfig; } async applyDebugSettingsToConfig(config, debugSettings) { if (debugSettings.isServer) { config.mode = undefined; } else { config.mode = "listen"; } if (config.name === undefined) { config.name = "Debug with Minecraft"; } if (config.preLaunchTask === undefined) { config.preLaunchTask = "build"; } let bpName = debugSettings.behaviorPackFolderName; if (bpName === undefined) { bpName = "starterbp"; } if (!config.targetedModuleUuid && this.project) { const pack = await this.project.getDefaultBehaviorPack(); if (pack) { if (pack.manifest instanceof BehaviorManifestDefinition_1.default) { const module = pack.manifest.getScriptModule(); if (module) { config.targetedModuleUuid = module.uuid; } } } } if (!config.sourceMapRoot) { // eslint-disable-next-line no-template-curly-in-string config.sourceMapRoot = "${workspaceFolder}/dist/debug/"; } if (!config.generatedSourceRoot) { // eslint-disable-next-line no-template-curly-in-string config.generatedSourceRoot = "${workspaceFolder}/dist/scripts/"; } if (debugSettings.port) { config.port = debugSettings.port; } } async persist() { if (this._file === undefined) { return false; } Log_1.default.assert(this.definition !== null, "VSLP"); if (!this.definition) { return false; } return this._file.setObjectContentIfSemanticallyDifferent(this.definition); } async save() { if (this._file === undefined) { return; } this.persist(); await this._file.saveContent(false); } async load() { if (this._file === undefined || this._isLoaded) { return; } await this._file.loadContent(); if (this._file.content === null || this._file.content instanceof Uint8Array) { return; } this.id = this._file.name; this.definition = StorageUtilities_1.default.getJsonObject(this._file); this._isLoaded = true; } } exports.default = VsCodeLaunchDefinition;