Skip to content

Commit

Permalink
Update Windows App SDK version to 1.5.240428000
Browse files Browse the repository at this point in the history
Update Windows App SDK version to 1.5.240428000
  • Loading branch information
Gaoyifei1011 committed May 3, 2024
1 parent 3fbfcac commit 30ea4dc
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 88 deletions.
2 changes: 1 addition & 1 deletion GetStoreApp/GetStoreApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<None Remove="Views\Windows\MainWindow.xaml" />
<None Remove="WinUIApp.xaml" />

<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240404000" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
<PackageReference Include="Mile.Aria2" Version="1.0.230" />

<Page Update="Styles\AppBarButton.xaml" Generator="MSBuild:Compile" />
Expand Down
Binary file modified GetStoreApp/GetStoreAppResource.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("获取商店应用")]
[assembly: AssemblyFileVersion("3.9.426.0")]
[assembly: AssemblyInformationalVersion("3.9.426.0")]
[assembly: AssemblyFileVersion("3.10.503.0")]
[assembly: AssemblyInformationalVersion("3.10.503.0")]
[assembly: AssemblyProduct("获取商店应用")]
[assembly: AssemblyTitle("获取商店应用")]
[assembly: AssemblyVersion("3.9.426.0")]
[assembly: AssemblyVersion("3.10.503.0")]

// 设置程序集对 COM 组件的访问权限
[assembly: ComVisible(false)]
Expand Down
159 changes: 104 additions & 55 deletions GetStoreApp/Services/Controls/Download/DeliveryOptimizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using GetStoreApp.WindowsAPI.PInvoke.Ole32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Threading.Tasks;
using Windows.Foundation.Diagnostics;

namespace GetStoreApp.Services.Controls.Download
Expand All @@ -15,31 +17,77 @@ namespace GetStoreApp.Services.Controls.Download
public static class DeliveryOptimizationService
{
private static string displayName = "GetStoreApp";
private static object deliveryOptimizationLock = new object();
private static Guid CLSID_DeliveryOptimization = new Guid("5b99fa76-721c-423c-adac-56d03c8a8007");
private static Guid IID_DOManager = new Guid("400E2D4A-1431-4C1A-A748-39CA472CFDB1");
private static StrategyBasedComWrappers comWrappers = new StrategyBasedComWrappers();

private static Dictionary<string, IDODownload> DeliveryOptimizationDict { get; } = new Dictionary<string, IDODownload>();

public static event Action<string, string, string, string, double> DownloadCreated;

public static event Action<string> DownloadContinued;

public static event Action<string> DownloadPaused;

public static event Action<string> DownloadAborted;

public static event Action<string, DO_DOWNLOAD_STATUS> DownloadProgressing;

public static event Action<string, DO_DOWNLOAD_STATUS> DownloadCompleted;

/// <summary>
/// 获取下载任务的数量
/// </summary>
public static int GetDownloadCount()
{
lock (deliveryOptimizationLock)
{
return DeliveryOptimizationDict.Count;
}
}

/// <summary>
/// 终止所有下载任务,仅用于应用关闭时
/// </summary>
public static void TerminateDownload()
{
if (GetDownloadCount() > 0)
{
lock (deliveryOptimizationLock)
{
foreach (KeyValuePair<string, IDODownload> deliveryOptimizationKeyValue in DeliveryOptimizationDict)
{
deliveryOptimizationKeyValue.Value.Abort();
}
}
}
}

/// <summary>
/// 使用下载链接创建下载
/// </summary>
public static unsafe string CreateDownload(string url, string saveFilePath)
public static unsafe void CreateDownload(string url, string saveFilePath)
{
string downloadID = string.Empty;

try
{
IDOManager doManager = null;
IDODownload doDownload = null;

// 创建 IDoManager
int createResult = Ole32Library.CoCreateInstance(ref CLSID_DeliveryOptimization, IntPtr.Zero, CLSCTX.CLSCTX_LOCAL_SERVER, ref IID_DOManager, out IntPtr doDownloadPointer);
int createResult = Ole32Library.CoCreateInstance(ref CLSID_DeliveryOptimization, IntPtr.Zero, CLSCTX.CLSCTX_LOCAL_SERVER, ref IID_DOManager, out IntPtr doManagerPointer);

if (createResult is 0)
{
StrategyBasedComWrappers strategyBasedComWrappers = new StrategyBasedComWrappers();
doManager = (IDOManager)strategyBasedComWrappers.GetOrCreateObjectForComInstance(doDownloadPointer, CreateObjectFlags.None);
doManager = (IDOManager)comWrappers.GetOrCreateObjectForComInstance(doManagerPointer, CreateObjectFlags.None);

// 创建下载
if (doManager is not null)
{
doManager.CreateDownload(out IDODownload doDownload);
doManager.CreateDownload(out doDownload);
ComWrappers.TryGetComInstance(doDownload, out IntPtr doDownloadPointer);
int proxyResult = Ole32Library.CoSetProxyBlanket(doDownloadPointer, uint.MaxValue, uint.MaxValue, unchecked((IntPtr)ulong.MaxValue), 0, 3, IntPtr.Zero, 32);

// 添加下载信息
Expand All @@ -49,93 +97,94 @@ public static unsafe string CreateDownload(string url, string saveFilePath)
doDownload.SetProperty(DODownloadProperty.DODownloadProperty_Uri, &urlVarient);
ComVariant filePathVarient = ComVariant.Create(saveFilePath);
doDownload.SetProperty(DODownloadProperty.DODownloadProperty_LocalPath, &filePathVarient);
ComVariant foregroundVarient = ComVariant.Create(true);

DODownloadStatusCallback doDownloadStatusCallback = new DODownloadStatusCallback();
doDownloadStatusCallback.StatusChanged += OnStatusChanged;

// 正在学习 COM 源生成......
ComVariant callbackInterfaceVarient = ComVariant.CreateRaw(VarEnum.VT_UNKNOWN, IntPtr.Zero);
IntPtr callbackPointer = comWrappers.GetOrCreateComInterfaceForObject(new UnknownWrapper(doDownloadStatusCallback).WrappedObject, CreateComInterfaceFlags.None);
ComVariant callbackInterfaceVarient = ComVariant.CreateRaw(VarEnum.VT_UNKNOWN, callbackPointer);
ComVariant foregroundVarient = ComVariant.Create(true);
doDownload.SetProperty(DODownloadProperty.DODownloadProperty_ForegroundPriority, &foregroundVarient);

int startResult = doDownload.Start(IntPtr.Zero);
ComVariant idVarient = ComVariant.Null;
doDownload.GetProperty(DODownloadProperty.DODownloadProperty_Id, &idVarient);
ComVariant totalSizeVarient = ComVariant.Null;
doDownload.GetProperty(DODownloadProperty.DODownloadProperty_TotalSizeBytes, &totalSizeVarient);
downloadID = idVarient.As<string>();
doDownloadStatusCallback.DownloadID = downloadID;
double size = Convert.ToDouble(totalSizeVarient.As<ulong>());
DownloadCreated?.Invoke(downloadID, Path.GetFileName(saveFilePath), saveFilePath, url, Convert.ToDouble(totalSizeVarient.As<ulong>()));

// 下载启动成功,获取下载 Id,并添加到下载字典中
if (startResult is 0)
lock (deliveryOptimizationLock)
{
ComVariant idVarient = ComVariant.Null;
doDownload.GetProperty(DODownloadProperty.DODownloadProperty_Id, &idVarient);
downloadID = idVarient.As<string>();
doDownloadStatusCallback.DownloadID = downloadID;
DeliveryOptimizationDict.TryAdd(downloadID, doDownload);
}

int result = doDownload.Start(IntPtr.Zero);
}
}
}
catch (Exception e)
{
LogService.WriteLog(LoggingLevel.Error, "Create delivery optimization download failed", e);
}
return downloadID;
}

/// <summary>
/// 删除下载
/// </summary>
public static bool DeleteDownload(string downloadID)
public static void DeleteDownload(string downloadID)
{
try
Task.Run(() =>
{
if (DeliveryOptimizationDict.TryGetValue(downloadID, out IDODownload doDownload))
try
{
int abortResult = doDownload.Abort();
return abortResult is 0;
if (DeliveryOptimizationDict.TryGetValue(downloadID, out IDODownload doDownload))
{
int abortResult = doDownload.Abort();
if (abortResult is 0)
{
DownloadAborted?.Invoke(downloadID);

DeliveryOptimizationDict.Remove(downloadID);
}
}
}
else
catch (Exception e)
{
return false;
LogService.WriteLog(LoggingLevel.Error, "Delete delivery optimization download failed", e);
}
}
catch (Exception e)
{
LogService.WriteLog(LoggingLevel.Error, "Delete delivery optimization download failed", e);
return false;
}
});
}

/// <summary>
/// 下载状态发生变化触发的事件
/// </summary>
private static void OnStatusChanged(DODownloadStatusCallback callback, IDODownload download, DO_DOWNLOAD_STATUS status)
private static void OnStatusChanged(DODownloadStatusCallback callback, IDODownload doDownload, DO_DOWNLOAD_STATUS status)
{
switch (status.State)
if (status.State is DODownloadState.DODownloadState_Transferring)
{
case DODownloadState.DODownloadState_Created:
{
break;
}
case DODownloadState.DODownloadState_Transferring:
{
break;
}
case DODownloadState.DODownloadState_Transferred:
{
break;
}
case DODownloadState.DODownloadState_Aborted:
{
break;
}
case DODownloadState.DODownloadState_Finalized:
DownloadProgressing?.Invoke(callback.DownloadID, status);
}
else if (status.State is DODownloadState.DODownloadState_Transferred)
{
DownloadCompleted?.Invoke(callback.DownloadID, status);
try
{
callback.StatusChanged -= OnStatusChanged;
doDownload.Finalize();

lock (deliveryOptimizationLock)
{
try
{
callback.StatusChanged -= OnStatusChanged;
}
catch (Exception)
if (DeliveryOptimizationDict.ContainsKey(callback.DownloadID))
{
DeliveryOptimizationDict.Remove(callback.DownloadID);
}
break;
}
}
catch (Exception e)
{
LogService.WriteLog(LoggingLevel.Warning, "Finalize download task failed", e);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion GetStoreApp/Views/Pages/StorePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GetStoreApp.Extensions.DataType.Enums;
using GetStoreApp.Services.Controls.Download;
using GetStoreApp.Views.Windows;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -93,7 +94,8 @@ private void OnLoaded(object sender, RoutedEventArgs args)
/// </summary>
private async void OnLanguageAndRegionClicked(object sender, RoutedEventArgs args)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:regionformatting"));
//await Launcher.LaunchUriAsync(new Uri("ms-settings:regionformatting"));
DeliveryOptimizationService.CreateDownload("https://dldir1v6.qq.com/weixin/Windows/WeChatSetup.exe", @"C:\Users\Gaoyifei\Download\WeChatSetup.exe");
}

/// <summary>
Expand Down
40 changes: 20 additions & 20 deletions GetStoreApp/WindowsAPI/ComTypes/IDODownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ namespace GetStoreApp.WindowsAPI.ComTypes
public unsafe partial interface IDODownload
{
/// <summary>
/// 中止下载
/// 启动或恢复下载,将可选范围作为指向 DO_DOWNLOAD_RANGES_INFO 结构的指针传递
/// </summary>
/// <param name="ranges">
/// 可选。 指向 DO_DOWNLOAD_RANGES_INFO 结构的指针 (仅下载文件) 的特定范围。 此处传递的值指示下载是针对整个文件或文件中的部分范围,还是只是为了在不更改范围信息的情况下继续下载。 通过传递指向空DO_DOWNLOAD_RANGES_INFO结构的指针来指示下载整个文件的请求,即,其 RangeCount 成员设置为零。 通过传递指向非空DO_DOWNLOAD_RANGES_INFO结构的指针来指示下载部分文件的请求。 nullptr仅当之前已使用空/非空范围开始下载一次时,才允许传递,并且指示必须恢复下载而不对请求的范围进行任何更改。
/// </param>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int Abort();
int Start(IntPtr ranges);

/// <summary>
/// 完成下载。 完成后,无法通过调用 “开始”来恢复下载
/// 暂停下载
/// </summary>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int Finalize();
int Pause();

/// <summary>
/// 检索指向包含特定下载属性的 VARIANT 的指针
/// 中止下载
/// </summary>
/// <param name="propId">获取 (DODownloadProperty) 类型所需的属性 ID。</param>
/// <param name="propVal">生成的属性值,存储在 VARIANT 中。</param>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int GetProperty(DODownloadProperty propId, ComVariant* propVal);
int Abort();

/// <summary>
/// 完成下载。 完成后,无法通过调用 “开始”来恢复下载。
/// </summary>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int Finalize();

/// <summary>
/// 指向 DO_DOWNLOAD_STATUS 结构的指针。
Expand All @@ -42,11 +50,13 @@ public unsafe partial interface IDODownload
int GetStatus(out DO_DOWNLOAD_STATUS status);

/// <summary>
/// 暂停下载
/// 检索指向包含特定下载属性的 VARIANT 的指针
/// </summary>
/// <param name="propId">获取 (DODownloadProperty) 类型所需的属性 ID。</param>
/// <param name="propVal">生成的属性值,存储在 VARIANT 中。</param>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int Pause();
int GetProperty(DODownloadProperty propId, ComVariant* propVal);

/// <summary>
/// 检索指向包含特定下载属性的 VARIANT 的指针。
Expand All @@ -56,15 +66,5 @@ public unsafe partial interface IDODownload
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int SetProperty(DODownloadProperty propId, ComVariant* propVal);

/// <summary>
/// 启动或恢复下载,将可选范围作为指向 DO_DOWNLOAD_RANGES_INFO 结构的指针传递。
/// </summary>
/// <param name="ranges">
/// 可选。 指向 DO_DOWNLOAD_RANGES_INFO 结构的指针 (仅下载文件) 的特定范围。 此处传递的值指示下载是针对整个文件或文件中的部分范围,还是只是为了在不更改范围信息的情况下继续下载。 通过传递指向空DO_DOWNLOAD_RANGES_INFO结构的指针来指示下载整个文件的请求,即,其 RangeCount 成员设置为零。 通过传递指向非空DO_DOWNLOAD_RANGES_INFO结构的指针来指示下载部分文件的请求。 nullptr仅当之前已使用空/非空范围开始下载一次时,才允许传递,并且指示必须恢复下载而不对请求的范围进行任何更改。
/// </param>
/// <returns>如果函数成功,则返回 S_OK。 否则,它将返回 HRESULT错误代码。</returns>
[PreserveSig]
int Start(IntPtr ranges);
}
}
2 changes: 1 addition & 1 deletion GetStoreAppPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Identity
Name="Gaoyifei1011.GetStoreApp"
Publisher="CN=高怡飞"
Version="3.9.426.0" />
Version="3.10.503.0" />

<Properties>
<DisplayName>ms-resource:PackageDisplayName</DisplayName>
Expand Down
2 changes: 1 addition & 1 deletion GetStoreAppWebView/GetStoreAppWebView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<None Remove="WebApp.xaml" />
<None Remove="WebWindow.xaml" />

<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240404000" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" GeneratePathProperty="true" />

<Page Remove="WebApp.xaml" />

Expand Down
Binary file modified GetStoreAppWebView/GetStoreAppWebViewResource.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreAppWebView/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("网页浏览器")]
[assembly: AssemblyFileVersion("3.9.426.0")]
[assembly: AssemblyInformationalVersion("3.9.426.0")]
[assembly: AssemblyFileVersion("3.10.503.0")]
[assembly: AssemblyInformationalVersion("3.10.503.0")]
[assembly: AssemblyProduct("网页浏览器")]
[assembly: AssemblyTitle("网页浏览器")]
[assembly: AssemblyVersion("3.9.426.0")]
[assembly: AssemblyVersion("3.10.503.0")]

// 设置程序集对 COM 组件的访问权限
[assembly: ComVisible(false)]
Expand Down
Loading

0 comments on commit 30ea4dc

Please sign in to comment.