From 0daa24defaed5f58d3369ea163303a54049565fc Mon Sep 17 00:00:00 2001 From: Doriel Rivalet <100863878+DorielRivalet@users.noreply.github.com> Date: Mon, 31 Jul 2023 17:08:29 -0300 Subject: [PATCH] fix(IO): previous version file path handling closes DorielRivalet/mhfz-overlay#147 --- MHFZ_Overlay/Services/DatabaseService.cs | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/MHFZ_Overlay/Services/DatabaseService.cs b/MHFZ_Overlay/Services/DatabaseService.cs index b140dd5d..bee3b3e2 100644 --- a/MHFZ_Overlay/Services/DatabaseService.cs +++ b/MHFZ_Overlay/Services/DatabaseService.cs @@ -265,6 +265,8 @@ public bool SetupLocalDatabase(DataLoader dataLoader) this.CreateDatabaseIndexes(conn); this.CreateDatabaseTriggers(conn); this.CheckDatabaseVersion(conn, dataLoader); + + // TODO: avoid using version files. find alternatives to IO. WriteNewVersionToFile(); } @@ -14811,7 +14813,25 @@ private void CheckPreviousVersionFile() { logger.Info(CultureInfo.InvariantCulture, "previousVersionFilePath found, reading version number."); previousVersion = File.ReadAllText(previousVersionFilePath).Trim(); - logger.Info(CultureInfo.InvariantCulture, "previousVersionFilePath version number {0}", previousVersion); + if (string.IsNullOrEmpty(previousVersion)) + { + logger.Info(CultureInfo.InvariantCulture, "previousVersionFilePath contents are empty, writing to file"); + if (App.CurrentProgramVersion == null) + { + logger.Fatal(CultureInfo.InvariantCulture, "CurrentProgramVersion does not exist"); + MessageBox.Show("Current Program Version not found."); + LoggingService.WriteCrashLog(new Exception("CurrentProgramVersion not found."), logMessage); + return; + } + + previousVersion = App.CurrentProgramVersion.Trim(); + File.WriteAllText(previousVersionFilePath, previousVersion); + logger.Info(CultureInfo.InvariantCulture, "Writing previous version {0} to file {1}", previousVersion, previousVersionFilePath); + } + else + { + logger.Info(CultureInfo.InvariantCulture, "previousVersionFilePath version number {0}", previousVersion); + } } else { @@ -14848,9 +14868,8 @@ private void WriteNewVersionToFile() var versionInFile = File.ReadAllText(previousVersionFilePath).Trim(); if (string.IsNullOrEmpty(versionInFile)) { - logger.Fatal(CultureInfo.InvariantCulture, "previousVersionFilePath file is empty"); - MessageBox.Show("previous-version.txt is empty."); - LoggingService.WriteCrashLog(new Exception("previous-version.txt is empty."), logMessage); + logger.Warn(CultureInfo.InvariantCulture, "previousVersionFilePath file is empty"); + versionInFile = "Fresh Install"; } if (App.CurrentProgramVersion == null)