Skip to content

Commit

Permalink
closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 16, 2024
1 parent eaf03a8 commit d5e7091
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
21 changes: 21 additions & 0 deletions app/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,32 @@ export function setupIPC(sendToUI: SendToUI) {

fs.writeJSONSync(res, fullMod, { spaces: 2 });
sendToUI('notify', { type: 'info', text: `Saved ${res}!` });
sendToUI('updatesetting', { setting: 'autosaveFilePath', value: res });
} catch {
sendToUI('notify', { type: 'error', text: `Failed to save ${res}!` });
}
});

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

if (!fs.existsSync(quicksaveFilepath)) return;

try {
const fullMod = modData;
const backupPath = `${baseUrl}/resources/backup.rairmod`;

fs.writeJSONSync(backupPath, fullMod, { spaces: 2 });
fs.moveSync(backupPath, quicksaveFilepath, { overwrite: true });
} catch (e) {
console.error('Could not quick save?');
console.error(e);
}
}
);

ipcMain.on('LOAD_MOD', () => {
const res = dialog.showOpenDialogSync(null, {
title: 'Load Land of the Rair Mod',
Expand Down
21 changes: 21 additions & 0 deletions src/app/services/electron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IEditorMap, IModKit } from '../../interfaces';
import { importMod } from '../helpers/importer';
import { ModService } from './mod.service';
import { NotifyService } from './notify.service';
import { ModSettings, SettingsService } from './settings.service';

declare global {
interface Window {
Expand All @@ -24,13 +25,22 @@ export class ElectronService {

private modService = inject(ModService);
private notifyService = inject(NotifyService);
private settingsService = inject(SettingsService);

constructor() {
this.watchIPC();

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,
});
}
});
}

Expand Down Expand Up @@ -80,6 +90,17 @@ export class ElectronService {
this.modService.updateMod(importedMod);
});

window.api.receive(
'updatesetting',
(settingsData: { setting: keyof ModSettings; value: any }) => {
this.settingsService.setSettingForMod(
this.modService.mod().meta.id,
settingsData.setting,
settingsData.value
);
}
);

this.send('READY_CHECK');
}

Expand Down
20 changes: 14 additions & 6 deletions src/app/services/mod.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
IModKit,
ItemSlotType,
} from '../../interfaces';
import { id } from '../helpers';
import { SettingsService } from './settings.service';

export function defaultModKit(): IModKit {
return {
meta: {
id: id(),
author: 'Anonymous',
name: 'Unnamed Mod',
savedAt: 0,
Expand All @@ -33,6 +36,7 @@ export function defaultModKit(): IModKit {
})
export class ModService {
private localStorage = inject(LocalStorageService);
private settingsService = inject(SettingsService);

public mod = signal<IModKit>(defaultModKit());
public modName = computed(() => this.mod().meta.name);
Expand All @@ -50,10 +54,14 @@ export class ModService {
this.updateMod(oldModData);
}

effect(() => {
const newModData = this.mod();
this.localStorage.store('mod', newModData);
});
effect(
() => {
const newModData = this.mod();
this.settingsService.createSettingsForMod(newModData.meta.id);
this.localStorage.store('mod', newModData);
},
{ allowSignalWrites: true }
);

this.ensureMapsExist();
}
Expand Down Expand Up @@ -81,8 +89,8 @@ export class ModService {
mod.meta.savedAt = Date.now();

mod.maps.forEach(({ map }) => {
map.properties = map.properties || {};
map.propertytypes = map.propertytypes || {};
map.properties ??= {};
map.propertytypes ??= {};

map.properties.creator = mod.meta.author || 'Unknown';
map.propertytypes.creator = 'string';
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ISpawnerData } from './spawner';

export interface IModKit {
meta: {
id: string;
name: string;
author: string;
version: number;
Expand Down

0 comments on commit d5e7091

Please sign in to comment.