Files
NebulaLauncher/Nebula.Shared/Services/HubService.cs

90 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-01-05 17:05:23 +03:00
using Nebula.Shared.Models;
using Nebula.Shared.Services.Logging;
2024-12-22 16:38:47 +03:00
2025-01-05 17:05:23 +03:00
namespace Nebula.Shared.Services;
2024-12-22 16:38:47 +03:00
[ServiceRegister]
public class HubService
{
2024-12-27 19:15:33 +03:00
private readonly ConfigurationService _configurationService;
2024-12-22 16:38:47 +03:00
private readonly RestService _restService;
private readonly ILogger _logger;
2024-12-22 16:38:47 +03:00
2025-01-14 22:10:16 +03:00
private readonly List<ServerHubInfo> _serverList = new();
2024-12-22 16:38:47 +03:00
public Action<HubServerChangedEventArgs>? HubServerChangedEventArgs;
2025-01-11 20:39:58 +03:00
public Action? HubServerLoaded;
2025-02-02 10:57:29 +03:00
public Action<Exception>? HubServerLoadingError;
2025-01-12 15:15:01 +03:00
2025-05-03 18:20:24 +03:00
public HubService(ConfigurationService configurationService, RestService restService, DebugService debugService)
2024-12-22 16:38:47 +03:00
{
2024-12-27 19:15:33 +03:00
_configurationService = configurationService;
2024-12-22 16:38:47 +03:00
_restService = restService;
_logger = debugService.GetLogger(this);
2025-01-14 22:10:16 +03:00
2024-12-27 19:15:33 +03:00
UpdateHub();
2024-12-22 16:38:47 +03:00
}
2025-01-14 22:10:16 +03:00
public IReadOnlyList<ServerHubInfo> ServerList => _serverList;
public bool IsUpdating { get; private set; }
2024-12-27 19:15:33 +03:00
public async void UpdateHub()
2024-12-22 16:38:47 +03:00
{
2025-01-14 22:10:16 +03:00
if (IsUpdating) return;
2025-01-12 15:15:01 +03:00
_serverList.Clear();
2024-12-22 16:38:47 +03:00
2025-01-14 22:10:16 +03:00
IsUpdating = true;
2024-12-27 19:15:33 +03:00
HubServerChangedEventArgs?.Invoke(new HubServerChangedEventArgs([], HubServerChangeAction.Clear));
2025-01-14 22:10:16 +03:00
2024-12-27 19:15:33 +03:00
foreach (var urlStr in _configurationService.GetConfigValue(CurrentConVar.Hub)!)
2024-12-22 16:38:47 +03:00
{
2025-05-03 18:20:24 +03:00
var invoked = false;
Exception? exception = null;
foreach (var uri in urlStr)
2025-02-02 10:57:29 +03:00
{
2025-05-03 18:20:24 +03:00
try
{
var servers =
await _restService.GetAsync<List<ServerHubInfo>>(new Uri(uri), CancellationToken.None);
_serverList.AddRange(servers);
HubServerChangedEventArgs?.Invoke(new HubServerChangedEventArgs(servers, HubServerChangeAction.Add));
invoked = true;
break;
}
catch (Exception e)
{
_logger.Error($"Failed to get servers for {uri}");
_logger.Error(e);
2025-05-03 18:20:24 +03:00
exception = e;
}
2025-02-02 10:57:29 +03:00
}
2025-05-03 18:20:24 +03:00
if(exception is not null && !invoked)
HubServerLoadingError?.Invoke(new Exception("No hub is available.", exception));
2024-12-22 16:38:47 +03:00
}
2025-01-14 22:10:16 +03:00
IsUpdating = false;
2025-01-11 20:39:58 +03:00
HubServerLoaded?.Invoke();
2024-12-22 16:38:47 +03:00
}
}
public class HubServerChangedEventArgs : EventArgs
{
public HubServerChangeAction Action;
2024-12-22 21:38:19 +03:00
public List<ServerHubInfo> Items;
2024-12-22 16:38:47 +03:00
2024-12-22 21:38:19 +03:00
public HubServerChangedEventArgs(List<ServerHubInfo> items, HubServerChangeAction action)
2024-12-22 16:38:47 +03:00
{
Items = items;
Action = action;
}
}
public enum HubServerChangeAction
{
2025-01-14 22:10:16 +03:00
Add,
Remove,
Clear
2024-12-22 16:38:47 +03:00
}