PalacePal/Pal.Client/Scheduled/QueuedImport.cs

123 lines
4.3 KiB
C#
Raw Normal View History

2023-03-26 13:47:18 +00:00
using System;
using System.IO;
2023-02-22 19:29:58 +00:00
using System.Threading.Tasks;
2023-03-26 13:47:18 +00:00
using Export;
2023-02-18 20:12:36 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2023-02-16 18:51:54 +00:00
using Pal.Client.Database;
using Pal.Client.DependencyInjection;
2023-02-10 19:48:14 +00:00
using Pal.Client.Properties;
2023-02-16 18:51:54 +00:00
using Pal.Client.Windows;
2023-03-26 13:47:18 +00:00
using Pal.Common;
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Scheduled;
internal sealed class QueuedImport : IQueueOnFrameworkThread
{
2023-03-30 20:01:43 +00:00
private ExportRoot Export { get; }
private Guid ExportId { get; set; }
private int ImportedTraps { get; set; }
private int ImportedHoardCoffers { get; set; }
public QueuedImport(string sourcePath)
{
2023-03-30 20:01:43 +00:00
using var input = File.OpenRead(sourcePath);
Export = ExportRoot.Parser.ParseFrom(input);
}
2023-03-30 20:01:43 +00:00
internal sealed class Handler : IQueueOnFrameworkThread.Handler<QueuedImport>
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly Chat _chat;
private readonly ImportService _importService;
private readonly ConfigWindow _configWindow;
public Handler(
ILogger<Handler> logger,
IServiceScopeFactory serviceScopeFactory,
Chat chat,
ImportService importService,
ConfigWindow configWindow)
: base(logger)
{
2023-03-30 20:01:43 +00:00
_serviceScopeFactory = serviceScopeFactory;
_chat = chat;
_importService = importService;
_configWindow = configWindow;
}
2023-03-30 20:01:43 +00:00
protected override void Run(QueuedImport import, ref bool recreateLayout)
{
2023-03-30 20:01:43 +00:00
recreateLayout = true;
2023-03-30 20:01:43 +00:00
try
{
2023-03-30 20:01:43 +00:00
if (!Validate(import))
return;
2023-03-30 20:01:43 +00:00
Task.Run(() =>
{
2023-03-30 20:01:43 +00:00
try
{
2023-03-30 20:01:43 +00:00
using (var scope = _serviceScopeFactory.CreateScope())
2023-02-22 19:29:58 +00:00
{
2023-03-30 20:01:43 +00:00
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
(import.ImportedTraps, import.ImportedHoardCoffers) =
_importService.Import(import.Export);
}
2023-03-30 20:01:43 +00:00
_configWindow.UpdateLastImport();
2023-03-30 20:01:43 +00:00
_logger.LogInformation(
"Imported {ExportId} for {Traps} traps, {Hoard} hoard coffers", import.ExportId,
import.ImportedTraps, import.ImportedHoardCoffers);
_chat.Message(string.Format(Localization.ImportCompleteStatistics, import.ImportedTraps,
import.ImportedHoardCoffers));
}
catch (Exception e)
{
_logger.LogError(e, "Import failed in inner task");
_chat.Error(string.Format(Localization.Error_ImportFailed, e));
}
});
}
2023-03-30 20:01:43 +00:00
catch (Exception e)
{
2023-03-30 20:01:43 +00:00
_logger.LogError(e, "Import failed");
_chat.Error(string.Format(Localization.Error_ImportFailed, e));
}
}
2023-03-30 20:01:43 +00:00
private bool Validate(QueuedImport import)
{
if (import.Export.ExportVersion != ExportConfig.ExportVersion)
{
_logger.LogError(
"Import: Different version in export file, {ExportVersion} != {ConfiguredVersion}",
import.Export.ExportVersion, ExportConfig.ExportVersion);
_chat.Error(Localization.Error_ImportFailed_IncompatibleVersion);
return false;
}
2023-03-30 20:01:43 +00:00
if (!Guid.TryParse(import.Export.ExportId, out Guid exportId) || exportId == Guid.Empty)
{
_logger.LogError("Import: Invalid export id '{Id}'", import.Export.ExportId);
_chat.Error(Localization.Error_ImportFailed_InvalidFile);
return false;
}
2023-03-30 20:01:43 +00:00
import.ExportId = exportId;
2023-03-30 20:01:43 +00:00
if (string.IsNullOrEmpty(import.Export.ServerUrl))
{
// If we allow for backups as import/export, this should be removed
_logger.LogError("Import: No server URL");
_chat.Error(Localization.Error_ImportFailed_InvalidFile);
return false;
}
2023-03-30 20:01:43 +00:00
return true;
}
}
}