Browse Source

Installer works

flabbet 4 years ago
parent
commit
994cef6a1a

+ 9 - 0
PixiEditor.UpdateInstaller/App.xaml

@@ -0,0 +1,9 @@
+<Application x:Class="PixiEditor.UpdateInstaller.App"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:local="clr-namespace:PixiEditor.UpdateInstaller"
+             StartupUri="MainWindow.xaml">
+    <Application.Resources>
+         
+    </Application.Resources>
+</Application>

+ 17 - 0
PixiEditor.UpdateInstaller/App.xaml.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Data;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PixiEditor.UpdateInstaller
+{
+    /// <summary>
+    /// Interaction logic for App.xaml
+    /// </summary>
+    public partial class App : Application
+    {
+    }
+}

+ 10 - 0
PixiEditor.UpdateInstaller/AssemblyInfo.cs

@@ -0,0 +1,10 @@
+using System.Windows;
+
+[assembly: ThemeInfo(
+    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
+                                     //(used if a resource is not found in the page,
+                                     // or application resource dictionaries)
+    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
+                                              //(used if a resource is not found in the page,
+                                              // app, or any theme specific resource dictionaries)
+)]

BIN
PixiEditor.UpdateInstaller/Images/PixiEditorLogo.png


+ 25 - 0
PixiEditor.UpdateInstaller/MainWindow.xaml

@@ -0,0 +1,25 @@
+<Window x:Class="PixiEditor.UpdateInstaller.MainWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:PixiEditor.UpdateInstaller"
+        mc:Ignorable="d"
+        Title="MainWindow" Height="350" Width="250" Background="#2D2D30" ResizeMode="NoResize"
+        WindowStyle="None" WindowStartupLocation="CenterScreen">
+    <WindowChrome.WindowChrome>
+        <WindowChrome ResizeBorderThickness="6"
+            CaptionHeight="30"/>
+    </WindowChrome.WindowChrome>
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="130"/>
+            <RowDefinition/>
+        </Grid.RowDefinitions>
+        <Image Source="Images/PixiEditorLogo.png" Width="75" Height="75" Grid.Row="0"/>
+        <StackPanel Grid.Row="1" Margin="0,20,0,0">
+            <Label Content="Installing update" HorizontalAlignment="Center" Foreground="White" FontSize="20"/>
+            <ProgressBar Margin="0,20,0,0" Height="20" Width="200" Value="{Binding ProgressValue}"/>
+        </StackPanel>
+    </Grid>
+</Window>

+ 17 - 0
PixiEditor.UpdateInstaller/MainWindow.xaml.cs

@@ -0,0 +1,17 @@
+using System.Reflection;
+using System.Windows;
+
+namespace PixiEditor.UpdateInstaller
+{
+    /// <summary>
+    /// Interaction logic for MainWindow.xaml
+    /// </summary>
+    public partial class MainWindow : Window
+    {
+        public MainWindow()
+        {
+            InitializeComponent();
+            DataContext = new ViewModelMain(Close);
+        }
+    }
+}

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

@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
+
+  <PropertyGroup>
+    <OutputType>WinExe</OutputType>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <UseWPF>true</UseWPF>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <None Remove="Images\PixiEditorLogo.png" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <Resource Include="Images\PixiEditorLogo.png" />
+  </ItemGroup>
+</Project>

+ 17 - 0
PixiEditor.UpdateInstaller/ViewModelBase.cs

@@ -0,0 +1,17 @@
+using System.ComponentModel;
+using System.Linq;
+using System.Windows;
+using System.Windows.Input;
+
+namespace PixiEditor.UpdateInstaller
+{
+    public class ViewModelBase : INotifyPropertyChanged
+    {
+        public event PropertyChangedEventHandler PropertyChanged = delegate { };
+
+        protected void RaisePropertyChanged(string property)
+        {
+            if (property != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
+        }
+    }
+}

+ 62 - 0
PixiEditor.UpdateInstaller/ViewModelMain.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+
+namespace PixiEditor.UpdateInstaller
+{
+    public class ViewModelMain : ViewModelBase
+    {
+        public ViewModelMain Current { get; private set; }
+        public UpdateModule.UpdateInstaller Installer { get; set; }
+
+        private float _progressValue;
+
+        public float ProgressValue
+        {
+            get => _progressValue;
+            set 
+            { 
+                _progressValue = value;
+                RaisePropertyChanged(nameof(ProgressValue));
+            }
+        }
+
+        public ViewModelMain(Action closeAction)
+        {
+            Current = this;
+
+            string updateDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
+
+#if DEBUG
+            updateDirectory = Environment.GetCommandLineArgs()[1];
+#endif
+
+            string[] files = Directory.GetFiles(updateDirectory, "update-*.zip");
+
+            if (files.Length > 0)
+            {
+                Installer = new UpdateModule.UpdateInstaller(files[0]);
+                Installer.ProgressChanged += Installer_ProgressChanged;
+                Installer.Install();
+            }
+
+            string pixiEditorExecutablePath = Directory.GetFiles(updateDirectory, "PixiEditor.exe")[0];
+            StartPixiEditor(pixiEditorExecutablePath);
+            closeAction();
+        }
+
+        private void StartPixiEditor(string executablePath)
+        {
+            Process process = Process.Start(executablePath);
+        }
+
+        private void Installer_ProgressChanged(object sender, UpdateModule.UpdateProgressChangedEventArgs e)
+        {
+            ProgressValue = e.Progress;
+        }
+    }
+}

+ 1 - 1
PixiEditor.UpdateModule/UpdateDownloader.cs

@@ -23,7 +23,7 @@ namespace PixiEditor.UpdateModule
                 if (response.StatusCode == HttpStatusCode.OK)
                 {
                     byte[] bytes = await response.Content.ReadAsByteArrayAsync();
-                    File.WriteAllBytes(Path.Join(DownloadLocation, "update.zip"), bytes);
+                    File.WriteAllBytes(Path.Join(DownloadLocation, $"update-{release.TagName}.zip"), bytes);
                 }
             }
         }

+ 57 - 0
PixiEditor.UpdateModule/UpdateInstaller.cs

@@ -0,0 +1,57 @@
+using System;
+using System.IO;
+using System.IO.Compression;
+
+namespace PixiEditor.UpdateModule
+{
+    public class UpdateInstaller
+    {
+        public const string TargetDirectoryName = "UpdateFiles";
+
+        public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
+        private float _progress = 0;
+        public float Progress 
+        {
+            get => _progress;
+            set
+            {
+                _progress = value;
+                ProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(value));
+            }
+        }
+        public string ArchiveFileName { get; set; }
+
+        public UpdateInstaller(string archiveFileName)
+        {
+            ArchiveFileName = archiveFileName;
+        }
+
+        public void Install()
+        {
+            ZipFile.ExtractToDirectory(ArchiveFileName, TargetDirectoryName, true);
+            Progress = 25; //25% for unzip
+            string dirWithFiles = Directory.GetDirectories(TargetDirectoryName)[0];
+            string[] files = Directory.GetFiles(dirWithFiles);
+            CopyFilesToDestination(files);
+            DeleteArchive();
+            Progress = 100;
+        }
+
+        private void DeleteArchive()
+        {
+            File.Delete(ArchiveFileName);
+        }
+
+        private void CopyFilesToDestination(string[] files)
+        {
+            float fileCopiedVal = 74f / files.Length; //74% is reserved for copying
+            string destinationDir = Path.GetDirectoryName(ArchiveFileName);
+            foreach (string file in files)
+            {
+                string targetFileName = Path.GetFileName(file);
+                File.Copy(file, Path.Join(destinationDir, targetFileName), true);
+                Progress += fileCopiedVal;
+            }
+        }
+    }
+}

+ 14 - 0
PixiEditor.UpdateModule/UpdateProgressChangedEventArgs.cs

@@ -0,0 +1,14 @@
+using System;
+
+namespace PixiEditor.UpdateModule
+{
+    public class UpdateProgressChangedEventArgs : EventArgs
+    {
+        public float Progress { get; set; }
+
+        public UpdateProgressChangedEventArgs(float progress)
+        {
+            Progress = progress;
+        }
+    }
+}

+ 0 - 0
PixiEditor/Helpers/NotifyableObject.cs → PixiEditor/NotifyableObject.cs


+ 1 - 3
PixiEditor/PixiEditor.csproj

@@ -64,9 +64,7 @@
     <Resource Include="Images\MoveImage.png" />
     <Resource Include="Images\PenImage.png" />
     <Resource Include="Images\ColorPickerImage.png" />
-    <Resource Include="Images\PixiEditorLogo.png">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Resource>
+    <Resource Include="Images\PixiEditorLogo.png" />
     <Resource Include="Images\RectangleImage.png" />
     <Resource Include="Images\SelectImage.png" />
     <Resource Include="Images\transparentbg.png" />

+ 7 - 1
PixiEditor/PixiEditor.sln

@@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditor", "PixiEditor.cs
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditorTests", "..\PixiEditorTests\PixiEditorTests.csproj", "{D61922EA-3BF3-4AFA-8930-3A8B30A9A195}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PixiEditor.UpdateModule", "..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj", "{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditor.UpdateModule", "..\PixiEditor.UpdateModule\PixiEditor.UpdateModule.csproj", "{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PixiEditor.UpdateInstaller", "..\PixiEditor.UpdateInstaller\PixiEditor.UpdateInstaller.csproj", "{BE4F478D-E903-4011-B24B-F6AB96D66514}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
 		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9A0AB1E7-435C-4567-9EAC-FF102A9F1B01}.Release|Any CPU.Build.0 = Release|Any CPU
+		{BE4F478D-E903-4011-B24B-F6AB96D66514}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{BE4F478D-E903-4011-B24B-F6AB96D66514}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{BE4F478D-E903-4011-B24B-F6AB96D66514}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{BE4F478D-E903-4011-B24B-F6AB96D66514}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 37 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -110,6 +110,19 @@ namespace PixiEditor.ViewModels
             }
         }
 
+        private string _versionText;
+
+        public string VersionText
+        {
+            get => _versionText;
+            set
+            {
+                _versionText = value;
+                RaisePropertyChanged(nameof(VersionText));
+            }
+        }
+
+
         public bool RecenterZoombox
         {
             get => _recenterZoombox;
@@ -300,13 +313,34 @@ namespace PixiEditor.ViewModels
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             Current = this;
             InitUpdateChecker();
+            CheckForUpdate();
+        }
+
+        private void CheckForUpdate()
+        {
+            bool close = false;
             Task.Run(async () => {
                 bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
-                if (updateAvailable)
+                bool updateFileDoesNotExists = !File.Exists($"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip");
+                if (updateAvailable && updateFileDoesNotExists)
+                {
+                    VersionText = "Downloading update...";
+                    await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
+                    VersionText = "Restart to install update";
+                }
+                else if (!updateFileDoesNotExists) 
                 {
-                    UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
+                    string path = 
+                        Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"PixiEditor.UpdateInstaller.exe");
+                    Process.Start(path);
+                    close = true;
                 }
             });
+            if (close)
+            {
+                CloseAction();
+
+            }
         }
 
         private void InitUpdateChecker()
@@ -314,6 +348,7 @@ namespace PixiEditor.ViewModels
             var assembly = Assembly.GetExecutingAssembly();
             FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
             UpdateChecker = new UpdateChecker(info.FileVersion);
+            VersionText = $"Version {info.FileVersion}";
         }
 
         private void ZoomViewport(object parameter)

+ 3 - 0
PixiEditor/Views/MainWindow.xaml

@@ -391,5 +391,8 @@
                 <TextBlock Margin="4,0,10,0" Text="{Binding MouseYOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
             </StackPanel>
         </DockPanel>
+        <TextBlock Padding="10" HorizontalAlignment="Right"
+                   Foreground="White" FontSize="16" VerticalAlignment="Center" Grid.Row="3"
+                   Grid.Column="3" Text="{Binding VersionText}"/>
     </Grid>
 </Window>