Skip to content

Commit 1caa237

Browse files
author
RandomEngy
committed
Changing update system to use sqlite db for storage of values. Made a few changes to verify the downloaded installer exists and added some logging.
1 parent ac5fbfc commit 1caa237

10 files changed

Lines changed: 107 additions & 121 deletions

File tree

Installer/VidCoder-x64.iss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[Setup]
55
AppName=VidCoder
6-
AppVerName=VidCoder 0.9.2 (x64)
6+
AppVerName=VidCoder 0.9.3 (x64)
77

88
DefaultDirName={pf}\VidCoder
99
DisableProgramGroupPage=yes
@@ -13,7 +13,7 @@ Compression=lzma
1313
SolidCompression=yes
1414

1515
OutputDir=BuiltInstallers
16-
OutputBaseFilename=VidCoder-0.9.2-x64
16+
OutputBaseFilename=VidCoder-0.9.3-x64
1717

1818
AppId=VidCoder-x64
1919
UsePreviousAppDir=yes

Installer/VidCoder-x86.iss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[Setup]
55
AppName=VidCoder
6-
AppVerName=VidCoder 0.9.2 (x86)
6+
AppVerName=VidCoder 0.9.3 (x86)
77

88
DefaultDirName={pf}\VidCoder
99
DisableProgramGroupPage=yes
@@ -13,7 +13,7 @@ Compression=lzma
1313
SolidCompression=yes
1414

1515
OutputDir=BuiltInstallers
16-
OutputBaseFilename=VidCoder-0.9.2-x86
16+
OutputBaseFilename=VidCoder-0.9.3-x86
1717

1818
AppId=VidCoder
1919
UsePreviousAppDir=yes

VidCoder/Model/Database.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public static string DatabaseFile
1717
{
1818
get
1919
{
20-
string appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Utilities.AppDataFolderName);
20+
string appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Utilities.AppDataFolderName);
2121

22-
if (!Directory.Exists(appDataFolder))
23-
{
24-
Directory.CreateDirectory(appDataFolder);
25-
}
22+
if (!Directory.Exists(appDataFolder))
23+
{
24+
Directory.CreateDirectory(appDataFolder);
25+
}
2626

27-
return Path.Combine(appDataFolder, ConfigDatabaseFile);
27+
return Path.Combine(appDataFolder, ConfigDatabaseFile);
2828
}
2929
}
3030

VidCoder/Model/DatabaseConfig.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Data.SQLite;
55
using System.Linq;
66
using System.Text;
7+
using VidCoder.Services;
78

89
namespace VidCoder.Model
910
{
@@ -12,13 +13,22 @@ public static class DatabaseConfig
1213
private static Dictionary<string, string> configDefaults;
1314

1415
static DatabaseConfig()
15-
{
16-
configDefaults = new Dictionary<string, string>
16+
{
17+
configDefaults = new Dictionary<string, string>
1718
{
1819
{"Version", Utilities.CurrentDatabaseVersion.ToString()},
19-
{"EncodeJobs", string.Empty}
20+
{"EncodeJobs", string.Empty},
21+
{Updater.UpdateInProgress, "false"},
22+
{Updater.UpdateVersion, string.Empty},
23+
{Updater.UpdateInstallerLocation, string.Empty},
24+
{Updater.UpdateChangelogLocation, string.Empty}
2025
};
21-
}
26+
}
27+
28+
public static bool GetConfigBool(string configName, SQLiteConnection connection)
29+
{
30+
return bool.Parse(GetConfigString(configName, connection));
31+
}
2232

2333
public static string GetConfigString(string configName, SQLiteConnection connection)
2434
{
@@ -57,6 +67,11 @@ private static void AddConfigValue(string configName, string configValue, SQLite
5767
}
5868
}
5969

70+
public static void SetConfigValue(string configName, bool value, SQLiteConnection connection)
71+
{
72+
SetConfigValue(configName, value.ToString(), connection);
73+
}
74+
6075
public static void SetConfigValue(string configName, string configValue, SQLiteConnection connection)
6176
{
6277
var command = new SQLiteCommand("UPDATE settings SET value = ? WHERE name = ?", connection);

VidCoder/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// You can specify all the values or you can default the Build and Revision Numbers
5252
// by using the '*' as shown below:
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("0.9.2.0")]
55-
[assembly: AssemblyFileVersion("0.9.2.0")]
54+
[assembly: AssemblyVersion("0.9.3.0")]
55+
[assembly: AssemblyFileVersion("0.9.3.0")]

VidCoder/Properties/Settings.Designer.cs

Lines changed: 0 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VidCoder/Properties/Settings.settings

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@
6868
<Setting Name="PreviewWindowOpen" Type="System.Boolean" Scope="User">
6969
<Value Profile="(Default)">False</Value>
7070
</Setting>
71-
<Setting Name="UpdateInstallerLocation" Type="System.String" Scope="User">
72-
<Value Profile="(Default)" />
73-
</Setting>
74-
<Setting Name="UpdateInProgress" Type="System.Boolean" Scope="User">
75-
<Value Profile="(Default)">False</Value>
76-
</Setting>
77-
<Setting Name="UpdateVersion" Type="System.String" Scope="User">
78-
<Value Profile="(Default)" />
79-
</Setting>
80-
<Setting Name="UpdateChangelogLocation" Type="System.String" Scope="User">
81-
<Value Profile="(Default)" />
82-
</Setting>
8371
<Setting Name="UpdatesEnabled" Type="System.Boolean" Scope="User">
8472
<Value Profile="(Default)">True</Value>
8573
</Setting>

VidCoder/Services/Updater.cs

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.SQLite;
34
using System.Linq;
45
using System.Text;
56
using System.ComponentModel;
67
using System.Xml.Linq;
78
using System.IO;
89
using System.Net;
910
using System.Windows;
11+
using VidCoder.Model;
1012
using VidCoder.Properties;
1113
using System.Diagnostics;
1214
using Microsoft.Practices.Unity;
@@ -15,6 +17,11 @@ namespace VidCoder.Services
1517
{
1618
public class Updater : IUpdater
1719
{
20+
public const string UpdateInProgress = "UpdateInProgress";
21+
public const string UpdateVersion = "UpdateVersion";
22+
public const string UpdateInstallerLocation = "UpdateInstallerLocation";
23+
public const string UpdateChangelogLocation = "UpdateChangelogLocation";
24+
1825
public event EventHandler<EventArgs<double>> UpdateDownloadProgress;
1926
public event EventHandler<EventArgs> UpdateDownloadCompleted;
2027

@@ -30,6 +37,7 @@ private static bool DebugMode
3037
}
3138
}
3239

40+
private ILogger logger = Unity.Container.Resolve<ILogger>();
3341
private BackgroundWorker updateDownloader;
3442
private bool processDownloadsUpdates = true;
3543

@@ -44,9 +52,9 @@ public void PromptToApplyUpdate()
4452
// If updates are enabled, and we are the last process instance, prompt to apply the update.
4553
if (Settings.Default.UpdatesEnabled && Utilities.CurrentProcessInstances == 1)
4654
{
47-
string installerPath = Settings.Default.UpdateInstallerLocation;
55+
string installerPath = DatabaseConfig.GetConfigString(UpdateInstallerLocation, Database.Connection);
4856

49-
if (installerPath != string.Empty)
57+
if (installerPath != string.Empty && File.Exists(installerPath))
5058
{
5159
// An update is ready, to give a prompt to apply it.
5260
var updateConfirmation = new ApplyUpdateConfirmation();
@@ -79,10 +87,9 @@ public void ApplyUpdate()
7987
// Re-check the process count in case another one was opened while the prompt was active.
8088
if (Utilities.CurrentProcessInstances == 1)
8189
{
82-
string installerPath = Settings.Default.UpdateInstallerLocation;
90+
string installerPath = DatabaseConfig.GetConfigString(UpdateInstallerLocation, Database.Connection);
8391

84-
Settings.Default.UpdateInProgress = true;
85-
Settings.Default.Save();
92+
DatabaseConfig.SetConfigValue(UpdateInProgress, true, Database.Connection);
8693

8794
var installerProcess = new Process();
8895
installerProcess.StartInfo = new ProcessStartInfo { FileName = installerPath, Arguments = "/silent /noicons" };
@@ -100,7 +107,7 @@ public void HandleUpdatedSettings(bool updatesEnabled)
100107
if (updatesEnabled)
101108
{
102109
// If we don't already have an update waiting to install, check for updates.
103-
if (processDownloadsUpdates && Settings.Default.UpdateInstallerLocation == string.Empty)
110+
if (processDownloadsUpdates && DatabaseConfig.GetConfigString(UpdateInstallerLocation, Database.Connection) == string.Empty)
104111
{
105112
this.StartBackgroundUpdate();
106113
}
@@ -115,17 +122,29 @@ public void HandleUpdatedSettings(bool updatesEnabled)
115122

116123
public void HandlePendingUpdate()
117124
{
118-
bool updateInProgress = Settings.Default.UpdateInProgress;
119-
string targetUpdateVersion = Settings.Default.UpdateVersion;
120-
125+
// This flag signifies VidCoder is being run by the installer after an update.
126+
// In this case we report success, delete the installer, clean up the update flags and exit.
127+
bool updateInProgress = DatabaseConfig.GetConfigBool(UpdateInProgress, Database.Connection);
121128
if (updateInProgress)
122129
{
123-
Settings.Default.UpdateInProgress = false;
124-
Settings.Default.Save();
130+
string targetUpdateVersion = DatabaseConfig.GetConfigString(UpdateVersion, Database.Connection);
131+
bool updateSucceeded = Utilities.CompareVersions(targetUpdateVersion, Utilities.CurrentVersion) == 0;
132+
133+
using (SQLiteTransaction transaction = Database.Connection.BeginTransaction())
134+
{
135+
DatabaseConfig.SetConfigValue(UpdateInProgress, false, Database.Connection);
136+
137+
if (updateSucceeded)
138+
{
139+
DatabaseConfig.SetConfigValue(UpdateVersion, string.Empty, Database.Connection);
140+
DatabaseConfig.SetConfigValue(UpdateInstallerLocation, string.Empty, Database.Connection);
141+
DatabaseConfig.SetConfigValue(UpdateChangelogLocation, string.Empty, Database.Connection);
142+
}
143+
144+
transaction.Commit();
145+
}
125146

126-
// This flag signifies VidCoder is being run by the installer after an update.
127-
// In this case we report success, delete the installer, clean up the update flags and exit.
128-
if (Utilities.CompareVersions(targetUpdateVersion, Utilities.CurrentVersion) == 0)
147+
if (updateSucceeded)
129148
{
130149
MessageBox.Show("VidCoder has been successfully updated.");
131150

@@ -134,18 +153,15 @@ public void HandlePendingUpdate()
134153
Directory.Delete(Utilities.UpdatesFolder, true);
135154
}
136155

137-
Settings.Default.UpdateVersion = string.Empty;
138-
Settings.Default.UpdateInstallerLocation = string.Empty;
139-
Settings.Default.UpdateChangelogLocation = string.Empty;
140-
Settings.Default.Save();
141-
142156
Environment.Exit(0);
143157
}
144-
145-
// If the target version is different from the currently running version,
146-
// this means the attempted upgrade failed. We give an error message but
147-
// continue with the program.
148-
MessageBox.Show("The update was not applied. If you did not cancel it, try installing it manually.");
158+
else
159+
{
160+
// If the target version is different from the currently running version,
161+
// this means the attempted upgrade failed. We give an error message but
162+
// continue with the program.
163+
MessageBox.Show("The update was not applied. If you did not cancel it, try installing it manually.");
164+
}
149165
}
150166
}
151167

@@ -164,8 +180,7 @@ public void CheckUpdates()
164180
if (!Settings.Default.UpdatesEnabled)
165181
{
166182
// On a program restart, if updates are disabled, clean any pending installers.
167-
Settings.Default.UpdateInstallerLocation = string.Empty;
168-
Settings.Default.Save();
183+
DatabaseConfig.SetConfigValue(UpdateInstallerLocation, string.Empty, Database.Connection);
169184

170185
if (Directory.Exists(Utilities.UpdatesFolder))
171186
{
@@ -200,6 +215,7 @@ private void StartBackgroundUpdate()
200215
private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
201216
{
202217
var updateDownloader = sender as BackgroundWorker;
218+
SQLiteConnection connection = Database.CreateConnection();
203219

204220
try
205221
{
@@ -235,9 +251,28 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
235251

236252
if (Utilities.CompareVersions(updateVersion, Utilities.CurrentVersion) > 0)
237253
{
254+
// If an update is reported to be ready but the installer doesn't exist, clear out all the
255+
// installer info and redownload.
256+
string updateInstallerLocation = DatabaseConfig.GetConfigString(UpdateInstallerLocation, connection);
257+
if (updateInstallerLocation != string.Empty && !File.Exists(updateInstallerLocation))
258+
{
259+
using (SQLiteTransaction transaction = connection.BeginTransaction())
260+
{
261+
DatabaseConfig.SetConfigValue(UpdateVersion, string.Empty, Database.Connection);
262+
DatabaseConfig.SetConfigValue(UpdateInstallerLocation, string.Empty, Database.Connection);
263+
DatabaseConfig.SetConfigValue(UpdateChangelogLocation, string.Empty, Database.Connection);
264+
265+
transaction.Commit();
266+
}
267+
268+
this.logger.Log("Downloaded update (" + updateInstallerLocation + ") could not be found. Re-downloading it.");
269+
}
270+
238271
// If we have not finished the download update yet, start/resume the download.
239-
if (Settings.Default.UpdateInstallerLocation == string.Empty)
272+
if (DatabaseConfig.GetConfigString(UpdateInstallerLocation, connection) == string.Empty)
240273
{
274+
this.logger.Log("Started downloading update " + updateVersion);
275+
241276
string downloadLocation = downloadElement.Value;
242277
string changelogLink = changelogLinkElement.Value;
243278
string installerFileName = Path.GetFileName(downloadLocation);
@@ -269,7 +304,7 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
269304
responseStream = response.GetResponseStream();
270305

271306
byte[] downloadBuffer = new byte[2048];
272-
int bytesRead = 0;
307+
int bytesRead;
273308

274309
while ((bytesRead = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length)) > 0 && !updateDownloader.CancellationPending)
275310
{
@@ -285,12 +320,18 @@ private void CheckAndDownloadUpdate(object sender, DoWorkEventArgs e)
285320

286321
if (bytesRead == 0)
287322
{
288-
Settings.Default.UpdateVersion = updateVersion;
289-
Settings.Default.UpdateInstallerLocation = installerFilePath;
290-
Settings.Default.UpdateChangelogLocation = changelogLink;
291-
Settings.Default.Save();
323+
using (SQLiteTransaction transaction = connection.BeginTransaction())
324+
{
325+
DatabaseConfig.SetConfigValue(UpdateVersion, updateVersion, connection);
326+
DatabaseConfig.SetConfigValue(UpdateInstallerLocation, installerFilePath, connection);
327+
DatabaseConfig.SetConfigValue(UpdateChangelogLocation, changelogLink, connection);
328+
329+
transaction.Commit();
330+
}
292331

293332
this.UpdateReady = true;
333+
334+
this.logger.Log("Update " + updateVersion + " has finished downloading and will install on exit.");
294335
}
295336
}
296337
finally

0 commit comments

Comments
 (0)