From f3934ce2cde87a86b6f795512b0d379ad90eaded Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Mon, 22 Jul 2024 14:00:20 +0800 Subject: [PATCH] refactor GetApplicationFileFromUriAsync calls --- .../Snap.Hutao/Core/InstalledLocation.cs | 15 +++++++++++++ .../Snap.Hutao/Core/RuntimeOptions.cs | 3 --- .../Snap.Hutao/Core/Shell/ShellLinkInterop.cs | 17 +++++--------- .../Unlocker/Island/IslandGameFpsUnlocker.cs | 22 +++++-------------- .../Service/Update/IUpdateService.cs | 2 +- .../Service/Update/UpdateService.cs | 6 ++--- .../UI/Shell/NotifyIconController.cs | 4 ++-- .../UI/Windowing/XamlWindowController.cs | 2 +- .../UI/Xaml/Data/AdvancedCollectionView.cs | 1 - .../Snap.Hutao/ViewModel/TitleViewModel.cs | 2 +- 10 files changed, 33 insertions(+), 41 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs b/src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs new file mode 100644 index 00000000..a58c51c0 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs @@ -0,0 +1,15 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using System.IO; +using Windows.ApplicationModel; + +namespace Snap.Hutao.Core; + +internal static class InstalledLocation +{ + public static string GetAbsolutePath(string relativePath) + { + return Path.Combine(Package.Current.InstalledLocation.Path, relativePath); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/RuntimeOptions.cs b/src/Snap.Hutao/Snap.Hutao/Core/RuntimeOptions.cs index 00703cb4..9e09ea36 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/RuntimeOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/RuntimeOptions.cs @@ -82,7 +82,6 @@ internal sealed class RuntimeOptions }); private readonly LazySlim lazyLocalCache = new(() => ApplicationData.Current.LocalCacheFolder.Path); - private readonly LazySlim lazyInstalledLocation = new(() => Package.Current.InstalledLocation.Path); private readonly LazySlim lazyFamilyName = new(() => Package.Current.Id.FamilyName); private readonly LazySlim lazyToastAvailable = new(() => @@ -100,8 +99,6 @@ internal sealed class RuntimeOptions public string UserAgent { get => lazyVersionAndUserAgent.Value.UserAgent; } - public string InstalledLocation { get => lazyInstalledLocation.Value; } - public string DataFolder { get => lazyDataFolder.Value; } public string LocalCache { get => lazyLocalCache.Value; } diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs index 67123d28..48cdf381 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs @@ -6,7 +6,6 @@ using Snap.Hutao.Win32.System.Com; using Snap.Hutao.Win32.UI.Shell; using Snap.Hutao.Win32.UI.WindowsAndMessaging; using System.IO; -using Windows.Storage; using static Snap.Hutao.Win32.Macros; using static Snap.Hutao.Win32.Ole32; @@ -18,27 +17,23 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop { private readonly RuntimeOptions runtimeOptions; - public async ValueTask TryCreateDesktopShoutcutForElevatedLaunchAsync() + public ValueTask TryCreateDesktopShoutcutForElevatedLaunchAsync() { string targetLogoPath = Path.Combine(runtimeOptions.DataFolder, "ShellLinkLogo.ico"); string elevatedLauncherPath = Path.Combine(runtimeOptions.DataFolder, "Snap.Hutao.Elevated.Launcher.exe"); try { - Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri(); - StorageFile iconFile = await StorageFile.GetFileFromApplicationUriAsync(sourceLogoUri); - await iconFile.OverwriteCopyAsync(targetLogoPath).ConfigureAwait(false); - - Uri elevatedLauncherUri = "ms-appx:///Snap.Hutao.Elevated.Launcher.exe".ToUri(); - StorageFile launcherFile = await StorageFile.GetFileFromApplicationUriAsync(elevatedLauncherUri); - await launcherFile.OverwriteCopyAsync(elevatedLauncherPath).ConfigureAwait(false); + File.Copy(InstalledLocation.GetAbsolutePath("Assets/Logo.ico"), targetLogoPath, true); + File.Copy(InstalledLocation.GetAbsolutePath("Snap.Hutao.Elevated.Launcher.exe"), elevatedLauncherPath, true); } catch { - return false; + return ValueTask.FromResult(false); } - return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(targetLogoPath, elevatedLauncherPath); + bool result = UnsafeTryCreateDesktopShoutcutForElevatedLaunch(targetLogoPath, elevatedLauncherPath); + return ValueTask.FromResult(result); } private unsafe bool UnsafeTryCreateDesktopShoutcutForElevatedLaunch(string targetLogoPath, string elevatedLauncherPath) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs index d8d16799..d9bdf6c7 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/Island/IslandGameFpsUnlocker.cs @@ -31,7 +31,11 @@ internal sealed class IslandGameFpsUnlocker : GameFpsUnlocker protected override async ValueTask PostUnlockOverrideAsync(GameFpsUnlockerContext context, LaunchOptions launchOptions, ILogger logger, CancellationToken token = default(CancellationToken)) { - if (!await InitializeIslandFileAsync().ConfigureAwait(false)) + try + { + File.Copy(InstalledLocation.GetAbsolutePath("Snap.Hutao.UnlockerIsland.dll"), dataFolderIslandPath, true); + } + catch { context.Logger.LogError("Failed to copy island file."); return; @@ -76,22 +80,6 @@ internal sealed class IslandGameFpsUnlocker : GameFpsUnlocker } } - private async ValueTask InitializeIslandFileAsync() - { - try - { - Uri islandUri = "ms-appx:///Snap.Hutao.UnlockerIsland.dll".ToUri(); - StorageFile islandFile = await StorageFile.GetFileFromApplicationUriAsync(islandUri); - await islandFile.OverwriteCopyAsync(dataFolderIslandPath).ConfigureAwait(false); - - return true; - } - catch - { - return false; - } - } - private unsafe void InitializeIsland(Process gameProcess) { HANDLE hModule = default; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs index d1984bd4..f6de3c3a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs @@ -11,5 +11,5 @@ internal interface IUpdateService ValueTask DownloadUpdateAsync(CheckUpdateResult checkUpdateResult, IProgress progress, CancellationToken token = default); - ValueTask LaunchUpdaterAsync(); + LaunchUpdaterResult LaunchUpdater(); } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs index c74d1518..de79d83d 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs @@ -88,14 +88,12 @@ internal sealed partial class UpdateService : IUpdateService return DownloadUpdatePackageAsync(checkUpdateResult.HutaoVersionInformation, GetUpdatePackagePath(), progress, token); } - public async ValueTask LaunchUpdaterAsync() + public LaunchUpdaterResult LaunchUpdater() { RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService(); string updaterTargetPath = runtimeOptions.GetDataFolderUpdateCacheFolderFile(UpdaterFilename); - Uri updaterSourceUri = $"ms-appx:///{UpdaterFilename}".ToUri(); - StorageFile updaterFile = await StorageFile.GetFileFromApplicationUriAsync(updaterSourceUri); - await updaterFile.OverwriteCopyAsync(updaterTargetPath).ConfigureAwait(false); + File.Copy(InstalledLocation.GetAbsolutePath(UpdaterFilename), updaterTargetPath, true); string commandLine = new CommandLineBuilder() .Append("--package-path", GetUpdatePackagePath(runtimeOptions)) diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs index 25d2867f..7c7eb43f 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconController.cs @@ -28,8 +28,8 @@ internal sealed class NotifyIconController : IDisposable { lazyMenu = new(() => new(serviceProvider)); - RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService(); - string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico"); + string iconPath = InstalledLocation.GetAbsolutePath("Assets/Logo.ico"); + icon = new(iconPath); id = Unsafe.As(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconPath)))); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs index 85e85614..b4d5c89c 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs @@ -58,7 +58,7 @@ internal sealed class XamlWindowController AppOptions appOptions = serviceProvider.GetRequiredService(); window.AppWindow.Title = SH.FormatAppNameAndVersion(runtimeOptions.Version); - window.AppWindow.SetIcon(Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico")); + window.AppWindow.SetIcon(InstalledLocation.GetAbsolutePath("Assets/Logo.ico")); // ExtendContentIntoTitleBar if (window is IXamlWindowExtendContentIntoTitleBar xamlWindow) diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/AdvancedCollectionView.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/AdvancedCollectionView.cs index 10af4d69..37d9e491 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/AdvancedCollectionView.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/AdvancedCollectionView.cs @@ -7,7 +7,6 @@ using Microsoft.UI.Xaml.Data; using System.Collections; using System.Collections.ObjectModel; using System.Collections.Specialized; -using System.Diagnostics; using System.Runtime.CompilerServices; using Windows.Foundation; using Windows.Foundation.Collections; diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs index 0fa848d6..70d713d8 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/TitleViewModel.cs @@ -117,7 +117,7 @@ internal sealed partial class TitleViewModel : Abstraction.ViewModel if (installUpdateUserConsentResult is ContentDialogResult.Primary) { - LaunchUpdaterResult launchUpdaterResult = await updateService.LaunchUpdaterAsync().ConfigureAwait(false); + LaunchUpdaterResult launchUpdaterResult = updateService.LaunchUpdater(); if (launchUpdaterResult.IsSuccess) { ContentDialog contentDialog = await contentDialogFactory