From f64b85f9b81e957f518981de70dc62e2c310f7c0 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sat, 24 Dec 2022 10:27:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20undo=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pal.Client/LocalState.cs | 22 ++++++++++++--- Pal.Client/Pal.Client.csproj | 2 +- Pal.Client/Scheduled/QueuedImport.cs | 10 +------ Pal.Client/Scheduled/QueuedUndoImport.cs | 35 ++++++++++++++++++++++++ Pal.Client/Windows/ConfigWindow.cs | 20 +++++++++++--- 5 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 Pal.Client/Scheduled/QueuedUndoImport.cs diff --git a/Pal.Client/LocalState.cs b/Pal.Client/LocalState.cs index 8cd23f2..8929b12 100644 --- a/Pal.Client/LocalState.cs +++ b/Pal.Client/LocalState.cs @@ -1,4 +1,5 @@ using Pal.Common; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; @@ -79,11 +80,16 @@ namespace Pal.Client string path = GetSaveLocation(TerritoryType); ApplyFilters(); - File.WriteAllText(path, JsonSerializer.Serialize(new SaveFile + if (Markers.Count == 0) + File.Delete(path); + else { - Version = _currentVersion, - Markers = new HashSet(Markers) - }, _jsonSerializerOptions)); + File.WriteAllText(path, JsonSerializer.Serialize(new SaveFile + { + Version = _currentVersion, + Markers = new HashSet(Markers) + }, _jsonSerializerOptions)); + } } private static string GetSaveLocation(uint territoryType) => Path.Join(Service.PluginInterface.GetPluginConfigDirectory(), $"{territoryType}.json"); @@ -98,6 +104,14 @@ namespace Pal.Client } } + public void UndoImport(List 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 int Version { get; set; } diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index e6078f9..ff0c432 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -3,7 +3,7 @@ net6.0-windows 10.0 - 1.26 + 1.27 enable diff --git a/Pal.Client/Scheduled/QueuedImport.cs b/Pal.Client/Scheduled/QueuedImport.cs index 02f4261..f8d38c1 100644 --- a/Pal.Client/Scheduled/QueuedImport.cs +++ b/Pal.Client/Scheduled/QueuedImport.cs @@ -37,7 +37,7 @@ namespace Pal.Client.Scheduled ushort territoryType = (ushort)remoteFloor.TerritoryType; var localState = plugin.GetFloorMarkers(territoryType); - CleanupFloor(localState, oldExportIds); + localState.UndoImport(oldExportIds); ImportFloor(remoteFloor, localState); localState.Save(); @@ -89,14 +89,6 @@ namespace Pal.Client.Scheduled return true; } - private void CleanupFloor(LocalState localState, List 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) { var remoteMarkers = remoteFloor.Objects.Select(m => new Marker((Marker.EType)m.Type, new Vector3(m.X, m.Y, m.Z)) { WasImported = true }); diff --git a/Pal.Client/Scheduled/QueuedUndoImport.cs b/Pal.Client/Scheduled/QueuedUndoImport.cs new file mode 100644 index 0000000..1c0163e --- /dev/null +++ b/Pal.Client/Scheduled/QueuedUndoImport.cs @@ -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 { _exportId }); + localState.Save(); + } + + Service.Configuration.ImportHistory.RemoveAll(hist => hist.Id == _exportId); + } + } +} diff --git a/Pal.Client/Windows/ConfigWindow.cs b/Pal.Client/Windows/ConfigWindow.cs index a32e61a..2597491 100644 --- a/Pal.Client/Windows/ConfigWindow.cs +++ b/Pal.Client/Windows/ConfigWindow.cs @@ -11,7 +11,6 @@ using Google.Protobuf; using ImGuiNET; using Pal.Client.Net; using Pal.Client.Scheduled; -using Pal.Common; using System; using System.Collections; using System.Collections.Generic; @@ -199,9 +198,6 @@ namespace Pal.Client.Windows private void DrawImportTab() { - if (Service.Configuration.BetaKey != "import") - return; - 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."); @@ -232,6 +228,17 @@ namespace Pal.Client.Windows if (ImGui.Button("Start Import")) DoImport(_openImportPath); 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(); } } @@ -406,6 +413,11 @@ namespace Pal.Client.Windows Service.Plugin.EarlyEventQueue.Enqueue(new QueuedImport(sourcePath)); } + internal void UndoImport(Guid importId) + { + Service.Plugin.EarlyEventQueue.Enqueue(new QueuedUndoImport(importId)); + } + internal void DoExport(string destinationPath) { Task.Run(async () =>