UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

110 lines (108 loc) 3.58 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); const Log_1 = require("../core/Log"); const ste_events_1 = require("ste-events"); const StorageUtilities_1 = require("../storage/StorageUtilities"); class TextureDefinition { constructor() { this._isLoaded = false; this._onLoaded = new ste_events_1.EventDispatcher(); } get data() { if (!this._file || !this._file.content || typeof this._file.content === "string") { return undefined; } return this._file.content; } get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } static async ensureOnFile(file, loadHandler) { let texd; if (file.manager === undefined) { texd = new TextureDefinition(); texd.file = file; file.manager = texd; } if (file.manager !== undefined && file.manager instanceof TextureDefinition) { texd = file.manager; if (!texd.isLoaded && loadHandler) { texd.onLoaded.subscribe(loadHandler); } await texd.load(); } return texd; } getReferencePath() { if (!this._file) { return undefined; } let projectPath = this._file.storageRelativePath; return TextureDefinition.getTexturePath(projectPath); } static canonicalizeTexturePath(projectPath) { if (projectPath === undefined) { return undefined; } projectPath = projectPath.toLowerCase(); const lastPeriod = projectPath.lastIndexOf("."); if (lastPeriod >= 0) { const removedPart = projectPath.substring(lastPeriod + 1); if (StorageUtilities_1.AllowedExtensions.includes(removedPart)) { projectPath = projectPath.substring(0, lastPeriod); } } // particles has atlas.terrain in bedrock // JsonUI has # and $ as variabbles /*Log.assert( projectPath.startsWith("textures/") || projectPath.startsWith("images/") || projectPath.startsWith("#") || projectPath.startsWith("$") || projectPath.startsWith("atlas"), "Unexpected texture path: " + projectPath );*/ return projectPath; } static getTexturePath(projectPath) { const lastPeriod = projectPath.lastIndexOf("."); if (lastPeriod >= 0) { projectPath = projectPath.substring(0, lastPeriod); } const ppLower = projectPath.toLowerCase(); const texturesIndex = ppLower.indexOf("/textures/"); if (texturesIndex < 0) { return undefined; } return projectPath.substring(texturesIndex + 1); } persist() { } async load() { if (this._isLoaded) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("TSCDF"); return; } await this._file.loadContent(); if (!this._file.content || typeof this._file.content === "string") { return; } this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = TextureDefinition; //# sourceMappingURL=../maps/minecraft/TextureDefinition.js.map