diff --git a/app/main.ts b/app/main.ts index 71b92c3..1264ef5 100644 --- a/app/main.ts +++ b/app/main.ts @@ -2,6 +2,9 @@ import {app, BrowserWindow, dialog, globalShortcut, ipcMain, Menu, screen} from import * as path from 'path'; import * as fs from 'fs'; 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; const args = process.argv.slice(1), @@ -92,10 +95,9 @@ function createWindow(): BrowserWindow { contextMenu.popup({ window: win!, x: params.x, y: params.y }); }) - /*ipcMain.on('save-model', (event, storageModels: StorageModel[]) => { - console.log("Save Model") - SaveProject.saveProject(projectDirectory, storageModels); - })*/ + ipcMain.on('save-model', (event, storedGameModel: StoredGameModel) => { + recieveGameModelToStore(storedGameModel) + }) /*ipcMain.on('delete-component', (event, deletedComponent: DeleteModel) => { console.log("Delete Model: ", deletedComponent) @@ -129,7 +131,6 @@ function createWindow(): BrowserWindow { ] const menu = Menu.buildFromTemplate(menuTemplate); Menu.setApplicationMenu(menu) - loadDevProjectAtStart() win.webContents.on('did-finish-load', () => { loadDevProjectAtStart() @@ -211,6 +212,11 @@ function saveProject() { 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) { console.log("Delete Component") if(component.modeltype == ModelComponentType.SCRIPTACCOUNT) { diff --git a/app/storage/storing/ScriptAccountStoring.js b/app/storage/storing/ScriptAccountStoring.js new file mode 100644 index 0000000..49b0eab --- /dev/null +++ b/app/storage/storing/ScriptAccountStoring.js @@ -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 \ No newline at end of file diff --git a/app/storage/storing/ScriptAccountStoring.ts b/app/storage/storing/ScriptAccountStoring.ts new file mode 100644 index 0000000..6e73b17 --- /dev/null +++ b/app/storage/storing/ScriptAccountStoring.ts @@ -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') + } +} diff --git a/src/app/app.component.html b/src/app/app.component.html index e345414..2801b21 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -22,7 +22,7 @@ - + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 9a41423..5a07598 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -17,6 +17,8 @@ import {StoredGameModel} from "../../app/storage/StoredGameModel"; import {GamesystemParser} from "./project/parser/gamesystemParser/GamesystemParser"; import {ScriptAccountParser} from "./project/parser/ScriptAccountParser"; import {ElectronService} from "./core/services"; +import {ScriptAccountSerializer} from "./project/serializer/ScriptAccountSerializer"; +import {StoreComponent} from "../../app/storage/StoreComponent"; @Component({ selector: 'app-root', @@ -162,11 +164,19 @@ export class AppComponent implements OnInit{ gameModel.scriptAccounts = scriptAccounts gameModel.gamesystems = gamesystems + console.log(gameModel.scriptAccounts) + this.gameModel = gameModel; } 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() { diff --git a/src/app/project/game-model/GameModel.ts b/src/app/project/game-model/GameModel.ts index 6a7b5d5..e1005d9 100644 --- a/src/app/project/game-model/GameModel.ts +++ b/src/app/project/game-model/GameModel.ts @@ -7,13 +7,13 @@ import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem"; import {StorageModel} from "./fs/StorageModel"; export class GameModel { - private readonly _gameModelName: string + gameModelName: string gamesystems: Gamesystem[] = []; scriptAccounts: ScriptAccount[] = []; constructor(gameModelName: string) { - this._gameModelName = gameModelName; + this.gameModelName = gameModelName; } addGamesystem(gamesystem: Gamesystem) { diff --git a/src/app/project/serializer/ScriptAccountSerializer.ts b/src/app/project/serializer/ScriptAccountSerializer.ts new file mode 100644 index 0000000..bdbe960 --- /dev/null +++ b/src/app/project/serializer/ScriptAccountSerializer.ts @@ -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); + } +} diff --git a/src/app/project/serializer/SerializeConstants.ts b/src/app/project/serializer/SerializeConstants.ts new file mode 100644 index 0000000..cc7e432 --- /dev/null +++ b/src/app/project/serializer/SerializeConstants.ts @@ -0,0 +1,3 @@ +export class SerializeConstants { + public static JSON_INDENT = 4 +} diff --git a/testModel/script-accounts/Temperature.json b/testModel/script-accounts/Temperature.json index db4d1c8..91a3a6c 100644 --- a/testModel/script-accounts/Temperature.json +++ b/testModel/script-accounts/Temperature.json @@ -1,6 +1,6 @@ { "componentName": "Temperature", "componentDescription": "", - "minValue": -50, + "minValue": -30, "maxValue": 50 } \ No newline at end of file