fix method call

This commit is contained in:
DismissedLight
2024-01-08 22:18:07 +08:00
parent 5f9b4a7cb2
commit f1d9787e45
3 changed files with 29 additions and 28 deletions

View File

@@ -1,7 +1,6 @@
using System;
using Windows.Win32.Foundation;
using Windows.Win32.System.Com;
using Windows.Win32.System.Registry;
namespace Windows.Win32;

View File

@@ -1,7 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Core.Shell;
using Snap.Hutao.Win32.Registry;
using System.Net;
using System.Reflection;
@@ -10,15 +10,29 @@ namespace Snap.Hutao.Core.IO.Http.DynamicProxy;
[Injection(InjectAs.Singleton)]
internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable
{
private const string ProxySettingPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections";
private static readonly MethodInfo ConstructSystemProxyMethod;
private readonly RegistryWatcher watcher;
private IWebProxy innerProxy = default!;
static DynamicHttpProxy()
{
Type? systemProxyInfoType = typeof(System.Net.Http.SocketsHttpHandler).Assembly.GetType("System.Net.Http.SystemProxyInfo");
ArgumentNullException.ThrowIfNull(systemProxyInfoType);
MethodInfo? constructSystemProxyMethod = systemProxyInfoType.GetMethod("ConstructSystemProxy", BindingFlags.Static | BindingFlags.Public);
ArgumentNullException.ThrowIfNull(constructSystemProxyMethod);
ConstructSystemProxyMethod = constructSystemProxyMethod;
}
public DynamicHttpProxy()
{
UpdateProxy();
watcher = new (@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections");
watcher = new(ProxySettingPath, UpdateProxy);
watcher.Start();
}
@@ -32,6 +46,8 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable
private IWebProxy InnerProxy
{
get => innerProxy;
[MemberNotNull(nameof(innerProxy))]
set
{
if (ReferenceEquals(innerProxy, value))
@@ -39,24 +55,17 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable
return;
}
if (innerProxy is IDisposable disposable)
{
disposable.Dispose();
}
(innerProxy as IDisposable)?.Dispose();
innerProxy = value;
}
}
[MemberNotNull(nameof(innerProxy))]
public void UpdateProxy()
{
Assembly httpNamespace = Assembly.Load("System.Net.Http");
Type? systemProxyInfoType = httpNamespace.GetType("System.Net.Http.SystemProxyInfo");
ArgumentNullException.ThrowIfNull(systemProxyInfoType);
MethodInfo? constructSystemProxyMethod = systemProxyInfoType.GetMethod("ConstructSystemProxy", BindingFlags.Static | BindingFlags.Public);
ArgumentNullException.ThrowIfNull(constructSystemProxyMethod);
IWebProxy? proxy = (IWebProxy?)constructSystemProxyMethod.Invoke(null, null);
IWebProxy? proxy = ConstructSystemProxyMethod.Invoke(default, default) as IWebProxy;
ArgumentNullException.ThrowIfNull(proxy);
InnerProxy = proxy;
}
@@ -72,16 +81,7 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable
public void Dispose()
{
if (innerProxy is IDisposable disposable)
{
disposable.Dispose();
}
(innerProxy as IDisposable)?.Dispose();
watcher.Dispose();
}
private void OnRegistryChanged(object? sender, EventArgs e)
{
UpdateProxy();
}
}

View File

@@ -6,7 +6,7 @@ using Windows.Win32.Foundation;
using Windows.Win32.System.Registry;
using static Windows.Win32.PInvoke;
namespace Snap.Hutao.Core.Shell;
namespace Snap.Hutao.Win32.Registry;
internal sealed partial class RegistryWatcher : IDisposable
{
@@ -32,7 +32,6 @@ internal sealed partial class RegistryWatcher : IDisposable
public RegistryWatcher(string keyName, Action valueChangedCallback)
{
ArgumentException.ThrowIfNullOrEmpty(keyName);
string[] pathArray = keyName.Split('\\');
hKey = pathArray[0] switch
@@ -143,8 +142,11 @@ internal sealed partial class RegistryWatcher : IDisposable
}
}
// Before exiting, signal the Dispose method.
disposeEvent.Reset();
if (!disposed)
{
// Before exiting, signal the Dispose method.
disposeEvent.Reset();
}
}
catch (OperationCanceledException)
{