From 30ce61cc419cb61ca8f5293f5eb7faa84fc388fc Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 14 Dec 2023 19:00:47 +0100 Subject: [PATCH] Upgrade project to .NET 8 --- .../HyperbolicDownloader.csproj | 2 +- HyperbolicDownloader/Program.cs | 6 ++--- .../Commands/DirectoryCommands.cs | 9 +------ .../Commands/DownloadCommands.cs | 11 +-------- .../Commands/FileCommands.cs | 18 ++++---------- .../Commands/HostCommands.cs | 9 +------ .../FileProcessing/DirectoryWatcher.cs | 2 +- .../FileProcessing/FilesManager.cs | 4 ++-- .../FileProcessing/PrivateHyperFileInfo.cs | 12 +++------- .../FileProcessing/PublicHyperFileInfo.cs | 2 +- HyperbolicDownloaderApi/GlobalSuppressions.cs | 1 + .../HyperbolicDownloaderApi.csproj | 2 +- .../Managment/NotificationMessageEventArgs.cs | 12 +++------- .../Networking/BroadcastRecivedEventArgs.cs | 12 +++------- .../Networking/DataContainer.cs | 12 +++------- .../Networking/HostsManager.cs | 15 ++++++------ .../Networking/MessageRecivedEventArgs.cs | 15 +++--------- .../Networking/NetworkClient.cs | 24 +++++-------------- .../Networking/NetworkSocket.cs | 15 ++++-------- .../Utilities/UnitFormatter.cs | 7 +++--- 20 files changed, 53 insertions(+), 137 deletions(-) diff --git a/HyperbolicDownloader/HyperbolicDownloader.csproj b/HyperbolicDownloader/HyperbolicDownloader.csproj index 27e6095..08aa8d4 100644 --- a/HyperbolicDownloader/HyperbolicDownloader.csproj +++ b/HyperbolicDownloader/HyperbolicDownloader.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 enable enable diff --git a/HyperbolicDownloader/Program.cs b/HyperbolicDownloader/Program.cs index f5b2855..5e288c2 100644 --- a/HyperbolicDownloader/Program.cs +++ b/HyperbolicDownloader/Program.cs @@ -28,19 +28,19 @@ internal static class Program if (File.Exists(ApiConfiguration.HostsFilePath)) { string hostsJson = await File.ReadAllTextAsync(ApiConfiguration.HostsFilePath); - _ = apiManager.HostsManager.AddRange(JsonSerializer.Deserialize>(hostsJson) ?? new()); + _ = apiManager.HostsManager.AddRange(JsonSerializer.Deserialize>(hostsJson) ?? []); } if (File.Exists(ApiConfiguration.FilesInfoPath)) { string filesJson = await File.ReadAllTextAsync(ApiConfiguration.FilesInfoPath); - apiManager.FilesManager.AddRange(JsonSerializer.Deserialize>(filesJson) ?? new()); + apiManager.FilesManager.AddRange(JsonSerializer.Deserialize>(filesJson) ?? []); } if (File.Exists(ApiConfiguration.DirectoriesInfoPath)) { string directoriesJson = await File.ReadAllTextAsync(ApiConfiguration.DirectoriesInfoPath); - apiManager.DirectoryWatcher.AddRange(JsonSerializer.Deserialize>(directoriesJson) ?? new()); + apiManager.DirectoryWatcher.AddRange(JsonSerializer.Deserialize>(directoriesJson) ?? []); } Console.WriteLine($"HyperbolicDownloader - {Assembly.GetExecutingAssembly().GetName().Version}"); diff --git a/HyperbolicDownloaderApi/Commands/DirectoryCommands.cs b/HyperbolicDownloaderApi/Commands/DirectoryCommands.cs index 8531381..6b0e385 100644 --- a/HyperbolicDownloaderApi/Commands/DirectoryCommands.cs +++ b/HyperbolicDownloaderApi/Commands/DirectoryCommands.cs @@ -3,15 +3,8 @@ using HyperbolicDownloaderApi.Managment; namespace HyperbolicDownloaderApi.Commands; -public class DirectoryCommands +public class DirectoryCommands(DirectoryWatcher directoryWatcher) { - private readonly DirectoryWatcher directoryWatcher; - - public DirectoryCommands(DirectoryWatcher directoryWatcher) - { - this.directoryWatcher = directoryWatcher; - } - public void AddDirectory(string directoryPath) { if (directoryWatcher.TryAdd(directoryPath, out string? message)) diff --git a/HyperbolicDownloaderApi/Commands/DownloadCommands.cs b/HyperbolicDownloaderApi/Commands/DownloadCommands.cs index c5027c6..76b8fd5 100644 --- a/HyperbolicDownloaderApi/Commands/DownloadCommands.cs +++ b/HyperbolicDownloaderApi/Commands/DownloadCommands.cs @@ -13,17 +13,8 @@ using System.Text.Json; namespace HyperbolicDownloaderApi.Commands; -public class DownloadCommands +public class DownloadCommands(HostsManager hostsManager, FilesManager filesManager) { - private readonly HostsManager hostsManager; - private readonly FilesManager filesManager; - - public DownloadCommands(HostsManager hostsManager, FilesManager filesManager) - { - this.hostsManager = hostsManager; - this.filesManager = filesManager; - } - public void GetFileFrom(string path) { if (string.IsNullOrWhiteSpace(path)) diff --git a/HyperbolicDownloaderApi/Commands/FileCommands.cs b/HyperbolicDownloaderApi/Commands/FileCommands.cs index c6c8fc0..76d620d 100644 --- a/HyperbolicDownloaderApi/Commands/FileCommands.cs +++ b/HyperbolicDownloaderApi/Commands/FileCommands.cs @@ -9,17 +9,9 @@ using System.Text.Json; namespace HyperbolicDownloaderApi.Commands; -public class FileCommands +public class FileCommands(HostsManager hostsManager, FilesManager filesManager) { - private readonly HostsManager hostsManager; - - private readonly FilesManager filesManager; - - public FileCommands(HostsManager hostsManager, FilesManager filesManager) - { - this.hostsManager = hostsManager; - this.filesManager = filesManager; - } + private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true }; public void AddFile(string path) { @@ -242,8 +234,7 @@ public class FileCommands publicHyperFileInfo.Hosts.Add(localHost); - JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; - string json = JsonSerializer.Serialize(publicHyperFileInfo, options); + string json = JsonSerializer.Serialize(publicHyperFileInfo, jsonSerializerOptions); File.WriteAllText(filePath, json); @@ -334,8 +325,7 @@ public class FileCommands publicHyperFileInfo.Hosts.Add(localHost); - JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; - string json = JsonSerializer.Serialize(publicHyperFileInfo, options); + string json = JsonSerializer.Serialize(publicHyperFileInfo, jsonSerializerOptions); File.WriteAllText(filePath, json); diff --git a/HyperbolicDownloaderApi/Commands/HostCommands.cs b/HyperbolicDownloaderApi/Commands/HostCommands.cs index e59e3a1..2a66bd3 100644 --- a/HyperbolicDownloaderApi/Commands/HostCommands.cs +++ b/HyperbolicDownloaderApi/Commands/HostCommands.cs @@ -8,15 +8,8 @@ using System.Net.Sockets; namespace HyperbolicDownloaderApi.Commands; -public class HostCommands +public class HostCommands(HostsManager hostsManager) { - private readonly HostsManager hostsManager; - - public HostCommands(HostsManager hostsManager) - { - this.hostsManager = hostsManager; - } - public void Discover(string _) { ApiManager.SendNotificationMessageNewLine("Running local discovery routine...", NotificationMessageType.Info); diff --git a/HyperbolicDownloaderApi/FileProcessing/DirectoryWatcher.cs b/HyperbolicDownloaderApi/FileProcessing/DirectoryWatcher.cs index 76bad51..98e47be 100644 --- a/HyperbolicDownloaderApi/FileProcessing/DirectoryWatcher.cs +++ b/HyperbolicDownloaderApi/FileProcessing/DirectoryWatcher.cs @@ -9,7 +9,7 @@ public class DirectoryWatcher { private readonly FilesManager filesManager; - private readonly List directories = new(); + private readonly List directories = []; private readonly System.Timers.Timer timer = new(1); public DirectoryWatcher(FilesManager filesManager) diff --git a/HyperbolicDownloaderApi/FileProcessing/FilesManager.cs b/HyperbolicDownloaderApi/FileProcessing/FilesManager.cs index 93d37f7..a6f64b9 100644 --- a/HyperbolicDownloaderApi/FileProcessing/FilesManager.cs +++ b/HyperbolicDownloaderApi/FileProcessing/FilesManager.cs @@ -7,7 +7,7 @@ namespace HyperbolicDownloaderApi.FileProcessing; public class FilesManager { - private readonly List files = new List(); + private readonly List files = []; public bool TryAdd(string filePath, out PrivateHyperFileInfo? fileInfo, out string? errorMessage) { @@ -95,7 +95,7 @@ public class FilesManager public List ToList() { - return files.ToList(); + return [.. files]; } internal void RemoveFilesThatDontExist() diff --git a/HyperbolicDownloaderApi/FileProcessing/PrivateHyperFileInfo.cs b/HyperbolicDownloaderApi/FileProcessing/PrivateHyperFileInfo.cs index 6acf9f5..fe88ce1 100644 --- a/HyperbolicDownloaderApi/FileProcessing/PrivateHyperFileInfo.cs +++ b/HyperbolicDownloaderApi/FileProcessing/PrivateHyperFileInfo.cs @@ -1,13 +1,7 @@ namespace HyperbolicDownloaderApi.FileProcessing; -public class PrivateHyperFileInfo +public class PrivateHyperFileInfo(string hash, string filePath) { - public string Hash { get; set; } - public string FilePath { get; set; } - - public PrivateHyperFileInfo(string hash, string filePath) - { - Hash = hash; - FilePath = filePath; - } + public string Hash { get; set; } = hash; + public string FilePath { get; set; } = filePath; } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/FileProcessing/PublicHyperFileInfo.cs b/HyperbolicDownloaderApi/FileProcessing/PublicHyperFileInfo.cs index 330ba46..ad997c3 100644 --- a/HyperbolicDownloaderApi/FileProcessing/PublicHyperFileInfo.cs +++ b/HyperbolicDownloaderApi/FileProcessing/PublicHyperFileInfo.cs @@ -5,7 +5,7 @@ namespace HyperbolicDownloaderApi.FileProcessing; public class PublicHyperFileInfo { public string Hash { get; set; } = string.Empty; - public List Hosts { get; set; } = new(); + public List Hosts { get; set; } = []; public PublicHyperFileInfo() { diff --git a/HyperbolicDownloaderApi/GlobalSuppressions.cs b/HyperbolicDownloaderApi/GlobalSuppressions.cs index 3733cf6..377154e 100644 --- a/HyperbolicDownloaderApi/GlobalSuppressions.cs +++ b/HyperbolicDownloaderApi/GlobalSuppressions.cs @@ -6,3 +6,4 @@ using System.Diagnostics.CodeAnalysis; [assembly: SuppressMessage("Major Code Smell", "S6561:Avoid using \"DateTime.Now\" for benchmarking or timing operations", Justification = "Stop")] +[assembly: SuppressMessage("Minor Code Smell", "S3604:Member initializer values should not be redundant", Justification = "False positive")] \ No newline at end of file diff --git a/HyperbolicDownloaderApi/HyperbolicDownloaderApi.csproj b/HyperbolicDownloaderApi/HyperbolicDownloaderApi.csproj index 957ae0e..2367984 100644 --- a/HyperbolicDownloaderApi/HyperbolicDownloaderApi.csproj +++ b/HyperbolicDownloaderApi/HyperbolicDownloaderApi.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs index 0f0aad5..ad87cf4 100644 --- a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs +++ b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs @@ -1,15 +1,9 @@ namespace HyperbolicDownloaderApi.Managment; -public class NotificationMessageEventArgs : EventArgs +public class NotificationMessageEventArgs(NotificationMessageType notificationMessageType, string? message) : EventArgs { - public NotificationMessageType NotificationMessageType { get; } - public string? Message { get; } - - public NotificationMessageEventArgs(NotificationMessageType notificationMessageType, string? message) - { - NotificationMessageType = notificationMessageType; - Message = message; - } + public NotificationMessageType NotificationMessageType { get; } = notificationMessageType; + public string? Message { get; } = message; } public enum NotificationMessageType diff --git a/HyperbolicDownloaderApi/Networking/BroadcastRecivedEventArgs.cs b/HyperbolicDownloaderApi/Networking/BroadcastRecivedEventArgs.cs index 10fa0b1..e940580 100644 --- a/HyperbolicDownloaderApi/Networking/BroadcastRecivedEventArgs.cs +++ b/HyperbolicDownloaderApi/Networking/BroadcastRecivedEventArgs.cs @@ -2,14 +2,8 @@ namespace HyperbolicDownloaderApi.Networking; -internal class BroadcastRecivedEventArgs : EventArgs +internal class BroadcastRecivedEventArgs(IPEndPoint ipEndPoint, string message) : EventArgs { - public BroadcastRecivedEventArgs(IPEndPoint iPEndPoint, string message) - { - IPEndPoint = iPEndPoint; - Message = message; - } - - public IPEndPoint IPEndPoint { get; } - public string Message { get; } + public IPEndPoint IPEndPoint { get; } = ipEndPoint; + public string Message { get; } = message; } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Networking/DataContainer.cs b/HyperbolicDownloaderApi/Networking/DataContainer.cs index 42ef91f..ef526a3 100644 --- a/HyperbolicDownloaderApi/Networking/DataContainer.cs +++ b/HyperbolicDownloaderApi/Networking/DataContainer.cs @@ -1,13 +1,7 @@ namespace HyperbolicDownloaderApi.Networking; -internal class DataContainer +internal class DataContainer(string eventName, string jsonData) { - public string EventName { get; set; } - public string JsonData { get; set; } - - public DataContainer(string eventName, string jsonData) - { - JsonData = jsonData; - EventName = eventName; - } + public string EventName { get; set; } = eventName; + public string JsonData { get; set; } = jsonData; } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Networking/HostsManager.cs b/HyperbolicDownloaderApi/Networking/HostsManager.cs index 8f28e99..a8aa23d 100644 --- a/HyperbolicDownloaderApi/Networking/HostsManager.cs +++ b/HyperbolicDownloaderApi/Networking/HostsManager.cs @@ -8,7 +8,7 @@ namespace HyperbolicDownloaderApi.Networking; public class HostsManager { - private List hosts = new List(); + private List hosts = []; public int Count => hosts.Count; public int AddRange(IEnumerable hosts) @@ -48,7 +48,7 @@ public class HostsManager public int CheckHostsActivity() { - List hostsToRemove = new List(); + List hostsToRemove = []; int activeHostsCount = 0; foreach (NetworkSocket host in hosts) { @@ -113,7 +113,7 @@ public class HostsManager Console.CursorLeft = 0; if (sendTask.IsCompletedSuccessfully) { - int newHosts = AddRange(sendTask.Result ?? new()); + int newHosts = AddRange(sendTask.Result ?? []); newHostsCount += newHosts; if (newHosts > 0) @@ -145,15 +145,14 @@ public class HostsManager public List ToList() { - return hosts.ToList(); + return [.. hosts]; } public void SaveHosts() { - hosts = hosts - .OrderByDescending(s => s.DownloadSpeed) - .ThenByDescending(s => s.LastActive) - .ToList(); + hosts = [.. hosts + .OrderByDescending(s => s.DownloadSpeed) + .ThenByDescending(s => s.LastActive)]; File.WriteAllText(ApiConfiguration.HostsFilePath, JsonSerializer.Serialize(hosts)); } } \ No newline at end of file diff --git a/HyperbolicDownloaderApi/Networking/MessageRecivedEventArgs.cs b/HyperbolicDownloaderApi/Networking/MessageRecivedEventArgs.cs index 299bc5a..ff415a1 100644 --- a/HyperbolicDownloaderApi/Networking/MessageRecivedEventArgs.cs +++ b/HyperbolicDownloaderApi/Networking/MessageRecivedEventArgs.cs @@ -5,20 +5,11 @@ using System.Text.Json; namespace HyperbolicDownloaderApi.Networking; -internal class MessageRecivedEventArgs : EventArgs +internal class MessageRecivedEventArgs(NetworkStream networkStream, IPAddress ipAddress, T data) : EventArgs { - private readonly NetworkStream networkStream; + public T Data { get; set; } = data; - public MessageRecivedEventArgs(NetworkStream networkStream, IPAddress ipAddress, T data) - { - this.networkStream = networkStream; - Data = data; - IpAddress = ipAddress; - } - - public T Data { get; set; } - - public IPAddress IpAddress { get; set; } + public IPAddress IpAddress { get; set; } = ipAddress; public async Task SendResponseAsync(object response) { diff --git a/HyperbolicDownloaderApi/Networking/NetworkClient.cs b/HyperbolicDownloaderApi/Networking/NetworkClient.cs index b22285a..b53ca82 100644 --- a/HyperbolicDownloaderApi/Networking/NetworkClient.cs +++ b/HyperbolicDownloaderApi/Networking/NetworkClient.cs @@ -9,24 +9,15 @@ using System.Text.Json; namespace HyperbolicDownloaderApi.Networking; -internal class NetworkClient +internal class NetworkClient(FilesManager filesManager) { - private readonly FilesManager filesManager; - private readonly Dictionary events = new(); + private readonly Dictionary events = []; private TcpListener? tcpListener; public bool IsListening { get; private set; } = false; - public NetworkClient(FilesManager filesManager) - { - this.filesManager = filesManager; - } - public static async Task SendAsync(IPAddress remoteIp, int remotePort, string eventName, object data) { - if (remoteIp is null) - { - throw new ArgumentNullException(nameof(remoteIp)); - } + ArgumentNullException.ThrowIfNull(remoteIp); TcpClient client = new TcpClient(); @@ -57,10 +48,7 @@ internal class NetworkClient public static async Task SendAsync(IPAddress remoteIp, int remotePort, string eventName, object data) { - if (remoteIp is null) - { - throw new ArgumentNullException(nameof(remoteIp)); - } + ArgumentNullException.ThrowIfNull(remoteIp); TcpClient client = new TcpClient(); @@ -125,9 +113,9 @@ internal class NetworkClient DataContainer? dataContainer = JsonSerializer.Deserialize(dataReceived); - if (dataContainer is not null && events.ContainsKey(dataContainer.EventName)) + if (dataContainer is not null && events.TryGetValue(dataContainer.EventName, out (Type type, Delegate method) value)) { - (Type type, Delegate method) = events[dataContainer.EventName]; + (Type type, Delegate method) = value; Type eventArgsType = typeof(MessageRecivedEventArgs<>).MakeGenericType(type); diff --git a/HyperbolicDownloaderApi/Networking/NetworkSocket.cs b/HyperbolicDownloaderApi/Networking/NetworkSocket.cs index bb23e14..58a1986 100644 --- a/HyperbolicDownloaderApi/Networking/NetworkSocket.cs +++ b/HyperbolicDownloaderApi/Networking/NetworkSocket.cs @@ -1,20 +1,13 @@ namespace HyperbolicDownloaderApi.Networking; -public class NetworkSocket +public class NetworkSocket(string ipAddress, int port, DateTime lastActive) { - public string IPAddress { get; set; } - public int Port { get; set; } - public DateTime LastActive { get; set; } + public string IPAddress { get; set; } = ipAddress; + public int Port { get; set; } = port; + public DateTime LastActive { get; set; } = lastActive; public long DownloadSpeed { get; set; } - public NetworkSocket(string ipAddress, int port, DateTime lastActive) - { - IPAddress = ipAddress; - Port = port; - LastActive = lastActive; - } - public override bool Equals(object? obj) { if (obj is not NetworkSocket networkSocket) diff --git a/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs b/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs index 7e5beb9..b76f0f5 100644 --- a/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs +++ b/HyperbolicDownloaderApi/Utilities/UnitFormatter.cs @@ -1,9 +1,10 @@ namespace HyperbolicDownloaderApi.Utilities; + internal static class UnitFormatter { public static string TransferRate(long bytesPerSecond) { - string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" }; + string[] ordinals = ["", "K", "M", "G", "T", "P", "E"]; decimal rate = bytesPerSecond * 8; @@ -20,7 +21,7 @@ internal static class UnitFormatter public static string FileSize(long bytes) { - string[] ordinals = new[] { "", "K", "M", "G", "T", "P", "E" }; + string[] ordinals = ["", "K", "M", "G", "T", "P", "E"]; decimal rate = bytes; @@ -34,4 +35,4 @@ internal static class UnitFormatter return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B"; } -} +} \ No newline at end of file