Browse Source

Updated download path to temp folder and added admin rights to installer

flabbet 4 years ago
parent
commit
00d6ca49b1

+ 1 - 0
PixiEditor.UpdateInstaller/PixiEditor.UpdateInstaller.csproj

@@ -4,6 +4,7 @@
     <OutputType>WinExe</OutputType>
     <TargetFramework>netcoreapp3.1</TargetFramework>
     <UseWPF>true</UseWPF>
+    <ApplicationManifest>app.manifest</ApplicationManifest>
   </PropertyGroup>
 
   <ItemGroup>

+ 5 - 4
PixiEditor.UpdateInstaller/ViewModelMain.cs

@@ -1,4 +1,5 @@
-using System;
+using PixiEditor.UpdateModule;
+using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
@@ -43,11 +44,11 @@ namespace PixiEditor.UpdateInstaller
 
         public void InstallUpdate()
         {
-            string[] files = Directory.GetFiles(UpdateDirectory, "update-*.zip");
+            string[] files = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip");
 
             if (files.Length > 0)
             {
-                Installer = new UpdateModule.UpdateInstaller(files[0]);
+                Installer = new UpdateModule.UpdateInstaller(files[0], UpdateDirectory);
                 Installer.ProgressChanged += Installer_ProgressChanged;
                 Installer.Install();
             }
@@ -57,7 +58,7 @@ namespace PixiEditor.UpdateInstaller
             }
         }
 
-        private void Installer_ProgressChanged(object sender, UpdateModule.UpdateProgressChangedEventArgs e)
+        private void Installer_ProgressChanged(object sender, UpdateProgressChangedEventArgs e)
         {
             ProgressValue = e.Progress;
         }

+ 76 - 0
PixiEditor.UpdateInstaller/app.manifest

@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
+  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
+    <security>
+      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
+        <!-- UAC Manifest Options
+             If you want to change the Windows User Account Control level replace the 
+             requestedExecutionLevel node with one of the following.
+
+        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
+        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
+        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
+
+            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
+            Remove this element if your application requires this virtualization for backwards
+            compatibility.
+        -->
+        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
+      </requestedPrivileges>
+    </security>
+  </trustInfo>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- A list of the Windows versions that this application has been tested on
+           and is designed to work with. Uncomment the appropriate elements
+           and Windows will automatically select the most compatible environment. -->
+
+      <!-- Windows Vista -->
+      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
+
+      <!-- Windows 7 -->
+      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
+
+      <!-- Windows 8 -->
+      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
+
+      <!-- Windows 8.1 -->
+      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
+
+      <!-- Windows 10 -->
+      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
+
+    </application>
+  </compatibility>
+
+  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
+       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
+       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
+       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
+  <!--
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
+    </windowsSettings>
+  </application>
+  -->
+
+  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
+  <!--
+  <dependency>
+    <dependentAssembly>
+      <assemblyIdentity
+          type="win32"
+          name="Microsoft.Windows.Common-Controls"
+          version="6.0.0.0"
+          processorArchitecture="*"
+          publicKeyToken="6595b64144ccf1df"
+          language="*"
+        />
+    </dependentAssembly>
+  </dependency>
+  -->
+
+</assembly>

+ 10 - 2
PixiEditor.UpdateModule/UpdateDownloader.cs

@@ -3,14 +3,13 @@ using System.IO;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
-using System.Text;
 using System.Threading.Tasks;
 
 namespace PixiEditor.UpdateModule
 {
     public static class UpdateDownloader
     {
-        public static string DownloadLocation = AppDomain.CurrentDomain.BaseDirectory;
+        public static string DownloadLocation = Path.Join(Path.GetTempPath(), "PixiEditor");
         public static async Task DownloadReleaseZip(ReleaseInfo release)
         {
             Asset matchingAsset = GetMatchingAsset(release);
@@ -23,11 +22,20 @@ namespace PixiEditor.UpdateModule
                 if (response.StatusCode == HttpStatusCode.OK)
                 {
                     byte[] bytes = await response.Content.ReadAsByteArrayAsync();
+                    CreateTempDirectory();
                     File.WriteAllBytes(Path.Join(DownloadLocation, $"update-{release.TagName}.zip"), bytes);
                 }
             }
         }
 
+        public static void CreateTempDirectory()
+        {
+            if (!Directory.Exists(DownloadLocation))
+            {
+                Directory.CreateDirectory(DownloadLocation);
+            }
+        }
+
         private static Asset GetMatchingAsset(ReleaseInfo release)
         {
             string arch = IntPtr.Size == 8 ? "x64" : "x86";

+ 9 - 5
PixiEditor.UpdateModule/UpdateInstaller.cs

@@ -8,6 +8,7 @@ namespace PixiEditor.UpdateModule
     public class UpdateInstaller
     {
         public const string TargetDirectoryName = "UpdateFiles";
+        public static string UpdateFilesPath = Path.Join(UpdateDownloader.DownloadLocation, TargetDirectoryName);
 
         public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
         private float _progress = 0;
@@ -21,22 +22,24 @@ namespace PixiEditor.UpdateModule
             }
         }
         public string ArchiveFileName { get; set; }
+        public string TargetDirectory { get; set; }
 
-        public UpdateInstaller(string archiveFileName)
+        public UpdateInstaller(string archiveFileName, string targetDirectory)
         {
             ArchiveFileName = archiveFileName;
+            TargetDirectory = targetDirectory;
         }
 
         public void Install()
         {
             var processes = Process.GetProcessesByName("PixiEditor");
-            if(processes.Length > 0)
+            if (processes.Length > 0)
             {
                 processes[0].WaitForExit();
             }
-            ZipFile.ExtractToDirectory(ArchiveFileName, TargetDirectoryName, true);
+            ZipFile.ExtractToDirectory(ArchiveFileName, UpdateFilesPath, true);
             Progress = 25; //25% for unzip
-            string dirWithFiles = Directory.GetDirectories(TargetDirectoryName)[0];
+            string dirWithFiles = Directory.GetDirectories(UpdateFilesPath)[0];
             string[] files = Directory.GetFiles(dirWithFiles);
             CopyFilesToDestination(files);
             DeleteArchive();
@@ -46,12 +49,13 @@ namespace PixiEditor.UpdateModule
         private void DeleteArchive()
         {
             File.Delete(ArchiveFileName);
+            Directory.Delete(UpdateFilesPath, true);
         }
 
         private void CopyFilesToDestination(string[] files)
         {
             float fileCopiedVal = 74f / files.Length; //74% is reserved for copying
-            string destinationDir = Path.GetDirectoryName(ArchiveFileName);
+            string destinationDir = TargetDirectory;
             foreach (string file in files)
             {
                 string targetFileName = Path.GetFileName(file);

+ 25 - 0
PixiEditor/Models/Processes/ProcessHelper.cs

@@ -0,0 +1,25 @@
+using System.ComponentModel;
+using System.Diagnostics;
+
+namespace PixiEditor.Models.Processes
+{
+    public static class ProcessHelper
+    {
+        public static Process RunAsAdmin(string path)
+        {
+            Process proc = new Process();
+            try
+            {
+                proc.StartInfo.FileName = path;
+                proc.StartInfo.Verb = "runas";
+                proc.StartInfo.UseShellExecute = true;
+                proc.Start();
+            }
+            catch (Win32Exception ex)
+            {
+                throw ex;
+            }
+            return proc;
+        }
+    }
+}

+ 2 - 2
PixiEditor/Properties/AssemblyInfo.cs

@@ -50,5 +50,5 @@ using System.Windows;
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
 
-[assembly: AssemblyVersion("0.1.3.1")]
-[assembly: AssemblyFileVersion("0.1.3.1")]
+[assembly: AssemblyVersion("0.1.3.0")]
+[assembly: AssemblyFileVersion("0.1.3.0")]

+ 4 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -21,6 +21,7 @@ using PixiEditor.Models.Enums;
 using PixiEditor.Models.Events;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Position;
+using PixiEditor.Models.Processes;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.UpdateModule;
@@ -329,7 +330,7 @@ namespace PixiEditor.ViewModels
 
         private void RestartApplication(object parameter)
         {
-            Process.Start(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
+            ProcessHelper.RunAsAdmin(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
             Application.Current.Shutdown();
         }
 
@@ -338,7 +339,8 @@ namespace PixiEditor.ViewModels
             return await Task.Run(async () =>
             {
                 bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
-                bool updateFileDoesNotExists = !File.Exists($"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip");
+                bool updateFileDoesNotExists = !File.Exists(
+                    Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip"));
                 if (updateAvailable && updateFileDoesNotExists)
                 {
                     VersionText = "Downloading update...";

+ 16 - 4
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,9 +1,12 @@
 using System;
+using System.ComponentModel;
 using System.Diagnostics;
 using System.IO;
 using System.Reflection;
 using System.Windows;
 using System.Windows.Input;
+using PixiEditor.Models.Processes;
+using PixiEditor.UpdateModule;
 using PixiEditor.ViewModels;
 
 namespace PixiEditor
@@ -69,12 +72,21 @@ namespace PixiEditor
         private void mainWindow_Initialized(object sender, EventArgs e)
         {
             string dir = AppDomain.CurrentDomain.BaseDirectory;
-            bool updateFileExists = Directory.GetFiles(dir, "update-*.zip").Length > 0;
+            UpdateDownloader.CreateTempDirectory();
+            bool updateFileExists = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip").Length > 0;
             string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
             if (updateFileExists && File.Exists(updaterPath))
-            {                
-                Process.Start(updaterPath);
-                Close();
+            {
+                try
+                {
+                    ProcessHelper.RunAsAdmin(updaterPath);
+                    Close();
+                }
+                catch(Win32Exception)
+                {
+                    MessageBox.Show("Couldn't update without administrator rights.", "Insufficient permissions", 
+                        MessageBoxButton.OK, MessageBoxImage.Error);
+                }
             }
         }
     }