2022-12-22 00:01:09 +00:00
|
|
|
|
using Account;
|
|
|
|
|
using Pal.Common;
|
|
|
|
|
using System;
|
2023-02-16 09:25:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Numerics;
|
2023-02-15 22:17:19 +00:00
|
|
|
|
using Dalamud.Game.Gui;
|
2023-02-16 09:25:33 +00:00
|
|
|
|
using Dalamud.Logging;
|
|
|
|
|
using Pal.Client.Configuration;
|
|
|
|
|
using Pal.Client.DependencyInjection;
|
|
|
|
|
using Pal.Client.Extensions;
|
2023-02-10 19:48:14 +00:00
|
|
|
|
using Pal.Client.Properties;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
|
|
|
|
namespace Pal.Client.Scheduled
|
|
|
|
|
{
|
2023-02-15 22:17:19 +00:00
|
|
|
|
internal sealed class QueuedImport : IQueueOnFrameworkThread
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
private ExportRoot Export { get; }
|
|
|
|
|
private Guid ExportId { get; set; }
|
|
|
|
|
private int ImportedTraps { get; set; }
|
|
|
|
|
private int ImportedHoardCoffers { get; set; }
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
|
|
|
|
public QueuedImport(string sourcePath)
|
|
|
|
|
{
|
2023-02-11 20:10:45 +00:00
|
|
|
|
using var input = File.OpenRead(sourcePath);
|
2023-02-15 22:17:19 +00:00
|
|
|
|
Export = ExportRoot.Parser.ParseFrom(input);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
internal sealed class Handler : IQueueOnFrameworkThread.Handler<QueuedImport>
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
private readonly ChatGui _chatGui;
|
|
|
|
|
private readonly IPalacePalConfiguration _configuration;
|
|
|
|
|
private readonly ConfigurationManager _configurationManager;
|
|
|
|
|
private readonly FloorService _floorService;
|
|
|
|
|
|
|
|
|
|
public Handler(ChatGui chatGui, IPalacePalConfiguration configuration,
|
|
|
|
|
ConfigurationManager configurationManager, FloorService floorService)
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
_chatGui = chatGui;
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
|
_floorService = floorService;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
protected override void Run(QueuedImport import, ref bool recreateLayout, ref bool saveMarkers)
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
recreateLayout = true;
|
|
|
|
|
saveMarkers = true;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Validate(import))
|
|
|
|
|
return;
|
2023-02-15 22:17:19 +00:00
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
var oldExportIds = string.IsNullOrEmpty(import.Export.ServerUrl)
|
|
|
|
|
? _configuration.ImportHistory.Where(x => x.RemoteUrl == import.Export.ServerUrl)
|
|
|
|
|
.Select(x => x.Id)
|
|
|
|
|
.Where(x => x != Guid.Empty).ToList()
|
|
|
|
|
: new List<Guid>();
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
foreach (var remoteFloor in import.Export.Floors)
|
|
|
|
|
{
|
|
|
|
|
ushort territoryType = (ushort)remoteFloor.TerritoryType;
|
|
|
|
|
var localState = _floorService.GetFloorMarkers(territoryType);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
localState.UndoImport(oldExportIds);
|
|
|
|
|
ImportFloor(import, remoteFloor, localState);
|
|
|
|
|
|
|
|
|
|
localState.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_configuration.ImportHistory.RemoveAll(hist =>
|
|
|
|
|
oldExportIds.Contains(hist.Id) || hist.Id == import.ExportId);
|
|
|
|
|
_configuration.ImportHistory.Add(new ConfigurationV1.ImportHistoryEntry
|
|
|
|
|
{
|
|
|
|
|
Id = import.ExportId,
|
|
|
|
|
RemoteUrl = import.Export.ServerUrl,
|
|
|
|
|
ExportedAt = import.Export.CreatedAt.ToDateTime(),
|
|
|
|
|
ImportedAt = DateTime.UtcNow,
|
|
|
|
|
});
|
|
|
|
|
_configurationManager.Save(_configuration);
|
|
|
|
|
|
|
|
|
|
_chatGui.Print(string.Format(Localization.ImportCompleteStatistics, import.ImportedTraps,
|
|
|
|
|
import.ImportedHoardCoffers));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
PluginLog.Error(e, "Import failed");
|
|
|
|
|
_chatGui.PalError(string.Format(Localization.Error_ImportFailed, e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Validate(QueuedImport import)
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
if (import.Export.ExportVersion != ExportConfig.ExportVersion)
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-16 09:25:33 +00:00
|
|
|
|
_chatGui.PrintError(Localization.Error_ImportFailed_IncompatibleVersion);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
if (!Guid.TryParse(import.Export.ExportId, out Guid exportId) || import.ExportId == Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
_chatGui.PrintError(Localization.Error_ImportFailed_InvalidFile);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import.ExportId = exportId;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(import.Export.ServerUrl))
|
|
|
|
|
{
|
|
|
|
|
// If we allow for backups as import/export, this should be removed
|
|
|
|
|
_chatGui.PrintError(Localization.Error_ImportFailed_InvalidFile);
|
|
|
|
|
return false;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 09:25:33 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ImportFloor(QueuedImport import, ExportFloor remoteFloor, LocalState localState)
|
|
|
|
|
{
|
|
|
|
|
var remoteMarkers = remoteFloor.Objects.Select(m =>
|
|
|
|
|
new Marker((Marker.EType)m.Type, new Vector3(m.X, m.Y, m.Z)) { WasImported = true });
|
|
|
|
|
foreach (var remoteMarker in remoteMarkers)
|
|
|
|
|
{
|
|
|
|
|
Marker? localMarker = localState.Markers.SingleOrDefault(x => x == remoteMarker);
|
|
|
|
|
if (localMarker == null)
|
|
|
|
|
{
|
|
|
|
|
localState.Markers.Add(remoteMarker);
|
|
|
|
|
localMarker = remoteMarker;
|
|
|
|
|
|
|
|
|
|
if (localMarker.Type == Marker.EType.Trap)
|
|
|
|
|
import.ImportedTraps++;
|
|
|
|
|
else if (localMarker.Type == Marker.EType.Hoard)
|
|
|
|
|
import.ImportedHoardCoffers++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remoteMarker.Imports.Add(import.ExportId);
|
|
|
|
|
}
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|