Skip to content

Commit

Permalink
Added auto update.
Browse files Browse the repository at this point in the history
  • Loading branch information
arise-project committed Apr 19, 2015
1 parent 5684025 commit 033f812
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 10 deletions.
2 changes: 2 additions & 0 deletions LadderLogic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
<Compile Include="CTool\FunctionRule\PulseTimerRule.cs" />
<Compile Include="CTool\FunctionRule\SetCoilRule.cs" />
<Compile Include="CTool\FunctionRule\ResetCoilRule.cs" />
<Compile Include="Updater\UpdateHelper.cs" />
<Compile Include="Updater\Versions.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
23 changes: 14 additions & 9 deletions Presentation/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using LadderLogic.Updater;

namespace LadderLogic.Presentation
{
Expand Down Expand Up @@ -353,18 +354,17 @@ void InitializeComponents()

var surfaces = AppController.Instance.GetPalette ();

uint count = (uint)surfaces.Sum (s => s.Segments.Count);
table1 = new Gtk.Table (2, count / 2, true);
var count = (uint)surfaces.Sum (s => s.Segments.Count);
table1 = new Table (2, count / 2, true);

int index = 0;
var index = 0;
for (uint i = 0; i < count / 2; i++) {
for (uint j = 0; j < 2; j++) {
if (surfaces.Count () > index) {
var surf = surfaces [index++];
var el = new ElementDrawing(surf, 2, 2 );
table1.Attach (el, i, i+1, j, j+(uint)surf.Segments.Count());
el.Show ();
}
if (surfaces.Count() <= index) continue;
var surf = surfaces [index++];
var el = new ElementDrawing(surf, 2, 2 );
table1.Attach (el, i, i+1, j, j+(uint)surf.Segments.Count());
el.Show ();
}
}

Expand All @@ -385,7 +385,12 @@ void InitializeComponents()
tblArdulino.Attach (code, 0, 1, 0, 1);
code.CreateCode += CController.Instance.CreateCode;
code.Show ();
this.Shown += AppWindow_Shown;
}

void AppWindow_Shown(object sender, EventArgs e)
{
UpdateHelper.CompareVersions(this);
}


Expand Down
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Configuration;
using GLib;
using Gtk;
using LadderLogic.Updater;

namespace LadderLogic
{
Expand All @@ -24,7 +25,7 @@ public static void Main (string[] args)
ExceptionManager.UnhandledException +=
e => new UnhandledExceptionDialog(_main, e.ExceptionObject as Exception).ShowDialog();


var isElementsView = ConfigurationManager
.AppSettings ["ElementsView"]
.ToLowerInvariant () == "true";
Expand Down
80 changes: 80 additions & 0 deletions Updater/UpdateHelper.cs
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);
}
}
}
86 changes: 86 additions & 0 deletions Updater/Versions.cs
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 modified bin/Debug/LadderLogic.exe
Binary file not shown.
Binary file modified bin/Debug/LadderLogic.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions obj/x86/Debug/LadderLogic.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,4 @@ G:\Disk_H\ArduinoLadder\bin\Debug\Element\ResetCoil.xml
G:\Disk_H\ArduinoLadder\bin\Debug\ladder_icon.png
G:\Disk_H\ArduinoLadder\bin\Debug\LadderLogic.exe
G:\Disk_H\ArduinoLadder\bin\Debug\LadderLogic.pdb
G:\Disk_H\ArduinoLadder\obj\x86\Debug\LadderLogic.csprojResolveAssemblyReference.cache
Binary file modified obj/x86/Debug/LadderLogic.exe
Binary file not shown.
Binary file modified obj/x86/Debug/LadderLogic.pdb
Binary file not shown.

0 comments on commit 033f812

Please sign in to comment.