UNPKG

@bscotch/stitch

Version:

Stitch: The GameMaker Studio 2 Asset Pipeline Development Kit.

137 lines 5.34 kB
import { Pathy, pathy } from '@bscotch/pathy'; import { stitchConfigFilename, stitchConfigSchema, } from '@bscotch/stitch-config'; import { ok } from 'assert'; import { assert } from '../utility/errors.js'; import paths from '../utility/paths.js'; import { StitchStorage } from './StitchStorage.js'; /** The Project Config lives alongside the .yyp file */ export class StitchProjectConfig { storage; static fileSchema = stitchConfigSchema; // TODO: Make all of this ASYNNCCCC!!!! constructor(storage) { this.storage = storage; } get filePathAbsolute() { return this.storage.toAbsolutePath(stitchConfigFilename); } async getRuntimeVersion() { return await this.loadField('runtimeVersion'); } async setRuntimeVersion(version) { return await this.saveField('runtimeVersion', version); } async getTextureGroupAssignments() { return await this.loadField('textureGroupAssignments'); } async getTextureGroupsWithAssignedFolders() { const assignments = await this.getTextureGroupAssignments(); return Object.values(assignments); } /** * The folders that have an assigned texture group, * sorted from *least* to *most* specific (allowing * texture groups of contained sprites to be assigned * in order). */ async getFoldersWithAssignedTextureGroups() { return sortedKeys(await this.getTextureGroupAssignments()); } async getAudioGroupAssignments() { return await this.loadField('audioGroupAssignments'); } async getAudioGroupsWithAssignedFolders() { const assignments = await this.getAudioGroupAssignments(); return Object.values(assignments); } /** * The folders that have an assigned texture group, * sorted from *least* to *most* specific (allowing * texture groups of contained sprites to be assigned * in order). */ async getFoldersWithAssignedAudioGroups() { return sortedKeys(await this.getAudioGroupAssignments()); } async addGroupAssignment(type, folder, group) { assert(!['', '/', '\\'].includes(folder), `Cannot assign groups to the root level. Use default groups for that.`); const data = await this.load(); data[type][folder] = group; return await this.save(data); } async deleteGroupAssignment(type, folder) { const data = await this.load(); Reflect.deleteProperty(data[type], folder); return this.save(data); } async getGroupAssignmentForPath(type, path) { const data = await this.load(); const assignments = data[type]; const assignmentPaths = Object.keys(assignments); // Find the closest path const closestAssignmentPath = paths.findClosestParent(assignmentPaths, path); if (closestAssignmentPath) { return assignments[closestAssignmentPath]; } return; } async findTextureGroupForPath(path) { return await this.getGroupAssignmentForPath('textureGroupAssignments', path); } async addTextureGroupAssignment(folder, textureGroup) { return await this.addGroupAssignment('textureGroupAssignments', folder, textureGroup); } async deleteTextureGroupAssignment(folder) { return await this.deleteGroupAssignment('textureGroupAssignments', folder); } async addAudioGroupAssignment(folder, textureGroup) { return await this.addGroupAssignment('audioGroupAssignments', folder, textureGroup); } async findAudioGroupForPath(path) { return await this.getGroupAssignmentForPath('audioGroupAssignments', path); } async deleteAudioGroupAssignment(folder) { return await this.deleteGroupAssignment('audioGroupAssignments', folder); } async load() { return await pathy(this.filePathAbsolute).read({ fallback: {}, schema: StitchProjectConfig.fileSchema, }); } async loadField(field) { const data = await this.load(); return data[field]; } async saveField(field, value) { const data = await this.load(); data[field] = value; return await this.save(data); } async save(data) { return await pathy(this.filePathAbsolute).write(data, { schema: StitchProjectConfig.fileSchema, }); } static from(pathOrStorage) { if (pathOrStorage instanceof StitchStorage) { return new StitchProjectConfig(pathOrStorage); } ok(pathOrStorage instanceof Pathy || typeof pathOrStorage === 'string', 'Arguument must be a path string or a StitchStorage instance'); let yypPath = new Pathy(pathOrStorage); if (!yypPath.hasExtension('.yyp')) { const folder = yypPath.isDirectorySync() ? yypPath : yypPath.up(); yypPath = folder .listChildrenSync() .find((child) => child.hasExtension('yyp')); ok(yypPath, `No .yyp file found in ${folder}`); } return new StitchProjectConfig(new StitchStorage(yypPath.absolute, false, true)); } } function sortedKeys(object) { const keys = Object.keys(object); keys.sort(paths.pathSpecificitySort); return keys; } //# sourceMappingURL=StitchProjectConfig.js.map