ConceptCreator/app/storage/loader/ItemLoader.js

59 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-05-09 15:24:36 +02:00
"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");
2024-05-09 16:03:52 +02:00
const fs = require("node:fs");
2024-05-09 15:24:36 +02:00
class ItemLoader {
constructor(itemDir) {
2024-05-09 16:03:52 +02:00
this._loadedItemgroups = [];
this._loadedItems = [];
2024-05-09 15:24:36 +02:00
this.itemDir = itemDir;
}
loadItemgroups() {
2024-05-09 16:03:52 +02:00
this.loadItemgroupsRecursively(this.itemDir);
}
get loadedItemgroups() {
return this._loadedItemgroups;
}
get loadedItems() {
return this._loadedItems;
2024-05-09 15:24:36 +02:00
}
loadItemgroupsRecursively(currentDir) {
const itemgroupFiles = FileUtils_1.FileUtils.listFilesInDirectory(currentDir);
itemgroupFiles.forEach(itemgroupFile => {
if (fs.lstatSync(itemgroupFile).isDirectory()) {
2024-05-09 16:03:52 +02:00
this.loadItemgroupsRecursively(itemgroupFile);
2024-05-09 15:24:36 +02:00
}
else if (path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)) {
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile);
2024-05-09 16:03:52 +02:00
this._loadedItemgroups.push(loadedItemgroup);
}
else if (path.basename(itemgroupFile).endsWith(".json")) {
const loadedItem = this.loadSingleItem(itemgroupFile);
this._loadedItems.push(loadedItem);
2024-05-09 15:24:36 +02:00
}
});
}
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;
}
2024-05-09 16:03:52 +02:00
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;
}
2024-05-09 15:24:36 +02:00
fileRepresentsItemgroup(file) {
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
}
}
exports.ItemLoader = ItemLoader;
//# sourceMappingURL=ItemLoader.js.map