From 9bf055b78cb6e5eaf90bed176c6584d08bffda50 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sun, 21 Jan 2024 06:46:11 +0100 Subject: [PATCH] Change Import/export format to a teamcraft-style list This means that your new clipboard export would look like: 1x Modified Shark-class Pressure Hull 1x Modified Shark-class Stern 1x Modified Unkiu-class Bow instead of the previous base64 string. --- Workshoppa/Data/ClipboardItem.cs | 12 ------ Workshoppa/Windows/MainWindow.cs | 71 +++++++++++++++++--------------- Workshoppa/Workshoppa.csproj | 2 +- 3 files changed, 38 insertions(+), 47 deletions(-) delete mode 100644 Workshoppa/Data/ClipboardItem.cs diff --git a/Workshoppa/Data/ClipboardItem.cs b/Workshoppa/Data/ClipboardItem.cs deleted file mode 100644 index 0c613f3..0000000 --- a/Workshoppa/Data/ClipboardItem.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.Json.Serialization; - -namespace Workshoppa.Data; - -public class ClipboardItem -{ - [JsonPropertyName("Id")] - public uint WorkshopItemId { get; set; } - - [JsonPropertyName("Q")] - public int Quantity { get; set; } -} diff --git a/Workshoppa/Windows/MainWindow.cs b/Workshoppa/Windows/MainWindow.cs index 497b5d4..8837747 100644 --- a/Workshoppa/Windows/MainWindow.cs +++ b/Workshoppa/Windows/MainWindow.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; -using System.Text.Json; +using System.Text.RegularExpressions; using Dalamud.Interface; using Dalamud.Interface.Colors; using Dalamud.Interface.Components; @@ -13,7 +13,6 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game; using ImGuiNET; using LLib; -using Workshoppa.Data; using Workshoppa.GameData; namespace Workshoppa.Windows; @@ -21,7 +20,7 @@ namespace Workshoppa.Windows; // FIXME The close button doesn't work near the workshop, either hide it or make it work internal sealed class MainWindow : LImGui.LWindow { - private const string JsonPrefix = "workshoppa:v1:"; + private static readonly Regex CountAndName = new(@"^(\d{1,5})x?\s+(.*)$", RegexOptions.Compiled); private readonly WorkshopPlugin _plugin; private readonly DalamudPluginInterface _pluginInterface; @@ -386,17 +385,28 @@ internal sealed class MainWindow : LImGui.LWindow { if (ImGui.BeginMenu("Clipboard")) { - List fromClipboardItems = new(); + List fromClipboardItems = new(); try { string? clipboardText = GetClipboardText(); if (!string.IsNullOrWhiteSpace(clipboardText)) { - clipboardText = Encoding.UTF8.GetString(Convert.FromBase64String(clipboardText)); - if (clipboardText.StartsWith(JsonPrefix)) + foreach (var clipboardLine in clipboardText.ReplaceLineEndings().Split(Environment.NewLine)) { - clipboardText = clipboardText.Substring(JsonPrefix.Length); - fromClipboardItems = JsonSerializer.Deserialize>(clipboardText) ?? new(); + var match = CountAndName.Match(clipboardLine); + if (!match.Success) + continue; + + var craft = _workshopCache.Crafts.FirstOrDefault(x => + x.Name.Equals(match.Groups[2].Value, StringComparison.CurrentCultureIgnoreCase)); + if (craft != null && int.TryParse(match.Groups[1].Value, out int quantity)) + { + fromClipboardItems.Add(new Configuration.QueuedItem + { + WorkshopItemId = craft.WorkshopItemId, + Quantity = quantity, + }); + } } } } @@ -412,24 +422,20 @@ internal sealed class MainWindow : LImGui.LWindow int count = 0; foreach (var item in fromClipboardItems) { - var craft = _workshopCache.Crafts.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId); - if (craft != null) + var queuedItem = + _configuration.ItemQueue.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId); + if (queuedItem != null) + queuedItem.Quantity += item.Quantity; + else { - var queuedItem = - _configuration.ItemQueue.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId); - if (queuedItem != null) - queuedItem.Quantity += item.Quantity; - else + _configuration.ItemQueue.Add(new Configuration.QueuedItem { - _configuration.ItemQueue.Add(new Configuration.QueuedItem - { - WorkshopItemId = item.WorkshopItemId, - Quantity = item.Quantity, - }); - } - - ++count; + WorkshopItemId = item.WorkshopItemId, + Quantity = item.Quantity, + }); } + + ++count; } Save(); @@ -441,17 +447,14 @@ internal sealed class MainWindow : LImGui.LWindow ImGui.BeginDisabled(_configuration.ItemQueue.Count == 0); if (ImGui.MenuItem("Export Queue to Clipboard")) { - var toClipboardItems = _configuration.ItemQueue.Select(x => new ClipboardItem - { - WorkshopItemId = x.WorkshopItemId, - Quantity = x.Quantity - }) - .ToList(); - - var clipboardText = - Convert.ToBase64String( - Encoding.UTF8.GetBytes(JsonPrefix + JsonSerializer.Serialize(toClipboardItems))); - ImGui.SetClipboardText(clipboardText); + var toClipboardItems = _configuration.ItemQueue.Select(x => + new + { + _workshopCache.Crafts.Single(y => x.WorkshopItemId == y.WorkshopItemId).Name, + x.Quantity + }) + .Select(x => $"{x.Quantity}x {x.Name}"); + ImGui.SetClipboardText(string.Join(Environment.NewLine, toClipboardItems)); _chatGui.Print("Copied queue content to clipboard."); } diff --git a/Workshoppa/Workshoppa.csproj b/Workshoppa/Workshoppa.csproj index 24aeff1..7b0dc2b 100644 --- a/Workshoppa/Workshoppa.csproj +++ b/Workshoppa/Workshoppa.csproj @@ -1,7 +1,7 @@ net7.0-windows - 4.0 + 4.1 11.0 enable true