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,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol.Message
{
@ -41,7 +37,7 @@ namespace MinecraftClient.Protocol.Message
isSystemChat = false;
this.content = content;
this.isJson = isJson;
this.chatTypeId = chatType;
chatTypeId = chatType;
this.senderUUID = senderUUID;
this.unsignedContent = unsignedContent;
this.displayName = displayName;
@ -57,18 +53,18 @@ namespace MinecraftClient.Protocol.Message
this.isSystemChat = isSystemChat;
this.content = content;
this.isJson = isJson;
this.chatTypeId = chatType;
chatTypeId = chatType;
this.senderUUID = senderUUID;
}
public LastSeenMessageList.Entry? toLastSeenMessageEntry()
public LastSeenMessageList.Entry? ToLastSeenMessageEntry()
{
return signature != null ? new LastSeenMessageList.Entry(senderUUID, signature) : null;
}
public bool lacksSender()
public bool LacksSender()
{
return this.senderUUID == Guid.Empty;
return senderUUID == Guid.Empty;
}
}
}

View file

@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using MinecraftClient.Protocol.Message;
namespace MinecraftClient.Protocol
@ -140,27 +141,31 @@ namespace MinecraftClient.Protocol
/// <returns>Color code</returns>
private static string Color2tag(string colorname)
{
switch (colorname.ToLower())
return colorname.ToLower() switch
{
/* MC 1.7+ Name MC 1.6 Name Classic tag */
case "black": /* Blank if same */ return "§0";
case "dark_blue": return "§1";
case "dark_green": return "§2";
case "dark_aqua": case "dark_cyan": return "§3";
case "dark_red": return "§4";
case "dark_purple": case "dark_magenta": return "§5";
case "gold": case "dark_yellow": return "§6";
case "gray": return "§7";
case "dark_gray": return "§8";
case "blue": return "§9";
case "green": return "§a";
case "aqua": case "cyan": return "§b";
case "red": return "§c";
case "light_purple": case "magenta": return "§d";
case "yellow": return "§e";
case "white": return "§f";
default: return "";
}
#pragma warning disable format // @formatter:off
/* MC 1.7+ Name || MC 1.6 Name || Classic tag */
"black" => "§0",
"dark_blue" => "§1",
"dark_green" => "§2",
"dark_aqua" or "dark_cyan" => "§3",
"dark_red" => "§4",
"dark_purple" or "dark_magenta" => "§5",
"gold" or "dark_yellow" => "§6",
"gray" => "§7",
"dark_gray" => "§8",
"blue" => "§9",
"green" => "§a",
"aqua" or "cyan" => "§b",
"red" => "§c",
"light_purple" or "magenta" => "§d",
"yellow" => "§e",
"white" => "§f",
_ => "" ,
#pragma warning restore format // @formatter:on
};
}
/// <summary>
@ -171,7 +176,7 @@ namespace MinecraftClient.Protocol
/// <summary>
/// Set of translation rules for formatting text
/// </summary>
private static Dictionary<string, string> TranslationRules = new Dictionary<string, string>();
private static readonly Dictionary<string, string> TranslationRules = new();
/// <summary>
/// Initialize translation rules.
@ -204,29 +209,38 @@ namespace MinecraftClient.Protocol
if (!File.Exists(Language_File))
{
ConsoleIO.WriteLineFormatted(Translations.Get("chat.download", Settings.Language));
HttpClient httpClient = new();
try
{
string assets_index = DownloadString(Settings.TranslationsFile_Website_Index);
Task<string> fetch_index = httpClient.GetStringAsync(Settings.TranslationsFile_Website_Index);
fetch_index.Wait();
string assets_index = fetch_index.Result;
fetch_index.Dispose();
string[] tmp = assets_index.Split(new string[] { "minecraft/lang/" + Settings.Language.ToLower() + ".json" }, StringSplitOptions.None);
tmp = tmp[1].Split(new string[] { "hash\": \"" }, StringSplitOptions.None);
string hash = tmp[1].Split('"')[0]; //Translations file identifier on Mojang's servers
string translation_file_location = Settings.TranslationsFile_Website_Download + '/' + hash.Substring(0, 2) + '/' + hash;
string translation_file_location = Settings.TranslationsFile_Website_Download + '/' + hash[..2] + '/' + hash;
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("chat.request", translation_file_location));
StringBuilder stringBuilder = new StringBuilder();
foreach (KeyValuePair<string, Json.JSONData> entry in Json.ParseJson(DownloadString(translation_file_location)).Properties)
{
stringBuilder.Append(entry.Key + "=" + entry.Value.StringValue + Environment.NewLine);
}
Task<string> fetch_file = httpClient.GetStringAsync(translation_file_location);
fetch_file.Wait();
string translation_file = fetch_file.Result;
fetch_file.Dispose();
StringBuilder stringBuilder = new();
foreach (KeyValuePair<string, Json.JSONData> entry in Json.ParseJson(translation_file).Properties)
stringBuilder.Append(entry.Key).Append('=').Append(entry.Value.StringValue).Append(Environment.NewLine);
File.WriteAllText(Language_File, stringBuilder.ToString());
ConsoleIO.WriteLineFormatted(Translations.Get("chat.done", Language_File));
}
catch
{
Translations.WriteLineFormatted("chat.fail");
}
httpClient.Dispose();
}
//Download Failed? Defaulting to en_GB.lang if the game is installed
@ -275,7 +289,7 @@ namespace MinecraftClient.Protocol
{
int using_idx = 0;
string rule = TranslationRules[rulename];
StringBuilder result = new StringBuilder();
StringBuilder result = new();
for (int i = 0; i < rule.Length; i++)
{
if (rule[i] == '%' && i + 1 < rule.Length)
@ -354,7 +368,7 @@ namespace MinecraftClient.Protocol
}
else if (data.Properties.ContainsKey("translate"))
{
List<string> using_data = new List<string>();
List<string> using_data = new();
if (data.Properties.ContainsKey("using") && !data.Properties.ContainsKey("with"))
data.Properties["with"] = data.Properties["using"];
if (data.Properties.ContainsKey("with"))
@ -383,22 +397,5 @@ namespace MinecraftClient.Protocol
return "";
}
/// <summary>
/// Do a HTTP request to get a webpage or text data from a server file
/// </summary>
/// <param name="url">URL of resource</param>
/// <returns>Returns resource data if success, otherwise a WebException is raised</returns>
private static string DownloadString(string url)
{
System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
myRequest.Method = "GET";
System.Net.WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
return result;
}
}
}

View file

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol.Message
{
@ -11,7 +8,7 @@ namespace MinecraftClient.Protocol.Message
/// </summary>
public class LastSeenMessageList
{
public static readonly LastSeenMessageList EMPTY = new(new Entry[0]);
public static readonly LastSeenMessageList EMPTY = new(Array.Empty<Entry>());
public static readonly int MAX_ENTRIES = 5;
public Entry[] entries;
@ -103,7 +100,7 @@ namespace MinecraftClient.Protocol.Message
entries[size++] = lastEntry;
LastSeenMessageList.Entry[] msgList = new LastSeenMessageList.Entry[size];
for (int i = 0; i < size; ++i)
for (int i = 0; i < size; ++i)
msgList[i] = entries[i];
lastSeenMessages = new LastSeenMessageList(msgList);
}