This commit is contained in:
parent
e6073d4f2f
commit
c184c667fd
@ -5,6 +5,7 @@ import {GameModelLoader} from "./storage/loader/GameModelLoader";
|
||||
import {StoredGameModel} from "./storage/StoredGameModel";
|
||||
import {ScriptAccountStorage} from "./storage/storing/ScriptAccountStoring";
|
||||
import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory";
|
||||
import {GamesystemStorage} from "./storage/storing/GamesystemStorage";
|
||||
|
||||
let win: BrowserWindow | null = null;
|
||||
const args = process.argv.slice(1),
|
||||
@ -215,6 +216,9 @@ function saveProject() {
|
||||
function recieveGameModelToStore(gameModel: StoredGameModel) {
|
||||
const scriptAccountStorage = new ScriptAccountStorage(path.join(projectDirectory, ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME))
|
||||
scriptAccountStorage.storeScriptAccounts(gameModel.storedScriptAccounts)
|
||||
|
||||
const gamesystemStorage = new GamesystemStorage(path.join(projectDirectory, ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME))
|
||||
gamesystemStorage.storeGamesystems(gameModel.storedGamesystems)
|
||||
}
|
||||
|
||||
/*function deleteComponent(component: DeleteModel) {
|
||||
|
@ -19,6 +19,16 @@ class FileUtils {
|
||||
(0, fs_1.mkdirSync)(parentDirectory, { recursive: true });
|
||||
}
|
||||
}
|
||||
static removeFiles(files) {
|
||||
files.forEach(file => {
|
||||
if (fs.lstatSync(file).isDirectory()) {
|
||||
fs.rmdirSync(file);
|
||||
}
|
||||
else {
|
||||
fs.unlinkSync(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.FileUtils = FileUtils;
|
||||
//# sourceMappingURL=FileUtils.js.map
|
@ -1,6 +1,7 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "node:path";
|
||||
import {mkdirSync} from "fs";
|
||||
import {lstatSync} from "node:fs";
|
||||
|
||||
|
||||
export class FileUtils {
|
||||
@ -19,4 +20,14 @@ export class FileUtils {
|
||||
mkdirSync(parentDirectory, {recursive: true})
|
||||
}
|
||||
}
|
||||
|
||||
public static removeFiles(files: string[]) {
|
||||
files.forEach(file => {
|
||||
if(fs.lstatSync(file).isDirectory()) {
|
||||
fs.rmdirSync(file)
|
||||
} else {
|
||||
fs.unlinkSync(file);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -9,33 +9,46 @@ class GamesystemStorage {
|
||||
this.gamesystemRootDir = gamesystemRootDir;
|
||||
}
|
||||
storeGamesystems(gamesystems) {
|
||||
this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems);
|
||||
const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems);
|
||||
FileUtils_1.FileUtils.removeFiles(unreferencedFiles);
|
||||
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem));
|
||||
}
|
||||
detectUnusedGamesystemFiles(gamesystemDir, gamesystems) {
|
||||
const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(gamesystemDir);
|
||||
gamesystemFiles.forEach(gamesystemFile => {
|
||||
if (fs.lstatSync(gamesystemFile).isDirectory()) {
|
||||
this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems);
|
||||
detectUnusedGamesystemFiles(gamesystems) {
|
||||
const unreferencedFiles = [];
|
||||
const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.gamesystemRootDir);
|
||||
while (gamesystemFiles.length > 0) {
|
||||
const currentGamesystemFile = gamesystemFiles.shift();
|
||||
const referencedGamesystemName = path.parse(path.basename(currentGamesystemFile)).name;
|
||||
const referencedGamesystem = this.findReferencedGamesystem(referencedGamesystemName, gamesystems);
|
||||
if (referencedGamesystem == undefined) {
|
||||
unreferencedFiles.push(currentGamesystemFile);
|
||||
}
|
||||
else {
|
||||
const currentFileName = path.parse(path.basename(gamesystemFile)).name;
|
||||
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName);
|
||||
if (searchedGamesystem != undefined) {
|
||||
if (path.dirname(searchedGamesystem.fileName) === currentFileName) {
|
||||
//Aus Simple wurde Product => Delete .json
|
||||
fs.unlinkSync(gamesystemFile);
|
||||
const decodedJSONData = JSON.parse(referencedGamesystem.jsonString);
|
||||
if (decodedJSONData.childsystems != undefined) {
|
||||
//Check if current file is a directory. When it is a directory, everything is fine
|
||||
if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
|
||||
}
|
||||
else {
|
||||
const parentDirName = path.basename(path.dirname(currentGamesystemFile));
|
||||
if (parentDirName !== referencedGamesystemName) {
|
||||
unreferencedFiles.push(currentGamesystemFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
fs.unlinkSync(gamesystemFile);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
|
||||
gamesystemFiles.push(...FileUtils_1.FileUtils.listFilesInDirectory(currentGamesystemFile));
|
||||
}
|
||||
}
|
||||
return unreferencedFiles;
|
||||
}
|
||||
findReferencedGamesystem(referencedName, gamesystems) {
|
||||
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === referencedName);
|
||||
return searchedGamesystem;
|
||||
}
|
||||
storeGamesystem(gamesystem) {
|
||||
const gamesystemFile = path.join(...gamesystem.fileName.split("/"));
|
||||
console.log(gamesystem.jsonString);
|
||||
const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile);
|
||||
FileUtils_1.FileUtils.prepareFileForWriting(completeGamesystemFile);
|
||||
fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8');
|
||||
|
@ -13,33 +13,50 @@ export class GamesystemStorage {
|
||||
}
|
||||
|
||||
public storeGamesystems(gamesystems: StoreComponent[]) {
|
||||
this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems)
|
||||
const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems)
|
||||
FileUtils.removeFiles(unreferencedFiles)
|
||||
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem))
|
||||
}
|
||||
|
||||
private detectUnusedGamesystemFiles(gamesystemDir: string, gamesystems: StoreComponent[]) {
|
||||
const gamesystemFiles = FileUtils.listFilesInDirectory(gamesystemDir);
|
||||
gamesystemFiles.forEach(gamesystemFile => {
|
||||
if(fs.lstatSync(gamesystemFile).isDirectory()) {
|
||||
this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems)
|
||||
private detectUnusedGamesystemFiles(gamesystems: StoreComponent[]) {
|
||||
const unreferencedFiles: string[] = []
|
||||
const gamesystemFiles = FileUtils.listFilesInDirectory(this.gamesystemRootDir);
|
||||
while(gamesystemFiles.length > 0) {
|
||||
const currentGamesystemFile = gamesystemFiles.shift()
|
||||
const referencedGamesystemName = path.parse(path.basename(currentGamesystemFile!)).name
|
||||
const referencedGamesystem = this.findReferencedGamesystem(referencedGamesystemName, gamesystems)
|
||||
if(referencedGamesystem == undefined) {
|
||||
unreferencedFiles.push(currentGamesystemFile!)
|
||||
} else {
|
||||
const currentFileName = path.parse(path.basename(gamesystemFile)).name
|
||||
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName);
|
||||
if(searchedGamesystem != undefined) {
|
||||
if(path.dirname(searchedGamesystem.fileName) === currentFileName) {
|
||||
//Aus Simple wurde Product => Delete .json
|
||||
fs.unlinkSync(gamesystemFile)
|
||||
const decodedJSONData = JSON.parse(referencedGamesystem!.jsonString)
|
||||
if(decodedJSONData.childsystems != undefined) {
|
||||
//Check if current file is a directory. When it is a directory, everything is fine
|
||||
if(fs.lstatSync(currentGamesystemFile!).isDirectory()) {
|
||||
|
||||
} else {
|
||||
const parentDirName = path.basename(path.dirname(currentGamesystemFile!))
|
||||
if(parentDirName !== referencedGamesystemName) {
|
||||
unreferencedFiles.push(currentGamesystemFile!)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fs.unlinkSync(gamesystemFile)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if(fs.lstatSync(currentGamesystemFile!).isDirectory()) {
|
||||
gamesystemFiles.push(... FileUtils.listFilesInDirectory(currentGamesystemFile!))
|
||||
}
|
||||
}
|
||||
|
||||
return unreferencedFiles;
|
||||
}
|
||||
|
||||
private findReferencedGamesystem(referencedName: string, gamesystems: StoreComponent[]): StoreComponent | undefined {
|
||||
return gamesystems.find(gamesystem =>
|
||||
path.basename(gamesystem.fileName) === referencedName)
|
||||
}
|
||||
|
||||
private storeGamesystem(gamesystem: StoreComponent) {
|
||||
const gamesystemFile = path.join(... gamesystem.fileName.split("/"))
|
||||
console.log(gamesystem.jsonString)
|
||||
const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile)
|
||||
FileUtils.prepareFileForWriting(completeGamesystemFile)
|
||||
fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8')
|
||||
|
@ -106,7 +106,7 @@ export class AppComponent implements OnInit{
|
||||
this.gameModel!.removeScriptAccount(affectedModelComponent);
|
||||
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.SCRIPTACCOUNT))
|
||||
} else if(affectedModelComponent instanceof Gamesystem) {
|
||||
//this.gameModel!.removeGamesystem(affectedModelComponent);
|
||||
this.gameModel!.removeGamesystem(affectedModelComponent);
|
||||
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.GAMESYTEM))
|
||||
this.gamesystemOverview!.refresh()
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ export class GamesystemSerializer {
|
||||
}
|
||||
}, SerializeConstants.JSON_INDENT)
|
||||
|
||||
console.log(jsonString)
|
||||
return new StoreComponent(jsonString, fileName, ModelComponentType.GAMESYTEM)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user