Upgrade project to .NET 8

This commit is contained in:
Stone_Red
2023-12-14 19:00:47 +01:00
parent 9de227050a
commit 30ce61cc41
20 changed files with 53 additions and 137 deletions
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
+3 -3
View File
@@ -28,19 +28,19 @@ internal static class Program
if (File.Exists(ApiConfiguration.HostsFilePath)) if (File.Exists(ApiConfiguration.HostsFilePath))
{ {
string hostsJson = await File.ReadAllTextAsync(ApiConfiguration.HostsFilePath); string hostsJson = await File.ReadAllTextAsync(ApiConfiguration.HostsFilePath);
_ = apiManager.HostsManager.AddRange(JsonSerializer.Deserialize<List<NetworkSocket>>(hostsJson) ?? new()); _ = apiManager.HostsManager.AddRange(JsonSerializer.Deserialize<List<NetworkSocket>>(hostsJson) ?? []);
} }
if (File.Exists(ApiConfiguration.FilesInfoPath)) if (File.Exists(ApiConfiguration.FilesInfoPath))
{ {
string filesJson = await File.ReadAllTextAsync(ApiConfiguration.FilesInfoPath); string filesJson = await File.ReadAllTextAsync(ApiConfiguration.FilesInfoPath);
apiManager.FilesManager.AddRange(JsonSerializer.Deserialize<List<PrivateHyperFileInfo>>(filesJson) ?? new()); apiManager.FilesManager.AddRange(JsonSerializer.Deserialize<List<PrivateHyperFileInfo>>(filesJson) ?? []);
} }
if (File.Exists(ApiConfiguration.DirectoriesInfoPath)) if (File.Exists(ApiConfiguration.DirectoriesInfoPath))
{ {
string directoriesJson = await File.ReadAllTextAsync(ApiConfiguration.DirectoriesInfoPath); string directoriesJson = await File.ReadAllTextAsync(ApiConfiguration.DirectoriesInfoPath);
apiManager.DirectoryWatcher.AddRange(JsonSerializer.Deserialize<List<string>>(directoriesJson) ?? new()); apiManager.DirectoryWatcher.AddRange(JsonSerializer.Deserialize<List<string>>(directoriesJson) ?? []);
} }
Console.WriteLine($"HyperbolicDownloader - {Assembly.GetExecutingAssembly().GetName().Version}"); Console.WriteLine($"HyperbolicDownloader - {Assembly.GetExecutingAssembly().GetName().Version}");
@@ -3,15 +3,8 @@ using HyperbolicDownloaderApi.Managment;
namespace HyperbolicDownloaderApi.Commands; 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) public void AddDirectory(string directoryPath)
{ {
if (directoryWatcher.TryAdd(directoryPath, out string? message)) if (directoryWatcher.TryAdd(directoryPath, out string? message))
@@ -13,17 +13,8 @@ using System.Text.Json;
namespace HyperbolicDownloaderApi.Commands; 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) public void GetFileFrom(string path)
{ {
if (string.IsNullOrWhiteSpace(path)) if (string.IsNullOrWhiteSpace(path))
@@ -9,17 +9,9 @@ using System.Text.Json;
namespace HyperbolicDownloaderApi.Commands; namespace HyperbolicDownloaderApi.Commands;
public class FileCommands public class FileCommands(HostsManager hostsManager, FilesManager filesManager)
{ {
private readonly HostsManager hostsManager; private readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
private readonly FilesManager filesManager;
public FileCommands(HostsManager hostsManager, FilesManager filesManager)
{
this.hostsManager = hostsManager;
this.filesManager = filesManager;
}
public void AddFile(string path) public void AddFile(string path)
{ {
@@ -242,8 +234,7 @@ public class FileCommands
publicHyperFileInfo.Hosts.Add(localHost); publicHyperFileInfo.Hosts.Add(localHost);
JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(publicHyperFileInfo, jsonSerializerOptions);
string json = JsonSerializer.Serialize(publicHyperFileInfo, options);
File.WriteAllText(filePath, json); File.WriteAllText(filePath, json);
@@ -334,8 +325,7 @@ public class FileCommands
publicHyperFileInfo.Hosts.Add(localHost); publicHyperFileInfo.Hosts.Add(localHost);
JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(publicHyperFileInfo, jsonSerializerOptions);
string json = JsonSerializer.Serialize(publicHyperFileInfo, options);
File.WriteAllText(filePath, json); File.WriteAllText(filePath, json);
@@ -8,15 +8,8 @@ using System.Net.Sockets;
namespace HyperbolicDownloaderApi.Commands; 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 _) public void Discover(string _)
{ {
ApiManager.SendNotificationMessageNewLine("Running local discovery routine...", NotificationMessageType.Info); ApiManager.SendNotificationMessageNewLine("Running local discovery routine...", NotificationMessageType.Info);
@@ -9,7 +9,7 @@ public class DirectoryWatcher
{ {
private readonly FilesManager filesManager; private readonly FilesManager filesManager;
private readonly List<string> directories = new(); private readonly List<string> directories = [];
private readonly System.Timers.Timer timer = new(1); private readonly System.Timers.Timer timer = new(1);
public DirectoryWatcher(FilesManager filesManager) public DirectoryWatcher(FilesManager filesManager)
@@ -7,7 +7,7 @@ namespace HyperbolicDownloaderApi.FileProcessing;
public class FilesManager public class FilesManager
{ {
private readonly List<PrivateHyperFileInfo> files = new List<PrivateHyperFileInfo>(); private readonly List<PrivateHyperFileInfo> files = [];
public bool TryAdd(string filePath, out PrivateHyperFileInfo? fileInfo, out string? errorMessage) public bool TryAdd(string filePath, out PrivateHyperFileInfo? fileInfo, out string? errorMessage)
{ {
@@ -95,7 +95,7 @@ public class FilesManager
public List<PrivateHyperFileInfo> ToList() public List<PrivateHyperFileInfo> ToList()
{ {
return files.ToList(); return [.. files];
} }
internal void RemoveFilesThatDontExist() internal void RemoveFilesThatDontExist()
@@ -1,13 +1,7 @@
namespace HyperbolicDownloaderApi.FileProcessing; namespace HyperbolicDownloaderApi.FileProcessing;
public class PrivateHyperFileInfo public class PrivateHyperFileInfo(string hash, string filePath)
{ {
public string Hash { get; set; } public string Hash { get; set; } = hash;
public string FilePath { get; set; } public string FilePath { get; set; } = filePath;
public PrivateHyperFileInfo(string hash, string filePath)
{
Hash = hash;
FilePath = filePath;
}
} }
@@ -5,7 +5,7 @@ namespace HyperbolicDownloaderApi.FileProcessing;
public class PublicHyperFileInfo public class PublicHyperFileInfo
{ {
public string Hash { get; set; } = string.Empty; public string Hash { get; set; } = string.Empty;
public List<NetworkSocket> Hosts { get; set; } = new(); public List<NetworkSocket> Hosts { get; set; } = [];
public PublicHyperFileInfo() public PublicHyperFileInfo()
{ {
@@ -6,3 +6,4 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Major Code Smell", "S6561:Avoid using \"DateTime.Now\" for benchmarking or timing operations", Justification = "Stop")] [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")]
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
@@ -1,15 +1,9 @@
namespace HyperbolicDownloaderApi.Managment; namespace HyperbolicDownloaderApi.Managment;
public class NotificationMessageEventArgs : EventArgs public class NotificationMessageEventArgs(NotificationMessageType notificationMessageType, string? message) : EventArgs
{ {
public NotificationMessageType NotificationMessageType { get; } public NotificationMessageType NotificationMessageType { get; } = notificationMessageType;
public string? Message { get; } public string? Message { get; } = message;
public NotificationMessageEventArgs(NotificationMessageType notificationMessageType, string? message)
{
NotificationMessageType = notificationMessageType;
Message = message;
}
} }
public enum NotificationMessageType public enum NotificationMessageType
@@ -2,14 +2,8 @@
namespace HyperbolicDownloaderApi.Networking; namespace HyperbolicDownloaderApi.Networking;
internal class BroadcastRecivedEventArgs : EventArgs internal class BroadcastRecivedEventArgs(IPEndPoint ipEndPoint, string message) : EventArgs
{ {
public BroadcastRecivedEventArgs(IPEndPoint iPEndPoint, string message) public IPEndPoint IPEndPoint { get; } = ipEndPoint;
{ public string Message { get; } = message;
IPEndPoint = iPEndPoint;
Message = message;
}
public IPEndPoint IPEndPoint { get; }
public string Message { get; }
} }
@@ -1,13 +1,7 @@
namespace HyperbolicDownloaderApi.Networking; namespace HyperbolicDownloaderApi.Networking;
internal class DataContainer internal class DataContainer(string eventName, string jsonData)
{ {
public string EventName { get; set; } public string EventName { get; set; } = eventName;
public string JsonData { get; set; } public string JsonData { get; set; } = jsonData;
public DataContainer(string eventName, string jsonData)
{
JsonData = jsonData;
EventName = eventName;
}
} }
@@ -8,7 +8,7 @@ namespace HyperbolicDownloaderApi.Networking;
public class HostsManager public class HostsManager
{ {
private List<NetworkSocket> hosts = new List<NetworkSocket>(); private List<NetworkSocket> hosts = [];
public int Count => hosts.Count; public int Count => hosts.Count;
public int AddRange(IEnumerable<NetworkSocket> hosts) public int AddRange(IEnumerable<NetworkSocket> hosts)
@@ -48,7 +48,7 @@ public class HostsManager
public int CheckHostsActivity() public int CheckHostsActivity()
{ {
List<NetworkSocket> hostsToRemove = new List<NetworkSocket>(); List<NetworkSocket> hostsToRemove = [];
int activeHostsCount = 0; int activeHostsCount = 0;
foreach (NetworkSocket host in hosts) foreach (NetworkSocket host in hosts)
{ {
@@ -113,7 +113,7 @@ public class HostsManager
Console.CursorLeft = 0; Console.CursorLeft = 0;
if (sendTask.IsCompletedSuccessfully) if (sendTask.IsCompletedSuccessfully)
{ {
int newHosts = AddRange(sendTask.Result ?? new()); int newHosts = AddRange(sendTask.Result ?? []);
newHostsCount += newHosts; newHostsCount += newHosts;
if (newHosts > 0) if (newHosts > 0)
@@ -145,15 +145,14 @@ public class HostsManager
public List<NetworkSocket> ToList() public List<NetworkSocket> ToList()
{ {
return hosts.ToList(); return [.. hosts];
} }
public void SaveHosts() public void SaveHosts()
{ {
hosts = hosts hosts = [.. hosts
.OrderByDescending(s => s.DownloadSpeed) .OrderByDescending(s => s.DownloadSpeed)
.ThenByDescending(s => s.LastActive) .ThenByDescending(s => s.LastActive)];
.ToList();
File.WriteAllText(ApiConfiguration.HostsFilePath, JsonSerializer.Serialize(hosts)); File.WriteAllText(ApiConfiguration.HostsFilePath, JsonSerializer.Serialize(hosts));
} }
} }
@@ -5,20 +5,11 @@ using System.Text.Json;
namespace HyperbolicDownloaderApi.Networking; namespace HyperbolicDownloaderApi.Networking;
internal class MessageRecivedEventArgs<T> : EventArgs internal class MessageRecivedEventArgs<T>(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) public IPAddress IpAddress { get; set; } = ipAddress;
{
this.networkStream = networkStream;
Data = data;
IpAddress = ipAddress;
}
public T Data { get; set; }
public IPAddress IpAddress { get; set; }
public async Task SendResponseAsync(object response) public async Task SendResponseAsync(object response)
{ {
@@ -9,24 +9,15 @@ using System.Text.Json;
namespace HyperbolicDownloaderApi.Networking; namespace HyperbolicDownloaderApi.Networking;
internal class NetworkClient internal class NetworkClient(FilesManager filesManager)
{ {
private readonly FilesManager filesManager; private readonly Dictionary<string, (Type type, Delegate method)> events = [];
private readonly Dictionary<string, (Type type, Delegate method)> events = new();
private TcpListener? tcpListener; private TcpListener? tcpListener;
public bool IsListening { get; private set; } = false; public bool IsListening { get; private set; } = false;
public NetworkClient(FilesManager filesManager)
{
this.filesManager = filesManager;
}
public static async Task<T?> SendAsync<T>(IPAddress remoteIp, int remotePort, string eventName, object data) public static async Task<T?> SendAsync<T>(IPAddress remoteIp, int remotePort, string eventName, object data)
{ {
if (remoteIp is null) ArgumentNullException.ThrowIfNull(remoteIp);
{
throw new ArgumentNullException(nameof(remoteIp));
}
TcpClient client = new TcpClient(); TcpClient client = new TcpClient();
@@ -57,10 +48,7 @@ internal class NetworkClient
public static async Task SendAsync(IPAddress remoteIp, int remotePort, string eventName, object data) public static async Task SendAsync(IPAddress remoteIp, int remotePort, string eventName, object data)
{ {
if (remoteIp is null) ArgumentNullException.ThrowIfNull(remoteIp);
{
throw new ArgumentNullException(nameof(remoteIp));
}
TcpClient client = new TcpClient(); TcpClient client = new TcpClient();
@@ -125,9 +113,9 @@ internal class NetworkClient
DataContainer? dataContainer = JsonSerializer.Deserialize<DataContainer>(dataReceived); DataContainer? dataContainer = JsonSerializer.Deserialize<DataContainer>(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); Type eventArgsType = typeof(MessageRecivedEventArgs<>).MakeGenericType(type);
@@ -1,20 +1,13 @@
namespace HyperbolicDownloaderApi.Networking; namespace HyperbolicDownloaderApi.Networking;
public class NetworkSocket public class NetworkSocket(string ipAddress, int port, DateTime lastActive)
{ {
public string IPAddress { get; set; } public string IPAddress { get; set; } = ipAddress;
public int Port { get; set; } public int Port { get; set; } = port;
public DateTime LastActive { get; set; } public DateTime LastActive { get; set; } = lastActive;
public long DownloadSpeed { get; set; } 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) public override bool Equals(object? obj)
{ {
if (obj is not NetworkSocket networkSocket) if (obj is not NetworkSocket networkSocket)
@@ -1,9 +1,10 @@
namespace HyperbolicDownloaderApi.Utilities; namespace HyperbolicDownloaderApi.Utilities;
internal static class UnitFormatter internal static class UnitFormatter
{ {
public static string TransferRate(long bytesPerSecond) 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; decimal rate = bytesPerSecond * 8;
@@ -20,7 +21,7 @@ internal static class UnitFormatter
public static string FileSize(long bytes) 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; decimal rate = bytes;
@@ -34,4 +35,4 @@ internal static class UnitFormatter
return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B"; return $"{Math.Round(rate, 0, MidpointRounding.AwayFromZero)}{ordinals[ordinal]}B";
} }
} }