2024-02-17 10:13:32 +01:00
|
|
|
import {StorageModel} from "../src/app/game-model/fs/StorageModel";
|
2024-02-17 07:30:29 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "node:path";
|
2024-02-17 08:45:22 +01:00
|
|
|
import {LoadModel} from "./LoadModel";
|
|
|
|
import {ModelComponentType} from "../src/app/game-model/ModelComponentType";
|
|
|
|
import {BrowserWindow} from "electron";
|
|
|
|
import {LoadedProject} from "./LoadedProject";
|
2024-02-17 07:30:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
export class SaveProject {
|
|
|
|
static saveProject(projectDir: string, storageModels: StorageModel[]) {
|
2024-02-17 08:45:22 +01:00
|
|
|
if(!fs.existsSync(projectDir)) {
|
|
|
|
fs.mkdirSync(projectDir, {recursive: true});
|
2024-02-17 07:30:29 +01:00
|
|
|
}
|
|
|
|
|
2024-02-17 10:13:32 +01:00
|
|
|
console.log(storageModels)
|
2024-02-17 07:30:29 +01:00
|
|
|
storageModels.forEach(storageModel => {
|
2024-02-17 10:13:32 +01:00
|
|
|
let modelDir = path.join(projectDir, storageModel.storageRootDir);
|
|
|
|
storageModel.storagePath.forEach(pathElement => modelDir = path.join(modelDir, pathElement));
|
2024-02-17 07:30:29 +01:00
|
|
|
if(!fs.existsSync(modelDir)) {
|
|
|
|
fs.mkdirSync(modelDir, {recursive: true});
|
|
|
|
}
|
|
|
|
|
2024-02-17 08:45:22 +01:00
|
|
|
const filePath = path.join(modelDir, storageModel.fileName + ".json");
|
2024-02-17 07:30:29 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-02-17 08:45:22 +01:00
|
|
|
|
|
|
|
static loadProject(projectDir: string) {
|
|
|
|
const loadedScriptAccounts: LoadModel[] = SaveProject.loadScriptAccounts(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;
|
|
|
|
}
|
|
|
|
|
2024-02-17 07:30:29 +01:00
|
|
|
}
|