Add undo import

This commit is contained in:
Liza 2022-12-24 10:27:59 +01:00
parent fc4a627c92
commit f64b85f9b8
5 changed files with 71 additions and 18 deletions

View File

@ -1,4 +1,5 @@
using Pal.Common; using Pal.Common;
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -79,11 +80,16 @@ namespace Pal.Client
string path = GetSaveLocation(TerritoryType); string path = GetSaveLocation(TerritoryType);
ApplyFilters(); ApplyFilters();
File.WriteAllText(path, JsonSerializer.Serialize(new SaveFile if (Markers.Count == 0)
File.Delete(path);
else
{ {
Version = _currentVersion, File.WriteAllText(path, JsonSerializer.Serialize(new SaveFile
Markers = new HashSet<Marker>(Markers) {
}, _jsonSerializerOptions)); Version = _currentVersion,
Markers = new HashSet<Marker>(Markers)
}, _jsonSerializerOptions));
}
} }
private static string GetSaveLocation(uint territoryType) => Path.Join(Service.PluginInterface.GetPluginConfigDirectory(), $"{territoryType}.json"); private static string GetSaveLocation(uint territoryType) => Path.Join(Service.PluginInterface.GetPluginConfigDirectory(), $"{territoryType}.json");
@ -98,6 +104,14 @@ namespace Pal.Client
} }
} }
public void UndoImport(List<Guid> importIds)
{
// When saving a floor state, any markers not seen, not remote seen, and not having an import id are removed;
// so it is possible to remove "wrong" markers by not having them be in the current import.
foreach (var marker in Markers)
marker.Imports.RemoveAll(id => importIds.Contains(id));
}
public class SaveFile public class SaveFile
{ {
public int Version { get; set; } public int Version { get; set; }

View File

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net6.0-windows</TargetFramework>
<LangVersion>10.0</LangVersion> <LangVersion>10.0</LangVersion>
<Version>1.26</Version> <Version>1.27</Version>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>

View File

@ -37,7 +37,7 @@ namespace Pal.Client.Scheduled
ushort territoryType = (ushort)remoteFloor.TerritoryType; ushort territoryType = (ushort)remoteFloor.TerritoryType;
var localState = plugin.GetFloorMarkers(territoryType); var localState = plugin.GetFloorMarkers(territoryType);
CleanupFloor(localState, oldExportIds); localState.UndoImport(oldExportIds);
ImportFloor(remoteFloor, localState); ImportFloor(remoteFloor, localState);
localState.Save(); localState.Save();
@ -89,14 +89,6 @@ namespace Pal.Client.Scheduled
return true; return true;
} }
private void CleanupFloor(LocalState localState, List<Guid> oldExportIds)
{
// When saving a floor state, any markers not seen, not remote seen, and not having an import id are removed;
// so it is possible to remove "wrong" markers by not having them be in the current import.
foreach (var marker in localState.Markers)
marker.Imports.RemoveAll(id => oldExportIds.Contains(id));
}
private void ImportFloor(ExportFloor remoteFloor, LocalState localState) 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 }); var remoteMarkers = remoteFloor.Objects.Select(m => new Marker((Marker.EType)m.Type, new Vector3(m.X, m.Y, m.Z)) { WasImported = true });

View File

@ -0,0 +1,35 @@
using ECommons.Configuration;
using Pal.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pal.Client.Scheduled
{
internal class QueuedUndoImport : IQueueOnFrameworkThread
{
private readonly Guid _exportId;
public QueuedUndoImport(Guid exportId)
{
_exportId = exportId;
}
public void Run(Plugin plugin, ref bool recreateLayout, ref bool saveMarkers)
{
recreateLayout = true;
saveMarkers = true;
foreach (ETerritoryType territoryType in typeof(ETerritoryType).GetEnumValues())
{
var localState = plugin.GetFloorMarkers((ushort)territoryType);
localState.UndoImport(new List<Guid> { _exportId });
localState.Save();
}
Service.Configuration.ImportHistory.RemoveAll(hist => hist.Id == _exportId);
}
}
}

View File

@ -11,7 +11,6 @@ using Google.Protobuf;
using ImGuiNET; using ImGuiNET;
using Pal.Client.Net; using Pal.Client.Net;
using Pal.Client.Scheduled; using Pal.Client.Scheduled;
using Pal.Common;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -199,9 +198,6 @@ namespace Pal.Client.Windows
private void DrawImportTab() private void DrawImportTab()
{ {
if (Service.Configuration.BetaKey != "import")
return;
if (ImGui.BeginTabItem("Import")) if (ImGui.BeginTabItem("Import"))
{ {
ImGui.TextWrapped("Using an export is useful if you're unable to connect to the server, or don't wish to share your findings."); ImGui.TextWrapped("Using an export is useful if you're unable to connect to the server, or don't wish to share your findings.");
@ -232,6 +228,17 @@ namespace Pal.Client.Windows
if (ImGui.Button("Start Import")) if (ImGui.Button("Start Import"))
DoImport(_openImportPath); DoImport(_openImportPath);
ImGui.EndDisabled(); ImGui.EndDisabled();
var importHistory = Service.Configuration.ImportHistory.OrderByDescending(x => x.ImportedAt).ThenBy(x => x.Id).FirstOrDefault();
if (importHistory != null)
{
ImGui.Separator();
ImGui.TextWrapped($"Your last import was on {importHistory.ImportedAt}, which added the trap/hoard coffer database from {importHistory.RemoteUrl} created on {importHistory.ExportedAt:d}.");
ImGui.TextWrapped("If you think that was a mistake, you can remove all locations only found in the import (any location you've seen yourself is not changed).");
if (ImGui.Button("Undo Import"))
UndoImport(importHistory.Id);
}
ImGui.EndTabItem(); ImGui.EndTabItem();
} }
} }
@ -406,6 +413,11 @@ namespace Pal.Client.Windows
Service.Plugin.EarlyEventQueue.Enqueue(new QueuedImport(sourcePath)); Service.Plugin.EarlyEventQueue.Enqueue(new QueuedImport(sourcePath));
} }
internal void UndoImport(Guid importId)
{
Service.Plugin.EarlyEventQueue.Enqueue(new QueuedUndoImport(importId));
}
internal void DoExport(string destinationPath) internal void DoExport(string destinationPath)
{ {
Task.Run(async () => Task.Run(async () =>