-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5684025
commit 033f812
Showing
10 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.IO; | ||
using Gtk; | ||
|
||
namespace LadderLogic.Updater | ||
{ | ||
public static class UpdateHelper | ||
{ | ||
private static Window _parent; | ||
|
||
public static string RemoteVersion = "http://localhost/ArduinoLadder/updateVersion.txt"; | ||
public static string RemoteFile = "http://localhost/ArduinoLadder/ArduinoLadder.exe"; | ||
public static string LocalFile = "ArduinoLadder.exe"; | ||
|
||
public static void CompareVersions(Window parent) | ||
{ | ||
_parent = parent; | ||
var downloadToPath = Path.GetTempPath(); | ||
var localVersion = Versions.LocalVersion(); | ||
var remoteVersion = Versions.RemoteVersion(RemoteVersion); | ||
if (string.IsNullOrWhiteSpace(remoteVersion)) //prevent to reload first version | ||
{ | ||
return; | ||
} | ||
var c = 0; | ||
try | ||
{ | ||
c = Version.Parse(localVersion).CompareTo(Version.Parse(remoteVersion)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
} | ||
|
||
if (c < 0) | ||
{ | ||
BeginDownload(RemoteFile, downloadToPath, remoteVersion, LocalFile); | ||
} | ||
} | ||
|
||
private static void BeginDownload(string remoteUrl, string downloadToPath, string version, string executeTarget) | ||
{ | ||
var filePath = Versions.CreateTargetLocation(downloadToPath, version); | ||
|
||
filePath = Path.Combine(filePath, executeTarget); | ||
|
||
var remoteUri = new Uri(remoteUrl); | ||
var downloader = new System.Net.WebClient(); | ||
|
||
downloader.DownloadFileCompleted += downloader_DownloadFileCompleted; | ||
|
||
downloader.DownloadFileAsync(remoteUri, filePath, | ||
new[] { version, downloadToPath, executeTarget }); | ||
} | ||
|
||
|
||
private static void downloader_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) | ||
{ | ||
if (e.Error != null) | ||
{ | ||
return; | ||
} | ||
var us = (string[])e.UserState; | ||
var currentVersion = us[0]; | ||
var downloadToPath = us[1]; | ||
var executeTarget = us[2]; | ||
|
||
if (!downloadToPath.EndsWith("\\")) // Give a trailing \ if there isn't one | ||
downloadToPath += "\\"; | ||
|
||
var exePath = downloadToPath + currentVersion + "\\" + executeTarget; // Download folder\version\ + executable | ||
var md = new MessageDialog(_parent, | ||
DialogFlags.DestroyWithParent, MessageType.Info, | ||
ButtonsType.YesNo, "New version available. Do you want to install?"); | ||
if ((ResponseType) md.Run() != ResponseType.Yes) return; | ||
System.Diagnostics.Process.Start(exePath); | ||
Environment.Exit(0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace LadderLogic.Updater | ||
{ | ||
public static class Versions | ||
{ | ||
public static string RemoteVersion(string url) | ||
{ | ||
var rv = ""; | ||
|
||
try | ||
{ | ||
var req = (System.Net.HttpWebRequest) | ||
System.Net.WebRequest.Create(url); | ||
var response = (System.Net.HttpWebResponse)req.GetResponse(); | ||
var receiveStream = response.GetResponseStream(); | ||
if (receiveStream != null) | ||
{ | ||
var readStream = new StreamReader(receiveStream, Encoding.UTF8); | ||
var s = readStream.ReadToEnd(); | ||
response.Close(); | ||
if (ValidateFile(s)) | ||
{ | ||
rv = s; | ||
} | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
rv = null; | ||
} | ||
return rv; | ||
} | ||
|
||
public static string LocalVersion() | ||
{ | ||
return Assembly.GetExecutingAssembly().GetName().Version.ToString(); | ||
} | ||
|
||
public static bool ValidateFile(string contents) | ||
{ | ||
if (string.IsNullOrEmpty(contents)) return false; | ||
const string pattern = @"^\d*\.\d*\.\d*\.\d*$"; | ||
var re = new System.Text.RegularExpressions.Regex(pattern); | ||
var val = re.IsMatch(contents); | ||
return val; | ||
} | ||
|
||
public static string CreateLocalVersionFile(string folderPath, string fileName, string version) | ||
{ | ||
if (!new DirectoryInfo(folderPath).Exists) | ||
{ | ||
Directory.CreateDirectory(folderPath); | ||
} | ||
|
||
var path = folderPath + "\\" + fileName; | ||
|
||
if (new FileInfo(path).Exists) | ||
{ | ||
new FileInfo(path).Delete(); | ||
} | ||
|
||
if (!new FileInfo(path).Exists) | ||
{ | ||
System.IO.File.WriteAllText(path, version); | ||
} | ||
return path; | ||
} | ||
|
||
public static string CreateTargetLocation(string downloadToPath, string version) | ||
{ | ||
if (!downloadToPath.EndsWith("\\")) // Give a trailing \ if there isn't one | ||
downloadToPath += "\\"; | ||
|
||
var filePath = Path.Combine(downloadToPath, version); | ||
|
||
var newFolder = new DirectoryInfo(filePath); | ||
newFolder.Create(); | ||
|
||
return filePath; | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.