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.
This commit is contained in:
Liza 2024-01-21 06:46:11 +01:00
parent 2b5fb62842
commit 4b8a3988b8
Signed by: liza
GPG Key ID: 7199F8D727D55F67
3 changed files with 38 additions and 47 deletions

View File

@ -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; }
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.RegularExpressions;
using Dalamud.Interface; using Dalamud.Interface;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Interface.Components; using Dalamud.Interface.Components;
@ -13,7 +13,6 @@ using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game; using FFXIVClientStructs.FFXIV.Client.Game;
using ImGuiNET; using ImGuiNET;
using LLib; using LLib;
using Workshoppa.Data;
using Workshoppa.GameData; using Workshoppa.GameData;
namespace Workshoppa.Windows; 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 // FIXME The close button doesn't work near the workshop, either hide it or make it work
internal sealed class MainWindow : LImGui.LWindow 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 WorkshopPlugin _plugin;
private readonly DalamudPluginInterface _pluginInterface; private readonly DalamudPluginInterface _pluginInterface;
@ -386,17 +385,28 @@ internal sealed class MainWindow : LImGui.LWindow
{ {
if (ImGui.BeginMenu("Clipboard")) if (ImGui.BeginMenu("Clipboard"))
{ {
List<ClipboardItem> fromClipboardItems = new(); List<Configuration.QueuedItem> fromClipboardItems = new();
try try
{ {
string? clipboardText = GetClipboardText(); string? clipboardText = GetClipboardText();
if (!string.IsNullOrWhiteSpace(clipboardText)) if (!string.IsNullOrWhiteSpace(clipboardText))
{ {
clipboardText = Encoding.UTF8.GetString(Convert.FromBase64String(clipboardText)); foreach (var clipboardLine in clipboardText.ReplaceLineEndings().Split(Environment.NewLine))
if (clipboardText.StartsWith(JsonPrefix))
{ {
clipboardText = clipboardText.Substring(JsonPrefix.Length); var match = CountAndName.Match(clipboardLine);
fromClipboardItems = JsonSerializer.Deserialize<List<ClipboardItem>>(clipboardText) ?? new(); 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,
});
}
} }
} }
} }
@ -411,9 +421,6 @@ internal sealed class MainWindow : LImGui.LWindow
_pluginLog.Information($"Importing {fromClipboardItems.Count} items..."); _pluginLog.Information($"Importing {fromClipboardItems.Count} items...");
int count = 0; int count = 0;
foreach (var item in fromClipboardItems) foreach (var item in fromClipboardItems)
{
var craft = _workshopCache.Crafts.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId);
if (craft != null)
{ {
var queuedItem = var queuedItem =
_configuration.ItemQueue.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId); _configuration.ItemQueue.FirstOrDefault(x => x.WorkshopItemId == item.WorkshopItemId);
@ -430,7 +437,6 @@ internal sealed class MainWindow : LImGui.LWindow
++count; ++count;
} }
}
Save(); Save();
_chatGui.Print($"Imported {count} items from clipboard."); _chatGui.Print($"Imported {count} items from clipboard.");
@ -441,17 +447,14 @@ internal sealed class MainWindow : LImGui.LWindow
ImGui.BeginDisabled(_configuration.ItemQueue.Count == 0); ImGui.BeginDisabled(_configuration.ItemQueue.Count == 0);
if (ImGui.MenuItem("Export Queue to Clipboard")) if (ImGui.MenuItem("Export Queue to Clipboard"))
{ {
var toClipboardItems = _configuration.ItemQueue.Select(x => new ClipboardItem var toClipboardItems = _configuration.ItemQueue.Select(x =>
new
{ {
WorkshopItemId = x.WorkshopItemId, _workshopCache.Crafts.Single(y => x.WorkshopItemId == y.WorkshopItemId).Name,
Quantity = x.Quantity x.Quantity
}) })
.ToList(); .Select(x => $"{x.Quantity}x {x.Name}");
ImGui.SetClipboardText(string.Join(Environment.NewLine, toClipboardItems));
var clipboardText =
Convert.ToBase64String(
Encoding.UTF8.GetBytes(JsonPrefix + JsonSerializer.Serialize(toClipboardItems)));
ImGui.SetClipboardText(clipboardText);
_chatGui.Print("Copied queue content to clipboard."); _chatGui.Print("Copied queue content to clipboard.");
} }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<Version>4.0</Version> <Version>4.1</Version>
<LangVersion>11.0</LangVersion> <LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>