diff --git a/FileFormats/BinaryViewer/BinaryViewer.sln b/FileFormats/BinaryViewer/BinaryViewer.sln new file mode 100644 index 0000000..2687d14 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26430.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinaryViewer", "BinaryViewer\BinaryViewer.csproj", "{5CDC1CF1-6873-465D-8098-B08D67CAF7A7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5CDC1CF1-6873-465D-8098-B08D67CAF7A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CDC1CF1-6873-465D-8098-B08D67CAF7A7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CDC1CF1-6873-465D-8098-B08D67CAF7A7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CDC1CF1-6873-465D-8098-B08D67CAF7A7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FileFormats/BinaryViewer/BinaryViewer/App.config b/FileFormats/BinaryViewer/BinaryViewer/App.config new file mode 100644 index 0000000..8d23437 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/FileFormats/BinaryViewer/BinaryViewer/App.xaml b/FileFormats/BinaryViewer/BinaryViewer/App.xaml new file mode 100644 index 0000000..7401f22 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/App.xaml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/FileFormats/BinaryViewer/BinaryViewer/App.xaml.cs b/FileFormats/BinaryViewer/BinaryViewer/App.xaml.cs new file mode 100644 index 0000000..3dd3e93 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace BinaryViewer +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/BaseAttachedProperty.cs b/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/BaseAttachedProperty.cs new file mode 100644 index 0000000..21403ae --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/BaseAttachedProperty.cs @@ -0,0 +1,117 @@ +using System; +using System.Windows; + +namespace BinaryViewer +{ + /// + /// A base attached property to replace the vanilla WPF attached property + /// + /// The parent class to be the attached property + /// The type of this attached property + public abstract class BaseAttachedProperty + where Parent : new() + { + #region Public Events + + /// + /// Fired when the value changes + /// + public event Action ValueChanged = (sender, e) => { }; + + /// + /// Fired when the value changes, even when the value is the same + /// + public event Action ValueUpdated = (sender, value) => { }; + + #endregion + + #region Public Properties + + /// + /// A singleton instance of our parent class + /// + public static Parent Instance { get; private set; } = new Parent(); + + #endregion + + #region Attached Property Definitions + + /// + /// The attached property for this class + /// + public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( + "Value", + typeof(Property), + typeof(BaseAttachedProperty), + new UIPropertyMetadata( + default(Property), + new PropertyChangedCallback(OnValuePropertyChanged), + new CoerceValueCallback(OnValuePropertyUpdated) + )); + + /// + /// The callback event when the is changed + /// + /// The UI element that had it's property changed + /// The arguments for the event + private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + // Call the parent function + (Instance as BaseAttachedProperty)?.OnValueChanged(d, e); + + // Call event listeners + (Instance as BaseAttachedProperty)?.ValueChanged(d, e); + } + + /// + /// The callback event when the is changed, even if it is the same value + /// + /// The UI element that had it's property changed + /// The arguments for the event + private static object OnValuePropertyUpdated(DependencyObject d, object value) + { + // Call the parent function + (Instance as BaseAttachedProperty)?.OnValueUpdated(d, value); + + // Call event listeners + (Instance as BaseAttachedProperty)?.ValueUpdated(d, value); + + // Return the value + return value; + } + + /// + /// Gets the attached property + /// + /// The element to get the property from + /// + public static Property GetValue(DependencyObject d) => (Property)d.GetValue(ValueProperty); + + /// + /// Sets the attached property + /// + /// The element to get the property from + /// The value to set the property to + public static void SetValue(DependencyObject d, Property value) => d.SetValue(ValueProperty, value); + + #endregion + + #region Event Methods + + /// + /// The method that is called when any attached property of this type is changed + /// + /// The UI element that this property was changed for + /// The arguments for this event + public virtual void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { } + + /// + /// The method that is called when any attached property of this type is changed, even if the value is the same + /// + /// The UI element that this property was changed for + /// The arguments for this event + public virtual void OnValueUpdated(DependencyObject sender, object value) { } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/TextAttachedProperties.cs b/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/TextAttachedProperties.cs new file mode 100644 index 0000000..2f3eb9c --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/AttachedProperties/TextAttachedProperties.cs @@ -0,0 +1,21 @@ +using System.Windows; +using System.Windows.Controls; + +namespace BinaryViewer +{ + /// + /// Focuses (keyboard focus) this element on load + /// + public class IsFocusedProperty : BaseAttachedProperty + { + public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) + { + // If we don't have a control, return + if (!(sender is Control control)) + return; + + // Focus this control once loaded + control.Loaded += (s, se) => control.Focus(); + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/BinaryViewer.csproj b/FileFormats/BinaryViewer/BinaryViewer/BinaryViewer.csproj new file mode 100644 index 0000000..bb3284d --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/BinaryViewer.csproj @@ -0,0 +1,139 @@ + + + + + Debug + AnyCPU + {5CDC1CF1-6873-465D-8098-B08D67CAF7A7} + WinExe + BinaryViewer + BinaryViewer + v4.6.2 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + ..\packages\PropertyChanged.Fody.2.1.1\lib\netstandard1.0\PropertyChanged.dll + False + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + + + + + + + + + + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + BinaryListItemControl.xaml + + + MainWindow.xaml + Code + + + Designer + MSBuild:Compile + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml b/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml new file mode 100644 index 0000000..13308e7 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml @@ -0,0 +1,21 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml.cs b/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml.cs new file mode 100644 index 0000000..33775b4 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Controls/BinaryListItemControl.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace BinaryViewer +{ + /// + /// Interaction logic for BinaryListItemControl.xaml + /// + public partial class BinaryListItemControl : UserControl + { + public BinaryListItemControl() + { + InitializeComponent(); + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/DataModels/StringToBinaryFormat.cs b/FileFormats/BinaryViewer/BinaryViewer/DataModels/StringToBinaryFormat.cs new file mode 100644 index 0000000..131938e --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/DataModels/StringToBinaryFormat.cs @@ -0,0 +1,29 @@ +namespace BinaryViewer +{ + /// + /// The format to use when converting strings to binary + /// + public enum StringToBinaryFormat + { + /// + /// ASCII format + /// + ASCII, + /// + /// UTF7 unicode format + /// + UTF7, + /// + /// UTF8 unicode format + /// + UTF8, + /// + /// UTF32 unicode format + /// + UTF32, + /// + /// Unicode format (USC2) + /// + Unicode + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/Expressions/ExpressionHelpers.cs b/FileFormats/BinaryViewer/BinaryViewer/Expressions/ExpressionHelpers.cs new file mode 100644 index 0000000..5d50fe1 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Expressions/ExpressionHelpers.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace BinaryViewer +{ + /// + /// A helper for expressions + /// + public static class ExpressionHelpers + { + /// + /// Compiles an expression and gets the functions return value + /// + /// The type of return value + /// The expression to compile + /// + public static T GetPropertyValue(this Expression> lamba) + { + return lamba.Compile().Invoke(); + } + + /// + /// Sets the underlying properties value to the given value + /// from an expression that contains the property + /// + /// The type of value to set + /// The expression + /// The value to set the property to + public static void SetPropertyValue(this Expression> lamba, T value) + { + // Converts a lamba () => some.Property, to some.Property + var expression = (lamba as LambdaExpression).Body as MemberExpression; + + // Get the property information so we can set it + var propertyInfo = (PropertyInfo)expression.Member; + var target = Expression.Lambda(expression.Expression).Compile().DynamicInvoke(); + + // Set the property value + propertyInfo.SetValue(target, value); + + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/FodyWeavers.xml b/FileFormats/BinaryViewer/BinaryViewer/FodyWeavers.xml new file mode 100644 index 0000000..bb0f322 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml b/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml new file mode 100644 index 0000000..68139c1 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml.cs b/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml.cs new file mode 100644 index 0000000..62d7afd --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/MainWindow.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace BinaryViewer +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/Properties/AssemblyInfo.cs b/FileFormats/BinaryViewer/BinaryViewer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a0548ad --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BinaryViewer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BinaryViewer")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +//In order to begin building localizable applications, set +//CultureYouAreCodingWith in your .csproj file +//inside a . For example, if you are using US english +//in your source files, set the to en-US. Then uncomment +//the NeutralResourceLanguage attribute below. Update the "en-US" in +//the line below to match the UICulture setting in the project file. + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.Designer.cs b/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.Designer.cs new file mode 100644 index 0000000..d2c02d8 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace BinaryViewer.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("BinaryViewer.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.resx b/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.Designer.cs b/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.Designer.cs new file mode 100644 index 0000000..cea5ab1 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace BinaryViewer.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.1.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.settings b/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/Styles/ComboBox.xaml b/FileFormats/BinaryViewer/BinaryViewer/Styles/ComboBox.xaml new file mode 100644 index 0000000..8b25d20 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/Styles/ComboBox.xaml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/BaseViewModel.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/BaseViewModel.cs new file mode 100644 index 0000000..02feb46 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/BaseViewModel.cs @@ -0,0 +1,62 @@ +using PropertyChanged; +using System; +using System.ComponentModel; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace BinaryViewer +{ + /// + /// A base view model that fires Property Changed events as needed + /// + public class BaseViewModel : INotifyPropertyChanged + { + /// + /// The event that is fired when any child property changes its value + /// + public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { }; + + /// + /// Call this to fire a event + /// + /// + public void OnPropertyChanged(string name) + { + PropertyChanged(this, new PropertyChangedEventArgs(name)); + } + + #region Command Helpers + + /// + /// Runs a command if the updating flag is not set. + /// If the flag is true (indicating the function is already running) then the action is not run. + /// If the flag is false (indicating no running function) then the action is run. + /// Once the action is finished if it was run, then the flag is reset to false + /// + /// The boolean property flag defining if the command is already running + /// The action to run if the command is not already running + /// + protected async Task RunCommandAsync(Expression> updatingFlag, Func action) + { + // Check if the flag property is true (meaning the function is already running) + if (updatingFlag.GetPropertyValue()) + return; + + // Set the property flag to true to indicate we are running + updatingFlag.SetPropertyValue(true); + + try + { + // Run the passed in action + await action(); + } + finally + { + // Set the property flag back to false now it's finished + updatingFlag.SetPropertyValue(false); + } + } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayCommand.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayCommand.cs new file mode 100644 index 0000000..06af813 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayCommand.cs @@ -0,0 +1,64 @@ +using System; +using System.Windows.Input; + +namespace BinaryViewer +{ + /// + /// A basic command that runs an Action + /// + public class RelayCommand : ICommand + { + #region Private Members + + /// + /// The action to run + /// + private Action mAction; + + #endregion + + #region Public Events + + /// + /// The event thats fired when the value has changed + /// + public event EventHandler CanExecuteChanged = (sender, e) => { }; + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public RelayCommand(Action action) + { + mAction = action; + } + + #endregion + + #region Command Methods + + /// + /// A relay command can always execute + /// + /// + /// + public bool CanExecute(object parameter) + { + return true; + } + + /// + /// Executes the commands Action + /// + /// + public void Execute(object parameter) + { + mAction(); + } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayParameterizedCommand.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayParameterizedCommand.cs new file mode 100644 index 0000000..55ae24f --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/Base/RelayParameterizedCommand.cs @@ -0,0 +1,64 @@ +using System; +using System.Windows.Input; + +namespace BinaryViewer +{ + /// + /// A basic command that runs an Action + /// + public class RelayParameterizedCommand : ICommand + { + #region Private Members + + /// + /// The action to run + /// + private Action mAction; + + #endregion + + #region Public Events + + /// + /// The event thats fired when the value has changed + /// + public event EventHandler CanExecuteChanged = (sender, e) => { }; + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public RelayParameterizedCommand(Action action) + { + mAction = action; + } + + #endregion + + #region Command Methods + + /// + /// A relay command can always execute + /// + /// + /// + public bool CanExecute(object parameter) + { + return true; + } + + /// + /// Executes the commands Action + /// + /// + public void Execute(object parameter) + { + mAction(parameter); + } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListDesignModel.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListDesignModel.cs new file mode 100644 index 0000000..0629641 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListDesignModel.cs @@ -0,0 +1,31 @@ +using System; + +namespace BinaryViewer +{ + /// + /// The design time view model of + /// + public class BinaryListDesignModel : BinaryListViewModel + { + #region Singleton + + /// + /// A single instance of the design time view model + /// + public static BinaryListDesignModel Instance => new BinaryListDesignModel(); + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public BinaryListDesignModel() + { + Text = "Hello world"; + } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemDesignModel.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemDesignModel.cs new file mode 100644 index 0000000..7616d4a --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemDesignModel.cs @@ -0,0 +1,31 @@ +using System; + +namespace BinaryViewer +{ + /// + /// The design time view model of + /// + public class BinaryListItemDesignModel : BinaryListItemViewModel + { + #region Singleton + + /// + /// A single instance of the design time view model + /// + public static BinaryListItemDesignModel Instance => new BinaryListItemDesignModel(); + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public BinaryListItemDesignModel() + { + Byte = 0x41; + } + + #endregion + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemViewModel.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemViewModel.cs new file mode 100644 index 0000000..e35c536 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListItemViewModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Text; + +namespace BinaryViewer +{ + /// + /// The view model for a single binary item in the binary list + /// + public class BinaryListItemViewModel : BaseViewModel + { + /// + /// The byte to display + /// + public byte Byte { get; set; } + + /// + /// The hexadecimal display of the byte (e.g. 0x41) + /// + public string HexString => "0x" + Byte.ToString("X2"); + + /// + /// The binary display of the byte (e.g 01000001) + /// + public string BinaryString => Convert.ToString(Byte, 2).PadLeft(8, '0'); + + /// + /// The decimal display of the byte (e.g. 65) + /// + public string DecimalString => ((int)Byte).ToString(); + + /// + /// The UTF8 (Unicode) character representation of the byte + /// + public string UTF8String => new string((char)Byte, 1); + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListViewModel.cs b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListViewModel.cs new file mode 100644 index 0000000..a96ade2 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/ViewModels/BinaryListViewModel.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Text; + +namespace BinaryViewer +{ + /// + /// The view model for a list of bytes in the binary list + /// + public class BinaryListViewModel : BaseViewModel + { + #region Public Properties + + /// + /// The byte to display + /// + public ObservableCollection Bytes { get; set; } + + /// + /// The direct text to bind to that will convert to the Bytes as it changes + /// + public string Text { get; set; } + + /// + /// The format of the string when converting it to binary + /// + public StringToBinaryFormat StringFormat { get; set; } = StringToBinaryFormat.UTF8; + + /// + /// A list of all string to binary formats + /// + public List StringFormats => Enum.GetValues(typeof(StringToBinaryFormat)).Cast().ToList(); + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public BinaryListViewModel() + { + PropertyChanged += BinaryListViewModel_PropertyChanged; + } + + /// + /// Construct from a byte array + /// + /// The byte array to use + public BinaryListViewModel(byte[] bytes) + { + FromBytes(bytes); + } + + /// + /// Construct from a string + /// + /// The string to use + public BinaryListViewModel(string @string) + { + FromString(@string); + } + + #endregion + + #region Public Methods + + /// + /// Set bytes from byte[] + /// + public void FromBytes(byte[] bytes) + { + // Create list from bytes + Bytes = new ObservableCollection(bytes.Select(b => new BinaryListItemViewModel { Byte = b }).ToArray()); + } + + /// + /// Set bytes from a string + /// + public void FromString(string @string) + { + switch (StringFormat) + { + // ASCII string + case StringToBinaryFormat.ASCII: + FromBytes(Encoding.ASCII.GetBytes(@string)); + break; + + // UTF7 string + case StringToBinaryFormat.UTF7: + FromBytes(Encoding.UTF7.GetBytes(@string)); + break; + + // UTF8 string + case StringToBinaryFormat.UTF8: + FromBytes(Encoding.UTF8.GetBytes(@string)); + break; + + // UTF32 string + case StringToBinaryFormat.UTF32: + FromBytes(Encoding.UTF32.GetBytes(@string)); + break; + + // Unicode string + case StringToBinaryFormat.Unicode: + FromBytes(Encoding.Unicode.GetBytes(@string)); + break; + + } + } + + #endregion + + private void BinaryListViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + // If the text changes, process it + if (string.Equals(e.PropertyName, nameof(Text))) + DetectSpecialText(); + // If the format of the string changes, process it + else if (string.Equals(e.PropertyName, nameof(StringFormat))) + DetectSpecialText(); + } + + private void DetectSpecialText() + { + if (string.IsNullOrEmpty(Text)) + return; + + // When text changes, update the bytes + + // If the bytes are all 0's and 1's and exactly 8 long + // presume the user wants to create a byte from it + if (Text.Length == 8 && !Text.Any(f => f != '0' && f != '1')) + { + // Convert the 8 characters into a single byte + var result = 0; + for (var i = 7; i > 0; i--) + { + if (Text[i] == '1') + result |= (1 << (7 - i)); + } + + FromBytes(new[] { (byte)result }); + } + // Hex value (i.e. 0x45) + else if (Text.Length == 4 && Text.StartsWith("0x") && !Text.Skip(2).Any(f => !IsHex(f))) + { + FromBytes(new[] { Convert.ToByte(Convert.ToInt16(Text, 16)) }); + } + // Otherwise, normal string + else + FromString(Text); + } + + /// + /// Checks if this character is a hexadecimal value (between 0-9 or A-F) + /// + /// The character to check + /// + private bool IsHex(char character) + { + return (character >= '0' && character <= '9') || + (character >= 'a' && character <= 'f') || + (character >= 'A' && character <= 'F'); + } + } +} diff --git a/FileFormats/BinaryViewer/BinaryViewer/packages.config b/FileFormats/BinaryViewer/BinaryViewer/packages.config new file mode 100644 index 0000000..616e0e3 --- /dev/null +++ b/FileFormats/BinaryViewer/BinaryViewer/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/FileFormats/BinaryViewer/README.md b/FileFormats/BinaryViewer/README.md new file mode 100644 index 0000000..434cf9a --- /dev/null +++ b/FileFormats/BinaryViewer/README.md @@ -0,0 +1,12 @@ +# Binary Viewer +A simple WPF application that gives easy and quick ways to visualize text and data in all binary formats. + +View strings, files and bytes instantly in: +- binary +- hexadecimal +- decimal +- ascii + +http://www.angelsix.com/youtube + + diff --git a/FileFormats/README.md b/FileFormats/README.md new file mode 100644 index 0000000..18df7dc --- /dev/null +++ b/FileFormats/README.md @@ -0,0 +1,6 @@ +# File Formats +Any source code from the File Formats video series on the AngelSix YouTube channel + +http://www.angelsix.com/youtube + +