@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
388 lines (387 loc) • 16.3 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 Project_1 = require("../app/Project");
const IProjectItemData_1 = require("../app/IProjectItemData");
const SoundDefinitionCatalogDefinition_1 = __importDefault(require("./SoundDefinitionCatalogDefinition"));
const Database_1 = __importDefault(require("./Database"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
const MinecraftDefinitions_1 = __importDefault(require("./MinecraftDefinitions"));
class SoundCatalogDefinition {
_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();
}
get data() {
return this._data;
}
set file(newFile) {
this._file = newFile;
}
get entityIdList() {
if (!this._data) {
return undefined;
}
const entityIdList = [];
if (this._data.entity_sounds && this._data.entity_sounds.entities) {
for (const key in this._data.entity_sounds.entities) {
if (!entityIdList.includes(key)) {
entityIdList.push(key);
}
}
}
return entityIdList;
}
getSoundEventNameList() {
if (!this._data) {
return undefined;
}
const soundEventNameList = [];
if (this._data.entity_sounds && this._data.entity_sounds.entities) {
for (const key in this._data.entity_sounds.entities) {
const def = this._data.entity_sounds.entities[key];
if (def && def.events) {
for (const eventKey in def.events) {
const event = def.events[eventKey];
if (event) {
for (const eventInstanceKey in def.events) {
const eventInstance = def.events[eventInstanceKey];
if (typeof eventInstance === "string") {
if (!soundEventNameList.includes(eventInstance)) {
soundEventNameList.push(eventInstance);
}
}
else if (eventInstance.sound) {
if (!soundEventNameList.includes(eventInstance.sound)) {
soundEventNameList.push(eventInstance.sound);
}
}
}
}
}
}
}
}
if (this._data.entity_sounds && this._data.entity_sounds.defaults && this._data.entity_sounds.defaults.events) {
for (const eventKey in this._data.entity_sounds.defaults.events) {
const eventInstance = this._data.entity_sounds.defaults.events[eventKey];
if (typeof eventInstance === "string") {
if (!soundEventNameList.includes(eventInstance)) {
soundEventNameList.push(eventInstance);
}
}
else if (eventInstance.sound) {
if (!soundEventNameList.includes(eventInstance.sound)) {
soundEventNameList.push(eventInstance.sound);
}
}
}
}
if (this._data.block_sounds) {
for (const key in this._data.block_sounds) {
const def = this._data.block_sounds[key];
if (def && def.events) {
for (const eventKey in def.events) {
const event = def.events[eventKey];
if (event) {
for (const eventInstanceKey in def.events) {
const eventInstance = def.events[eventInstanceKey];
if (typeof eventInstance === "string") {
if (!soundEventNameList.includes(eventInstance)) {
soundEventNameList.push(eventInstance);
}
}
else if (eventInstance.sound) {
if (!soundEventNameList.includes(eventInstance.sound)) {
soundEventNameList.push(eventInstance.sound);
}
}
}
}
}
}
}
}
if (this._data.individual_event_sounds && this._data.individual_event_sounds.events) {
for (const key in this._data.individual_event_sounds.events) {
const eventInstance = this._data.individual_event_sounds.events[key];
if (typeof eventInstance === "string") {
if (!soundEventNameList.includes(eventInstance)) {
soundEventNameList.push(eventInstance);
}
}
else if (eventInstance.sound) {
if (!soundEventNameList.includes(eventInstance.sound)) {
soundEventNameList.push(eventInstance.sound);
}
}
}
}
return soundEventNameList;
}
ensureEntityEvent(idSound) {
this.ensureDefault();
if (!this._data) {
return;
}
let es = this._data.entity_sounds;
if (es === undefined) {
es = {
entities: {},
};
this._data.entity_sounds = es;
}
let entities = es.entities;
if (entities === undefined) {
entities = {};
es.entities = entities;
}
let elt = entities[idSound];
if (!elt) {
if (idSound.startsWith("minecraft:") && Utilities_1.default.isUsableAsObjectKey(idSound.substring(10))) {
elt = entities[idSound.substring(10)];
}
if (!elt) {
elt = {
events: {},
};
}
if (Utilities_1.default.isUsableAsObjectKey(idSound)) {
entities[idSound] = elt;
}
return elt;
}
return elt;
}
ensureDefault() {
if (this._data === undefined) {
this._data = {};
}
}
static async ensureForProject(project) {
const items = project.getItemsCopy();
for (const item of items) {
if (item.itemType === IProjectItemData_1.ProjectItemType.soundCatalog) {
if (!item.isContentLoaded) {
await item.loadContent();
}
if (item.primaryFile) {
const soundCatalog = await SoundCatalogDefinition.ensureOnFile(item.primaryFile);
if (soundCatalog) {
return soundCatalog;
}
}
}
}
const defaultRpFolder = await project.getDefaultResourcePackFolder();
if (defaultRpFolder) {
const newFile = defaultRpFolder.ensureFile("sounds.json");
const soundGen = await SoundCatalogDefinition.ensureOnFile(newFile);
if (soundGen) {
soundGen.ensureDefault();
project.ensureItemFromFile(newFile, IProjectItemData_1.ProjectItemType.soundCatalog, Project_1.FolderContext.resourcePack);
return soundGen;
}
}
return undefined;
}
static async ensureOnFile(file, loadHandler) {
let et;
if (file.manager === undefined) {
et = new SoundCatalogDefinition();
et.file = file;
file.manager = et;
}
if (file.manager !== undefined && file.manager instanceof SoundCatalogDefinition) {
et = file.manager;
if (!et.isLoaded) {
if (loadHandler) {
et.onLoaded.subscribe(loadHandler);
}
await et.load();
}
}
return et;
}
/**
* Converts ISoundEvent objects that only have {sound: "..."} back
* to plain strings, keeping the file compact and canonical.
*/
_downscaleEvents() {
if (!this._data) {
return;
}
const catalogs = [
this._data.entity_sounds?.entities,
this._data.block_sounds,
this._data.interactive_sounds?.block_sounds,
this._data.interactive_sounds?.entity_sounds?.entities,
];
const eventSets = [
this._data.entity_sounds?.defaults,
this._data.interactive_sounds?.entity_sounds?.defaults,
];
for (const catalog of catalogs) {
if (catalog) {
for (const entityKey in catalog) {
eventSets.push(catalog[entityKey]);
}
}
}
for (const eventSet of eventSets) {
if (eventSet && eventSet.events) {
for (const key in eventSet.events) {
const val = eventSet.events[key];
if (typeof val === "object" && val.sound) {
const objKeys = Object.keys(val);
if (objKeys.length === 1 && objKeys[0] === "sound") {
eventSet.events[key] = val.sound;
}
}
}
}
}
}
persist() {
if (this._file === undefined) {
return false;
}
if (!this._data) {
Log_1.default.unexpectedUndefined("SCDP");
return false;
}
this._downscaleEvents();
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, index) {
let soundEventList = this.getSoundEventNameList();
let entityIdList = this.entityIdList;
// Process entity type items using index for O(1) lookups
if (entityIdList) {
if (index) {
for (const entityId of entityIdList) {
const resourceItems = index.getItemsById(index.entityResourcesById, entityId);
const behaviorItems = index.getItemsById(index.entityBehaviorsById, entityId);
const allMatches = [...resourceItems, ...behaviorItems];
for (const matchItem of allMatches) {
item.addParentItem(matchItem);
}
if (allMatches.length > 0) {
entityIdList = Utilities_1.default.removeItemInArray(entityId, entityIdList);
}
}
}
else {
const entityResourceItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.entityTypeResource);
const entityBehaviorItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.entityTypeBehavior);
const entityItems = [...entityResourceItems, ...entityBehaviorItems];
for (const candItem of entityItems) {
const entityDef = (await MinecraftDefinitions_1.default.get(candItem));
if (entityDef && entityDef.id && entityIdList?.includes(entityDef?.id)) {
item.addParentItem(candItem);
entityIdList = Utilities_1.default.removeItemInArray(entityDef.id, entityIdList);
}
}
}
}
// Process sound definition catalog items
if (soundEventList) {
const soundDefItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.soundDefinitionCatalog);
for (const candItem of soundDefItems) {
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 soundEventName of soundEventList) {
if (typeof soundEventName === "string" && soundEventName.trim().length > 0) {
if (soundSetNames.includes(soundEventName)) {
item.addChildItem(candItem);
soundEventList = Utilities_1.default.removeItemInArray(soundEventName, soundEventList);
}
}
}
}
}
}
}
if (soundEventList && Array.isArray(soundEventList)) {
for (const soundEvent of soundEventList) {
if (typeof soundEvent === "string" && soundEvent.trim().length > 0) {
const isVanilla = await Database_1.default.isVanillaToken(soundEvent);
item.addUnfulfilledRelationship(soundEvent, IProjectItemData_1.ProjectItemType.soundDefinitionCatalog, isVanilla);
}
}
}
if (entityIdList && Array.isArray(entityIdList)) {
for (const entityId of entityIdList) {
if (entityId.length > 0) {
const isVanilla = await Database_1.default.isVanillaToken(entityId);
item.addUnfulfilledRelationship(entityId, IProjectItemData_1.ProjectItemType.entityTypeBehavior, isVanilla);
}
}
}
}
}
exports.default = SoundCatalogDefinition;