@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
199 lines (198 loc) • 7.96 kB
JavaScript
"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 Log_1 = __importDefault(require("../core/Log"));
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const IProjectItemData_1 = require("../app/IProjectItemData");
const Utilities_1 = __importDefault(require("../core/Utilities"));
const Database_1 = __importDefault(require("./Database"));
const TextureDefinition_1 = __importDefault(require("./TextureDefinition"));
class JsonUIResourceDefinition {
jsonUIScreen;
_file;
_isLoaded = false;
_loadedWithComments = false;
_onLoaded = new ste_events_1.EventDispatcher();
get isLoaded() {
return this._isLoaded;
}
get file() {
return this._file;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
set file(newFile) {
this._file = newFile;
}
get namespaceId() {
if (!this.jsonUIScreen || !this.jsonUIScreen["namespace"]) {
return undefined;
}
return this.jsonUIScreen["namespace"];
}
static async ensureOnFile(file, loadHandler) {
let et;
if (file.manager === undefined) {
et = new JsonUIResourceDefinition();
et.file = file;
file.manager = et;
}
if (file.manager !== undefined && file.manager instanceof JsonUIResourceDefinition) {
et = file.manager;
if (!et.isLoaded) {
if (loadHandler) {
et.onLoaded.subscribe(loadHandler);
}
await et.load();
}
}
return et;
}
persist() {
if (this._file === undefined) {
return false;
}
if (!this.jsonUIScreen) {
Log_1.default.unexpectedUndefined("ITRDP");
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this.jsonUIScreen);
}
getControlRefs() {
const namespaceId = this.namespaceId;
const jsonUITextureRefs = [];
if (namespaceId && this.jsonUIScreen) {
for (const jsonControlId in this.jsonUIScreen) {
const jsonControl = this.jsonUIScreen[jsonControlId];
if (jsonControlId !== "namespace" && jsonControl && jsonControl.texture) {
if (!jsonUITextureRefs.includes(jsonControlId)) {
jsonUITextureRefs.push(jsonControlId);
}
}
}
}
return jsonUITextureRefs;
}
getTexturePaths() {
const texturePaths = [];
if (this.jsonUIScreen) {
this.addTexturesFromArrayOfKeyPaths([this.jsonUIScreen], texturePaths);
}
return texturePaths;
}
addTexturesFromArrayOfKeyPaths(controlSets, texturePaths) {
if (!Array.isArray(controlSets)) {
return;
}
for (const controlSet of controlSets) {
for (const key in controlSet) {
const control = controlSet[key];
if (key !== "namespace" && key !== "controls") {
if (typeof control === "string") {
texturePaths.push(control);
}
else {
if (control.texture) {
const tex = TextureDefinition_1.default.canonicalizeTexturePath(control.texture);
if (tex) {
texturePaths.push(tex);
}
}
if (control.controls) {
this.addTexturesFromArrayOfKeyPaths(control.controls, texturePaths);
}
}
}
}
}
}
/**
* 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) {
Log_1.default.unexpectedUndefined("PERPF");
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
if (!this._file.content || this._file.content instanceof Uint8Array) {
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
return;
}
let data = {};
// Use comment-preserving parser only when needed for editing
let result = preserveComments
? StorageUtilities_1.default.getJsonObjectWithComments(this._file)
: StorageUtilities_1.default.getJsonObject(this._file);
if (result) {
data = result;
}
this.jsonUIScreen = data;
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
}
getPackRootFolder() {
let packRootFolder = undefined;
if (this.file && this.file.parentFolder) {
let parentFolder = this.file.parentFolder;
packRootFolder = StorageUtilities_1.default.getParentOfParentFolderNamed("ui", parentFolder);
}
return packRootFolder;
}
async addChildItems(project, item) {
const textureItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.texture);
const uiTextureItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.uiTexture);
const allTextureItems = [...textureItems, ...uiTextureItems];
let packRootFolder = this.getPackRootFolder();
let textureList = this.getTexturePaths();
if (textureList.length > 0) {
for (const candItem of allTextureItems) {
if (packRootFolder && textureList) {
if (!candItem.isContentLoaded) {
await candItem.loadContent();
}
if (candItem.primaryFile) {
let relativePath = TextureDefinition_1.default.canonicalizeTexturePath(StorageUtilities_1.default.getBaseRelativePath(candItem.primaryFile, packRootFolder));
if (relativePath) {
if (textureList && textureList.includes(relativePath)) {
item.addChildItem(candItem);
textureList = Utilities_1.default.removeItemInArray(relativePath, textureList);
}
}
}
}
}
if (textureList) {
for (const texturePath of textureList) {
if (!texturePath.startsWith("$")) {
const isVanilla = await Database_1.default.isVanillaToken(texturePath);
item.addUnfulfilledRelationship(texturePath, IProjectItemData_1.ProjectItemType.texture, isVanilla);
}
}
}
}
}
}
exports.default = JsonUIResourceDefinition;