Browse Source

Added downloading release asset

flabbet 4 years ago
parent
commit
61fa8ca612

+ 17 - 0
PixiEditor.UpdateModule/Asset.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.Json.Serialization;
+
+namespace PixiEditor.UpdateModule
+{
+    public class Asset
+    {
+        [JsonPropertyName("url")]
+        public string Url { get; set; }
+        [JsonPropertyName("name")]
+        public string Name { get; set; }
+        [JsonPropertyName("content_type")]
+        public string ContentType { get; set; }
+    }
+}

+ 2 - 0
PixiEditor.UpdateModule/ReleaseInfo.cs

@@ -13,6 +13,8 @@ namespace PixiEditor.UpdateModule
         public bool IsDraft { get; set; }
         public bool IsDraft { get; set; }
         [JsonPropertyName("prerelease")]
         [JsonPropertyName("prerelease")]
         public bool IsPrerelease { get; set; }
         public bool IsPrerelease { get; set; }
+        [JsonPropertyName("assets")]
+        public Asset[] Assets { get; set; }
         public bool WasDataFetchSuccessfull { get; set; } = true;
         public bool WasDataFetchSuccessfull { get; set; } = true;
 
 
         public ReleaseInfo() { }
         public ReleaseInfo() { }

+ 5 - 2
PixiEditor.UpdateModule/UpdateChecker.cs

@@ -7,9 +7,11 @@ namespace PixiEditor.UpdateModule
 {
 {
     public class UpdateChecker
     public class UpdateChecker
     {
     {
-        public string ReleaseApiUrl = "https://api.github.com/repos/PixiEditor/PixiEditor/releases/latest";
+        public const string ReleaseApiUrl = "https://api.github.com/repos/PixiEditor/PixiEditor/releases/latest";
         public string CurrentVersionTag { get; set; }
         public string CurrentVersionTag { get; set; }
 
 
+        public ReleaseInfo LatestReleaseInfo { get; set; }
+
         public UpdateChecker(string currentVersionTag)
         public UpdateChecker(string currentVersionTag)
         {
         {
             CurrentVersionTag = currentVersionTag;
             CurrentVersionTag = currentVersionTag;
@@ -17,7 +19,8 @@ namespace PixiEditor.UpdateModule
 
 
         public async Task<bool> CheckUpdateAvailable()
         public async Task<bool> CheckUpdateAvailable()
         {
         {
-            return CheckUpdateAvailable(await GetLatestReleaseInfo());
+            LatestReleaseInfo = await GetLatestReleaseInfo();
+            return CheckUpdateAvailable(LatestReleaseInfo);
         }
         }
 
 
         public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
         public bool CheckUpdateAvailable(ReleaseInfo latestRelease)

+ 29 - 2
PixiEditor.UpdateModule/UpdateDownloader.cs

@@ -1,11 +1,38 @@
 using System;
 using System;
-using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
 using System.Text;
 using System.Text;
+using System.Threading.Tasks;
 
 
 namespace PixiEditor.UpdateModule
 namespace PixiEditor.UpdateModule
 {
 {
-    public class UpdateDownloader
+    public static class UpdateDownloader
     {
     {
+        public static string DownloadLocation = AppDomain.CurrentDomain.BaseDirectory;
+        public static async Task DownloadReleaseZip(ReleaseInfo release)
+        {
+            Asset matchingAsset = GetMatchingAsset(release);
 
 
+            using (HttpClient client = new HttpClient())
+            {
+                client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
+                client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");
+                var response = await client.GetAsync(matchingAsset.Url);
+                if (response.StatusCode == HttpStatusCode.OK)
+                {
+                    byte[] bytes = await response.Content.ReadAsByteArrayAsync();
+                    File.WriteAllBytes(Path.Join(DownloadLocation, "update.zip"), bytes);
+                }
+            }
+        }
+
+        private static Asset GetMatchingAsset(ReleaseInfo release)
+        {
+            string arch = IntPtr.Size == 8 ? "x64" : "x32";
+            return release.Assets.First(x => x.ContentType == "application/x-zip-compressed"
+            && x.Name.Contains(arch));
+        }
     }
     }
 }
 }

+ 3 - 0
PixiEditor/PixiEditor.csproj

@@ -78,6 +78,9 @@
       <PackagePath></PackagePath>
       <PackagePath></PackagePath>
     </None>
     </None>
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj" />
+  </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Compile Update="Properties\Settings.Designer.cs">
     <Compile Update="Properties\Settings.Designer.cs">
       <DesignTimeSharedInput>True</DesignTimeSharedInput>
       <DesignTimeSharedInput>True</DesignTimeSharedInput>

+ 20 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -5,6 +5,8 @@ using System.ComponentModel;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.IO;
 using System.IO;
 using System.Linq;
 using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
@@ -21,6 +23,7 @@ using PixiEditor.Models.IO;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.Tools.Tools;
+using PixiEditor.UpdateModule;
 
 
 namespace PixiEditor.ViewModels
 namespace PixiEditor.ViewModels
 {
 {
@@ -197,6 +200,8 @@ namespace PixiEditor.ViewModels
         private bool _restoreToolOnKeyUp = false;
         private bool _restoreToolOnKeyUp = false;
         private Tool _lastActionTool;
         private Tool _lastActionTool;
 
 
+        public UpdateChecker UpdateChecker { get; set; }
+
         public ViewModelMain()
         public ViewModelMain()
         {
         {
             BitmapManager = new BitmapManager();
             BitmapManager = new BitmapManager();
@@ -294,6 +299,21 @@ namespace PixiEditor.ViewModels
             BitmapManager.PrimaryColor = PrimaryColor;
             BitmapManager.PrimaryColor = PrimaryColor;
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             Current = this;
             Current = this;
+            InitUpdateChecker();
+            Task.Run(async () => {
+                bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
+                if (updateAvailable)
+                {
+                    UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
+                }
+            });
+        }
+
+        private void InitUpdateChecker()
+        {
+            var assembly = Assembly.GetExecutingAssembly();
+            FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
+            UpdateChecker = new UpdateChecker(info.FileVersion);
         }
         }
 
 
         private void ZoomViewport(object parameter)
         private void ZoomViewport(object parameter)