mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
refactor GetApplicationFileFromUriAsync calls
This commit is contained in:
15
src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs
Normal file
15
src/Snap.Hutao/Snap.Hutao/Core/InstalledLocation.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -11,5 +11,5 @@ internal interface IUpdateService
|
||||
|
||||
ValueTask<bool> DownloadUpdateAsync(CheckUpdateResult checkUpdateResult, IProgress<UpdateStatus> progress, CancellationToken token = default);
|
||||
|
||||
ValueTask<LaunchUpdaterResult> LaunchUpdaterAsync();
|
||||
LaunchUpdaterResult LaunchUpdater();
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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))));
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user