.NET 5+ Support (#1674)

Implement changes to support .NET 5 onwards.
Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
breadbyte 2022-07-03 22:34:07 +08:00 committed by GitHub
parent b3cc2351ee
commit d9f1a77ac2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 1028 additions and 9058 deletions

View file

@ -7,7 +7,6 @@ using System.Threading;
using System.Net;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace MinecraftClient.WinAPI
{
@ -22,7 +21,7 @@ namespace MinecraftClient.WinAPI
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
/// <summary>
/// An application sends the WM_SETICON message to associate a new large or small icon with a window.
/// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption.
@ -32,44 +31,49 @@ namespace MinecraftClient.WinAPI
SETICON = 0x0080,
}
private static void SetWindowIcon(System.Drawing.Icon icon)
private static void SetWindowIcon(System.Drawing.Icon icon)
{
IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
}
}
/// <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
public static void setPlayerIconAsync(string playerName) {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Thread t = new Thread(new ThreadStart(delegate
Thread t = new Thread(new ThreadStart(delegate
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://minotar.net/helm/" + playerName + "/100.png");
try
{
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
HttpWebRequest httpWebRequest = (HttpWebRequest) HttpWebRequest.Create("https://minotar.net/helm/" + playerName + "/100.png");
try
{
try
{
Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //Read skin from network
SetWindowIcon(Icon.FromHandle(skin.GetHicon())); // Windows 10+ (New console)
SetConsoleIcon(skin.GetHicon()); // Windows 8 and lower (Older console)
using (HttpWebResponse httpWebReponse = (HttpWebResponse) httpWebRequest.GetResponse()) {
try
{
Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //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 */
}
}
catch (ArgumentException) { /* Invalid image in HTTP response */ }
}
}
catch (WebException) //Skin not found? Reset to default icon
{
revertToMCCIcon();
}
catch (WebException) //Skin not found? Reset to default icon
{
revertToMCCIcon();
}
}
));
t.Name = "Player skin icon setter";
t.Start();
}
}
@ -78,12 +82,11 @@ namespace MinecraftClient.WinAPI
/// </summary>
public static void revertToMCCIcon()
{
if (!Program.isUsingMono) //Windows Only
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) //Windows Only
{
try
{
//Icon defaultIcon = Icon.ExtractAssociatedIcon(Environment.SystemDirectory + "\\cmd.exe");
Icon defaultIcon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
Icon defaultIcon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
SetWindowIcon(Icon.FromHandle(defaultIcon.Handle)); // Windows 10+ (New console)
SetConsoleIcon(defaultIcon.Handle); // Windows 8 and lower (Older console)
}

View file

@ -1,4 +1,5 @@
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace MinecraftClient.WinAPI
{
@ -21,23 +22,28 @@ namespace MinecraftClient.WinAPI
{
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))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return (uint) major;
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;
}
// 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;
return 0;
}
}
@ -48,24 +54,28 @@ namespace MinecraftClient.WinAPI
{
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))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return (uint) minor;
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;
}
// 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;
return 0;
}
}
@ -78,18 +88,22 @@ namespace MinecraftClient.WinAPI
/// <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;
try
{
var rk = Registry.LocalMachine.OpenSubKey(path);
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
catch
{
return false;
}
return false;
}
}
}