diff --git a/OpenBullet/Pages/StackerBlocks/PageBlockUtility.xaml.cs b/OpenBullet/Pages/StackerBlocks/PageBlockUtility.xaml.cs
index 7673a53..008b7b5 100644
--- a/OpenBullet/Pages/StackerBlocks/PageBlockUtility.xaml.cs
+++ b/OpenBullet/Pages/StackerBlocks/PageBlockUtility.xaml.cs
@@ -1,4 +1,5 @@
using RuriLib;
+using RuriLib.Functions.Conversions;
using System;
using System.Windows.Controls;
@@ -32,12 +33,12 @@ namespace OpenBullet.Pages.StackerBlocks
varActionCombobox.SelectedIndex = (int)block.VarAction;
- foreach (var c in Enum.GetNames(typeof(Conversion)))
+ foreach (var c in Enum.GetNames(typeof(Encoding)))
conversionFromCombobox.Items.Add(c);
conversionFromCombobox.SelectedIndex = (int)block.ConversionFrom;
- foreach (var c in Enum.GetNames(typeof(Conversion)))
+ foreach (var c in Enum.GetNames(typeof(Encoding)))
conversionToCombobox.Items.Add(c);
conversionToCombobox.SelectedIndex = (int)block.ConversionTo;
@@ -114,12 +115,12 @@ namespace OpenBullet.Pages.StackerBlocks
private void conversionFromCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
- block.ConversionFrom = (Conversion)((ComboBox)e.OriginalSource).SelectedIndex;
+ block.ConversionFrom = (Encoding)((ComboBox)e.OriginalSource).SelectedIndex;
}
private void conversionToCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
- block.ConversionTo = (Conversion)((ComboBox)e.OriginalSource).SelectedIndex;
+ block.ConversionTo = (Encoding)((ComboBox)e.OriginalSource).SelectedIndex;
}
private void fileActionCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
diff --git a/RuriLib/Blocks/BlockUtility.cs b/RuriLib/Blocks/BlockUtility.cs
index ca774ec..0a8bdb2 100644
--- a/RuriLib/Blocks/BlockUtility.cs
+++ b/RuriLib/Blocks/BlockUtility.cs
@@ -1,4 +1,5 @@
-using RuriLib.LS;
+using RuriLib.Functions.Conversions;
+using RuriLib.LS;
using RuriLib.Models;
using RuriLib.ViewModels;
using System;
@@ -7,7 +8,6 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
-using System.Text;
using System.Windows.Media;
namespace RuriLib
@@ -78,21 +78,6 @@ namespace RuriLib
Random
}
- ///
- /// The available conversion formats.
- ///
- public enum Conversion
- {
- /// A hexadecimal representation of a byte array.
- HEX,
-
- /// A binary representation of a byte array, containing a multiple of 8 binary digits.
- BIN,
-
- /// A base64 representation of a byte array.
- BASE64
- }
-
///
/// Actions that can be performed on files.
///
@@ -190,13 +175,13 @@ namespace RuriLib
#endregion
// Conversion
- private Conversion conversionFrom = Conversion.HEX;
+ private Encoding conversionFrom = Encoding.HEX;
/// The encoding to convert from.
- public Conversion ConversionFrom { get { return conversionFrom; } set { conversionFrom = value; OnPropertyChanged(); } }
+ public Encoding ConversionFrom { get { return conversionFrom; } set { conversionFrom = value; OnPropertyChanged(); } }
- private Conversion conversionTo = Conversion.BASE64;
+ private Encoding conversionTo = Encoding.BASE64;
/// The encoding to convert to.
- public Conversion ConversionTo { get { return conversionTo; } set { conversionTo = value; OnPropertyChanged(); } }
+ public Encoding ConversionTo { get { return conversionTo; } set { conversionTo = value; OnPropertyChanged(); } }
// Files
private string filePath = "test.txt";
@@ -277,8 +262,8 @@ namespace RuriLib
break;
case UtilityGroup.Conversion:
- ConversionFrom = (Conversion)LineParser.ParseEnum(ref input, "Conversion From", typeof(Conversion));
- ConversionTo = (Conversion)LineParser.ParseEnum(ref input, "Conversion To", typeof(Conversion));
+ ConversionFrom = (Encoding)LineParser.ParseEnum(ref input, "Conversion From", typeof(Encoding));
+ ConversionTo = (Encoding)LineParser.ParseEnum(ref input, "Conversion To", typeof(Encoding));
InputString = LineParser.ParseLiteral(ref input, "Input");
break;
@@ -520,8 +505,8 @@ namespace RuriLib
break;
case UtilityGroup.Conversion:
- byte[] convertedBytes = ConvertFrom(ReplaceValues(inputString, data), conversionFrom);
- data.Variables.Set(new CVar(variableName, ConvertTo(convertedBytes, conversionTo), isCapture));
+ byte[] convertedBytes = ReplaceValues(inputString, data).ConvertFrom(conversionFrom);
+ data.Variables.Set(new CVar(variableName, convertedBytes.ConvertTo(conversionTo), isCapture));
data.Log(new LogEntry(string.Format("Converted input from {0} to {1}", conversionFrom, conversionTo), Colors.White));
break;
@@ -563,65 +548,5 @@ namespace RuriLib
}
catch(Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); }
}
-
- ///
- /// Converts an encoded input to a byte array.
- ///
- /// The encoded input
- /// The encoding
- /// The converted byte array
- public byte[] ConvertFrom(string input, Conversion type)
- {
- switch (type)
- {
- case Conversion.BASE64:
- return Convert.FromBase64String(input);
-
- case Conversion.HEX:
- input = new string(input.ToCharArray()
- .Where(c => !char.IsWhiteSpace(c))
- .ToArray()).Replace("0x", "");
- return Enumerable.Range(0, input.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(input.Substring(x, 2), 16))
- .ToArray();
-
- case Conversion.BIN:
- int numOfBytes = input.Length / 8;
- byte[] bytes = new byte[numOfBytes];
- for (int i = 0; i < numOfBytes; ++i) { bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); }
- return bytes;
-
- default:
- return new byte[0];
- }
- }
-
- ///
- /// Converts a byte array to an encoded string.
- ///
- /// The byte array to encode
- /// The encoding
- /// The encoded string
- public string ConvertTo(byte[] input, Conversion type)
- {
- StringBuilder sb = new StringBuilder();
- switch (type)
- {
- case Conversion.BASE64:
- return Convert.ToBase64String(input);
-
- case Conversion.HEX:
- foreach (byte b in input)
- sb.AppendFormat("{0:x2}", b);
- return sb.ToString().ToUpper();
-
- case Conversion.BIN:
- return string.Concat(input.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
-
- default:
- return "";
- }
- }
}
}
diff --git a/RuriLib/Functions/Conversion/Conversion.cs b/RuriLib/Functions/Conversion/Conversion.cs
new file mode 100644
index 0000000..4118d48
--- /dev/null
+++ b/RuriLib/Functions/Conversion/Conversion.cs
@@ -0,0 +1,117 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace RuriLib.Functions.Conversions
+{
+
+ ///
+ /// The available conversion formats.
+ ///
+ public enum Encoding
+ {
+ /// A hexadecimal representation of a byte array.
+ HEX,
+
+ /// A binary representation of a byte array, containing a multiple of 8 binary digits.
+ BIN,
+
+ /// A base64 representation of a byte array.
+ BASE64,
+
+ /// An ASCII string representation of a byte array.
+ ASCII,
+
+ /// A UTF8 string representation of a byte array.
+ UTF8,
+
+ /// A UTF16 Unicode string representation of a byte array.
+ UNICODE
+ }
+
+ ///
+ /// Provides methods to convert between different representations of binary data.
+ ///
+ public static class Conversion
+ {
+ ///
+ /// Converts an encoded input to a byte array.
+ ///
+ /// The encoded input
+ /// The encoding
+ /// The converted byte array
+ public static byte[] ConvertFrom(this string input, Encoding encoding)
+ {
+ switch (encoding)
+ {
+ case Encoding.BASE64:
+ return Convert.FromBase64String(input);
+
+ case Encoding.HEX:
+ input = new string(input.ToCharArray()
+ .Where(c => !char.IsWhiteSpace(c))
+ .ToArray()).Replace("0x", "");
+ return Enumerable.Range(0, input.Length)
+ .Where(x => x % 2 == 0)
+ .Select(x => Convert.ToByte(input.Substring(x, 2), 16))
+ .ToArray();
+
+ case Encoding.BIN:
+ int numOfBytes = input.Length / 8;
+ byte[] bytes = new byte[numOfBytes];
+ for (int i = 0; i < numOfBytes; ++i) { bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); }
+ return bytes;
+
+ case Encoding.ASCII:
+ return System.Text.Encoding.ASCII.GetBytes(input);
+
+ case Encoding.UTF8:
+ return System.Text.Encoding.UTF8.GetBytes(input);
+
+ case Encoding.UNICODE:
+ return System.Text.Encoding.Unicode.GetBytes(input);
+
+ default:
+ return new byte[0];
+ }
+ }
+
+ ///
+ /// Converts a byte array to an encoded string.
+ ///
+ /// The byte array to encode
+ /// The encoding
+ /// The encoded string
+ public static string ConvertTo(this byte[] input, Encoding encoding)
+ {
+ StringBuilder sb = new StringBuilder();
+ switch (encoding)
+ {
+ case Encoding.BASE64:
+ return Convert.ToBase64String(input);
+
+ case Encoding.HEX:
+ foreach (byte b in input)
+ sb.AppendFormat("{0:x2}", b);
+ return sb.ToString().ToUpper();
+
+ case Encoding.BIN:
+ return string.Concat(input.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
+
+ case Encoding.ASCII:
+ return System.Text.Encoding.ASCII.GetString(input);
+
+ case Encoding.UTF8:
+ return System.Text.Encoding.UTF8.GetString(input);
+
+ case Encoding.UNICODE:
+ return System.Text.Encoding.Unicode.GetString(input);
+
+ default:
+ return "";
+ }
+ }
+ }
+}
diff --git a/RuriLib/RuriLib.csproj b/RuriLib/RuriLib.csproj
index 2cb967b..6264fe0 100644
--- a/RuriLib/RuriLib.csproj
+++ b/RuriLib/RuriLib.csproj
@@ -211,6 +211,7 @@
+