2022-12-22 00:01:09 +00:00
|
|
|
|
using Account;
|
|
|
|
|
using Dalamud.Logging;
|
|
|
|
|
using Pal.Common;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Numerics;
|
2023-02-10 19:48:14 +00:00
|
|
|
|
using Pal.Client.Extensions;
|
|
|
|
|
using Pal.Client.Properties;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
|
|
|
|
|
namespace Pal.Client.Scheduled
|
|
|
|
|
{
|
|
|
|
|
internal class QueuedImport : IQueueOnFrameworkThread
|
|
|
|
|
{
|
|
|
|
|
private readonly ExportRoot _export;
|
|
|
|
|
private Guid _exportId;
|
2023-02-11 20:10:45 +00:00
|
|
|
|
private int _importedTraps;
|
|
|
|
|
private int _importedHoardCoffers;
|
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);
|
|
|
|
|
_export = ExportRoot.Parser.ParseFrom(input);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run(Plugin plugin, ref bool recreateLayout, ref bool saveMarkers)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Validate())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var config = Service.Configuration;
|
|
|
|
|
var oldExportIds = string.IsNullOrEmpty(_export.ServerUrl) ? config.ImportHistory.Where(x => x.RemoteUrl == _export.ServerUrl).Select(x => x.Id).Where(x => x != Guid.Empty).ToList() : new List<Guid>();
|
|
|
|
|
|
|
|
|
|
foreach (var remoteFloor in _export.Floors)
|
|
|
|
|
{
|
|
|
|
|
ushort territoryType = (ushort)remoteFloor.TerritoryType;
|
|
|
|
|
var localState = plugin.GetFloorMarkers(territoryType);
|
|
|
|
|
|
2022-12-24 09:27:59 +00:00
|
|
|
|
localState.UndoImport(oldExportIds);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
ImportFloor(remoteFloor, localState);
|
|
|
|
|
|
|
|
|
|
localState.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.ImportHistory.RemoveAll(hist => oldExportIds.Contains(hist.Id) || hist.Id == _exportId);
|
|
|
|
|
config.ImportHistory.Add(new Configuration.ImportHistoryEntry
|
|
|
|
|
{
|
|
|
|
|
Id = _exportId,
|
|
|
|
|
RemoteUrl = _export.ServerUrl,
|
|
|
|
|
ExportedAt = _export.CreatedAt.ToDateTime(),
|
|
|
|
|
ImportedAt = DateTime.UtcNow,
|
|
|
|
|
});
|
|
|
|
|
config.Save();
|
|
|
|
|
|
|
|
|
|
recreateLayout = true;
|
|
|
|
|
saveMarkers = true;
|
|
|
|
|
|
2023-02-11 20:10:45 +00:00
|
|
|
|
Service.Chat.Print(string.Format(Localization.ImportCompleteStatistics, _importedTraps, _importedHoardCoffers));
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
PluginLog.Error(e, "Import failed");
|
2023-02-10 19:48:14 +00:00
|
|
|
|
Service.Chat.PalError(string.Format(Localization.Error_ImportFailed, e));
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Validate()
|
|
|
|
|
{
|
|
|
|
|
if (_export.ExportVersion != ExportConfig.ExportVersion)
|
|
|
|
|
{
|
2023-02-10 19:48:14 +00:00
|
|
|
|
Service.Chat.PrintError(Localization.Error_ImportFailed_IncompatibleVersion);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Guid.TryParse(_export.ExportId, out _exportId) || _exportId == Guid.Empty)
|
|
|
|
|
{
|
2023-02-10 19:48:14 +00:00
|
|
|
|
Service.Chat.PrintError(Localization.Error_ImportFailed_InvalidFile);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(_export.ServerUrl))
|
|
|
|
|
{
|
|
|
|
|
// If we allow for backups as import/export, this should be removed
|
2023-02-10 19:48:14 +00:00
|
|
|
|
Service.Chat.PrintError(Localization.Error_ImportFailed_InvalidFile);
|
2022-12-22 00:01:09 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ImportFloor(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)
|
2023-02-11 20:10:45 +00:00
|
|
|
|
_importedTraps++;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
else if (localMarker.Type == Marker.EType.Hoard)
|
2023-02-11 20:10:45 +00:00
|
|
|
|
_importedHoardCoffers++;
|
2022-12-22 00:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remoteMarker.Imports.Add(_exportId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|