Skip to content

Commit

Permalink
autoupdater fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
foglio1024 committed Apr 26, 2017
1 parent cae18e3 commit 257bc3f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
5 changes: 4 additions & 1 deletion TCC.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ private void OnStartup(object sender, StartupEventArgs ev)
var cd = AppDomain.CurrentDomain;
cd.UnhandledException += GlobalUnhandledExceptionHandler;
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High;

if (File.Exists(Environment.CurrentDirectory + "/TCCupdater.exe"))
{
File.Delete(Environment.CurrentDirectory + "/TCCupdater.exe");
}
UpdateManager.CheckAppVersion();
UpdateManager.CheckDatabaseVersion();

Expand Down
84 changes: 52 additions & 32 deletions TCC.UI/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ public static void CheckDatabaseVersion()
{
using (WebClient c = new WebClient())
{
c.DownloadFile(databaseVersion, "newDbVer");
}
int v = 0;
try
{
v = Convert.ToInt32(File.ReadAllText("resources/images/current_version"));
}
catch (Exception)
{
v = 0;
}
int newVer = Convert.ToInt32(File.ReadAllText("newDbVer"));
try
{
var st = c.OpenRead(databaseVersion);
StreamReader sr = new StreamReader(st);

if(v < newVer)
{
//update
if(MessageBox.Show(String.Format("Updated icons database available (v{0}). Download now?", newVer), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
int newVersion = Convert.ToInt32(sr.ReadLine());
int currentVersion = 0;
if (File.Exists("resources/images/current_version"))
{
currentVersion = Convert.ToInt32(File.OpenText("resources/images/current_version").ReadLine());
}

if (newVersion > currentVersion)
{
if (MessageBox.Show(String.Format("Icons database v{0} available. Download now?", newVersion), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
DownloadDatabase();
ExtractDatabase();
}
}
}
catch (Exception)
{
DownloadDatabase();
ExtractDatabase();
MessageBox.Show("Error while checking database updates.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
Expand Down Expand Up @@ -99,19 +103,26 @@ public static void CheckAppVersion()
{
using (WebClient c = new WebClient())
{
var st = c.OpenRead(appVersion);
StreamReader sr = new StreamReader(st);
string newVersionInfo = sr.ReadLine();
string newVersionUrl = sr.ReadLine();

var v = Version.Parse(newVersionInfo);
if(v > Assembly.GetExecutingAssembly().GetName().Version)
try
{
if (MessageBox.Show(String.Format("TCC v{0} available. Download now?", newVersionInfo), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
var st = c.OpenRead(appVersion);
StreamReader sr = new StreamReader(st);
string newVersionInfo = sr.ReadLine();
string newVersionUrl = sr.ReadLine();

var v = Version.Parse(newVersionInfo);
if (v > Assembly.GetExecutingAssembly().GetName().Version)
{
Update(newVersionUrl);
if (MessageBox.Show(String.Format("TCC v{0} available. Download now?", newVersionInfo), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
Update(newVersionUrl);
}
}
}
catch (Exception)
{
MessageBox.Show("Error while checking updates.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

Expand All @@ -120,11 +131,20 @@ private static void Update(string url)
{
using (WebClient c = new WebClient())
{
c.DownloadFile(url, "update.zip");
ZipFile.ExtractToDirectory("update.zip", Environment.CurrentDirectory + "/tmp");
File.Move(Environment.CurrentDirectory + "/tmp/TCCupdater.exe", Environment.CurrentDirectory + "/TCCupdater.exe");
Process.Start(Environment.CurrentDirectory + "/TCCupdater.exe");
Environment.Exit(0);
try
{
c.DownloadFile(url, "update.zip");
ZipFile.ExtractToDirectory("update.zip", Environment.CurrentDirectory + "/tmp");
File.Move(Environment.CurrentDirectory + "/tmp/TCCupdater.exe", Environment.CurrentDirectory + "/TCCupdater.exe");
Process.Start(Environment.CurrentDirectory + "/TCCupdater.exe");
Environment.Exit(0);
}
catch (Exception)
{

MessageBox.Show("Couldn't download update.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error);

}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion TCC.Updater/TCC.Updater/TCC.Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>..\..\TCC.UI\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand Down

0 comments on commit 257bc3f

Please sign in to comment.