Fix Windows version detection for UTF-8 console

Built-in Environment.OSVersion does not work on Win10.
This commit is contained in:
ORelio 2018-02-11 15:43:58 +01:00
parent 176bbccd50
commit 26716512c1
4 changed files with 100 additions and 3 deletions

View file

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace MinecraftClient.WinAPI
{
/// <summary>
/// Allow to set the player skin as console icon, on Windows only.
/// See StackOverflow no. 2986853
/// </summary>
public static class ConsoleIcon
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleIcon(IntPtr hIcon);
/// <summary>
/// Asynchronously download the player's skin and set the head as console icon
/// </summary>
public static void setPlayerIconAsync(string playerName)
{
if (!Program.isUsingMono) //Windows Only
{
Thread t = new Thread(new ThreadStart(delegate
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://skins.minecraft.net/MinecraftSkins/" + playerName + ".png");
try
{
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
try
{
Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //Read skin from network
skin = skin.Clone(new Rectangle(8, 8, 8, 8), skin.PixelFormat); //Crop skin
SetConsoleIcon(skin.GetHicon()); //Set skin as icon
}
catch (ArgumentException) { /* Invalid image in HTTP response */ }
}
}
catch (WebException) //Skin not found? Reset to default icon
{
try
{
SetConsoleIcon(Icon.ExtractAssociatedIcon(Application.ExecutablePath).Handle);
}
catch { }
}
}
));
t.Name = "Player skin icon setter";
t.Start();
}
}
/// <summary>
/// Set the icon back to the default CMD icon
/// </summary>
public static void revertToCMDIcon()
{
if (!Program.isUsingMono) //Windows Only
{
try
{
SetConsoleIcon(Icon.ExtractAssociatedIcon(Environment.SystemDirectory + "\\cmd.exe").Handle);
}
catch { }
}
}
}
}

View file

@ -0,0 +1,95 @@
using Microsoft.Win32;
namespace MinecraftClient.WinAPI
{
/// <summary>
/// Retrieve information about the current Windows version
/// </summary>
/// <remarks>
/// Environment.OSVersion does not work with Windows 10.
/// It returns 6.2 which is Windows 8
/// </remarks>
/// <seealso>
/// https://stackoverflow.com/a/37755503
/// </seealso>
class WindowsVersion
{
/// <summary>
/// Returns the Windows major version number for this computer.
/// </summary>
public static uint WinMajorVersion
{
get
{
dynamic major;
// The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
{
return (uint) major;
}
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint majorAsUInt;
return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
}
}
/// <summary>
/// Returns the Windows minor version number for this computer.
/// </summary>
public static uint WinMinorVersion
{
get
{
dynamic minor;
// The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
out minor))
{
return (uint) minor;
}
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint minorAsUInt;
return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
}
}
/// <summary>
/// Try retrieving a registry key
/// </summary>
/// <param name="path">key path</param>
/// <param name="key">Key</param>
/// <param name="value">Value (output)</param>
/// <returns>TRUE if successfully retrieved</returns>
private static bool TryGetRegistryKey(string path, string key, out dynamic value)
{
value = null;
try
{
var rk = Registry.LocalMachine.OpenSubKey(path);
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
catch
{
return false;
}
}
}
}