2
0
flabbet 4 жил өмнө
parent
commit
59b2a12830

+ 24 - 0
PixiEditor.UpdateInstaller/Extensions.cs

@@ -0,0 +1,24 @@
+using System;
+
+namespace PixiEditor.UpdateInstaller
+{
+    public static class Extensions
+	{
+		[System.Runtime.InteropServices.DllImport("kernel32.dll")]
+		static extern uint GetModuleFileName(IntPtr hModule, System.Text.StringBuilder lpFilename, int nSize);
+		static readonly int MAX_PATH = 255;
+		public static string GetExecutablePath()
+		{
+			if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
+			{
+				var sb = new System.Text.StringBuilder(MAX_PATH);
+				GetModuleFileName(IntPtr.Zero, sb, MAX_PATH);
+				return sb.ToString();
+			}
+			else
+			{
+				return System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
+			}
+		}
+	}
+}

+ 1 - 1
PixiEditor.UpdateInstaller/MainWindow.xaml

@@ -6,7 +6,7 @@
         xmlns:local="clr-namespace:PixiEditor.UpdateInstaller"
         mc:Ignorable="d"
         Title="MainWindow" Height="350" Width="250" Background="#2D2D30" ResizeMode="NoResize"
-        WindowStyle="None" WindowStartupLocation="CenterScreen">
+        WindowStyle="None" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
     <WindowChrome.WindowChrome>
         <WindowChrome ResizeBorderThickness="6"
             CaptionHeight="30"/>

+ 29 - 2
PixiEditor.UpdateInstaller/MainWindow.xaml.cs

@@ -1,4 +1,8 @@
-using System.Reflection;
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
+using System.Threading.Tasks;
 using System.Windows;
 
 namespace PixiEditor.UpdateInstaller
@@ -11,7 +15,30 @@ namespace PixiEditor.UpdateInstaller
         public MainWindow()
         {
             InitializeComponent();
-            DataContext = new ViewModelMain(Close);
+            DataContext = new ViewModelMain();
+        }
+
+        private async void Window_Loaded(object sender, RoutedEventArgs e)
+        {
+            ViewModelMain vmm = ((ViewModelMain)DataContext);
+            await Task.Run(() =>
+            {
+                try
+                {
+                    vmm.InstallUpdate();
+                }
+                catch(Exception ex)
+                {
+                    MessageBox.Show(ex.Message, "Update error", MessageBoxButton.OK, MessageBoxImage.Error);
+                    File.AppendAllText("ErrorLog.txt", $"Error PixiEditor.UpdateInstaller: {DateTime.Now}\n{ex.Message}\n{ex.StackTrace}\n-----\n");
+                }
+                finally
+                {
+                    string pixiEditorExecutablePath = Directory.GetFiles(vmm.UpdateDirectory, "PixiEditor.exe")[0];
+                    Process.Start(pixiEditorExecutablePath);
+                }
+            });
+            Close();
         }
     }
 }

+ 15 - 12
PixiEditor.UpdateInstaller/ViewModelMain.cs

@@ -5,6 +5,8 @@ using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
 
 namespace PixiEditor.UpdateInstaller
 {
@@ -13,6 +15,8 @@ namespace PixiEditor.UpdateInstaller
         public ViewModelMain Current { get; private set; }
         public UpdateModule.UpdateInstaller Installer { get; set; }
 
+        public string UpdateDirectory { get; private set; }
+
         private float _progressValue;
 
         public float ProgressValue
@@ -25,17 +29,21 @@ namespace PixiEditor.UpdateInstaller
             }
         }
 
-        public ViewModelMain(Action closeAction)
+        public ViewModelMain()
         {
             Current = this;
 
-            string updateDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
+            string updateDirectory = Path.GetDirectoryName(Extensions.GetExecutablePath());
 
 #if DEBUG
             updateDirectory = Environment.GetCommandLineArgs()[1];
 #endif
+            UpdateDirectory = updateDirectory;
+        }
 
-            string[] files = Directory.GetFiles(updateDirectory, "update-*.zip");
+        public void InstallUpdate()
+        {
+            string[] files = Directory.GetFiles(UpdateDirectory, "update-*.zip");
 
             if (files.Length > 0)
             {
@@ -43,15 +51,10 @@ namespace PixiEditor.UpdateInstaller
                 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);
+            else
+            {
+                ProgressValue = 100;
+            }
         }
 
         private void Installer_ProgressChanged(object sender, UpdateModule.UpdateProgressChangedEventArgs e)

+ 6 - 0
PixiEditor.UpdateModule/UpdateInstaller.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics;
 using System.IO;
 using System.IO.Compression;
 
@@ -28,6 +29,11 @@ namespace PixiEditor.UpdateModule
 
         public void Install()
         {
+            var processes = Process.GetProcessesByName("PixiEditor");
+            if(processes.Length > 0)
+            {
+                processes[0].WaitForExit();
+            }
             ZipFile.ExtractToDirectory(ArchiveFileName, TargetDirectoryName, true);
             Progress = 25; //25% for unzip
             string dirWithFiles = Directory.GetDirectories(TargetDirectoryName)[0];

+ 12 - 18
PixiEditor/ViewModels/ViewModelMain.cs

@@ -312,14 +312,13 @@ namespace PixiEditor.ViewModels
             BitmapManager.PrimaryColor = PrimaryColor;
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             Current = this;
-            InitUpdateChecker();
-            CheckForUpdate();
+            InitUpdateChecker();            
         }
 
-        private void CheckForUpdate()
+        public async Task<bool> CheckForUpdate()
         {
-            bool close = false;
-            Task.Run(async () => {
+            return await Task.Run(async () =>
+            {
                 bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
                 bool updateFileDoesNotExists = !File.Exists($"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip");
                 if (updateAvailable && updateFileDoesNotExists)
@@ -327,20 +326,10 @@ namespace PixiEditor.ViewModels
                     VersionText = "Downloading update...";
                     await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
                     VersionText = "Restart to install update";
+                    return true;
                 }
-                else if (!updateFileDoesNotExists) 
-                {
-                    string path = 
-                        Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"PixiEditor.UpdateInstaller.exe");
-                    Process.Start(path);
-                    close = true;
-                }
+                return false;
             });
-            if (close)
-            {
-                CloseAction();
-
-            }
         }
 
         private void InitUpdateChecker()
@@ -401,13 +390,18 @@ namespace PixiEditor.ViewModels
             if (result != ConfirmationType.Canceled) ((CancelEventArgs) property).Cancel = false;
         }
 
-        private void OnStartup(object parameter)
+        private async void OnStartup(object parameter)
         {
             var lastArg = Environment.GetCommandLineArgs().Last();
             if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
+            {
                 Open(lastArg);
+            }
             else
+            {
                 OpenNewFilePopup(null);
+            }
+            await CheckForUpdate();
         }
 
         private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -13,7 +13,7 @@
         xmlns:cmd="http://www.galasoft.ch/mvvmlight" 
         xmlns:avalondock="https://github.com/Dirkster99/AvalonDock"
         xmlns:colorpicker="clr-namespace:ColorPicker;assembly=ColorPicker"
-        mc:Ignorable="d" WindowStyle="None"
+        mc:Ignorable="d" WindowStyle="None" Initialized="mainWindow_Initialized"
         Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="{StaticResource MainColor}"
         WindowStartupLocation="CenterScreen" WindowState="Maximized" DataContext="{DynamicResource ViewModelMain}">
     <WindowChrome.WindowChrome>

+ 19 - 1
PixiEditor/Views/MainWindow.xaml.cs

@@ -1,4 +1,7 @@
 using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
 using System.Windows;
 using System.Windows.Input;
 using PixiEditor.ViewModels;
@@ -10,12 +13,14 @@ namespace PixiEditor
     /// </summary>
     public partial class MainWindow : Window
     {
+        ViewModelMain viewModel;
         public MainWindow()
         {
             InitializeComponent();
             StateChanged += MainWindowStateChangeRaised;
             MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
-            ((ViewModelMain) DataContext).CloseAction = Close;
+           viewModel = ((ViewModelMain)DataContext);
+            viewModel.CloseAction = Close;
         }
 
         private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
@@ -60,5 +65,18 @@ namespace PixiEditor
                 MaximizeButton.Visibility = Visibility.Visible;
             }
         }
+
+        private void mainWindow_Initialized(object sender, EventArgs e)
+        {
+            string dir = AppDomain.CurrentDomain.BaseDirectory;
+            bool updateFileExists = Directory.GetFiles(dir, "update-*.zip").Length > 0;
+            if (updateFileExists)
+            {
+                string path =
+                    Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
+                Process.Start(path);
+                Close();
+            }
+        }
     }
 }