2024-05-09 13:33:26 +02:00
|
|
|
import {StoreComponent} from "../StoreComponent";
|
|
|
|
import {FileUtils} from "../FileUtils";
|
|
|
|
import * as path from "node:path";
|
|
|
|
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
|
2024-05-09 16:03:52 +02:00
|
|
|
import * as fs from "node:fs";
|
2024-05-09 13:33:26 +02:00
|
|
|
|
|
|
|
export class ItemLoader {
|
|
|
|
|
|
|
|
itemDir: string
|
2024-05-09 16:03:52 +02:00
|
|
|
private _loadedItemgroups: StoreComponent[] = []
|
|
|
|
private _loadedItems: StoreComponent[] = []
|
2024-05-09 13:33:26 +02:00
|
|
|
|
|
|
|
constructor(itemDir: string) {
|
|
|
|
this.itemDir = itemDir;
|
|
|
|
}
|
|
|
|
|
2024-05-09 16:03:52 +02:00
|
|
|
loadItemgroups(){
|
|
|
|
this.loadItemgroupsRecursively(this.itemDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get loadedItemgroups(): StoreComponent[] {
|
|
|
|
return this._loadedItemgroups;
|
2024-05-09 13:33:26 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 16:03:52 +02:00
|
|
|
get loadedItems(): StoreComponent[] {
|
|
|
|
return this._loadedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadItemgroupsRecursively(currentDir: string) {
|
2024-05-09 13:33:26 +02:00
|
|
|
const itemgroupFiles = 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 13:33:26 +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 13:33:26 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-09 16:03:52 +02:00
|
|
|
private loadSingleItem(itemFile: string) {
|
|
|
|
if(itemFile.endsWith(".json")) {
|
|
|
|
const data = fs.readFileSync(itemFile, "utf-8");
|
|
|
|
return new StoreComponent(data, itemFile, ModelComponentType.ITEM);
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:33:26 +02:00
|
|
|
private fileRepresentsItemgroup(file: string): boolean {
|
|
|
|
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
|
|
|
|
}
|
|
|
|
}
|