mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-04 00:56:09 +02:00
Upgrade project to .NET 8
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -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<List<NetworkSocket>>(hostsJson) ?? new());
|
||||
_ = apiManager.HostsManager.AddRange(JsonSerializer.Deserialize<List<NetworkSocket>>(hostsJson) ?? []);
|
||||
}
|
||||
|
||||
if (File.Exists(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))
|
||||
{
|
||||
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}");
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -9,7 +9,7 @@ public class DirectoryWatcher
|
||||
{
|
||||
private readonly FilesManager filesManager;
|
||||
|
||||
private readonly List<string> directories = new();
|
||||
private readonly List<string> directories = [];
|
||||
private readonly System.Timers.Timer timer = new(1);
|
||||
|
||||
public DirectoryWatcher(FilesManager filesManager)
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace HyperbolicDownloaderApi.FileProcessing;
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -95,7 +95,7 @@ public class FilesManager
|
||||
|
||||
public List<PrivateHyperFileInfo> ToList()
|
||||
{
|
||||
return files.ToList();
|
||||
return [.. files];
|
||||
}
|
||||
|
||||
internal void RemoveFilesThatDontExist()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace HyperbolicDownloaderApi.FileProcessing;
|
||||
public class PublicHyperFileInfo
|
||||
{
|
||||
public string Hash { get; set; } = string.Empty;
|
||||
public List<NetworkSocket> Hosts { get; set; } = new();
|
||||
public List<NetworkSocket> Hosts { get; set; } = [];
|
||||
|
||||
public PublicHyperFileInfo()
|
||||
{
|
||||
|
||||
@@ -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")]
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace HyperbolicDownloaderApi.Networking;
|
||||
|
||||
public class HostsManager
|
||||
{
|
||||
private List<NetworkSocket> hosts = new List<NetworkSocket>();
|
||||
private List<NetworkSocket> hosts = [];
|
||||
public int Count => hosts.Count;
|
||||
|
||||
public int AddRange(IEnumerable<NetworkSocket> hosts)
|
||||
@@ -48,7 +48,7 @@ public class HostsManager
|
||||
|
||||
public int CheckHostsActivity()
|
||||
{
|
||||
List<NetworkSocket> hostsToRemove = new List<NetworkSocket>();
|
||||
List<NetworkSocket> 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<NetworkSocket> 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));
|
||||
}
|
||||
}
|
||||
@@ -5,20 +5,11 @@ using System.Text.Json;
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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<string, (Type type, Delegate method)> events = new();
|
||||
private readonly Dictionary<string, (Type type, Delegate method)> events = [];
|
||||
private TcpListener? tcpListener;
|
||||
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)
|
||||
{
|
||||
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<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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user