mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Trim & Bug fix
This commit is contained in:
parent
f538b9e948
commit
2e18317f3f
9 changed files with 102 additions and 196 deletions
|
|
@ -3,6 +3,7 @@ using System.Drawing;
|
|||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -42,56 +43,54 @@ namespace MinecraftClient.WinAPI
|
|||
/// <summary>
|
||||
/// Asynchronously download the player's skin and set the head as console icon
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static void SetPlayerIconAsync(string playerName)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
Thread t = new(new ThreadStart(delegate
|
||||
{
|
||||
Thread t = new(new ThreadStart(delegate
|
||||
HttpClient httpClient = new();
|
||||
try
|
||||
{
|
||||
HttpClient httpClient = new();
|
||||
Task<Stream> httpWebRequest = httpClient.GetStreamAsync("https://minotar.net/helm/" + playerName + "/100.png");
|
||||
httpWebRequest.Wait();
|
||||
Stream imageStream = httpWebRequest.Result;
|
||||
try
|
||||
{
|
||||
Task<Stream> httpWebRequest = httpClient.GetStreamAsync("https://minotar.net/helm/" + playerName + "/100.png");
|
||||
httpWebRequest.Wait();
|
||||
Stream imageStream = httpWebRequest.Result;
|
||||
try
|
||||
{
|
||||
Bitmap skin = new(Image.FromStream(imageStream)); //Read skin from network
|
||||
SetWindowIcon(Icon.FromHandle(skin.GetHicon())); // Windows 10+ (New console)
|
||||
SetConsoleIcon(skin.GetHicon()); // Windows 8 and lower (Older console)
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
/* Invalid image in HTTP response */
|
||||
}
|
||||
imageStream.Dispose();
|
||||
httpWebRequest.Dispose();
|
||||
Bitmap skin = new(Image.FromStream(imageStream)); //Read skin from network
|
||||
SetWindowIcon(Icon.FromHandle(skin.GetHicon())); // Windows 10+ (New console)
|
||||
SetConsoleIcon(skin.GetHicon()); // Windows 8 and lower (Older console)
|
||||
}
|
||||
catch (AggregateException ae)
|
||||
catch (ArgumentException)
|
||||
{
|
||||
foreach (var ex in ae.InnerExceptions)
|
||||
{
|
||||
if (ex is HttpRequestException) //Skin not found? Reset to default icon
|
||||
RevertToMCCIcon();
|
||||
else
|
||||
throw ex;
|
||||
}
|
||||
/* Invalid image in HTTP response */
|
||||
}
|
||||
catch (HttpRequestException) //Skin not found? Reset to default icon
|
||||
imageStream.Dispose();
|
||||
httpWebRequest.Dispose();
|
||||
}
|
||||
catch (AggregateException ae)
|
||||
{
|
||||
foreach (var ex in ae.InnerExceptions)
|
||||
{
|
||||
RevertToMCCIcon();
|
||||
}
|
||||
finally
|
||||
{
|
||||
httpClient.Dispose();
|
||||
if (ex is HttpRequestException) //Skin not found? Reset to default icon
|
||||
RevertToMCCIcon();
|
||||
else
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
))
|
||||
catch (HttpRequestException) //Skin not found? Reset to default icon
|
||||
{
|
||||
Name = "Player skin icon setter"
|
||||
};
|
||||
t.Start();
|
||||
RevertToMCCIcon();
|
||||
}
|
||||
finally
|
||||
{
|
||||
httpClient.Dispose();
|
||||
}
|
||||
}
|
||||
))
|
||||
{
|
||||
Name = "Player skin icon setter"
|
||||
};
|
||||
t.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,102 +0,0 @@
|
|||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
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
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// 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 dynamic? major))
|
||||
return (uint)major;
|
||||
|
||||
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
|
||||
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
|
||||
return 0;
|
||||
|
||||
var versionParts = ((string)version!).Split('.');
|
||||
if (versionParts.Length != 2) return 0;
|
||||
return uint.TryParse(versionParts[0], NumberStyles.Any, CultureInfo.CurrentCulture, out uint majorAsUInt) ? majorAsUInt : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Windows minor version number for this computer.
|
||||
/// </summary>
|
||||
public static uint WinMinorVersion
|
||||
{
|
||||
get
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// 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 dynamic? minor))
|
||||
return (uint)minor;
|
||||
|
||||
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
|
||||
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
|
||||
return 0;
|
||||
|
||||
var versionParts = ((string)version!).Split('.');
|
||||
if (versionParts.Length != 2) return 0;
|
||||
return uint.TryParse(versionParts[1], NumberStyles.Any, CultureInfo.CurrentCulture, out uint minorAsUInt) ? minorAsUInt : 0;
|
||||
}
|
||||
|
||||
return 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)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
value = null;
|
||||
try
|
||||
{
|
||||
var rk = Registry.LocalMachine.OpenSubKey(path);
|
||||
if (rk == null) return false;
|
||||
value = rk.GetValue(key);
|
||||
return value != null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue