Code commit

This commit is contained in:
RisaDev
2024-01-06 01:21:41 +03:00
parent a7d7297c59
commit a486dd2c96
90 changed files with 11576 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using Newtonsoft.Json;
namespace CustomizePlus.Core.Helpers;
public static class Base64Helper
{
// Compress any type to a base64 encoding of its compressed json representation, prepended with a version byte.
// Returns an empty string on failure.
// Original by Ottermandias: OtterGui <3
public static unsafe string ExportToBase64<T>(T obj, byte version)
{
try
{
var json = JsonConvert.SerializeObject(obj, Formatting.None);
var bytes = Encoding.UTF8.GetBytes(json);
using var compressedStream = new MemoryStream();
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(new ReadOnlySpan<byte>(&version, 1));
zipStream.Write(bytes, 0, bytes.Length);
}
return Convert.ToBase64String(compressedStream.ToArray());
}
catch
{
return string.Empty;
}
}
// Decompress a base64 encoded string to the given type and a prepended version byte if possible.
// On failure, data will be String error and version will be byte.MaxValue.
// Original by Ottermandias: OtterGui <3
public static byte ImportFromBase64(string base64, out string data)
{
var version = byte.MaxValue;
try
{
var bytes = Convert.FromBase64String(base64);
using var compressedStream = new MemoryStream(bytes);
using var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
using var resultStream = new MemoryStream();
zipStream.CopyTo(resultStream);
bytes = resultStream.ToArray();
version = bytes[0];
var json = Encoding.UTF8.GetString(bytes, 1, bytes.Length - 1);
data = json;
}
catch
{
data = "error";
}
return version;
}
}

View File

@@ -0,0 +1,122 @@
using System;
using Dalamud.Interface;
using Dalamud.Utility;
using ImGuiNET;
namespace CustomizePlus.Core.Helpers;
public static class CtrlHelper
{
/// <summary>
/// Gets the width of an icon button, checkbox, etc...
/// </summary>
/// per https://github.com/ocornut/imgui/issues/3714#issuecomment-759319268
public static float IconButtonWidth => ImGui.GetFrameHeight() + 2 * ImGui.GetStyle().ItemInnerSpacing.X;
public static bool TextBox(string label, ref string value)
{
ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X);
return ImGui.InputText(label, ref value, 1024);
}
public static bool TextPropertyBox(string label, Func<string> get, Action<string> set)
{
var temp = get();
var result = TextBox(label, ref temp);
if (result)
{
set(temp);
}
return result;
}
public static bool Checkbox(string label, ref bool value)
{
return ImGui.Checkbox(label, ref value);
}
public static bool CheckboxWithTextAndHelp(string label, string text, string helpText, ref bool value)
{
var checkBoxState = ImGui.Checkbox(label, ref value);
ImGui.SameLine();
ImGui.PushFont(UiBuilder.IconFont);
ImGui.Text(FontAwesomeIcon.InfoCircle.ToIconString());
ImGui.PopFont();
AddHoverText(helpText);
ImGui.SameLine();
ImGui.Text(text);
AddHoverText(helpText);
return checkBoxState;
}
public static bool CheckboxToggle(string label, in bool shown, Action<bool> toggle)
{
var temp = shown;
var toggled = ImGui.Checkbox(label, ref temp);
if (toggled)
{
toggle(temp);
}
return toggled;
}
public static bool ArrowToggle(string label, ref bool value)
{
var toggled = ImGui.ArrowButton(label, value ? ImGuiDir.Down : ImGuiDir.Right);
if (toggled)
{
value = !value;
}
return value;
}
public static void AddHoverText(string text)
{
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip(text);
}
}
public enum TextAlignment { Left, Center, Right };
public static void StaticLabel(string? text, TextAlignment align = TextAlignment.Left, string tooltip = "")
{
if (text != null)
{
if (align == TextAlignment.Center)
{
ImGui.Dummy(new System.Numerics.Vector2((ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(text).X) / 2, 0));
ImGui.SameLine();
}
else if (align == TextAlignment.Right)
{
ImGui.Dummy(new System.Numerics.Vector2(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(text).X, 0));
ImGui.SameLine();
}
ImGui.Text(text);
if (!tooltip.IsNullOrWhitespace())
{
AddHoverText(tooltip);
}
}
}
public static void LabelWithIcon(FontAwesomeIcon icon, string text, bool isSameLine = true)
{
if (isSameLine)
ImGui.SameLine();
ImGui.PushFont(UiBuilder.IconFont);
ImGui.Text(icon.ToIconString());
ImGui.PopFont();
ImGui.SameLine();
ImGui.TextWrapped(text);
}
}

View File

@@ -0,0 +1,22 @@
namespace CustomizePlus.Core.Helpers;
//todo: better name
internal static class NameParsingHelper
{
internal static (string Name, string? Path) ParseName(string name, bool handlePath)
{
var actualName = name;
string? path = null;
if (handlePath)
{
var slashPos = name.LastIndexOf('/');
if (slashPos >= 0)
{
path = name[..slashPos];
actualName = slashPos >= name.Length - 1 ? "<Unnamed>" : name[(slashPos + 1)..];
}
}
return (actualName, path);
}
}