mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-08 06:08:20 -05:00
* Update codebase to target .NET 8.0 and improve JSON serialization This commit switches our target framework from .NET 6.0 to .NET 8.0 and replaces Newtonsoft.Json with System.Text.Json for serialization. The JsonConverter classes have been updated to support the new JSON model and some enhancements were applied to the codebase such as fixing a command property and updating various package references. * Align with Develop * Update SharedLibraryCore package version The version of the SharedLibraryCore package reference has been updated across multiple projects from '2024.2.4.85' to '2024.2.5.9'. Meanwhile, version within SharedLibraryCore.csproj has been changed from '2024.02.04.085' to '2024.01.01.1'. Changes also include removal of .NET 8 requirement notice and reenabling of status upload to master communicator. * Update properties in IRConParser and IRConParserConfiguration to be settable The properties in the `IRConParser` and `IRConParserConfiguration` interfaces were updated to include setters. Previously, the properties in these interfaces were read-only. This change allows for the modifications and extensions of properties defined, thereby bolstering flexibility for the handling of games and parsers. * Replace RestEase with Refit in API usage Refit has been implemented as a replacement for RestEase in all API calls. As such, all related code, parameters and imports have been adjusted to function with Refit. Logic has also been added to handle certain Refit-specific behaviours. Occurrences of the RestEase package have been removed from the project. * Enable auto-redirect in HttpClient The HttpClient instance used in Application/Main.cs has been modified to automatically follow redirect responses. This was accomplished by adding "AllowAutoRedirect = true" to the HttpClientHandler used when creating the HttpClient. --------- Co-authored-by: Amos <amos2580@hotmail.co.uk>
177 lines
6.4 KiB
C#
177 lines
6.4 KiB
C#
using IW4MAdmin.Application.API.Master;
|
|
using SharedLibraryCore;
|
|
using SharedLibraryCore.Configuration;
|
|
using SharedLibraryCore.Helpers;
|
|
using SharedLibraryCore.Interfaces;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Refit;
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
{
|
|
/// <summary>
|
|
/// implementation of IMasterCommunication
|
|
/// talks to the master server
|
|
/// </summary>
|
|
class MasterCommunication : IMasterCommunication
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly ITranslationLookup _transLookup;
|
|
private readonly IMasterApi _apiInstance;
|
|
private readonly IManager _manager;
|
|
private readonly ApplicationConfiguration _appConfig;
|
|
private readonly BuildNumber _fallbackVersion = BuildNumber.Parse("99.99.99.99");
|
|
private readonly int _apiVersion = 1;
|
|
private bool _firstHeartBeat = true;
|
|
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(30);
|
|
private string _authorizationToken;
|
|
|
|
public MasterCommunication(ILogger<MasterCommunication> logger, ApplicationConfiguration appConfig, ITranslationLookup translationLookup, IMasterApi apiInstance, IManager manager)
|
|
{
|
|
_logger = logger;
|
|
_transLookup = translationLookup;
|
|
_apiInstance = apiInstance;
|
|
_appConfig = appConfig;
|
|
_manager = manager;
|
|
}
|
|
|
|
/// <summary>
|
|
/// checks for latest version of the application
|
|
/// notifies user if an update is available
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task CheckVersion()
|
|
{
|
|
var version = new VersionInfo()
|
|
{
|
|
CurrentVersionStable = _fallbackVersion
|
|
};
|
|
|
|
try
|
|
{
|
|
version = await _apiInstance.GetVersion(_apiVersion);
|
|
}
|
|
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogWarning(e, "Unable to retrieve IW4MAdmin version information");
|
|
}
|
|
|
|
if (version.CurrentVersionStable == _fallbackVersion)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine(_transLookup["MANAGER_VERSION_FAIL"]);
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
}
|
|
|
|
#if !PRERELEASE
|
|
else if (version.CurrentVersionStable > Program.Version)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
|
Console.WriteLine($"IW4MAdmin {_transLookup["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionStable.ToString()}]");
|
|
Console.WriteLine(_transLookup["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Program.Version.ToString()}]"));
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
}
|
|
#else
|
|
else if (version.CurrentVersionPrerelease > Program.Version)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
|
Console.WriteLine($"IW4MAdmin-Prerelease {_transLookup["MANAGER_VERSION_UPDATE"]} [v{version.CurrentVersionPrerelease.ToString()}-pr]");
|
|
Console.WriteLine(_transLookup["MANAGER_VERSION_CURRENT"].FormatExt($"[v{Program.Version.ToString()}-pr]"));
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
}
|
|
#endif
|
|
else
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.WriteLine(_transLookup["MANAGER_VERSION_SUCCESS"]);
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
}
|
|
}
|
|
|
|
public async Task RunUploadStatus(CancellationToken token)
|
|
{
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
if (_manager.IsRunning)
|
|
{
|
|
await UploadStatus();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning("Could not send heartbeat - {Message}", ex.Message);
|
|
}
|
|
|
|
try
|
|
{
|
|
await Task.Delay(Interval, token);
|
|
}
|
|
catch
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task UploadStatus()
|
|
{
|
|
if (_firstHeartBeat)
|
|
{
|
|
var token = await _apiInstance.Authenticate(new AuthenticationId
|
|
{
|
|
Id = _appConfig.Id
|
|
});
|
|
|
|
_authorizationToken = $"Bearer {token.AccessToken}";
|
|
}
|
|
|
|
var instance = new ApiInstance
|
|
{
|
|
Id = _appConfig.Id,
|
|
Uptime = (int)(DateTime.UtcNow - (_manager as ApplicationManager).StartTime).TotalSeconds,
|
|
Version = Program.Version,
|
|
Servers = _manager.GetServers().Select(s =>
|
|
new ApiServer()
|
|
{
|
|
ClientNum = s.ClientNum,
|
|
Game = s.GameName.ToString(),
|
|
Version = s.Version,
|
|
Gametype = s.Gametype,
|
|
Hostname = s.Hostname,
|
|
Map = s.CurrentMap.Name,
|
|
MaxClientNum = s.MaxClients,
|
|
Id = s.EndPoint,
|
|
Port = (short)s.ListenPort,
|
|
IPAddress = s.ListenAddress
|
|
}).ToList(),
|
|
WebfrontUrl = _appConfig.WebfrontUrl
|
|
};
|
|
|
|
IApiResponse<ResultMessage> response;
|
|
|
|
if (_firstHeartBeat)
|
|
{
|
|
response = await _apiInstance.AddInstance(instance, _authorizationToken);
|
|
}
|
|
|
|
else
|
|
{
|
|
response = await _apiInstance.UpdateInstance(instance.Id, instance, _authorizationToken);
|
|
_firstHeartBeat = false;
|
|
}
|
|
|
|
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
|
{
|
|
_logger.LogWarning("Non success response code from master is {StatusCode}, message is {Message}", response.StatusCode, response.Error?.Content);
|
|
}
|
|
}
|
|
}
|
|
}
|