@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
161 lines (160 loc) • 6.2 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 SoundDefinitionCatalogDefinition_1 = __importDefault(require("./SoundDefinitionCatalogDefinition"));
const Database_1 = __importDefault(require("./Database"));
class MusicDefinitionCatalogDefinition {
_data;
_file;
_isLoaded = false;
_loadedWithComments = false;
_onLoaded = new ste_events_1.EventDispatcher();
id;
get isLoaded() {
return this._isLoaded;
}
get file() {
return this._file;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
set file(newFile) {
this._file = newFile;
}
get musicDefinitionNameList() {
if (!this._data) {
return undefined;
}
const musicDefNameList = [];
for (const key in this._data) {
musicDefNameList.push(key);
}
return musicDefNameList;
}
get musicDefinitionEventNameList() {
if (!this._data) {
return undefined;
}
const musicDefNameList = [];
for (const key in this._data) {
const def = this._data[key];
if (def && def.event_name) {
if (!musicDefNameList.includes(def.event_name)) {
musicDefNameList.push(def.event_name);
}
}
}
return musicDefNameList;
}
static async ensureOnFile(file, loadHandler) {
let et;
if (file.manager === undefined) {
et = new MusicDefinitionCatalogDefinition();
et.file = file;
file.manager = et;
}
if (file.manager !== undefined && file.manager instanceof MusicDefinitionCatalogDefinition) {
et = file.manager;
if (!et.isLoaded && loadHandler) {
et.onLoaded.subscribe(loadHandler);
}
await et.load();
}
return et;
}
persist() {
if (this._file === undefined) {
return false;
}
if (!this._data) {
Log_1.default.unexpectedUndefined("MUSP");
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this._data);
}
/**
* 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("TTCDF");
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._data = data;
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
}
async addChildItems(project, item) {
const soundDefItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.soundDefinitionCatalog);
let musicDefList = this.musicDefinitionEventNameList;
for (const candItem of soundDefItems) {
if (musicDefList) {
if (!candItem.isContentLoaded) {
await candItem.loadContent();
}
if (candItem.primaryFile) {
const soundDef = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(candItem.primaryFile);
const soundSetNames = soundDef?.getSoundDefinitionSetNameList();
if (soundSetNames) {
for (const musicDefName of musicDefList) {
if (soundSetNames.includes(musicDefName)) {
item.addChildItem(candItem);
const nextMusicDefNames = [];
for (const texturePath of musicDefList) {
if (texturePath !== musicDefName) {
nextMusicDefNames.push(texturePath);
}
}
musicDefList = nextMusicDefNames;
}
}
}
}
}
}
if (musicDefList) {
for (const musicDef of musicDefList) {
item.addUnfulfilledRelationship(musicDef, IProjectItemData_1.ProjectItemType.soundDefinitionCatalog, await Database_1.default.isVanillaToken(musicDef));
}
}
}
}
exports.default = MusicDefinitionCatalogDefinition;