Skip to content

Commit

Permalink
* Added JSON support
Browse files Browse the repository at this point in the history
* Refactoring
* Version bump
  • Loading branch information
CodeDead committed May 21, 2019
1 parent a862fbf commit 0e53050
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 36 deletions.
5 changes: 2 additions & 3 deletions Package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>CodeDead.UpdateManager</id>
<version>1.4.4</version>
<version>1.5</version>
<authors>CodeDead</authors>
<owners>CodeDead</owners>
<license type="file">LICENSE.txt</license>
Expand All @@ -14,8 +14,7 @@
This library can be used to check for application updates. It is designed for WPF and Windows Forms applications. In order to use it, you require an XML file on a remote or local server that represents the Update class.
</description>
<releaseNotes>
* Upgrade to .NET Framework 4.8
* Code refactoring
Added JSON support and some refactoring
</releaseNotes>
<copyright>Copyright © 2019 CodeDead</copyright>
<tags>CodeDead UpdateManager Update Updater updates updating downloader</tags>
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ StringVariables stringVariables = new StringVariables
TitleText = "Your application title",
UpdateNowText = "Would you like to update the application now?"
};
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables);
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml);
```

Check for updates like this:
Expand All @@ -39,8 +39,30 @@ catch (Exception ex)
MessageBox.Show(ex.Message, "Application title", MessageBoxButton.OK, MessageBoxImage.Error);
}
```
## Update types
Updates can be stored on your server in two different formats: *JSON* or *XML*. Be sure to set the right DataType when initializing the UpdateManager:
```C#
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml);
```

```C#
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Json);
```

### JSON example
```JSON
{
"MajorVersion": 1,
"MinorVersion": 0,
"BuildVersion": 0,
"RevisionVersion": 0,
"UpdateUrl": "https://example.com/update.exe",
"InfoUrl": "https://codedead.com",
"UpdateInfo": "A new version is now available. Please click the download button to download version 1.0.0.0"
}
```

## Update XML example
### XML example
```XML
<?xml version="1.0"?>
<Update xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Expand Down
11 changes: 11 additions & 0 deletions UpdateManager/Classes/DataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CodeDead.UpdateManager.Classes
{
/// <summary>
/// Enum that contains the different data types that are supported in order to deserialize the Update information
/// </summary>
public enum DataType
{
Json,
Xml
}
}
89 changes: 60 additions & 29 deletions UpdateManager/Classes/UpdateManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
using System.Windows;
using System.Xml.Serialization;
using CodeDead.UpdateManager.Windows;
Expand All @@ -18,17 +19,17 @@ public sealed class UpdateManager
/// </summary>
private readonly string _updateUrl;
/// <summary>
/// The Update object containing data about the current update
/// </summary>
private Update _update;
/// <summary>
/// The version of the application
/// </summary>
private readonly Version _applicationVersion;
/// <summary>
/// The string variables that can be used to display information to the user
/// </summary>
private StringVariables _stringVariables;
/// <summary>
/// The DataType that can be used to deserialize the update information
/// </summary>
private readonly DataType _dataType;
#endregion

/// <summary>
Expand All @@ -37,14 +38,12 @@ public sealed class UpdateManager
/// <param name="version">Your application version</param>
/// <param name="updateUrl">The URL where your XML update file is located</param>
/// <param name="stringVariables">StringVariables object containing strings that can be used to display information to the user</param>
public UpdateManager(Version version, string updateUrl, StringVariables stringVariables)
/// <param name="datatype">The DataType that can be used to deserialize the update information</param>
public UpdateManager(Version version, string updateUrl, StringVariables stringVariables, DataType datatype)
{
_updateUrl = updateUrl;

_update = new Update();
_dataType = datatype;
_applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision);

_update.SetApplicationVersion(_applicationVersion);
SetStringVariables(stringVariables);
}

Expand All @@ -53,14 +52,12 @@ public UpdateManager(Version version, string updateUrl, StringVariables stringVa
/// </summary>
/// <param name="version">Your application version</param>
/// <param name="updateUrl">The URL where your XML update file is located</param>
public UpdateManager(Version version, string updateUrl)
/// <param name="dataType">The DataType that can be used to deserialize the update information</param>
public UpdateManager(Version version, string updateUrl, DataType dataType)
{
_updateUrl = updateUrl;

_update = new Update();
_dataType = dataType;
_applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision);

_update.SetApplicationVersion(_applicationVersion);
SetStringVariables(new StringVariables());
}

Expand All @@ -73,31 +70,30 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
{
try
{
string xml = await new WebClient().DownloadStringTaskAsync(_updateUrl);

XmlSerializer serializer = new XmlSerializer(_update.GetType());
using (MemoryStream stream = new MemoryStream())
string data = await new WebClient().DownloadStringTaskAsync(_updateUrl);
Update update;
switch (_dataType)
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
_update = (Update)serializer.Deserialize(stream);
_update.SetApplicationVersion(_applicationVersion);
writer.Dispose();
default:
update = DeserializeJson(data);
break;
case DataType.Xml:
update = DeserializeXml(data);
break;
}

if (_update.CheckForUpdate())

if (update.CheckForUpdate())
{
UpdateWindow window = new UpdateWindow
{
Title = _stringVariables.TitleText,
InformationTextBlockContent = _update.UpdateInfo,
InformationTextBlockContent = update.UpdateInfo,
InformationButtonContent = _stringVariables.InformationButtonText,
CancelButtonContent = _stringVariables.CancelButtonText,
DownloadButtonContent = _stringVariables.DownloadButtonText,
DownloadUrl = _update.UpdateUrl,
InformationUrl = _update.InfoUrl,
DownloadUrl = update.UpdateUrl,
InformationUrl = update.InfoUrl,
UpdateNowText = _stringVariables.UpdateNowText
};
window.ShowDialog();
Expand All @@ -119,6 +115,41 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
}
}

/// <summary>
/// Deserialize the XML data into an Update object
/// </summary>
/// <param name="data">The XML data that should be deserialized</param>
/// <returns>The Update object that was deserialized</returns>
private Update DeserializeXml(string data)
{
Update update;
XmlSerializer serializer = new XmlSerializer(typeof(Update));
using (MemoryStream stream = new MemoryStream())
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(data);
writer.Flush();
stream.Position = 0;
update = (Update)serializer.Deserialize(stream);
update.SetApplicationVersion(_applicationVersion);
writer.Dispose();
}

return update;
}

/// <summary>
/// Deserialize the Json data into an Update object
/// </summary>
/// <param name="data">The Json data that should be deserialized</param>
/// <returns>The Update object that was deserialized</returns>
private Update DeserializeJson(string data)
{
Update update = new JavaScriptSerializer().Deserialize<Update>(data);
update.SetApplicationVersion(_applicationVersion);
return update;
}

/// <summary>
/// Change the StringVariables during runtime
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions UpdateManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.4.0")]
[assembly: AssemblyFileVersion("1.4.4.0")]
[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
6 changes: 6 additions & 0 deletions UpdateManager/UpdateManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject>
</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Windows" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\DataType.cs" />
<Compile Include="Classes\StringVariables.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Classes\Update.cs" />
Expand Down

0 comments on commit 0e53050

Please sign in to comment.