Fix all warnings & Trim (#2226)

* Fix AutoFishing crash
* Fix all warnings
* Remove DotNetZip.
* Fix the usage of HttpClient.
This commit is contained in:
BruceChen 2022-10-02 18:31:08 +08:00 committed by GitHub
parent 4aa6c1c99f
commit 1d52d1eadd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 2201 additions and 43564 deletions

View file

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net;
using System.IO;
using System.Drawing;
using System.Threading.Tasks;
namespace MinecraftClient.WinAPI
{
@ -21,7 +19,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.
@ -31,62 +29,71 @@ namespace MinecraftClient.WinAPI
SETICON = 0x0080,
}
private static void SetWindowIcon(System.Drawing.Icon icon)
private static void SetWindowIcon(System.Drawing.Icon icon)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
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);
SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
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) {
public static void SetPlayerIconAsync(string playerName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Thread t = new Thread(new ThreadStart(delegate
Thread t = new(new ThreadStart(delegate
{
HttpWebRequest httpWebRequest = (HttpWebRequest) HttpWebRequest.Create("https://minotar.net/helm/" + playerName + "/100.png");
try
HttpClient httpClient = new();
try
{
Task<Stream> httpWebRequest = httpClient.GetStreamAsync("https://minotar.net/helm/" + playerName + "/100.png");
httpWebRequest.Wait();
Stream imageStream = httpWebRequest.Result;
try
{
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 */
}
}
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 (WebException) //Skin not found? Reset to default icon
catch (ArgumentException)
{
revertToMCCIcon();
/* Invalid image in HTTP response */
}
imageStream.Dispose();
httpWebRequest.Dispose();
}
catch (HttpRequestException) //Skin not found? Reset to default icon
{
RevertToMCCIcon();
}
finally
{
httpClient.Dispose();
}
}
));
t.Name = "Player skin icon setter";
))
{
Name = "Player skin icon setter"
};
t.Start();
}
}
/// <summary>
/// Set the icon back to the default MCC icon
/// </summary>
public static void revertToMCCIcon()
public static void RevertToMCCIcon()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) //Windows Only
{
try
{
Icon defaultIcon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
Icon defaultIcon = Icon.ExtractAssociatedIcon(Environment.ProcessPath!)!;
SetWindowIcon(Icon.FromHandle(defaultIcon.Handle)); // Windows 10+ (New console)
SetConsoleIcon(defaultIcon.Handle); // Windows 8 and lower (Older console)
}

View file

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MinecraftClient.WinAPI
@ -17,7 +15,7 @@ namespace MinecraftClient.WinAPI
/// <summary>
/// Store codes to run before quitting
/// </summary>
private static List<Action> actions = new List<Action>();
private static readonly List<Action> actions = new();
static ExitCleanUp()
{
@ -31,10 +29,12 @@ namespace MinecraftClient.WinAPI
catch (DllNotFoundException)
{
// Probably on mono, fallback to ctrl+c only
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
static void value(object sender, ConsoleCancelEventArgs e)
{
RunCleanUp();
};
}
Console.CancelKeyPress += value!;
}
}
@ -81,7 +81,7 @@ namespace MinecraftClient.WinAPI
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
private delegate bool ConsoleCtrlHandler(CtrlType sig);
private static ConsoleCtrlHandler _handler;
private static readonly ConsoleCtrlHandler? _handler;
enum CtrlType
{

View file

@ -22,25 +22,20 @@ namespace MinecraftClient.WinAPI
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
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;
}
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'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
return 0;
var versionParts = ((string) version).Split('.');
var versionParts = ((string)version!).Split('.');
if (versionParts.Length != 2) return 0;
uint majorAsUInt;
return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
return uint.TryParse(versionParts[0], out uint majorAsUInt) ? majorAsUInt : 0;
}
return 0;
@ -54,25 +49,20 @@ namespace MinecraftClient.WinAPI
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
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;
}
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'
dynamic version;
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
return 0;
var versionParts = ((string) version).Split('.');
var versionParts = ((string)version!).Split('.');
if (versionParts.Length != 2) return 0;
uint minorAsUInt;
return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
return uint.TryParse(versionParts[1], out uint minorAsUInt) ? minorAsUInt : 0;
}
return 0;
@ -86,18 +76,20 @@ namespace MinecraftClient.WinAPI
/// <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)
private static bool TryGetRegistryKey(string path, string key, out dynamic? value)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
value = null;
try {
try
{
var rk = Registry.LocalMachine.OpenSubKey(path);
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
catch {
catch
{
return false;
}
}