Skip to content

Commit

Permalink
use package manager to update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Dec 26, 2023
1 parent 268c2d0 commit 54d21b2
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 44 deletions.
16 changes: 16 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/RuntimeOptionsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using System.IO;

namespace Snap.Hutao.Core;

internal static class RuntimeOptionsExtension
{
public static string GetDataFolderUpdateCacheFolderFile(this RuntimeOptions options, string fileName)
{
string directory = Path.Combine(options.DataFolder, "UpdateCache");
Directory.CreateDirectory(directory);
return Path.Combine(directory, fileName);
}
}
37 changes: 21 additions & 16 deletions src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ namespace Snap.Hutao.Core.Setting;
/// 设置键
/// </summary>
[HighQuality]
[SuppressMessage("", "SA1124")]
internal static class SettingKeys
{
#region MainWindow
public const string WindowRect = "WindowRect";

public const string IsNavPaneOpen = "IsNavPaneOpen";
public const string IsInfoBarToggleChecked = "IsInfoBarToggleChecked";
public const string ExcludedAnnouncementIds = "ExcludedAnnouncementIds";
#endregion

#region Application
public const string LaunchTimes = "LaunchTimes";

public const string DataFolderPath = "DataFolderPath";
public const string Major1Minor7Revision0GuideState = "Major1Minor7Revision0GuideState";
public const string HotKeyMouseClickRepeatForever = "HotKeyMouseClickRepeatForever";
public const string IsAllocConsoleDebugModeEnabled = "IsAllocConsoleDebugModeEnabled";
#endregion

#region Passport
public const string PassportUserName = "PassportUserName";

public const string PassportPassword = "PassportPassword";
#endregion

public const string IsInfoBarToggleChecked = "IsInfoBarToggleChecked";

public const string Major1Minor7Revision0GuideState = "Major1Minor7Revision0GuideState";

public const string ExcludedAnnouncementIds = "ExcludedAnnouncementIds";

public const string SuppressMetadataInitialization = "SuppressMetadataInitialization";

public const string OverrideElevationRequirement = "OverrideElevationRequirement";

#region Cultivation
public const string CultivationAvatarLevelCurrent = "CultivationAvatarLevelCurrent";
public const string CultivationAvatarLevelTarget = "CultivationAvatarLevelTarget";
public const string CultivationAvatarSkillACurrent = "CultivationAvatarSkillACurrent";
Expand All @@ -43,13 +43,18 @@ internal static class SettingKeys
public const string CultivationWeapon90LevelTarget = "CultivationWeapon90LevelTarget";
public const string CultivationWeapon70LevelCurrent = "CultivationWeapon70LevelCurrent";
public const string CultivationWeapon70LevelTarget = "CultivationWeapon70LevelTarget";
#endregion

#region HomeCard Dashboard
public const string IsHomeCardLaunchGamePresented = "IsHomeCardLaunchGamePresented";
public const string IsHomeCardGachaStatisticsPresented = "IsHomeCardGachaStatisticsPresented";
public const string IsHomeCardAchievementPresented = "IsHomeCardAchievementPresented";
public const string IsHomeCardDailyNotePresented = "IsHomeCardDailyNotePresented";
#endregion

public const string HotKeyMouseClickRepeatForever = "HotKeyMouseClickRepeatForever";

public const string IsAllocConsoleDebugModeEnabled = "IsAllocConsoleDebugModeEnabled";
#region DevTool
public const string SuppressMetadataInitialization = "SuppressMetadataInitialization";
public const string OverrideElevationRequirement = "OverrideElevationRequirement";
public const string OverrideUpdateVersionComparison = "OverrideUpdateVersionComparison";
#endregion
}
10 changes: 2 additions & 8 deletions src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop

public async ValueTask<bool> TryCreateDesktopShoutcutForElevatedLaunchAsync()
{
Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri();
string targetLogoPath = Path.Combine(runtimeOptions.DataFolder, "ShellLinkLogo.ico");

try
{
Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri();
StorageFile iconFile = await StorageFile.GetFileFromApplicationUriAsync(sourceLogoUri);
using (Stream inputStream = (await iconFile.OpenReadAsync()).AsStream())
{
using (FileStream outputStream = File.Create(targetLogoPath))
{
await inputStream.CopyToAsync(outputStream).ConfigureAwait(false);
}
}
await iconFile.OverwriteCopyAsync(targetLogoPath).ConfigureAwait(false);
}
catch
{
Expand Down
21 changes: 21 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Extension/StorageFileExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using System.IO;
using Windows.Storage;

namespace Snap.Hutao.Extension;

internal static class StorageFileExtension
{
public static async ValueTask OverwriteCopyAsync(this StorageFile file, string targetFile)
{
using (Stream outputStream = (await file.OpenReadAsync()).AsStreamForRead())
{
using (FileStream inputStream = File.Create(targetFile))
{
await outputStream.CopyToAsync(inputStream).ConfigureAwait(false);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Service/Update/IUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ internal interface IUpdateService
{
ValueTask<bool> CheckForUpdateAndDownloadAsync(IProgress<UpdateStatus> progress, CancellationToken token = default);

void LaunchInstaller();
ValueTask LaunchUpdaterAsync();
}
44 changes: 32 additions & 12 deletions src/Snap.Hutao/Snap.Hutao/Service/Update/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
using Snap.Hutao.Core;
using Snap.Hutao.Core.IO.Hashing;
using Snap.Hutao.Core.IO.Http.Sharding;
using Snap.Hutao.Core.Setting;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Web.Hutao;
using Snap.Hutao.Web.Hutao.Response;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using Windows.Storage;

namespace Snap.Hutao.Service.Update;

[ConstructorGenerated]
[Injection(InjectAs.Singleton, typeof(IUpdateService))]
internal sealed partial class UpdateService : IUpdateService
{
private const string UpdaterFilename = "Snap.Hutao.Deployment.exe";

private readonly IServiceProvider serviceProvider;

public async ValueTask<bool> CheckForUpdateAndDownloadAsync(IProgress<UpdateStatus> progress, CancellationToken token = default)
Expand All @@ -37,14 +41,17 @@ public async ValueTask<bool> CheckForUpdateAndDownloadAsync(IProgress<UpdateStat
HutaoVersionInformation versionInformation = response.Data;
string msixPath = GetUpdatePackagePath();

if (scope.ServiceProvider.GetRequiredService<RuntimeOptions>().Version >= versionInformation.Version)
if (!LocalSetting.Get(SettingKeys.OverrideUpdateVersionComparison, false))
{
if (File.Exists(msixPath))
if (scope.ServiceProvider.GetRequiredService<RuntimeOptions>().Version >= versionInformation.Version)
{
File.Delete(msixPath);
}
if (File.Exists(msixPath))
{
File.Delete(msixPath);
}

return false;
return false;
}
}

progress.Report(new(versionInformation.Version.ToString(), 0, 0));
Expand All @@ -63,12 +70,27 @@ public async ValueTask<bool> CheckForUpdateAndDownloadAsync(IProgress<UpdateStat
}
}

public void LaunchInstaller()
public async ValueTask LaunchUpdaterAsync()
{
RuntimeOptions runtimeOptions = serviceProvider.GetRequiredService<RuntimeOptions>();
string updatetTargetPath = runtimeOptions.GetDataFolderUpdateCacheFolderFile(UpdaterFilename);

Uri updaterSourceUri = $"ms-appx:///{UpdaterFilename}".ToUri();
StorageFile updaterFile = await StorageFile.GetFileFromApplicationUriAsync(updaterSourceUri);
await updaterFile.OverwriteCopyAsync(updatetTargetPath).ConfigureAwait(false);

string commandLine = new CommandLineBuilder()
.Append("--package-path", GetUpdatePackagePath(runtimeOptions))
.Append("--family-name", runtimeOptions.FamilyName)
.Append("--update-behavior", true)
.ToString();

Process.Start(new ProcessStartInfo()
{
Arguments = commandLine,
WindowStyle = ProcessWindowStyle.Minimized,
FileName = updatetTargetPath,
UseShellExecute = true,
FileName = GetUpdatePackagePath(),
});
}

Expand All @@ -78,12 +100,10 @@ private static async ValueTask<bool> CheckUpdateCacheSHA256Async(string filePath
return string.Equals(localHash, remoteHash, StringComparison.OrdinalIgnoreCase);
}

private string GetUpdatePackagePath()
private string GetUpdatePackagePath(RuntimeOptions? runtimeOptions = default)
{
string dataFolder = serviceProvider.GetRequiredService<RuntimeOptions>().DataFolder;
string directory = Path.Combine(dataFolder, "UpdateCache");
Directory.CreateDirectory(directory);
return Path.Combine(directory, "Snap.Hutao.msix");
runtimeOptions ??= serviceProvider.GetRequiredService<RuntimeOptions>();
return runtimeOptions.GetDataFolderUpdateCacheFolderFile("Snap.Hutao.msix");
}

private async ValueTask<bool> DownloadUpdatePackageAsync(HutaoVersionInformation versionInformation, string filePath, IProgress<UpdateStatus> progress, CancellationToken token = default)
Expand Down
4 changes: 4 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231115000" />
<PackageReference Include="QRCoder" Version="1.4.3" />
<PackageReference Include="Snap.Discord.GameSDK" Version="1.5.0" />
<PackageReference Include="Snap.Hutao.Deployment.Runtime" Version="1.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Snap.Hutao.SourceGeneration" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
18 changes: 12 additions & 6 deletions src/Snap.Hutao/Snap.Hutao/View/Page/TestPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@
</cwc:SettingsCard>

<cwc:SettingsCard Header="Reset Guide State">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding ResetGuideStateCommand}" Content="Reset (No restart)"/>
</StackPanel>
<Button
Command="{Binding ResetGuideStateCommand}"
Content="Reset (No restart)"
Style="{ThemeResource SettingButtonStyle}"/>
</cwc:SettingsCard>

<cwc:SettingsCard Header="Resize MainWindow">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding ResetMainWindowSizeCommand}" Content="Reset"/>
</StackPanel>
<Button
Command="{Binding ResetMainWindowSizeCommand}"
Content="Reset"
Style="{ThemeResource SettingButtonStyle}"/>
</cwc:SettingsCard>

<cwc:SettingsCard Header="Suppress Metadata Initialization">
Expand All @@ -97,6 +99,10 @@
<ToggleSwitch IsOn="{Binding OverrideElevationRequirement, Mode=TwoWay}"/>
</cwc:SettingsCard>

<cwc:SettingsCard Header="Override Update Version Comparison">
<ToggleSwitch IsOn="{Binding OverrideUpdateVersionComparison, Mode=TwoWay}"/>
</cwc:SettingsCard>

<cwc:SettingsCard
Command="{Binding CompensationGachaLogServiceTimeCommand}"
Header="Compensation GachaLog Service Time For 15 Days"
Expand Down
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/View/TitleView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private async ValueTask DoCheckUpdateAsync(CancellationToken token)
.ConfigureAwait(false);
if (result == ContentDialogResult.Primary)
{
updateService.LaunchInstaller();
await updateService.LaunchUpdaterAsync().ConfigureAwait(false);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public bool OverrideElevationRequirement
set => LocalSetting.Set(SettingKeys.OverrideElevationRequirement, value);
}

[SuppressMessage("", "CA1822")]
public bool OverrideUpdateVersionComparison
{
get => LocalSetting.Get(SettingKeys.OverrideUpdateVersionComparison, false);
set => LocalSetting.Set(SettingKeys.OverrideUpdateVersionComparison, value);
}

[Command("ResetGuideStateCommand")]
private static void ResetGuideState()
{
Expand Down

0 comments on commit 54d21b2

Please sign in to comment.