UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

90 lines (89 loc) 2.62 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 Log_1 = __importDefault(require("../core/Log")); class JsonSchemaDefinition { _file; _id; _isLoaded = false; data; _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 id() { return this._id; } set id(newId) { this._id = newId; } get shortId() { if (this._id !== undefined) { if (this._id.startsWith("minecraft:")) { return this._id.substring(10, this._id.length); } return this._id; } return undefined; } _ensureDataInitialized() { if (this.data === undefined) { this.data = {}; } } static async ensureOnFile(file, loadHandler) { let rbd; if (file.manager === undefined) { rbd = new JsonSchemaDefinition(); rbd.file = file; file.manager = rbd; } if (file.manager !== undefined && file.manager instanceof JsonSchemaDefinition) { rbd = file.manager; if (!rbd.isLoaded && loadHandler) { rbd.onLoaded.subscribe(loadHandler); } await rbd.load(); } return rbd; } persist() { if (this._file === undefined) { return false; } Log_1.default.assert(this.data !== null, "JSDP"); if (this.data) { return this._file.setObjectContentIfSemanticallyDifferent(this.data); } return false; } async load() { if (this._file === undefined || this._isLoaded) { return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (this._file.content === null || this._file.content instanceof Uint8Array) { return; } this.data = StorageUtilities_1.default.getJsonObject(this._file); this._isLoaded = true; } } exports.default = JsonSchemaDefinition;