XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

HaloPixelToolBox

【WinUI3】基于USB HID通讯的花再音响工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
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;

/// <summary>
/// InstallProgressPage.xaml 的交互逻辑
/// </summary>
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);
        }
    }
}