ConceptCreator/app/SaveProject.ts
Sebastian Böckelmann 3b0d4e0194
All checks were successful
E2E Testing / test (push) Successful in 1m34s
Load Gamesystems from Filesystem
2024-02-17 14:32:19 +01:00

111 lines
4.4 KiB
TypeScript

import {StorageModel} from "../src/app/game-model/fs/StorageModel";
import * as fs from "fs";
import * as path from "node:path";
import {LoadModel} from "./LoadModel";
import {ModelComponentType} from "../src/app/game-model/ModelComponentType";
import {LoadedProject} from "./LoadedProject";
import {RecursiveLoadModel} from "./RecursiveLoadModel";
export class SaveProject {
static saveProject(projectDir: string, storageModels: StorageModel[]) {
if(!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, {recursive: true});
}
console.log(storageModels)
storageModels.forEach(storageModel => {
let modelDir = path.join(projectDir, storageModel.storageRootDir);
storageModel.storagePath.forEach(pathElement => modelDir = path.join(modelDir, pathElement));
if(!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, {recursive: true});
}
const filePath = path.join(modelDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString ,'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
} else {
console.log('JSON file saved successfully:', filePath);
}
})
})
}
static loadProject(projectDir: string) {
let loadedScriptAccounts: LoadModel[] = SaveProject.loadScriptAccounts(projectDir)
loadedScriptAccounts = loadedScriptAccounts.concat(SaveProject.loadGamesystems(projectDir))
return new LoadedProject(path.basename(projectDir), loadedScriptAccounts);
}
static loadScriptAccounts(projectDir: string): LoadModel[] {
const scriptAccountDir = path.join(projectDir, "script-accounts");
if (!fs.existsSync(scriptAccountDir)) {
return [];
}
const loadedScriptAccounts: LoadModel[] = [];
const scriptAccountFileNames = fs.readdirSync(scriptAccountDir);
scriptAccountFileNames.forEach(scriptAccountFileName => {
const scriptAccountFile = path.join(scriptAccountDir, scriptAccountFileName)
const scriptAccountData: string = fs.readFileSync(scriptAccountFile, 'utf-8');
loadedScriptAccounts.push({
modelType: ModelComponentType.SCRIPTACCOUNT,
jsonString: scriptAccountData
});
})
return loadedScriptAccounts;
}
static loadGamesystems(projectDir: string): LoadModel[] {
const gamesystemDir = path.join(projectDir, "gamesystems");
const loadedGamesystems = this.loadGamesystemsRecursively(gamesystemDir);
console.log("LoadedGamesystems: ", loadedGamesystems.length);
return loadedGamesystems;
}
static loadGamesystemsRecursively(gamesystemDir: string): LoadModel[] {
let loadedGamesystems: LoadModel[] = [];
const gamesystemFileNames = fs.readdirSync(gamesystemDir);
gamesystemFileNames.forEach(fileName => {
const gamesystemPath = path.join(gamesystemDir, fileName);
if(fs.lstatSync(gamesystemPath).isDirectory()) {
const childModels: LoadModel[] = SaveProject.loadGamesystemsRecursively(gamesystemPath);
loadedGamesystems = loadedGamesystems.concat(childModels);
} else {
const gamesystemData = fs.readFileSync(path.join(gamesystemDir, fileName), "utf-8");
if(path.parse(fileName).name === path.basename(gamesystemDir) ) {
if((path.basename(gamesystemDir) === path.parse(fileName).name) && path.basename(path.parse(gamesystemDir).dir) === "gamesystems") {
const loadedModel = new LoadModel(gamesystemData, ModelComponentType.GAMESYTEM);
loadedGamesystems.unshift(loadedModel)
} else {
const loadedModel = new RecursiveLoadModel(gamesystemData, ModelComponentType.GAMESYTEM, path.basename(gamesystemDir))
loadedGamesystems.unshift(loadedModel);
}
} else {
const secondCon = path.basename(gamesystemDir) === path.parse(fileName).name
const thirdCon = path.basename(path.parse(gamesystemDir).dir) === "gamesystems"
if(path.basename(gamesystemDir) === "gamesystems"){
const loadedModel = new LoadModel(gamesystemData, ModelComponentType.GAMESYTEM)
loadedGamesystems.push(loadedModel);
} else {
const loadedModel = new RecursiveLoadModel(gamesystemData, ModelComponentType.GAMESYTEM, path.basename(gamesystemDir))
loadedGamesystems.push(loadedModel);
}
}
}
})
return loadedGamesystems;
}
}