@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
133 lines (131 loc) • 4.79 kB
JavaScript
"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");
const IProjectItemData_1 = require("../app/IProjectItemData");
const SoundDefinitionCatalogDefinition_1 = require("./SoundDefinitionCatalogDefinition");
const Database_1 = require("./Database");
class MusicDefinitionCatalogDefinition {
constructor() {
this._isLoaded = false;
this._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 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;
}
const defString = JSON.stringify(this._data, null, 2);
this._file.setContent(defString);
}
async load() {
if (this._isLoaded) {
return;
}
if (this._file === undefined) {
Log_1.default.unexpectedUndefined("TTCDF");
return;
}
await this._file.loadContent();
if (!this._file.content || this._file.content instanceof Uint8Array) {
return;
}
let data = {};
let result = StorageUtilities_1.default.getJsonObject(this._file);
if (result) {
data = result;
}
this._data = data;
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
}
async addChildItems(project, item) {
const itemsCopy = project.getItemsCopy();
let musicDefList = this.musicDefinitionEventNameList;
for (const candItem of itemsCopy) {
if (candItem.itemType === IProjectItemData_1.ProjectItemType.soundDefinitionCatalog && musicDefList) {
await candItem.ensureStorage();
if (candItem.file) {
const soundDef = await SoundDefinitionCatalogDefinition_1.default.ensureOnFile(candItem.file);
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;
//# sourceMappingURL=../maps/minecraft/MusicDefinitionCatalogDefinition.js.map