ConceptCreator/app/storage/loader/ItemLoader.js
Sebastian Böckelmann f01d8c4dab
All checks were successful
E2E Testing / test (push) Successful in 1m33s
Parse Items
2024-05-09 16:03:52 +02:00

59 lines
2.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemLoader = void 0;
const StoreComponent_1 = require("../StoreComponent");
const FileUtils_1 = require("../FileUtils");
const path = require("node:path");
const ModelComponentType_1 = require("../../../src/app/project/game-model/ModelComponentType");
const fs = require("node:fs");
class ItemLoader {
constructor(itemDir) {
this._loadedItemgroups = [];
this._loadedItems = [];
this.itemDir = itemDir;
}
loadItemgroups() {
this.loadItemgroupsRecursively(this.itemDir);
}
get loadedItemgroups() {
return this._loadedItemgroups;
}
get loadedItems() {
return this._loadedItems;
}
loadItemgroupsRecursively(currentDir) {
const itemgroupFiles = FileUtils_1.FileUtils.listFilesInDirectory(currentDir);
itemgroupFiles.forEach(itemgroupFile => {
if (fs.lstatSync(itemgroupFile).isDirectory()) {
this.loadItemgroupsRecursively(itemgroupFile);
}
else if (path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)) {
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile);
this._loadedItemgroups.push(loadedItemgroup);
}
else if (path.basename(itemgroupFile).endsWith(".json")) {
const loadedItem = this.loadSingleItem(itemgroupFile);
this._loadedItems.push(loadedItem);
}
});
}
loadSingleItemgroup(itemgroupFile) {
if (itemgroupFile.endsWith(".json")) {
const data = fs.readFileSync(itemgroupFile, "utf-8");
return new StoreComponent_1.StoreComponent(data, itemgroupFile, ModelComponentType_1.ModelComponentType.ITEMGROUP);
}
return undefined;
}
loadSingleItem(itemFile) {
if (itemFile.endsWith(".json")) {
const data = fs.readFileSync(itemFile, "utf-8");
return new StoreComponent_1.StoreComponent(data, itemFile, ModelComponentType_1.ModelComponentType.ITEM);
}
return undefined;
}
fileRepresentsItemgroup(file) {
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
}
}
exports.ItemLoader = ItemLoader;
//# sourceMappingURL=ItemLoader.js.map