using HaloPixelToolBox.Installer.Profiles; using HaloPixelToolBox.Installer.Utilities; using HaloPixelToolBox.Installer.ViewModel.Pages; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Controls; namespace HaloPixelToolBox.Installer.Views.Pages; /// /// InstallProgressPage.xaml 的交互逻辑 /// public partial class InstallProgressPage : Page { public static InstallProgressPage? Current { get; set; } public InstallProgressPageViewModel ViewModel { get; set; } public InstallProgressPage() { ViewModel = new(Current = this); DataContext = ViewModel; InitializeComponent(); } private async void Page_Loaded(object sender, RoutedEventArgs e) { await Task.Run(() => { switch (SystemProfile.StartMode) { case "Upgrade": var filePath = Path.Combine(SystemProfile.InstallPath, "InstallPackage.zip"); if (File.Exists(filePath)) { using (var fileStream = File.OpenRead(filePath)) { if (Install(fileStream)) CreateApplicationShortcut(); } File.Delete(filePath); } break; default: if (Assembly.GetExecutingAssembly().GetManifestResourceStream("HaloPixelToolBox.Installer.Resources.Resource.Source.zip") is Stream innerStream) { if (SystemProfile.InstallPath != string.Empty && !Directory.Exists(SystemProfile.InstallPath)) Directory.CreateDirectory(SystemProfile.InstallPath); if (Install(innerStream)) CreateApplicationShortcut(); } break; } }); } private bool Install(Stream stream) { try { ZipHelper.ExtraZipStream(stream, SystemProfile.InstallPath); CleanupLegacyClientArtifacts(); Dispatcher.Invoke(() => { installGrid.Visibility = Visibility.Collapsed; successGrid.Visibility = Visibility.Visible; }); return true; } catch (Exception ex) { Dispatcher.Invoke(() => progress.IsError = true); MessageBox.Show($"安装时发生错误:\n{ex.Message}"); return false; } } private static void CreateApplicationShortcut() => FileHelper.CreateShortCut( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "花再工具箱.lnk"), Path.Combine(SystemProfile.InstallPath, SystemProfile.ApplicationExecutableName), null, "花再工具箱快捷方式", null, SystemProfile.InstallPath); private static void CleanupLegacyClientArtifacts() { string[] extensions = [".exe", ".dll", ".deps.json", ".runtimeconfig.json", ".pri", ".pdb"]; var installRoot = Path.GetFullPath(SystemProfile.InstallPath); foreach (var extension in extensions) { var legacyPath = Path.GetFullPath(Path.Combine(installRoot, SystemProfile.LegacyApplicationName + extension)); if (legacyPath.StartsWith(installRoot + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) && File.Exists(legacyPath)) File.Delete(legacyPath); } } }