From 81facacc9be67ec67bfbc57ad69158a38ba37348 Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Fri, 24 Nov 2023 17:43:28 +0100 Subject: [PATCH] Migrate settings.ini from utf16 to utf8 --- electron/src/background.ts | 3 ++- electron/src/lib/SettingsService.js | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/electron/src/background.ts b/electron/src/background.ts index 539dcbdc..494d7e03 100644 --- a/electron/src/background.ts +++ b/electron/src/background.ts @@ -28,8 +28,9 @@ export function getWindow() { } async function createWindow() { + await SettingsService.runPreSettingsMigrations() await SettingsService.makeSureSettingsFileExists() - await SettingsService.migrateSettingsVersion() + await SettingsService.runSettingsMigrations() registerUIIpcApi() diff --git a/electron/src/lib/SettingsService.js b/electron/src/lib/SettingsService.js index 58b62464..06894c79 100644 --- a/electron/src/lib/SettingsService.js +++ b/electron/src/lib/SettingsService.js @@ -98,7 +98,32 @@ export class SettingsService { return events } - static async migrateSettingsVersion() { + /** + * Runs migrations before having read the settings + * @returns {Promise} + */ + static async runPreSettingsMigrations() { + if (!fs.existsSync(SETTINGS_PATH)) { + return + } + + const currentVersion = (await updater.getVersion()).trim() + + if (fs.existsSync(LAST_VERSION_FILE) && currentVersion !== 'develop') { + const lastVersion = (await fs.promises.readFile(LAST_VERSION_FILE, { encoding: 'utf-8' })).trim() + + if (semver.lt(lastVersion, '3.0.0') && semver.gte(currentVersion, '3.0.0')) { // settings.ini encoding changed from utf16le to utf-8 in 3.0.0 + const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf16le' }); + await fs.promises.writeFile(SETTINGS_PATH, settingsIniContent, { encoding: 'utf-8' }); + } + } + } + + /** + * Run migrations after having read the settings + * @returns {Promise} + */ + static async runSettingsMigrations() { const currentVersion = (await updater.getVersion()).trim() if (fs.existsSync(LAST_VERSION_FILE) && currentVersion !== 'develop') { @@ -175,7 +200,7 @@ export class SettingsService { let settingsObject = getDefaultSettings() if (fs.existsSync(SETTINGS_PATH)) { - const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf16le' }); + const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf-8' }); settingsObject = merge(settingsObject, ini.parse(settingsIniContent.trimStart())) } else { console.log("Settings file not found. Using default settings.");