refactor GetApplicationFileFromUriAsync calls

This commit is contained in:
DismissedLight
2024-07-22 14:00:20 +08:00
parent 8dd74c6c89
commit f3934ce2cd
10 changed files with 33 additions and 41 deletions

View File

@@ -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);
}
}

View File

@@ -82,7 +82,6 @@ internal sealed class RuntimeOptions
});
private readonly LazySlim<string> lazyLocalCache = new(() => ApplicationData.Current.LocalCacheFolder.Path);
private readonly LazySlim<string> lazyInstalledLocation = new(() => Package.Current.InstalledLocation.Path);
private readonly LazySlim<string> lazyFamilyName = new(() => Package.Current.Id.FamilyName);
private readonly LazySlim<bool> 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; }

View File

@@ -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<bool> TryCreateDesktopShoutcutForElevatedLaunchAsync()
public ValueTask<bool> 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)

View File

@@ -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<bool> 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;

View File

@@ -11,5 +11,5 @@ internal interface IUpdateService
ValueTask<bool> DownloadUpdateAsync(CheckUpdateResult checkUpdateResult, IProgress<UpdateStatus> progress, CancellationToken token = default);
ValueTask<LaunchUpdaterResult> LaunchUpdaterAsync();
LaunchUpdaterResult LaunchUpdater();
}

View File

@@ -88,14 +88,12 @@ internal sealed partial class UpdateService : IUpdateService
return DownloadUpdatePackageAsync(checkUpdateResult.HutaoVersionInformation, GetUpdatePackagePath(), progress, token);
}
public async ValueTask<LaunchUpdaterResult> LaunchUpdaterAsync()
public LaunchUpdaterResult LaunchUpdater()
{
RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
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))

View File

@@ -28,8 +28,8 @@ internal sealed class NotifyIconController : IDisposable
{
lazyMenu = new(() => new(serviceProvider));
RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
string iconPath = Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico");
string iconPath = InstalledLocation.GetAbsolutePath("Assets/Logo.ico");
icon = new(iconPath);
id = Unsafe.As<byte, Guid>(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconPath))));

View File

@@ -58,7 +58,7 @@ internal sealed class XamlWindowController
AppOptions appOptions = serviceProvider.GetRequiredService<AppOptions>();
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)

View File

@@ -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;

View File

@@ -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