adjust nullability annotation

This commit is contained in:
DismissedLight
2023-10-08 21:51:24 +08:00
parent 1cd3ec0cdb
commit 174757f94d
19 changed files with 34 additions and 121 deletions

View File

@@ -3,9 +3,6 @@ DefSubclassProc
RemoveWindowSubclass
SetWindowSubclass
// CoreMessaging
CreateDispatcherQueueController
// DWMAPI
DwmSetWindowAttribute
@@ -40,14 +37,12 @@ GetCursorPos
GetDC
GetDpiForWindow
GetForegroundWindow
GetWindowLong
GetWindowPlacement
GetWindowThreadProcessId
ReleaseDC
RegisterHotKey
SendInput
SetForegroundWindow
SetWindowLong
UnregisterHotKey
// COM
@@ -61,7 +56,6 @@ IMemoryBufferByteAccess
// Const value
INFINITE
WM_ERASEBKGND
WM_GETMINMAXINFO
WM_HOTKEY
WM_NCRBUTTONDOWN
@@ -75,7 +69,6 @@ LPTHREAD_START_ROUTINE
// UI.WindowsAndMessaging
MINMAXINFO
WINDOW_EX_STYLE
// System.Com
CWMO_FLAGS

View File

@@ -1,52 +0,0 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml.Media;
using Windows.Win32.System.WinRT;
using static Windows.Win32.PInvoke;
namespace Snap.Hutao.Core.Windowing;
internal class TransparentBackdrop : SystemBackdrop
{
private static readonly Lazy<Windows.UI.Composition.Compositor> LazyCompositor = new(() =>
{
EnsureWindowsSystemDispatcherQueueController();
return new();
});
private static Windows.System.DispatcherQueueController? dispatcherQueueController;
private static Windows.UI.Composition.Compositor Compositor => LazyCompositor.Value;
protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop connectedTarget, Microsoft.UI.Xaml.XamlRoot xamlRoot)
{
connectedTarget.SystemBackdrop = Compositor.CreateColorBrush(Windows.UI.Color.FromArgb(0, 255, 255, 255));
}
protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget)
{
disconnectedTarget.SystemBackdrop = null;
}
private static unsafe void EnsureWindowsSystemDispatcherQueueController()
{
if (Windows.System.DispatcherQueue.GetForCurrentThread() is not null)
{
return;
}
if (dispatcherQueueController is null)
{
DispatcherQueueOptions options = new()
{
dwSize = (uint)sizeof(DispatcherQueueOptions),
threadType = DISPATCHERQUEUE_THREAD_TYPE.DQTYPE_THREAD_CURRENT,
apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE.DQTAT_COM_STA,
};
CreateDispatcherQueueController(options, out dispatcherQueueController);
}
}
}

View File

@@ -56,7 +56,6 @@ internal sealed class WindowController
RecoverOrInitWindowSize();
UpdateImmersiveDarkMode(options.TitleBar, default!);
//SetWindowTransparent();
// appWindow.Show(true);
// appWindow.Show can't bring window to top.
@@ -150,7 +149,6 @@ internal sealed class WindowController
{
window.SystemBackdrop = backdropType switch
{
//BackdropType.Transparent => new TransparentBackdrop(),
BackdropType.MicaAlt => new MicaBackdrop() { Kind = MicaKind.BaseAlt },
BackdropType.Mica => new MicaBackdrop() { Kind = MicaKind.Base },
BackdropType.Acrylic => new DesktopAcrylicBackdrop(),
@@ -190,14 +188,6 @@ internal sealed class WindowController
DwmSetWindowAttribute(options.Hwnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, &isDarkMode, unchecked((uint)sizeof(BOOL)));
}
private unsafe void SetWindowTransparent()
{
int result = GetWindowLong(options.Hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
WINDOW_EX_STYLE style = *(WINDOW_EX_STYLE*)&result;
style |= WINDOW_EX_STYLE.WS_EX_LAYERED;
SetWindowLong(options.Hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, *(int*)&style);
}
private void UpdateDragRectangles()
{
AppWindowTitleBar appTitleBar = window.AppWindow.TitleBar;

View File

@@ -89,11 +89,6 @@ internal sealed class WindowSubclass : IDisposable
{
switch (uMsg)
{
case WM_ERASEBKGND:
{
return (LRESULT)(nint)1;
}
case WM_GETMINMAXINFO:
{
uint dpi = GetDpiForWindow(hwnd);

View File

@@ -42,6 +42,7 @@ internal sealed partial class DailyNoteViewModel : Abstraction.ViewModel
public RuntimeOptions RuntimeOptions { get => runtimeOptions; }
[SuppressMessage("", "CA1822")]
public IWebViewerSource VerifyUrlSource { get => new DailyNoteWebViewerSource(); }
/// <summary>

View File

@@ -7,24 +7,28 @@ namespace Snap.Hutao.ViewModel;
internal sealed class HomeCardOptions
{
[SuppressMessage("", "CA1822")]
public bool IsHomeCardLaunchGamePresented
{
get => LocalSetting.Get(SettingKeys.IsHomeCardLaunchGamePresented, true);
set => LocalSetting.Set(SettingKeys.IsHomeCardLaunchGamePresented, value);
}
[SuppressMessage("", "CA1822")]
public bool IsHomeCardGachaStatisticsPresented
{
get => LocalSetting.Get(SettingKeys.IsHomeCardGachaStatisticsPresented, true);
set => LocalSetting.Set(SettingKeys.IsHomeCardGachaStatisticsPresented, value);
}
[SuppressMessage("", "CA1822")]
public bool IsHomeCardAchievementPresented
{
get => LocalSetting.Get(SettingKeys.IsHomeCardAchievementPresented, true);
set => LocalSetting.Set(SettingKeys.IsHomeCardAchievementPresented, value);
}
[SuppressMessage("", "CA1822")]
public bool IsHomeCardDailyNotePresented
{
get => LocalSetting.Get(SettingKeys.IsHomeCardDailyNotePresented, true);

View File

@@ -18,9 +18,8 @@ namespace Snap.Hutao.Web.Hoyolab.Bbs.User;
internal sealed partial class UserClientOversea : IUserClient
{
private readonly IHttpRequestMessageBuilderFactory httpRequestMessageBuilderFactory;
private readonly HttpClient httpClient;
private readonly JsonSerializerOptions options;
private readonly ILogger<UserClientOversea> logger;
private readonly HttpClient httpClient;
/// <summary>
/// 获取当前用户详细信息,使用 LToken

View File

@@ -34,7 +34,9 @@ internal static class DynamicSecretHttpRequestMessageBuilderExtension
dsContent = $"{dsContent}&b={b}&q={q}";
}
#pragma warning disable CA1308
string check = Core.Convert.ToMd5HexString(dsContent).ToLowerInvariant();
#pragma warning restore CA1308
builder.SetHeader("DS", $"{t},{r},{check}");
}

View File

@@ -23,7 +23,6 @@ internal sealed partial class GameRecordClientOversea : IGameRecordClient
{
private readonly IHttpRequestMessageBuilderFactory httpRequestMessageBuilderFactory;
private readonly ILogger<GameRecordClient> logger;
private readonly JsonSerializerOptions options;
private readonly HttpClient httpClient;
/// <summary>

View File

@@ -27,7 +27,6 @@ internal sealed partial class HomaSpiralAbyssClient
private readonly IHttpRequestMessageBuilderFactory httpRequestMessageBuilderFactory;
private readonly ILogger<HomaSpiralAbyssClient> logger;
private readonly IServiceProvider serviceProvider;
private readonly JsonSerializerOptions options;
private readonly HttpClient httpClient;
/// <summary>

View File

@@ -5,7 +5,7 @@ using System.Net.Http;
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
public interface IHttpContentDeserializer
internal interface IHttpContentDeserializer
{
Task<object?> DeserializeAsync(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken = default);
ValueTask<object?> DeserializeAsync(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken = default);
}

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace Snap.Hutao.Web.Request.Builder.Abstraction;
public interface IHttpContentSerializer
internal interface IHttpContentSerializer
{
HttpContent? Serialize(object? content, Type? contentType, Encoding? encoding);
HttpContent? Serialize(object? content, Type contentType, Encoding? encoding);
}

View File

@@ -47,10 +47,8 @@ internal static class HttpContentBuilderExtension
public static T SetStringContent<T>(this T builder, string content, Encoding? encoding = null, string mediaType = default!)
where T : IHttpContentBuilder
{
#pragma warning disable CA2000
ArgumentNullException.ThrowIfNull(content);
return builder.SetContent(new StringContent(content, encoding, mediaType));
#pragma warning restore CA2000
}
[DebuggerStepThrough]
@@ -64,10 +62,8 @@ internal static class HttpContentBuilderExtension
public static T SetByteArrayContent<T>(this T builder, byte[] content, int offset, int count)
where T : IHttpContentBuilder
{
#pragma warning disable CA2000
ArgumentNullException.ThrowIfNull(content);
return builder.SetContent(new ByteArrayContent(content, offset, count));
#pragma warning restore CA2000
}
public static TBuilder SetContent<TBuilder, TContent>(this TBuilder builder, IHttpContentSerializer serializer, TContent content, Encoding? encoding = null)
@@ -84,7 +80,7 @@ internal static class HttpContentBuilderExtension
return builder.SetContent(httpContent);
}
public static T SetContent<T>(this T builder, IHttpContentSerializer serializer, object? content, Type? contentType, Encoding? encoding = null)
public static T SetContent<T>(this T builder, IHttpContentSerializer serializer, object? content, Type contentType, Encoding? encoding = null)
where T : IHttpContentBuilder
{
// Validate builder here already so that no unnecessary serialization is done.

View File

@@ -6,9 +6,9 @@ using System.Net.Http;
namespace Snap.Hutao.Web.Request.Builder;
internal static class HttpContentDeserializerExtensions
internal static class HttpContentDeserializerExtension
{
public static async Task<T?> DeserializeAsync<T>(this IHttpContentDeserializer deserializer, HttpContent? httpContent, CancellationToken cancellationToken = default)
public static async ValueTask<T?> DeserializeAsync<T>(this IHttpContentDeserializer deserializer, HttpContent? httpContent, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(deserializer);
return (T?)await deserializer.DeserializeAsync(httpContent, typeof(T), cancellationToken).ConfigureAwait(false);

View File

@@ -6,28 +6,13 @@ using System.Runtime.Serialization;
namespace Snap.Hutao.Web.Request.Builder;
[Serializable]
public class HttpContentSerializationException : Exception
internal sealed class HttpContentSerializationException : Exception
{
public HttpContentSerializationException()
: this(message: null, innerException: null)
{
}
public HttpContentSerializationException(string? message)
: this(message, innerException: null)
{
}
public HttpContentSerializationException(string? message, Exception? innerException)
: base(message ?? GetDefaultMessage(), innerException)
{
}
protected HttpContentSerializationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
private static string GetDefaultMessage()
{
return """

View File

@@ -14,7 +14,7 @@ internal abstract class HttpContentSerializer : IHttpContentSerializer, IHttpCon
get => Encoding.UTF8;
}
public HttpContent? Serialize(object? content, Type? contentType, Encoding? encoding)
public HttpContent? Serialize(object? content, Type contentType, Encoding? encoding)
{
contentType = GetAndVerifyContentType(content, contentType);
@@ -27,13 +27,13 @@ internal abstract class HttpContentSerializer : IHttpContentSerializer, IHttpCon
{
return SerializeCore(content, contentType, encoding ?? DefaultEncoding);
}
catch (Exception ex) when (!(ex is HttpContentSerializationException))
catch (Exception ex) when (ex is not HttpContentSerializationException)
{
throw new HttpContentSerializationException(null, ex);
}
}
public async Task<object?> DeserializeAsync(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken = default)
public async ValueTask<object?> DeserializeAsync(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(contentType);
@@ -52,11 +52,11 @@ internal abstract class HttpContentSerializer : IHttpContentSerializer, IHttpCon
}
}
protected abstract HttpContent? SerializeCore(object? content, Type? contentType, Encoding encoding);
protected abstract HttpContent? SerializeCore(object? content, Type contentType, Encoding encoding);
protected abstract Task<object?> DeserializeAsyncCore(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken);
protected abstract ValueTask<object?> DeserializeAsyncCore(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken);
private static Type? GetAndVerifyContentType(object? content, Type? contentType)
private static Type GetAndVerifyContentType(object? content, Type contentType)
{
// If both types are given, ensure that they match. Otherwise serialization will be problematic.
Type? actualContentType = content?.GetType();
@@ -70,6 +70,7 @@ internal abstract class HttpContentSerializer : IHttpContentSerializer, IHttpCon
}
// The contentType is optional. In that case, try to get the type on our own.
ArgumentNullException.ThrowIfNull(actualContentType);
return contentType ?? actualContentType;
}
}

View File

@@ -14,7 +14,7 @@ internal static partial class JsonBuilderExtension
return builder.SetContent(serializer ?? builder.HttpContentSerializer, content, encoding);
}
public static T SetJsonContent<T>(this T builder, object? content, Type? contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
public static T SetJsonContent<T>(this T builder, object? content, Type contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
where T : IHttpContentBuilder
{
return builder.SetContent(serializer ?? builder.HttpContentSerializer, content, contentType, encoding);
@@ -26,7 +26,7 @@ internal static partial class JsonBuilderExtension
return builder.Post().SetJsonContent(content, encoding, serializer);
}
public static T PostJson<T>(this T builder, object? content, Type? contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
public static T PostJson<T>(this T builder, object? content, Type contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
where T : IHttpMethodBuilder, IHttpContentBuilder
{
return builder.Post().SetJsonContent(content, contentType, encoding, serializer);
@@ -38,7 +38,7 @@ internal static partial class JsonBuilderExtension
return builder.Put().SetJsonContent(content, encoding, serializer);
}
public static T PutJson<T>(this T builder, object? content, Type? contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
public static T PutJson<T>(this T builder, object? content, Type contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
where T : IHttpMethodBuilder, IHttpContentBuilder
{
return builder.Put().SetJsonContent(content, contentType, encoding, serializer);
@@ -50,7 +50,7 @@ internal static partial class JsonBuilderExtension
return builder.Patch().SetJsonContent(content, encoding, serializer);
}
public static T PatchJson<T>(this T builder, object? content, Type? contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
public static T PatchJson<T>(this T builder, object? content, Type contentType, Encoding? encoding = null, JsonHttpContentSerializer? serializer = null)
where T : IHttpMethodBuilder, IHttpContentBuilder
{
return builder.Patch().SetJsonContent(content, contentType, encoding, serializer);

View File

@@ -17,7 +17,7 @@ internal class JsonHttpContentSerializer : HttpContentSerializer
internal JsonSerializerOptions? JsonSerializerOptions { get; set; }
protected override HttpContent? SerializeCore(object? content, Type? contentType, Encoding encoding)
protected override HttpContent? SerializeCore(object? content, Type contentType, Encoding encoding)
{
// Note for the future:
// It can happen that content.GetType() is more specific than contentType, e.g.
@@ -49,7 +49,7 @@ internal class JsonHttpContentSerializer : HttpContentSerializer
}
}
protected override async Task<object?> DeserializeAsyncCore(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken)
protected override async ValueTask<object?> DeserializeAsyncCore(HttpContent? httpContent, Type contentType, CancellationToken cancellationToken)
{
// While we'd ideally read directly from the stream here (if UTF-8), this is a lot of messy work.
// For one, we don't know if the encoding is UTF-8 and finding this out is hard to do
@@ -62,9 +62,9 @@ internal class JsonHttpContentSerializer : HttpContentSerializer
return JsonSerializer.Deserialize(json, contentType, JsonSerializerOptions);
}
private HttpContent? SerializeUtf8(object? content, Type? contentType)
private HttpContent? SerializeUtf8(object? content, Type contentType)
{
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(content, contentType!, JsonSerializerOptions);
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(content, contentType, JsonSerializerOptions);
ByteArrayContent httpContent = new(bytes);
MediaTypeHeaderValue contentTypeHeader = new(MediaType.ApplicationJson)
{
@@ -75,9 +75,9 @@ internal class JsonHttpContentSerializer : HttpContentSerializer
return httpContent;
}
private HttpContent? SerializeOtherEncoding(object? content, Type? contentType, Encoding encoding)
private HttpContent? SerializeOtherEncoding(object? content, Type contentType, Encoding encoding)
{
string str = JsonSerializer.Serialize(content, contentType!, JsonSerializerOptions);
string str = JsonSerializer.Serialize(content, contentType, JsonSerializerOptions);
return new StringContent(str, encoding, MediaType.ApplicationJson);
}
}

View File

@@ -21,6 +21,7 @@ internal readonly struct NoContent : IEquatable<NoContent>
return obj is NoContent;
}
[SuppressMessage("", "SH002")]
public bool Equals(NoContent other)
{
return true;