From 3cb08134675cc5149b7e4802a593df5d63c727ee Mon Sep 17 00:00:00 2001 From: gobbo1008 Date: Fri, 22 Dec 2017 14:34:20 +0100 Subject: [PATCH] Auto-Install Implemented an update check and installation of ResearchDump by hooking into the update check and expanding the downloader routine. --- .../ESOResearchNotifier.csproj | 2 + ESOResearchNotifier/Form1.cs | 115 +++++++++++++++--- .../Properties/AssemblyInfo.cs | 4 +- Updater/Form1.cs | 10 +- 4 files changed, 104 insertions(+), 27 deletions(-) diff --git a/ESOResearchNotifier/ESOResearchNotifier.csproj b/ESOResearchNotifier/ESOResearchNotifier.csproj index 968b2a5..1068cfe 100644 --- a/ESOResearchNotifier/ESOResearchNotifier.csproj +++ b/ESOResearchNotifier/ESOResearchNotifier.csproj @@ -43,6 +43,8 @@ + + diff --git a/ESOResearchNotifier/Form1.cs b/ESOResearchNotifier/Form1.cs index 03368c0..3f93ff7 100644 --- a/ESOResearchNotifier/Form1.cs +++ b/ESOResearchNotifier/Form1.cs @@ -1,15 +1,16 @@ -using System; +using Lua; +using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; +using System.IO.Compression; +using System.Net; using System.Net.Http; using System.Runtime.InteropServices; using System.Windows.Forms; using ToastNotifications; -using Lua; using static System.Windows.Forms.Control; -using System.Net; -using Newtonsoft.Json; -using System.ComponentModel; namespace ESOResearchNotifier { @@ -23,9 +24,13 @@ private enum NotificationType { Balloon, Toast }; private List SelectedCharacters = new List(); private string ConfigPath = Application.StartupPath + "\\ESOResearchNotifier.xml"; private GitHubRelease LatestRelease = new GitHubRelease(); - private string LatestReleaseDownload; + private GitHubRelease LatestReleaseAddon = new GitHubRelease(); + private Queue> Downloads = new Queue>(); private bool UpdateReady = false; private bool UpdateAvailable = false; + private bool ESORNUpdate = false; + private bool AddonUpdate = false; + WebClient client = new WebClient(); public Form1() { @@ -111,6 +116,8 @@ private void CheckUpdate() client.DefaultRequestHeaders.Add("User-Agent", "ESOResearchNotifier"); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); client.Timeout = new TimeSpan(0, 0, 20); + + #region ESOResearchNotifier var sResult = client.GetStringAsync("/repos/gobbo1008/ESOResearchNotifier/releases/latest").Result.ToString(); GitHubRelease dResult = JsonConvert.DeserializeObject(sResult); LatestRelease = dResult; @@ -118,31 +125,107 @@ private void CheckUpdate() string versionString = LatestRelease.tag_name.Remove(0, 1); Version RemoteVersion = new Version(versionString); Version LocalVersion = GetType().Assembly.GetName().Version; - + if (LocalVersion.CompareTo(RemoteVersion) < 0) { GitHubDownload dDownload = JsonConvert.DeserializeObject(LatestRelease.assets[0].ToString()); - LatestReleaseDownload = dDownload.browser_download_url; + Downloads.Enqueue(new KeyValuePair(new Uri(dDownload.browser_download_url), Application.StartupPath + "\\ESOResearchNotifier.zip")); + ESORNUpdate = true; + UpdateAvailable = true; + } + #endregion + + #region ResearchDump + var sResultAddon = client.GetStringAsync("/repos/gobbo1008/ResearchDump/releases/latest").Result.ToString(); + GitHubRelease dResultAddon = JsonConvert.DeserializeObject(sResultAddon); + LatestReleaseAddon = dResultAddon; + + string versionStringAddon = LatestReleaseAddon.tag_name.Remove(0, 1); + Version RemoteVersionAddon = new Version(versionStringAddon); + Version LocalVersionAddon = new Version("0.0"); + + string addonLua = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Elder Scrolls Online\\live\\addons\\ResearchDump\\ResearchDump.lua").ToString(); + if (File.Exists(addonLua)) + { + TextReader reader = File.OpenText(addonLua); + string line; + while ((line = reader.ReadLine()) != null) + { + if (line.StartsWith("ResearchDump.version")) + { + string version = line.Substring(23); + LocalVersionAddon = new Version(version); + break; + } + } + reader.Close(); + } + + if (LocalVersionAddon.CompareTo(RemoteVersionAddon) < 0) + { + GitHubDownload dDownload = JsonConvert.DeserializeObject(LatestReleaseAddon.assets[0].ToString()); + Downloads.Enqueue(new KeyValuePair(new Uri(dDownload.browser_download_url), Application.StartupPath + "\\ResearchDump.zip")); + AddonUpdate = true; UpdateAvailable = true; } + #endregion } + private void DoUpdate() { - WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); - client.DownloadFileAsync(new Uri(LatestReleaseDownload), @"" + Application.StartupPath + "\\ESOResearchNotifier.zip"); + DoDownload(); + } + + private void DoDownload() + { + KeyValuePair DownloadItem = Downloads.Dequeue(); + client.DownloadFileAsync(DownloadItem.Key, DownloadItem.Value); } private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { - btnUpdate.Text = "Update ready!"; - btnUpdate.Visible = true; - btnUpdate.Enabled = true; - prgUpdate.Visible = false; - prgUpdate.Enabled = false; - UpdateReady = true; + if (Downloads.Count > 0) + { + DoDownload(); + } + else + { + if (AddonUpdate) + { + if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Elder Scrolls Online\\live\\addons\\ResearchDump\\"))) + { + Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Elder Scrolls Online\\live\\addons\\ResearchDump\\")); + } + + ZipArchive Archive = ZipFile.OpenRead(Application.StartupPath + "\\ResearchDump.zip"); + foreach (ZipArchiveEntry Entry in Archive.Entries) + { + if ((Entry.Name.Contains(".")) && (Entry.Name != "README.md")) + { + Entry.ExtractToFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Elder Scrolls Online\\live\\addons\\ResearchDump\\") + Entry.FullName.Substring(13).Replace("/", "\\"), true); + } + } + Archive.Dispose(); + } + if (ESORNUpdate) + { + btnUpdate.Text = "Update ready!"; + btnUpdate.Visible = true; + btnUpdate.Enabled = true; + prgUpdate.Visible = false; + prgUpdate.Enabled = false; + UpdateReady = true; + } else + { + btnUpdate.Visible = false; + btnUpdate.Enabled = false; + prgUpdate.Visible = false; + prgUpdate.Enabled = false; + } + } } private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) diff --git a/ESOResearchNotifier/Properties/AssemblyInfo.cs b/ESOResearchNotifier/Properties/AssemblyInfo.cs index 28d0138..92e2e12 100644 --- a/ESOResearchNotifier/Properties/AssemblyInfo.cs +++ b/ESOResearchNotifier/Properties/AssemblyInfo.cs @@ -32,5 +32,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.5")] -[assembly: AssemblyFileVersion("1.5")] +[assembly: AssemblyVersion("1.6")] +[assembly: AssemblyFileVersion("1.6")] diff --git a/Updater/Form1.cs b/Updater/Form1.cs index 77cecd0..b22c097 100644 --- a/Updater/Form1.cs +++ b/Updater/Form1.cs @@ -1,15 +1,7 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; +using System.ComponentModel; using System.Diagnostics; -using System.Drawing; -using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Tasks; using System.Windows.Forms; -using System.IO; using System.IO.Compression; namespace Updater