working on it ...

Filters

Explore Public Snippets

Sort by

Found 56 snippets

    public by Geometry  1039  0  5  0

    Allows to GetMonoVersion

    using System;
    using System.Reflection;
    using Serilog;
    
    static PlatformManager()
            {
                IdentifyPlatform();
            }
    
    private static string GetMonoVersion()
    {
    	// we can use reflection to get mono version using Mono.Runtime.GetDisplayName().
    
    	var type = Type.GetType("Mono.Runtime");
    
    	if (type == null)
    		return string.Empty;
                       
    	var displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); 
    
    	if (displayName == null)
    		return string.Empty;
    						  
    	return displayName.Invoke(null, null).ToString();
    }

    public by Geometry  965  0  4  0

    IdentifyPlatform: Identifies the current platform and used frameworks.

    Identifies the current platform and used frameworks.
    using System;
    using System.Reflection;
    using Serilog;
    
    /// <summary>
    /// Current framework we are running on.
    /// </summary>
    public static Frameworks Framework { get; private set; }
    /// <summary>
    /// Is the framework .Net 4.5?
    /// </summary>
    public static bool IsDotNet45 { get; private set; }
    /// <summary>
    /// Current framework's version.
    /// </summary>
    public static Version FrameworkVersion { get; private set; }
    /// <summary>
    /// Gets the mono version.
    /// </summary>
    /// <value>The mono version.</value>
    public static string MonoVersion { get; private set; }
    
    static PlatformManager()
            {
                IdentifyPlatform();
            }
    
    /// <summary>
            /// Identifies the current platform and used frameworks.
            /// </summary>
            private static void IdentifyPlatform()
            {
                Framework = Type.GetType("Mono.Runtime") != null ? Frameworks.Mono : Frameworks.DotNet;
                IsDotNet45 = Type.GetType("System.Reflection.ReflectionContext", false) != null; /* ReflectionContext exists from .NET 4.5 onwards. */
                FrameworkVersion = Environment.Version;
    
    			if (Framework == Frameworks.Mono)
    				MonoVersion = GetMonoVersion();
            }

    public by Geometry  1119  0  5  0

    Allows to PrintPlatformBanner

    using System;
    using System.Reflection;
    using Serilog;
    
    /// <summary>
    /// Current framework we are running on.
    /// </summary>
    public static Frameworks Framework { get; private set; }
    /// <summary>
    /// Gets the mono version.
    /// </summary>
    /// <value>The mono version.</value>
    public static string MonoVersion { get; private set; }
    /// <summary>
    /// Is the framework .Net 4.5?
    /// </summary>
    public static bool IsDotNet45 { get; private set; }
    /// <summary>
    /// Current framework's version.
    /// </summary>
    public static Version FrameworkVersion { get; private set; }
    
    static PlatformManager()
            {
                IdentifyPlatform();
            }
    
    public static void PrintPlatformBanner()
    {
        Log.ForContext<PlatformManager>().Information("Running over {0:l}, framework: {1:l} (v{2:l}).",
            Framework == Frameworks.DotNet ? ".Net" : string.Format("Mono {0}", MonoVersion),
            IsDotNet45 ? "4.5" : "4", FrameworkVersion);
    }

    public by Geometry  1412  1  4  0

    GetReadableDifficulty: Returns given difficulty value as human readable string.

    Returns given difficulty value as human readable string.
    using System;
    using System.Globalization;
    
    /// <summary>
    /// Returns given difficulty value as human readable string.
    /// </summary>
    /// <param name="difficulty"></param>
    /// <returns></returns>
    public static string GetReadableDifficulty(this double difficulty)
    {
        var index = -1;
        var rate = difficulty;
    
        var units = new[] {"K", "M", "B", "T", "Q"};
    
        if (difficulty < 1000)
            return difficulty.ToString(CultureInfo.InvariantCulture);
    
        do
        {
            rate = rate/1000;
            index++;
        } while (rate > 1000);
    
        return string.Format("{0:0.00} {1}", rate, units[index]);
    }

    public by Geometry  1315  1  4  0

    GetReadableHashrate: Returns given hashrate value as human readable string.

    Returns given hashrate value as human readable string.
    using System;
    using System.Globalization;
    
    /// <summary>
    /// Returns given hashrate value as human readable string.
    /// </summary>
    /// <param name="hashrate"></param>
    /// <returns></returns>
    public static string GetReadableHashrate(this UInt64 hashrate)
    {
        var index = -1;
        double rate = hashrate;
    
        var units = new[] {"KH/s", "MH/s", "GH/s", "TH/s", "PH/s", "EH/s", "ZH/s", "YH/s"};
    
        do
        {
            rate = rate/1000;
            index++;
        } while (rate > 1000);
    
        return string.Format("{0:0.00} {1}", rate, units[index]);
    }

    public by Geometry  1009  0  5  0

    Allows to GetFilesByExtension

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using CoiniumServ.Utils.Platform;
    using Serilog;
    
    public static List<string> GetFilesByExtension(string directory, string expectedExtension)
    {
        var files = new List<string>(); // Store results in the file results list.
    
        try
        {
            var topDir = GetAbsolutePath(directory);
            var dirInfo = new DirectoryInfo(topDir);
    
            files.AddRange(from fileInfo in dirInfo.GetFiles()
                where string.Compare(fileInfo.Extension, expectedExtension, StringComparison.OrdinalIgnoreCase) == 0
                select string.Format("{0}/{1}", directory, fileInfo.Name));
        }
        catch (DirectoryNotFoundException e)
        {
            Log.Error("Directory not found: {0:l}", e.Message);
        }
    
        return files;
    }

    public by Geometry  1095  0  5  0

    Allows to GetAbsolutePath

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using CoiniumServ.Utils.Platform;
    using Serilog;
    
    public static string AssemblyRoot
            {
                get { return AppDomain.CurrentDomain.BaseDirectory; }
            }
    
    public static string GetAbsolutePath(string file)
    {
        var path = Path.Combine(AssemblyRoot, file); // first get the path as *unix paths.
    
        if (PlatformManager.Framework == Frameworks.DotNet) // if we are running on windows,
            path = path.Replace('/', '\\'); // replace to windows-native paths.
    
        return path;
    }

    public by Geometry  1210  0  5  0

    GetName: Gets the name.

    Gets the name. The argument.
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    
    /// <summary>
    /// Gets the name.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="argument">The argument.</param>
    /// <returns></returns>
    private static string GetName<T>(Expression<Func<T>> argument)
    {
        var mbody = argument.Body as MemberExpression;
    
        if (mbody == null)
        {
            //This will handle Nullable<T> properties.
            var ubody = argument.Body as UnaryExpression;
    
            if (ubody != null)
            {
                mbody = ubody.Operand as MemberExpression;
            }
        }
    
        return mbody != null ? mbody.Member.Name : string.Empty;
    }

    public by Geometry  1232  0  5  0

    GetName: Gets the name.

    Gets the name. The argument.
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    
    /// <summary>
    /// Gets the name.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="argument">The argument.</param>
    /// <returns></returns>
    private static string GetName<T>(Func<T> argument)
    {
        // get IL code behind the delegate
        var methodBody = argument.Method.GetMethodBody();
        if (methodBody != null)
        {
            var il = methodBody.GetILAsByteArray();
    
            // bytes 2-6 represent the field handle
            var fieldHandle = BitConverter.ToInt32(il, 2);
    
            // resolve the handle
            var field = argument.Target.GetType().Module.ResolveField(fieldHandle);
    
            return field.Name;
        }
        return string.Empty;
    }

    public by Geometry  897  0  4  0

    EnumValueIsDefined: Enums the value is defined.

    Enums the value is defined. The type of the enum. The value. The name.
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    
    /// <summary>
    /// Enums the value is defined.
    /// </summary>
    /// <typeparam name="TEnum"> The type of the enum. </typeparam>
    /// <param name="value"> The value. </param>
    /// <param name="name"> The name. </param>
    public static void EnumValueIsDefined<TEnum>(TEnum value, string name)
    {
        if (!Enum.IsDefined(typeof(TEnum), value))
            throw new ArgumentException(string.Format("The enum value {0} does not exist in {1}.", value, typeof(TEnum).Name));
    }
    • Public Snippets
    • Channels Snippets