ConceptCreator/app/storage/loader/GamesystemLoader.ts
Sebastian Böckelmann ed2f28760e
Some checks failed
E2E Testing / test (push) Failing after 1m30s
Refactor Gamesystem parsing
2024-03-20 11:19:32 +01:00

45 lines
1.5 KiB
TypeScript

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);
}
}
}