47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
|
import {StoreComponent} from "../StoreComponent";
|
||
|
import {FileUtils} from "../FileUtils";
|
||
|
import * as fs from "node:fs";
|
||
|
import * as path from "node:path";
|
||
|
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
|
||
|
|
||
|
export class ItemLoader {
|
||
|
|
||
|
itemDir: string
|
||
|
|
||
|
constructor(itemDir: string) {
|
||
|
this.itemDir = itemDir;
|
||
|
}
|
||
|
|
||
|
loadItemgroups(): StoreComponent[] {
|
||
|
return this.loadItemgroupsRecursively(this.itemDir);
|
||
|
}
|
||
|
|
||
|
private loadItemgroupsRecursively(currentDir: string): StoreComponent[] {
|
||
|
let loadedItemgroups : StoreComponent[] = []
|
||
|
const itemgroupFiles = FileUtils.listFilesInDirectory(currentDir);
|
||
|
|
||
|
itemgroupFiles.forEach(itemgroupFile => {
|
||
|
if(fs.lstatSync(itemgroupFile).isDirectory()) {
|
||
|
const childgroup = this.loadItemgroupsRecursively(itemgroupFile);
|
||
|
loadedItemgroups = loadedItemgroups.concat(childgroup);
|
||
|
} else if(path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)){
|
||
|
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile)!;
|
||
|
loadedItemgroups.push(loadedItemgroup)
|
||
|
}
|
||
|
})
|
||
|
return loadedItemgroups;
|
||
|
}
|
||
|
|
||
|
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 fileRepresentsItemgroup(file: string): boolean {
|
||
|
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
|
||
|
}
|
||
|
}
|