From 63f605ca115715ee36262233cfe6eea58bbbc4aa Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Sun, 9 May 2021 22:50:12 +0800 Subject: [PATCH] Fix CircleImageStream --- src/Common.CoreLib/IOPath.cs | 7 ++++-- .../Models/ImageClipStream.cs | 25 ++++++++++++++----- .../Services/Mvvm/SteamConnectService.cs | 4 +-- .../Services/Mvvm/UserService.cs | 2 +- .../NotNullIfNotNullAttribute.cs | 10 ++++++++ 5 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 src/ST.Tools.Win7Troubleshoot/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs diff --git a/src/Common.CoreLib/IOPath.cs b/src/Common.CoreLib/IOPath.cs index fde8018e2..a814838b3 100644 --- a/src/Common.CoreLib/IOPath.cs +++ b/src/Common.CoreLib/IOPath.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Diagnostics.CodeAnalysis; +using System.IO; #if !NET35 && !NOT_XE using Xamarin.Essentials; #endif @@ -219,8 +220,10 @@ namespace System return ioPath; } - public static FileStream OpenRead(string filePath) + [return: NotNullIfNotNull("filePath")] + public static FileStream? OpenRead(string? filePath) { + if (filePath == null) return null; var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); return fileStream; } diff --git a/src/ST.Client.Desktop/Models/ImageClipStream.cs b/src/ST.Client.Desktop/Models/ImageClipStream.cs index 63a94cdd7..7963d71dc 100644 --- a/src/ST.Client.Desktop/Models/ImageClipStream.cs +++ b/src/ST.Client.Desktop/Models/ImageClipStream.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.IO; namespace System.Application.Models @@ -60,16 +61,28 @@ namespace System.Application.Models public class CircleImageStream : ImageClipStream { - public CircleImageStream(Stream stream) : base(stream) - { - Circle = true; - } - public CircleImageStream(string path) : base(IOPath.OpenRead(path)) + private CircleImageStream(Stream stream) : base(stream) { Circle = true; } - public static implicit operator CircleImageStream?(Stream? stream) + private CircleImageStream(string filePath) : base(IOPath.OpenRead(filePath)) + { + Circle = true; + } + + [return: NotNullIfNotNull("stream")] + public static CircleImageStream? Convert(Stream? stream) => stream == null ? null : new(stream); + + [return: NotNullIfNotNull("stream")] + public static implicit operator CircleImageStream?(Stream? stream) => Convert(stream); + + [return: NotNullIfNotNull("filePath")] + public static CircleImageStream? Convert(string? filePath) + => filePath == null ? null : new(filePath); + + [return: NotNullIfNotNull("filePath")] + public static implicit operator CircleImageStream?(string? filePath) => Convert(filePath); } } \ No newline at end of file diff --git a/src/ST.Client.Desktop/Services/Mvvm/SteamConnectService.cs b/src/ST.Client.Desktop/Services/Mvvm/SteamConnectService.cs index 45a8189dd..52dbea353 100644 --- a/src/ST.Client.Desktop/Services/Mvvm/SteamConnectService.cs +++ b/src/ST.Client.Desktop/Services/Mvvm/SteamConnectService.cs @@ -160,9 +160,7 @@ namespace System.Application.Services IsConnectToSteam = true; CurrentSteamUser = await SteamworksWebApiService.GetUserInfo(id); CurrentSteamUser.AvatarStream = IHttpService.Instance.GetImageAsync(CurrentSteamUser.AvatarFull, ImageChannelType.SteamAvatars); - var avater = await CurrentSteamUser.AvatarStream; - if (avater != null) - AvaterPath = new CircleImageStream(avater); + AvaterPath = CircleImageStream.Convert(await CurrentSteamUser.AvatarStream); CurrentSteamUser.IPCountry = ApiService.GetIPCountry(); IsSteamChinaLauncher = ApiService.IsSteamChinaLauncher(); diff --git a/src/ST.Client.Desktop/Services/Mvvm/UserService.cs b/src/ST.Client.Desktop/Services/Mvvm/UserService.cs index 8dc2005b6..309d7d338 100644 --- a/src/ST.Client.Desktop/Services/Mvvm/UserService.cs +++ b/src/ST.Client.Desktop/Services/Mvvm/UserService.cs @@ -133,7 +133,7 @@ namespace System.Application.Services { CurrentSteamUser = await ISteamworksWebApiService.Instance.GetUserInfo(User.SteamAccountId.Value); CurrentSteamUser.AvatarStream = IHttpService.Instance.GetImageAsync(CurrentSteamUser.AvatarFull, ImageChannelType.SteamAvatars); - AvaterPath = new CircleImageStream(await CurrentSteamUser.AvatarStream); + AvaterPath = CircleImageStream.Convert(await CurrentSteamUser.AvatarStream); } //AvaterPath = GetAvaterPath(User); diff --git a/src/ST.Tools.Win7Troubleshoot/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs b/src/ST.Tools.Win7Troubleshoot/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs new file mode 100644 index 000000000..6f783ee86 --- /dev/null +++ b/src/ST.Tools.Win7Troubleshoot/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs @@ -0,0 +1,10 @@ +namespace System.Diagnostics.CodeAnalysis +{ + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] + public sealed class NotNullIfNotNullAttribute : Attribute + { + public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; + + public string ParameterName { get; } + } +} \ No newline at end of file