phase 1
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using ModVersionChecker.data.model;
|
||||
using ModVersionChecker.model;
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.utils;
|
||||
using NuGet.Versioning;
|
||||
using ModVersionChecker.service.interfaces;
|
||||
|
||||
|
||||
namespace ModVersionChecker
|
||||
@@ -9,46 +8,33 @@ namespace ModVersionChecker
|
||||
public class VersionChecker
|
||||
{
|
||||
private readonly IConfigManager _configManager;
|
||||
private readonly IAppsManager _appsManager;
|
||||
private readonly ISourcesDefManager _sourcesDefManager;
|
||||
private readonly INotifyIconService _notifyIconService;
|
||||
private readonly IFlightSimsManager _fsManager;
|
||||
private List<string> errorMessages = new List<string>();
|
||||
private List<string> updateMessages = new List<string>();
|
||||
private NotifyIcon? _notifyIcon;
|
||||
private readonly IVersionService _versionService;
|
||||
|
||||
public event EventHandler<string>? OnFinished;
|
||||
|
||||
public VersionChecker(
|
||||
IConfigManager configManager,
|
||||
IAppsManager appsManager,
|
||||
ISourcesDefManager sourcesDefManager,
|
||||
INotifyIconService notifyIconService,
|
||||
IFlightSimsManager fsManager)
|
||||
IVersionService versionService,
|
||||
IConfigManager configManager)
|
||||
{
|
||||
_configManager = configManager ?? throw new ArgumentNullException(nameof(configManager));
|
||||
_appsManager = appsManager ?? throw new ArgumentNullException(nameof(appsManager));
|
||||
_sourcesDefManager = sourcesDefManager ?? throw new ArgumentNullException(nameof(sourcesDefManager));
|
||||
_notifyIconService = notifyIconService ?? throw new ArgumentNullException(nameof(notifyIconService));
|
||||
_fsManager = fsManager ?? throw new ArgumentNullException(nameof(fsManager));
|
||||
_versionService = versionService ?? throw new ArgumentNullException(nameof(versionService));
|
||||
}
|
||||
|
||||
private void HandleAppError(string message, AppConfig app)
|
||||
{
|
||||
errorMessages.Add(message);
|
||||
_appsManager.UpdateStatus(app, AppStatus.Error);
|
||||
}
|
||||
//private void HandleAppError(string message, AppConfig app)
|
||||
//{
|
||||
// errorMessages.Add(message);
|
||||
// _appsManager.UpdateStatus(app, AppStatus.ERROR);
|
||||
//}
|
||||
|
||||
public void StartVersionChecking(NotifyIcon notifyIcon)
|
||||
public void StartVersionChecking()
|
||||
{
|
||||
var config = _configManager.Load() ?? new GlobalConfig();
|
||||
_notifyIcon = notifyIcon ?? throw new ArgumentNullException(nameof(notifyIcon));
|
||||
var config = _configManager.Load() ?? new Config();
|
||||
// Run version checks in a background thread
|
||||
new Thread(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await CheckAsync();
|
||||
await _versionService.CheckAllApps();
|
||||
|
||||
Thread.Sleep(config.IntervalMinutes * 60 * 1000);
|
||||
}
|
||||
@@ -56,87 +42,95 @@ namespace ModVersionChecker
|
||||
})
|
||||
{ IsBackground = true }.Start();
|
||||
}
|
||||
|
||||
public async Task CheckAsync()
|
||||
{
|
||||
var config = _configManager.Load() ?? new GlobalConfig();
|
||||
var apps = _appsManager.Load() ?? new List<AppConfig>();
|
||||
var sources = _sourcesDefManager.List() ?? new List<SourceDef>();
|
||||
var fsMods = _fsManager.Load() ?? new List<FsModPathConfig>();
|
||||
|
||||
updateMessages = new List<string>();
|
||||
errorMessages = new List<string>();
|
||||
|
||||
|
||||
foreach (AppConfig app in apps)
|
||||
{
|
||||
if (app.Status != AppStatus.Error && app.LastCheckedAt != 0 && app.LastCheckedAt < TimeUtils.GetUnixTimeMillis(DateTime.Now.AddMinutes(-60)))
|
||||
continue;
|
||||
//public async Task CheckAsync()
|
||||
//{
|
||||
// var config = _configManager.Load() ?? new GlobalConfig();
|
||||
// var apps = _appsManager.Load() ?? new List<AppConfig>();
|
||||
// var sources = _sourcesDefManager.List() ?? new List<SourceDef>();
|
||||
// var fsMods = _fsManager.Load() ?? new List<TypeDef>();
|
||||
// var types = _typeConfigManager.GetTypeConfigs() ?? new List<TypeConfig>();
|
||||
|
||||
var status = AppStatus.None;
|
||||
var sourceId = app.Source;
|
||||
if (string.IsNullOrWhiteSpace(sourceId))
|
||||
{
|
||||
HandleAppError($"{app.Name} has no source configured.", app);
|
||||
continue;
|
||||
}
|
||||
var source = sources.FirstOrDefault(s => s.Id == sourceId);
|
||||
if (source == null)
|
||||
{
|
||||
HandleAppError($"{app.Name} has an invalid source: {sourceId}", app);
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (var fsVersion in app.MsfsVersions)
|
||||
{
|
||||
var fsConfig = _fsManager.GetByShortName(fsVersion);
|
||||
if (fsConfig == null)
|
||||
{
|
||||
HandleAppError($"{app.Name} has no FS mod path configured for version {fsVersion}.", app);
|
||||
continue;
|
||||
}
|
||||
var checker = CheckerFactory.CreateChecker(source.Type);
|
||||
var current = NuGetVersion.Parse(VersionUtils.GetCurrentVersion(app, fsConfig));
|
||||
var latest = NuGetVersion.Parse(await checker.GetLatestVersion(app.Params, source));
|
||||
// var appVersionsMap = await _apiVersionService.GetAppVersionsAsync(apps);
|
||||
|
||||
app.CurrentVersion = current.ToString();
|
||||
app.LatestVersion = latest.ToString();
|
||||
// updateMessages = new List<string>();
|
||||
// errorMessages = new List<string>();
|
||||
|
||||
if (latest.CompareTo(current) == 1)
|
||||
{
|
||||
updateMessages.Add($"{app.Name}: New version {latest} (current: {current})");
|
||||
status = AppStatus.UpdateAvailable;
|
||||
}
|
||||
}
|
||||
_appsManager.UpdateStatus(app, status);
|
||||
// foreach (AppConfig app in apps)
|
||||
// {
|
||||
// if (app.Status != AppStatus.ERROR && app.LastCheckedAt != 0 && app.LastCheckedAt < TimeUtils.GetUnixTimeMillis(DateTime.Now.AddMinutes(-60)))
|
||||
// continue;
|
||||
|
||||
// var status = AppStatus.NONE;
|
||||
// var sourceId = app.Source;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
HandleAppError($"Failed for {app.Name}: {ex.Message}", app);
|
||||
}
|
||||
}
|
||||
// // Skip apps that are not in the API response
|
||||
|
||||
if (updateMessages.Count > 0)
|
||||
{
|
||||
_notifyIconService.ShowBalloonTip(
|
||||
10000,
|
||||
"Updates Available",
|
||||
string.Join("\n", updateMessages),
|
||||
ToolTipIcon.Info
|
||||
);
|
||||
}
|
||||
if (errorMessages.Count > 0)
|
||||
{
|
||||
_notifyIconService.ShowBalloonTip(
|
||||
10000,
|
||||
"Errors",
|
||||
string.Join("\n", errorMessages),
|
||||
ToolTipIcon.Error
|
||||
);
|
||||
}
|
||||
OnFinished?.Invoke(this, "Version check completed.");
|
||||
}
|
||||
// if (!appVersionsMap.Any(a => app.Id == a.Id)) {
|
||||
// continue;
|
||||
// }
|
||||
// var latesstVersion = appVersionsMap.FirstOrDefault(a => a.Id == app.Id).LatestVersion;
|
||||
// if (string.IsNullOrWhiteSpace(sourceId))
|
||||
// {
|
||||
// HandleAppError($"{app.Name} has no source configured.", app);
|
||||
// continue;
|
||||
// }
|
||||
// var source = sources.FirstOrDefault(s => s.Id == sourceId);
|
||||
// if (source == null)
|
||||
// {
|
||||
// HandleAppError($"{app.Name} has an invalid source: {sourceId}", app);
|
||||
// continue;
|
||||
// }
|
||||
// try
|
||||
// {
|
||||
// var type = app.Types[0];
|
||||
// var typeConfig = types[0];
|
||||
// if (typeConfig == null)
|
||||
// {
|
||||
// HandleAppError($"{app.Name} has no FS mod path configured for version {type}.", app);
|
||||
// continue;
|
||||
// }
|
||||
// var current = NuGetVersion.Parse(VersionUtils.GetCurrentVersion(app, typeConfig));
|
||||
// var latest = NuGetVersion.Parse(latesstVersion);
|
||||
|
||||
// app.CurrentVersion = current.ToString();
|
||||
// app.LatestVersion = latest.ToString();
|
||||
|
||||
// if (latest.CompareTo(current) == 1)
|
||||
// {
|
||||
// updateMessages.Add($"{app.Name}: New version {latest} (current: {current})");
|
||||
// status = AppStatus.UPDATE_AVAILABLE;
|
||||
// }
|
||||
|
||||
// _appsManager.UpdateStatus(app, status);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// HandleAppError($"Failed for {app.Name}: {ex.Message}", app);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (updateMessages.Count > 0)
|
||||
// {
|
||||
// _notifyIconService.ShowBalloonTip(
|
||||
// 10000,
|
||||
// "Updates Available",
|
||||
// string.Join("\n", updateMessages),
|
||||
// ToolTipIcon.Info
|
||||
// );
|
||||
// }
|
||||
// if (errorMessages.Count > 0)
|
||||
// {
|
||||
// _notifyIconService.ShowBalloonTip(
|
||||
// 10000,
|
||||
// "Errors",
|
||||
// string.Join("\n", errorMessages),
|
||||
// ToolTipIcon.Error
|
||||
// );
|
||||
// }
|
||||
// OnFinished?.Invoke(this, "Version check completed.");
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user