@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
124 lines (123 loc) • 4.5 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 StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const IProjectItemData_1 = require("../app/IProjectItemData");
const JigsawTemplatePoolDefinition_1 = __importDefault(require("./JigsawTemplatePoolDefinition"));
class JigsawStructureDefinition {
_file;
_data;
_isLoaded = false;
_loadedWithComments = false;
get data() {
return this._data;
}
get file() {
return this._file;
}
set file(newFile) {
this._file = newFile;
}
get isLoaded() {
return this._isLoaded;
}
get id() {
if (this._data?.["minecraft:jigsaw"]?.description?.identifier) {
return this._data["minecraft:jigsaw"].description.identifier;
}
return "";
}
get startPool() {
if (this._data?.["minecraft:jigsaw"]?.start_pool) {
return this._data["minecraft:jigsaw"].start_pool;
}
return undefined;
}
async getFormatVersionIsCurrent() {
// For now, assume format version is current
return true;
}
async addChildItems(project, item) {
if (!this.startPool) {
return;
}
const templatePoolItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.jigsawTemplatePool);
// Find template pool referenced by start_pool
for (const candItem of templatePoolItems) {
if (!candItem.isContentLoaded) {
await candItem.loadContent();
}
if (candItem.primaryFile) {
const templatePool = await JigsawTemplatePoolDefinition_1.default.ensureOnFile(candItem.primaryFile);
if (templatePool && templatePool.id === this.startPool) {
item.addChildItem(candItem);
}
}
}
}
static async ensureOnFile(file) {
let jigsawStructure;
if (file.manager === undefined) {
jigsawStructure = new JigsawStructureDefinition();
jigsawStructure.file = file;
file.manager = jigsawStructure;
}
if (file.manager !== undefined && file.manager instanceof JigsawStructureDefinition) {
jigsawStructure = file.manager;
if (!jigsawStructure.isLoaded) {
await jigsawStructure.load();
}
}
return jigsawStructure;
}
/**
* 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) {
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
if (!this._file.content || this._file.content instanceof Uint8Array) {
this._isLoaded = true;
this._loadedWithComments = preserveComments;
return;
}
// Use comment-preserving parser only when needed for editing
const result = preserveComments
? StorageUtilities_1.default.getJsonObjectWithComments(this._file)
: StorageUtilities_1.default.getJsonObject(this._file);
if (result) {
this._data = result;
}
this._isLoaded = true;
this._loadedWithComments = preserveComments;
}
persist() {
if (this._file === undefined) {
return false;
}
if (!this._data) {
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this._data);
}
}
exports.default = JigsawStructureDefinition;