UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

133 lines (132 loc) 4.5 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); const IFile_1 = require("../storage/IFile"); const ste_events_1 = require("ste-events"); const ICreatorToolsData_1 = require("../app/ICreatorToolsData"); class EnvSettings { _file; _id; _isLoaded = false; _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 envf; if (file.manager === undefined) { envf = new EnvSettings(); envf.file = file; file.manager = envf; } if (file.manager !== undefined && file.manager instanceof EnvSettings) { envf = file.manager; if (!envf.isLoaded && loadHandler) { envf.onLoaded.subscribe(loadHandler); } await envf.load(); return envf; } return envf; } async ensureEnvFile(project) { if (this._file === undefined) { return; } await this.load(); let content = this._file?.content; if (content === null) { content = ""; } if (content && typeof content === "string") { } } async persist() { if (this._file === undefined) { return false; } return false; } async save() { if (this._file === undefined) { return; } if (this._file.isContentLoaded && this.project && this._file.content && typeof this._file.content === "string") { this._file.setContent(await EnvSettings.getContent(this.project, this._file.content), IFile_1.FileUpdateType.versionlessEdit); } await this._file.saveContent(false); } async load() { if (this._file === undefined || this._isLoaded) { return; } await this._file.loadContent(true); if (this._file.content === null || this._file.content instanceof Uint8Array) { return; } this._isLoaded = true; } static async getContent(project, content) { if (content === undefined) { content = 'PROJECT_NAME=""\nMINECRAFT_PRODUCT="BedrockGDK"\nCUSTOM_DEPLOYMENT_PATH=""\n'; } const folder = await project.getDefaultBehaviorPackFolder(); if (folder) { let projectNameIndex = content.indexOf('PROJECT_NAME="'); if (projectNameIndex >= 0) { let projectNameNewQuote = content.indexOf('"', projectNameIndex + 14); if (projectNameNewQuote > projectNameIndex) { content = content.substring(0, projectNameIndex) + 'PROJECT_NAME="' + folder.name + "" + content.substring(projectNameNewQuote); } } else { if (!content.endsWith("\n")) { content += "\n"; } content = content + 'PROJECT_NAME="' + folder.name + '"\n'; } } let minecraftProductIndex = content.indexOf('MINECRAFT_PRODUCT="'); const trackStr = project.track === ICreatorToolsData_1.MinecraftTrack.preview ? "PreviewGDK" : "BedrockGDK"; if (minecraftProductIndex >= 0) { let minecraftProductIndexNextQuote = content.indexOf('"', minecraftProductIndex + 19); if (minecraftProductIndexNextQuote > minecraftProductIndex) { content = content.substring(0, minecraftProductIndex) + 'MINECRAFT_PRODUCT="' + trackStr + content.substring(minecraftProductIndexNextQuote); } } else { if (!content.endsWith("\n")) { content += "\n"; } content = content + 'MINECRAFT_PRODUCT="' + trackStr + '"\n'; } return content; } } exports.default = EnvSettings;