66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import {StoreComponent} from "../StoreComponent";
|
|
import {FileUtils} from "../FileUtils";
|
|
import * as path from "node:path";
|
|
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
|
|
import * as fs from "node:fs";
|
|
|
|
export class ItemLoader {
|
|
|
|
itemDir: string
|
|
private _loadedItemgroups: StoreComponent[] = []
|
|
private _loadedItems: StoreComponent[] = []
|
|
|
|
constructor(itemDir: string) {
|
|
this.itemDir = itemDir;
|
|
}
|
|
|
|
loadItemgroups(){
|
|
this.loadItemgroupsRecursively(this.itemDir);
|
|
}
|
|
|
|
|
|
get loadedItemgroups(): StoreComponent[] {
|
|
return this._loadedItemgroups;
|
|
}
|
|
|
|
get loadedItems(): StoreComponent[] {
|
|
return this._loadedItems;
|
|
}
|
|
|
|
private loadItemgroupsRecursively(currentDir: string) {
|
|
const itemgroupFiles = 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);
|
|
}
|
|
})
|
|
}
|
|
|
|
private loadSingleItemgroup(itemgroupFile: string): StoreComponent | undefined {
|
|
if(itemgroupFile.endsWith(".json")) {
|
|
const data = fs.readFileSync(itemgroupFile, "utf-8");
|
|
return new StoreComponent(data, itemgroupFile, ModelComponentType.ITEMGROUP);
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
private loadSingleItem(itemFile: string) {
|
|
if(itemFile.endsWith(".json")) {
|
|
const data = fs.readFileSync(itemFile, "utf-8");
|
|
return new StoreComponent(data, itemFile, ModelComponentType.ITEM);
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
private fileRepresentsItemgroup(file: string): boolean {
|
|
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
|
|
}
|
|
}
|