feat: add clipboard export/import

This commit is contained in:
Anna 2020-07-31 13:29:40 -04:00
parent 7226458785
commit b69de5653a
3 changed files with 82 additions and 3 deletions

View File

@ -1,5 +1,8 @@
using Dalamud.Plugin;
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
namespace HudSwap {
@ -91,4 +94,49 @@ namespace HudSwap {
Three = 2,
Four = 3,
}
[Serializable]
public class SharedLayout {
[JsonProperty]
private readonly byte[] compressed;
[NonSerialized]
private byte[] uncompressed = null;
public byte[] Layout() {
if (this.uncompressed != null) {
return this.uncompressed;
}
try {
using (MemoryStream compressed = new MemoryStream(this.compressed)) {
using (GZipStream gzip = new GZipStream(compressed, CompressionMode.Decompress)) {
using (MemoryStream uncompressed = new MemoryStream()) {
gzip.CopyTo(uncompressed);
this.uncompressed = uncompressed.ToArray();
}
}
}
} catch (Exception) {
return null;
}
return this.uncompressed;
}
[JsonConstructor]
private SharedLayout() {
// For JSON
}
public SharedLayout(byte[] layout) {
using (MemoryStream compressed = new MemoryStream()) {
using (GZipStream gzip = new GZipStream(compressed, CompressionLevel.Optimal)) {
using (MemoryStream uncompressed = new MemoryStream(layout)) {
uncompressed.CopyTo(gzip);
}
}
this.compressed = compressed.ToArray();
}
}
}
}

View File

@ -48,6 +48,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\Lumina.Generated.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />

View File

@ -2,6 +2,7 @@
using Dalamud.Plugin;
using ImGuiNET;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
@ -116,6 +117,15 @@ namespace HudSwap {
this.renameName = "";
this.plugin.config.Save();
}
ImGui.SameLine();
if (ImGui.Button("Copy to clipboard") && this.selectedLayout != null) {
if (this.plugin.config.Layouts.TryGetValue(this.selectedLayout, out Tuple<string, byte[]> layout)) {
SharedLayout shared = new SharedLayout(layout.Item2);
string json = JsonConvert.SerializeObject(shared);
ImGui.SetClipboardText(json);
}
}
ImGui.InputText("##rename-input", ref this.renameName, 100);
ImGui.SameLine();
@ -138,8 +148,21 @@ namespace HudSwap {
this.ImportSlot(slot, this.importName);
this.importName = "";
}
if (slot != HudSlot.Four) {
ImGui.SameLine();
ImGui.SameLine();
}
if (ImGui.Button("Clipboard") && this.importName != "") {
SharedLayout shared = null;
try {
shared = (SharedLayout)JsonConvert.DeserializeObject(ImGui.GetClipboardText(), typeof(SharedLayout));
} catch (Exception) {
}
if (shared != null) {
byte[] layout = shared.Layout();
if (layout != null) {
this.Import(layout, this.importName);
this.importName = "";
}
}
}
@ -340,7 +363,11 @@ namespace HudSwap {
}
public void ImportSlot(HudSlot slot, string name, bool save = true) {
this.plugin.config.Layouts[Guid.NewGuid()] = new Tuple<string, byte[]>(name, this.plugin.hud.ReadLayout(slot));
this.Import(this.plugin.hud.ReadLayout(slot), name, save);
}
public void Import(byte[] layout, string name, bool save = true) {
this.plugin.config.Layouts[Guid.NewGuid()] = new Tuple<string, byte[]>(name, layout);
if (save) {
this.plugin.config.Save();
}