|
using System; |
|
using System.IO; |
|
using System.IO.Compression; |
|
using System.Text; |
|
using Dalamud.Plugin; |
|
using Dalamud.Plugin.Services; |
|
using Dalamud.Interface; |
|
using Dalamud.Interface.Components; |
|
using Dalamud.Bindings.ImGui; |
|
using Newtonsoft.Json; |
|
|
|
internal sealed class PluginTroubleshootingButton |
|
{ |
|
public static string DrawTroubleshootingButton(IDalamudPluginInterface pluginInterface, IChatGui chatGui) |
|
{ |
|
bool leftClicked = ImGuiComponents.IconButton(FontAwesomeIcon.Handshake); |
|
bool rightClicked = ImGui.IsItemClicked(ImGuiMouseButton.Right); |
|
if (ImGui.IsItemHovered()) |
|
ImGui.SetTooltip("Left click: Copy troubleshooting information to clipboard\nRight click: Copy uncompressed troubleshooting info to clipboard"); |
|
if (leftClicked || rightClicked) |
|
{ |
|
// Dalamud troubleshooting json is written after plugin manager changes |
|
string dalamudTroubleshooting = ""; |
|
try |
|
{ |
|
dalamudTroubleshooting = File.ReadAllText(Path.Join(pluginInterface.DalamudAssetDirectory.Parent?.Parent?.FullName, "dalamud.troubleshooting.json")); |
|
} |
|
catch (Exception e) |
|
{ |
|
dalamudTroubleshooting = $@"{{""Error"": {JsonConvert.SerializeObject(e.ToString())}}}"; |
|
} |
|
string pluginConfig = JsonConvert.SerializeObject(pluginInterface.GetPluginConfig(), Formatting.Indented); |
|
string output = $@"{{""Dalamud"": {dalamudTroubleshooting}, ""Plugin"": {pluginConfig}}}"; |
|
if (leftClicked) |
|
ImGui.SetClipboardText(Compress(output)); |
|
else if (rightClicked) |
|
ImGui.SetClipboardText(output); |
|
chatGui.Print("Troubleshooting information has been copied to clipboard."); |
|
} |
|
} |
|
|
|
// https://stackoverflow.com/questions/25134897/gzip-compression-and-decompression-in-c-sharp |
|
internal static string Decompress(string input) |
|
{ |
|
byte[] compressed = Convert.FromBase64String(input); |
|
byte[] decompressed = Decompress(compressed); |
|
string output = Encoding.UTF8.GetString(decompressed); |
|
Svc.Log.Debug($"decompressed {input.Length} to {output.Length}"); |
|
return output; |
|
} |
|
|
|
internal static string Compress(string input) |
|
{ |
|
byte[] encoded = Encoding.UTF8.GetBytes(input); |
|
byte[] compressed = Compress(encoded); |
|
string output = Convert.ToBase64String(compressed); |
|
Svc.Log.Debug($"compressed {input.Length} to {output.Length}"); |
|
return output; |
|
} |
|
public static byte[] Decompress(byte[] input) |
|
{ |
|
using var source = new MemoryStream(input); |
|
using var result = new MemoryStream(); |
|
using (var Decompress = new GZipStream(source, CompressionMode.Decompress)) |
|
{ |
|
Decompress.CopyTo(result); |
|
} |
|
|
|
return result.ToArray(); |
|
} |
|
|
|
public static byte[] Compress(byte[] input) |
|
{ |
|
using var source = new MemoryStream(input); |
|
using var result = new MemoryStream(); |
|
using (var Compress = new GZipStream(result, CompressionMode.Compress)) |
|
{ |
|
source.CopyTo(Compress); |
|
} |
|
|
|
return result.ToArray(); |
|
} |
|
} |