ConceptCreator/app/storage/loader/GamesystemLoader.ts

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-03-20 11:19:32 +01:00
import {StoreComponent} from "../StoreComponent";
import {FileUtils} from "../FileUtils";
import * as fs from "fs";
import * as path from "node:path";
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
export class GamesystemLoader {
gamesystemDir: string
constructor(gamesystemDir: string) {
this.gamesystemDir = gamesystemDir;
}
loadGamesystems() {
return this.loadGamesystemsRecursivly(this.gamesystemDir)
}
private loadGamesystemsRecursivly(currentGamesystemDir: string) {
let loadedGamesystems: StoreComponent[] = []
const gamesystemFiles = FileUtils.listFilesInDirectory(currentGamesystemDir);
gamesystemFiles.forEach(gamesystemFile => {
if(fs.lstatSync(gamesystemFile).isDirectory()) {
const childGamesystems = this.loadGamesystemsRecursivly(gamesystemFile);
loadedGamesystems = loadedGamesystems.concat(childGamesystems);
} else if(path.basename(gamesystemFile).endsWith(".json")) {
const loadedGamesystem = this.loadGamesystem(gamesystemFile);
if(loadedGamesystem != undefined) {
loadedGamesystems.push(loadedGamesystem);
}
}
})
return loadedGamesystems;
}
private loadGamesystem(gamesystemFile: string) {
if(gamesystemFile.endsWith(".json")) {
const gamesystemData = fs.readFileSync(gamesystemFile, 'utf-8');
return new StoreComponent(gamesystemData, gamesystemFile, ModelComponentType.GAMESYTEM);
}
}
}