From bb502a3a003d2ecdbd8aedeb440b3ecefdf52d77 Mon Sep 17 00:00:00 2001 From: Alexander Drozdov Date: Fri, 17 Sep 2021 15:28:53 +0300 Subject: [PATCH] remove CLOSE_SETTINGS_WINDOW --- src/ipc/ipc-event.ts | 2 -- src/ipc/main-process-bindings.ts | 6 ------ src/main/config.ts | 21 +++++++-------------- src/main/game-config.ts | 23 ++++++++++++++++------- src/web/settings/SettingsWindow.vue | 4 +--- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/ipc/ipc-event.ts b/src/ipc/ipc-event.ts index 8ec08266..a8ed00ed 100644 --- a/src/ipc/ipc-event.ts +++ b/src/ipc/ipc-event.ts @@ -3,8 +3,6 @@ export const PUSH_CONFIG = 'push-config' export const PRICE_CHECK_HIDE = 'OVERLAY->MAIN::price-check-hide' -export const CLOSE_SETTINGS_WINDOW = 'close-settings-window' - export const UPDATE_AVAILABLE = 'update-available' export interface IpcUpdateInfo { auto: boolean diff --git a/src/ipc/main-process-bindings.ts b/src/ipc/main-process-bindings.ts index eeff635e..0e56d5b7 100644 --- a/src/ipc/main-process-bindings.ts +++ b/src/ipc/main-process-bindings.ts @@ -126,12 +126,6 @@ class MainProcessBinding extends EventTarget { } } - closeSettingsWindow (config?: Config) { - if (electron) { - electron.ipcRenderer.send(ipcEvent.CLOSE_SETTINGS_WINDOW, config) - } - } - stashSearch (text: string) { if (electron) { electron.ipcRenderer.send(ipcEvent.STASH_SEARCH, { text }) diff --git a/src/main/config.ts b/src/main/config.ts index 1f514eb1..d021044b 100644 --- a/src/main/config.ts +++ b/src/main/config.ts @@ -2,8 +2,7 @@ import Store from 'electron-store' import { dialog, ipcMain, app } from 'electron' import isDeepEq from 'fast-deep-equal' import { Config, defaultConfig } from '@/ipc/types' -import { GET_CONFIG, PUSH_CONFIG, CLOSE_SETTINGS_WINDOW } from '@/ipc/ipc-event' -import { overlayWindow } from './overlay-window' +import { GET_CONFIG, PUSH_CONFIG } from '@/ipc/ipc-event' import { logger } from './logger' import { LogWatcher } from './LogWatcher' import { ItemCheckWidget } from '@/web/overlay/interfaces' @@ -14,13 +13,7 @@ export function setupConfigEvents () { e.returnValue = config.store }) ipcMain.on(PUSH_CONFIG, (e, cfg: Config) => { - batchUpdateConfig(cfg, false) - }) - ipcMain.on(CLOSE_SETTINGS_WINDOW, (e, cfg: Config | undefined) => { - if (cfg != null) { - loadAndCacheGameCfg() - batchUpdateConfig(cfg, true) - } + batchUpdateConfig(cfg) }) } @@ -46,18 +39,18 @@ export const config = (() => { return store })() -export function batchUpdateConfig (newCfg: Config, push = true) { +export function batchUpdateConfig (newCfg: Config) { const oldCfg = config.store Object.setPrototypeOf(oldCfg, Object.prototype) if (!isDeepEq(newCfg, oldCfg)) { config.store = newCfg - logger.verbose('Saved', { source: 'config', push }) - if (push) { - overlayWindow!.webContents.send(PUSH_CONFIG, newCfg) - } + logger.verbose('Saved.', { source: 'config' }) if (oldCfg.clientLog !== newCfg.clientLog) { LogWatcher.start() } + if (oldCfg.gameConfig !== newCfg.gameConfig) { + loadAndCacheGameCfg() + } } } diff --git a/src/main/game-config.ts b/src/main/game-config.ts index 5f7866a7..7d774844 100644 --- a/src/main/game-config.ts +++ b/src/main/game-config.ts @@ -10,13 +10,17 @@ export interface GameConfig { highlightKey: string | null } +const defaultConfig = (): GameConfig => ({ + highlightKey: 'Alt' +}) + export let gameConfig: GameConfig | null = null export function loadAndCache () { gameConfig = readConfig() return gameConfig } -export function readConfig (): GameConfig | null { +export function readConfig (): GameConfig { let filePath = appConfig.get('gameConfig') if (!filePath) { @@ -25,7 +29,8 @@ export function readConfig (): GameConfig | null { fs.accessSync(filePath) appConfig.set('gameConfig', filePath) } catch { - return null + logger.error('Failed to find game configuration file in the default location. Default values will be used instead.', { source: 'game-config', file: filePath }) + return defaultConfig() } } @@ -35,21 +40,24 @@ export function readConfig (): GameConfig | null { const parsed = ini.parse(contents) return { - highlightKey: parseConfigHotkey(parsed.ACTION_KEYS?.highlight || '') + highlightKey: parseConfigHotkey(parsed.ACTION_KEYS?.highlight) } } catch { - logger.error('Failed to read file', { source: 'game-config', file: filePath }) - return null + logger.error('Failed to read game configuration file. Default values will be used instead.', { source: 'game-config', file: filePath }) + return defaultConfig() } } -function parseConfigHotkey (cgfKey: string): string | null { - const [keyMain, keyMod] = cgfKey.split(' ') +function parseConfigHotkey (cfgKey?: string): string | null { + if (!cfgKey) return null + + const [keyMain, keyMod] = cfgKey.split(' ') let key1: string if (CodeToKey[keyMain]) { key1 = CodeToKey[keyMain] } else { + logger.error('Failed to read key.', { source: 'game-config', key: cfgKey }) return null } @@ -62,6 +70,7 @@ function parseConfigHotkey (cgfKey: string): string | null { } else if (keyMod === '3') { key2 = 'Alt' } else { + logger.error('Failed to read modifier key.', { source: 'game-config', key: cfgKey }) return null } } diff --git a/src/web/settings/SettingsWindow.vue b/src/web/settings/SettingsWindow.vue index e0aa157c..73582ffa 100644 --- a/src/web/settings/SettingsWindow.vue +++ b/src/web/settings/SettingsWindow.vue @@ -31,7 +31,6 @@ import { defineComponent, shallowRef, computed, Component, PropType, nextTick, i import { useI18n } from 'vue-i18n' import { Config } from '@/web/Config' import type { Widget, WidgetManager } from '../overlay/interfaces' -import { MainProcess } from '@/ipc/main-process-bindings' import SettingsHotkeys from './hotkeys.vue' import SettingsChat from './chat.vue' import SettingsGeneral from './general.vue' @@ -96,11 +95,10 @@ export default defineComponent({ t, save () { wm.hide(props.config.wmId) - MainProcess.closeSettingsWindow(JSON.parse(JSON.stringify(Config.store))) + Config.saveConfig() }, cancel () { wm.hide(props.config.wmId) - MainProcess.closeSettingsWindow() }, menusItems, selectedComponent