ConceptCreator/app/storage/loader/ItemLoader.ts

34 lines
891 B
TypeScript
Raw Normal View History

2024-04-20 10:55:50 +02:00
import * as fs from "node:fs";
import {StoreComponent} from "../StoreComponent";
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
import {FileUtils} from "../FileUtils";
export class ItemLoader {
private itemDir: string
constructor(itemDir: string) {
this.itemDir = itemDir;
}
loadItems() {
const itemFiles = FileUtils.listFilesInDirectory(this.itemDir);
const loadedItems :StoreComponent[] = []
itemFiles.forEach(itemFile => {
const loadedItem = this.loadItem(itemFile);
if(loadedItem != undefined) {
loadedItems.push(loadedItem);
}
})
return loadedItems;
}
private loadItem(itemFile: string) {
if(itemFile.endsWith(".json")) {
const itemData = fs.readFileSync(itemFile, 'utf-8');
return new StoreComponent(itemData, itemFile, ModelComponentType.ITEM)
}
}
}