34 lines
891 B
TypeScript
34 lines
891 B
TypeScript
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|