Skip to content

Commit

Permalink
#47
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 22, 2024
1 parent a50b11c commit bdc20c7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
11 changes: 6 additions & 5 deletions app/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ export function setupIPC(sendToUI: SendToUI) {
sendToUI('json', { name: json, data: jsonData });
});

ipcMain.on('BACKUP_MOD', (e: any, mod: any) => {
ipcMain.on('BACKUP_MOD', async (e: any, mod: any) => {
fs.ensureDirSync(`${baseUrl}/resources/modbackup`);
fs.writeJSONSync(

await fs.writeJSON(
`${baseUrl}/resources/modbackup/${mod.meta.name}.rairdevmod.bak`,
mod,
{ spaces: 2 }
Expand Down Expand Up @@ -187,7 +188,7 @@ export function setupIPC(sendToUI: SendToUI) {

ipcMain.on(
'SAVE_MOD_WITH_BACKUP',
(e: any, { modData, quicksaveFilepath }: any) => {
async (e: any, { modData, quicksaveFilepath }: any) => {
if (!quicksaveFilepath || !modData) return;

if (!fs.existsSync(quicksaveFilepath)) return;
Expand All @@ -196,8 +197,8 @@ export function setupIPC(sendToUI: SendToUI) {
const fullMod = modData;
const backupPath = `${baseUrl}/resources/backup.rairmod`;

fs.writeJSONSync(backupPath, fullMod, { spaces: 2 });
fs.moveSync(backupPath, quicksaveFilepath, { overwrite: true });
await fs.writeJSON(backupPath, fullMod, { spaces: 2 });
await fs.move(backupPath, quicksaveFilepath, { overwrite: true });
} catch (e) {
console.error('Could not quick save?');
console.error(e);
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/electron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ export class ElectronService {

effect(() => {
const mod = this.modService.mod();
this.send('BACKUP_MOD', mod);

const settings = this.settingsService.allSettings()[mod.meta.id];
if (settings.autosaveFilePath) {
this.send('SAVE_MOD_WITH_BACKUP', {
modData: mod,
quicksaveFilepath: settings.autosaveFilePath,
});
} else {
this.send('BACKUP_MOD', mod);
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/services/mod.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class ModService {
);

this.ensureMapsExist();

(window as any).modService = this;
}

// mod functions
Expand Down
5 changes: 4 additions & 1 deletion src/app/tabs/stems/stems.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
@if(editing) {
<app-stems-editor class="h-full w-full" [editing]="editingData()" (goBack)="cancelEditing()"
(save)="saveNewData($event)"></app-stems-editor>
}
}

<input #stemUpload type="file" multiple (change)="importPartialSTEMs($event)" accept="application/json"
class="hidden" />
44 changes: 43 additions & 1 deletion src/app/tabs/stems/stems.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Component, computed } from '@angular/core';
import {
Component,
computed,
ElementRef,
inject,
viewChild,
} from '@angular/core';
import { ColDef } from 'ag-grid-community';

import { IModKit } from '../../../interfaces';
import { ISTEM } from '../../../interfaces/stem';
import { id } from '../../helpers';
import { defaultSTEM } from '../../helpers/stem';
import { NotifyService } from '../../services/notify.service';
import { CellButtonsComponent } from '../../shared/components/cell-buttons/cell-buttons.component';
import { CellIconComponent } from '../../shared/components/cell-icon/cell-icon.component';
import { EditorBaseTableComponent } from '../../shared/components/editor-base-table/editor-base-table.component';
Expand All @@ -18,7 +25,11 @@ type EditingType = ISTEM;
styleUrl: './stems.component.scss',
})
export class StemsComponent extends EditorBaseTableComponent<EditingType> {
private notifyService = inject(NotifyService);

protected dataKey: keyof Omit<IModKit, 'meta'> = 'stems';
public importSTEMButton =
viewChild<ElementRef<HTMLInputElement>>('stemUpload');

public defaultData = defaultSTEM;

Expand Down Expand Up @@ -82,6 +93,8 @@ export class StemsComponent extends EditorBaseTableComponent<EditingType> {
headerComponentParams: {
showNewButton: true,
newCallback: () => this.createNew(),
showImportButton: false,
importCallback: () => this.importSTEMButton()?.nativeElement.click(),
},
cellRenderer: CellButtonsComponent,
cellClass: 'no-adjust',
Expand All @@ -100,4 +113,33 @@ export class StemsComponent extends EditorBaseTableComponent<EditingType> {
},
},
];

public importPartialSTEMs(event: any) {
const files = event.target.files;

Array.from(files as ArrayLike<File>).forEach((file: File) => {
if (!file || file.type !== 'application/json') return;

const reader = new FileReader();
reader.readAsText(file, 'UTF-8');

reader.onload = (evt: any) => {
try {
const stems = JSON.parse(evt.target.result as string);
stems.forEach((stem: ISTEM) => {
stem._id = stem._id ?? id();
this.modService.modAdd<ISTEM>(this.dataKey, stem);
});
} catch (e: any) {
this.notifyService.error({
message: `STEM upload error: ${e.message}`,
});
}
};

reader.onerror = () => {
this.notifyService.error({ message: `Generic STEM upload error.` });
};
});
}
}

0 comments on commit bdc20c7

Please sign in to comment.