Save ScriptAccount
Some checks failed
E2E Testing / test (push) Failing after 1m32s

This commit is contained in:
Sebastian Böckelmann 2024-03-20 16:23:37 +01:00
parent 4f4122fd6d
commit 751d998625
9 changed files with 122 additions and 9 deletions

View File

@ -2,6 +2,9 @@ import {app, BrowserWindow, dialog, globalShortcut, ipcMain, Menu, screen} from
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import {GameModelLoader} from "./storage/loader/GameModelLoader"; import {GameModelLoader} from "./storage/loader/GameModelLoader";
import {StoredGameModel} from "./storage/StoredGameModel";
import {ScriptAccountStorage} from "./storage/storing/ScriptAccountStoring";
import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory";
let win: BrowserWindow | null = null; let win: BrowserWindow | null = null;
const args = process.argv.slice(1), const args = process.argv.slice(1),
@ -92,10 +95,9 @@ function createWindow(): BrowserWindow {
contextMenu.popup({ window: win!, x: params.x, y: params.y }); contextMenu.popup({ window: win!, x: params.x, y: params.y });
}) })
/*ipcMain.on('save-model', (event, storageModels: StorageModel[]) => { ipcMain.on('save-model', (event, storedGameModel: StoredGameModel) => {
console.log("Save Model") recieveGameModelToStore(storedGameModel)
SaveProject.saveProject(projectDirectory, storageModels); })
})*/
/*ipcMain.on('delete-component', (event, deletedComponent: DeleteModel) => { /*ipcMain.on('delete-component', (event, deletedComponent: DeleteModel) => {
console.log("Delete Model: ", deletedComponent) console.log("Delete Model: ", deletedComponent)
@ -129,7 +131,6 @@ function createWindow(): BrowserWindow {
] ]
const menu = Menu.buildFromTemplate(menuTemplate); const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu) Menu.setApplicationMenu(menu)
loadDevProjectAtStart()
win.webContents.on('did-finish-load', () => { win.webContents.on('did-finish-load', () => {
loadDevProjectAtStart() loadDevProjectAtStart()
@ -211,6 +212,11 @@ function saveProject() {
win!.webContents.send('get-project-data') win!.webContents.send('get-project-data')
} }
function recieveGameModelToStore(gameModel: StoredGameModel) {
const scriptAccountStorage = new ScriptAccountStorage(path.join(projectDirectory, ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME))
scriptAccountStorage.storeScriptAccounts(gameModel.storedScriptAccounts)
}
/*function deleteComponent(component: DeleteModel) { /*function deleteComponent(component: DeleteModel) {
console.log("Delete Component") console.log("Delete Component")
if(component.modeltype == ModelComponentType.SCRIPTACCOUNT) { if(component.modeltype == ModelComponentType.SCRIPTACCOUNT) {

View File

@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScriptAccountStorage = void 0;
const FileUtils_1 = require("../FileUtils");
const path = require("node:path");
const fs = require("fs");
class ScriptAccountStorage {
constructor(scriptAccountDir) {
this.scriptAccountDir = scriptAccountDir;
}
storeScriptAccounts(scriptAccounts) {
this.persistDeletedScriptAccounts(scriptAccounts);
scriptAccounts.forEach(scriptAccount => {
this.storeScriptAccount(scriptAccount);
});
}
persistDeletedScriptAccounts(existingScriptAccount) {
const scriptAccountFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.scriptAccountDir);
scriptAccountFiles.forEach(scriptAccountFile => {
const scriptAccountFileName = path.parse(path.basename(scriptAccountFile)).name;
if (existingScriptAccount.find(scriptAccount => scriptAccount.fileName === scriptAccountFileName) == undefined) {
//No scriptAccountFile was found with that nae of the file. So the scriptAccount was deleted. Remove file
fs.unlinkSync(scriptAccountFile);
}
});
}
storeScriptAccount(scriptAccount) {
const completeScriptAccountFile = path.join(this.scriptAccountDir, scriptAccount.fileName + ".json");
fs.writeFileSync(completeScriptAccountFile, scriptAccount.jsonString, 'utf-8');
}
}
exports.ScriptAccountStorage = ScriptAccountStorage;
//# sourceMappingURL=ScriptAccountStoring.js.map

View File

@ -0,0 +1,36 @@
import {StoreComponent} from "../StoreComponent";
import {FileUtils} from "../FileUtils";
import * as path from "node:path";
import * as fs from "fs";
export class ScriptAccountStorage {
private scriptAccountDir: string
constructor(scriptAccountDir: string) {
this.scriptAccountDir = scriptAccountDir;
}
storeScriptAccounts(scriptAccounts: StoreComponent[]) {
this.persistDeletedScriptAccounts(scriptAccounts)
scriptAccounts.forEach(scriptAccount => {
this.storeScriptAccount(scriptAccount)
})
}
private persistDeletedScriptAccounts(existingScriptAccount: StoreComponent[]) {
const scriptAccountFiles = FileUtils.listFilesInDirectory(this.scriptAccountDir);
scriptAccountFiles.forEach(scriptAccountFile => {
const scriptAccountFileName = path.parse(path.basename(scriptAccountFile)).name
if(existingScriptAccount.find(scriptAccount => scriptAccount.fileName === scriptAccountFileName) == undefined) {
//No scriptAccountFile was found with that nae of the file. So the scriptAccount was deleted. Remove file
fs.unlinkSync(scriptAccountFile)
}
})
}
private storeScriptAccount(scriptAccount: StoreComponent) {
const completeScriptAccountFile = path.join(this.scriptAccountDir, scriptAccount.fileName + ".json")
fs.writeFileSync(completeScriptAccountFile, scriptAccount.jsonString, 'utf-8')
}
}

View File

@ -22,7 +22,7 @@
<button mat-menu-item (click)="openGamesystemsOverview()">{{ModelComponentTypeUtillities.toString(ModelComponentType.GAMESYTEM)}}</button> <button mat-menu-item (click)="openGamesystemsOverview()">{{ModelComponentTypeUtillities.toString(ModelComponentType.GAMESYTEM)}}</button>
</mat-menu> </mat-menu>
</div> </div>
<app-script-account-overview *ngIf="openContent == ModelComponentType.SCRIPTACCOUNT" #scriptAccountOverview [gameModel]="projectService.gameModel" (onOpenScriptAccount)="openModelComponent($event)"></app-script-account-overview> <app-script-account-overview *ngIf="openContent == ModelComponentType.SCRIPTACCOUNT" #scriptAccountOverview [gameModel]="gameModel" (onOpenScriptAccount)="openModelComponent($event)"></app-script-account-overview>
<app-gamescript-overview *ngIf="openContent == ModelComponentType.GAMESYTEM" #gamesystemOverview [gameModel]="gameModel" (openGamesystemEditor)="openModelComponent($event)"></app-gamescript-overview> <app-gamescript-overview *ngIf="openContent == ModelComponentType.GAMESYTEM" #gamesystemOverview [gameModel]="gameModel" (openGamesystemEditor)="openModelComponent($event)"></app-gamescript-overview>
</mat-drawer> </mat-drawer>

View File

@ -17,6 +17,8 @@ import {StoredGameModel} from "../../app/storage/StoredGameModel";
import {GamesystemParser} from "./project/parser/gamesystemParser/GamesystemParser"; import {GamesystemParser} from "./project/parser/gamesystemParser/GamesystemParser";
import {ScriptAccountParser} from "./project/parser/ScriptAccountParser"; import {ScriptAccountParser} from "./project/parser/ScriptAccountParser";
import {ElectronService} from "./core/services"; import {ElectronService} from "./core/services";
import {ScriptAccountSerializer} from "./project/serializer/ScriptAccountSerializer";
import {StoreComponent} from "../../app/storage/StoreComponent";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -162,11 +164,19 @@ export class AppComponent implements OnInit{
gameModel.scriptAccounts = scriptAccounts gameModel.scriptAccounts = scriptAccounts
gameModel.gamesystems = gamesystems gameModel.gamesystems = gamesystems
console.log(gameModel.scriptAccounts)
this.gameModel = gameModel; this.gameModel = gameModel;
} }
onSaveProject() { onSaveProject() {
const storedScriptAccounts = ScriptAccountSerializer.serializeScriptAccounts(this.gameModel!.scriptAccounts)
const storedGamesystems: StoreComponent[] = [];
const storeModel = new StoredGameModel(this.gameModel!.gameModelName, storedScriptAccounts, storedGamesystems)
if(this.electronService.isElectron) {
this.electronService.ipcRenderer.send('save-model', storeModel)
}
} }
openScriptAccountsOverview() { openScriptAccountsOverview() {

View File

@ -7,13 +7,13 @@ import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem";
import {StorageModel} from "./fs/StorageModel"; import {StorageModel} from "./fs/StorageModel";
export class GameModel { export class GameModel {
private readonly _gameModelName: string gameModelName: string
gamesystems: Gamesystem<any, any>[] = []; gamesystems: Gamesystem<any, any>[] = [];
scriptAccounts: ScriptAccount[] = []; scriptAccounts: ScriptAccount[] = [];
constructor(gameModelName: string) { constructor(gameModelName: string) {
this._gameModelName = gameModelName; this.gameModelName = gameModelName;
} }
addGamesystem(gamesystem: Gamesystem<any, any>) { addGamesystem(gamesystem: Gamesystem<any, any>) {

View File

@ -0,0 +1,25 @@
import {ScriptAccount} from "../game-model/scriptAccounts/ScriptAccount";
import {StoreComponent} from "../../../../app/storage/StoreComponent";
import {SerializeConstants} from "./SerializeConstants";
import {ModelComponentType} from "../game-model/ModelComponentType";
export class ScriptAccountSerializer {
public static serializeScriptAccounts(scriptAccounts: ScriptAccount[]): StoreComponent[] {
const storeComponents: StoreComponent[] = []
scriptAccounts.forEach(scriptAccount => storeComponents.push(this.serializeSingleScriptAccount(scriptAccount)))
return storeComponents;
}
private static serializeSingleScriptAccount(scriptAccount: ScriptAccount): StoreComponent {
const fileName = scriptAccount.componentName
const jsonString = JSON.stringify(scriptAccount, (key, value) => {
if(key === 'unsaved' || key === 'type') {
return undefined
} else {
return value;
}
}, SerializeConstants.JSON_INDENT)
return new StoreComponent(jsonString, fileName, ModelComponentType.SCRIPTACCOUNT);
}
}

View File

@ -0,0 +1,3 @@
export class SerializeConstants {
public static JSON_INDENT = 4
}

View File

@ -1,6 +1,6 @@
{ {
"componentName": "Temperature", "componentName": "Temperature",
"componentDescription": "", "componentDescription": "",
"minValue": -50, "minValue": -30,
"maxValue": 50 "maxValue": 50
} }