misu – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace misu
{
    public static class Utilities
    {
        #region Static Fields and Constants

        public static readonly string AssemblyName = Assembly.GetEntryAssembly().GetName().Name;

        public static readonly string AssemblyVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

        #endregion

        #region Public Methods

        public static async Task<byte[]> LoadResource(string resource)
        {
            var assembly = Assembly.GetExecutingAssembly();

            foreach (var assemblyResource in assembly.GetManifestResourceNames())
            {
                if (!assemblyResource.Contains(resource))
                {
                    continue;
                }

                using (var manifestResourceStream = assembly.GetManifestResourceStream(assemblyResource))
                {
                    if (manifestResourceStream == null)
                    {
                        break;
                    }

                    var memoryStream = new MemoryStream();

                    await manifestResourceStream.CopyToAsync(memoryStream);

                    memoryStream.Position = 0L;

                    return memoryStream.ToArray();
                }
            }

            throw new ArgumentException("Resource not found.");
        }

        public static void Execute(this Control control, Action lambda)
        {
            if (control.InvokeRequired)
            {
                control.Invoke((MethodInvoker) lambda.Invoke);
            }
            else
            {
                lambda.Invoke();
            }
        }

        public static TValue MapValueToRange<TKey, TValue>(this TValue value, TKey xMin, TKey xMax, TValue yMin,
                                                           TValue yMax)
            where TKey : struct, IComparable<TValue>, IConvertible
            where TValue : struct, IComparable<TKey>, IConvertible
        {
            return (dynamic) yMin +
                   ((dynamic) yMax - (dynamic) yMin) * ((dynamic) value - (dynamic) xMin) /
                   ((dynamic) xMax - (dynamic) xMin);
        }

        public static IEnumerable<TU> SequenceSubtract<TU, TV>(this IEnumerable<TU> a,
                                                               IEnumerable<TV> b,
                                                               Func<TU, TV, bool> cmp)
        {
            var eb = new List<TV>(b);

            using (var ea = a.GetEnumerator())
            {
                while (ea.MoveNext())
                {
                    if (ea.Current == null)
                    {
                        continue;
                    }

                    foreach (var ib in eb)
                    {
                        if (cmp.Invoke(ea.Current, ib))
                        {
                            continue;
                        }

                        yield return ea.Current;

                        break;
                    }
                }
            }
        }

        /// <summary>
        ///     Determines if the sequence a is contained within b.
        /// </summary>
        /// <typeparam name="T">an equatable type</typeparam>
        /// <param name="a">the first sequence</param>
        /// <param name="b">the second sequence</param>
        /// <returns>true if the first sequence is contained within the second sequence</returns>
        public static bool SequenceContains<T>(this IEnumerable<T> a, IEnumerable<T> b)
        {
            using (var ea = a.GetEnumerator())
            {
                using (var eb = b.GetEnumerator())
                {
                    var aea = ea.MoveNext();
                    var aeb = eb.MoveNext();

                    while (aea && aeb)
                    {
                        var eac = ea.Current;
                        var ebc = eb.Current;

                        if (eac.Equals(ebc))
                        {
                            aea = ea.MoveNext();
                            aeb = eb.MoveNext();

                            continue;
                        }

                        aeb = eb.MoveNext();
                    }

                    return !aea;
                }
            }
        }

        /// <summary>
        ///     Convert Windows forms keys to a textual representation.
        /// </summary>
        /// <param name="key">the keys to convert</param>
        /// <returns>a textual representation of the keys</returns>
        /// <remarks>https://stackoverflow.com/questions/23170259/convert-keycode-to-char-string</remarks>
        public static string KeysToString(this Keys key)
        {
            var keyboardState = new byte[255];
            var keyboardStateStatus = Natives.GetKeyboardState(keyboardState);

            if (!keyboardStateStatus)
            {
                return "";
            }

            var virtualKeyCode = (uint) key;
            var scanCode = Natives.MapVirtualKey(virtualKeyCode, 0);
            var inputLocaleIdentifier = Natives.GetKeyboardLayout(0);

            var result = new StringBuilder();
            Natives.ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, 5, 0, inputLocaleIdentifier);

            return result.ToString();
        }

        public static bool LaunchOnBootSet(bool enable)
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                if (key == null)
                {
                    return false;
                }

                switch (enable)
                {
                    case true:
                        key.SetValue(AssemblyName, Assembly.GetEntryAssembly().Location);
                        break;
                    default:
                        key.DeleteValue(AssemblyName, false);
                        break;
                }
            }

            return true;
        }

        public static bool LaunchOnBootGet()
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                return key?.GetValue(AssemblyName) != null;
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.