Removed debug logs

This commit is contained in:
Anon 2024-02-28 11:49:50 +01:00
parent bc0781cee9
commit 744de0dbd4

View file

@ -34,11 +34,12 @@ namespace MinecraftClient.Protocol.Message
public static void ReadChatType(Dictionary<string, object> registryCodec) public static void ReadChatType(Dictionary<string, object> registryCodec)
{ {
Dictionary<int, MessageType> chatTypeDictionary = ChatId2Type ?? new(); Dictionary<int, MessageType> chatTypeDictionary = ChatId2Type ?? new();
var chatTypeListNbt = (object[])(((Dictionary<string, object>)registryCodec["minecraft:chat_type"])["value"]); var chatTypeListNbt =
(object[])(((Dictionary<string, object>)registryCodec["minecraft:chat_type"])["value"]);
foreach (var (chatName, chatId) in from Dictionary<string, object> chatTypeNbt in chatTypeListNbt foreach (var (chatName, chatId) in from Dictionary<string, object> chatTypeNbt in chatTypeListNbt
let chatName = (string)chatTypeNbt["name"] let chatName = (string)chatTypeNbt["name"]
let chatId = (int)chatTypeNbt["id"] let chatId = (int)chatTypeNbt["id"]
select (chatName, chatId)) select (chatName, chatId))
{ {
chatTypeDictionary[chatId] = chatName switch chatTypeDictionary[chatId] = chatName switch
{ {
@ -52,6 +53,7 @@ namespace MinecraftClient.Protocol.Message
_ => MessageType.CHAT, _ => MessageType.CHAT,
}; };
} }
ChatId2Type = chatTypeDictionary; ChatId2Type = chatTypeDictionary;
} }
@ -147,6 +149,7 @@ namespace MinecraftClient.Protocol.Message
default: default:
goto case MessageType.CHAT; goto case MessageType.CHAT;
} }
return text; return text;
} }
@ -198,7 +201,14 @@ namespace MinecraftClient.Protocol.Message
/// Initialize translation rules. /// Initialize translation rules.
/// Necessary for properly printing some chat messages. /// Necessary for properly printing some chat messages.
/// </summary> /// </summary>
public static void InitTranslations() { if (!RulesInitialized) { InitRules(); RulesInitialized = true; } } public static void InitTranslations()
{
if (!RulesInitialized)
{
InitRules();
RulesInitialized = true;
}
}
/// <summary> /// <summary>
/// Internal rule initialization method. Looks for local rule file or download it from Mojang asset servers. /// Internal rule initialization method. Looks for local rule file or download it from Mojang asset servers.
@ -207,7 +217,9 @@ namespace MinecraftClient.Protocol.Message
{ {
if (Config.Main.Advanced.Language == "en_us") if (Config.Main.Advanced.Language == "en_us")
{ {
TranslationRules = JsonSerializer.Deserialize<Dictionary<string, string>>((byte[])MinecraftAssets.ResourceManager.GetObject("en_us.json")!)!; TranslationRules =
JsonSerializer.Deserialize<Dictionary<string, string>>(
(byte[])MinecraftAssets.ResourceManager.GetObject("en_us.json")!)!;
return; return;
} }
@ -222,13 +234,19 @@ namespace MinecraftClient.Protocol.Message
{ {
try try
{ {
TranslationRules = JsonSerializer.Deserialize<Dictionary<string, string>>(File.OpenRead(languageFilePath))!; TranslationRules =
JsonSerializer.Deserialize<Dictionary<string, string>>(File.OpenRead(languageFilePath))!;
}
catch (IOException)
{
}
catch (JsonException)
{
} }
catch (IOException) { }
catch (JsonException) { }
} }
if (TranslationRules.TryGetValue("Version", out string? version) && version == Settings.TranslationsFile_Version) if (TranslationRules.TryGetValue("Version", out string? version) &&
version == Settings.TranslationsFile_Version)
{ {
if (Config.Logging.DebugMessages) if (Config.Logging.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.chat_loaded, acceptnewlines: true); ConsoleIO.WriteLineFormatted(Translations.chat_loaded, acceptnewlines: true);
@ -236,32 +254,39 @@ namespace MinecraftClient.Protocol.Message
} }
// Try downloading language file from Mojang's servers? // Try downloading language file from Mojang's servers?
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.chat_download, Config.Main.Advanced.Language)); ConsoleIO.WriteLineFormatted(
"§8" + string.Format(Translations.chat_download, Config.Main.Advanced.Language));
HttpClient httpClient = new(); HttpClient httpClient = new();
try try
{ {
Task<string> fetch_index = httpClient.GetStringAsync(TranslationsFile_Website_Index); Task<string> fetch_index = httpClient.GetStringAsync(TranslationsFile_Website_Index);
fetch_index.Wait(); fetch_index.Wait();
Match match = Regex.Match(fetch_index.Result, $"minecraft/lang/{Config.Main.Advanced.Language}.json" + @""":\s\{""hash"":\s""([\d\w]{40})"""); Match match = Regex.Match(fetch_index.Result,
$"minecraft/lang/{Config.Main.Advanced.Language}.json" + @""":\s\{""hash"":\s""([\d\w]{40})""");
fetch_index.Dispose(); fetch_index.Dispose();
if (match.Success && match.Groups.Count == 2) if (match.Success && match.Groups.Count == 2)
{ {
string hash = match.Groups[1].Value; string hash = match.Groups[1].Value;
string translation_file_location = TranslationsFile_Website_Download + '/' + hash[..2] + '/' + hash; string translation_file_location = TranslationsFile_Website_Download + '/' + hash[..2] + '/' + hash;
if (Config.Logging.DebugMessages) if (Config.Logging.DebugMessages)
ConsoleIO.WriteLineFormatted(string.Format(Translations.chat_request, translation_file_location)); ConsoleIO.WriteLineFormatted(
string.Format(Translations.chat_request, translation_file_location));
Task<Dictionary<string, string>?> fetckFileTask = httpClient.GetFromJsonAsync<Dictionary<string, string>>(translation_file_location); Task<Dictionary<string, string>?> fetckFileTask =
httpClient.GetFromJsonAsync<Dictionary<string, string>>(translation_file_location);
fetckFileTask.Wait(); fetckFileTask.Wait();
if (fetckFileTask.Result != null && fetckFileTask.Result.Count > 0) if (fetckFileTask.Result != null && fetckFileTask.Result.Count > 0)
{ {
TranslationRules = fetckFileTask.Result; TranslationRules = fetckFileTask.Result;
TranslationRules["Version"] = TranslationsFile_Version; TranslationRules["Version"] = TranslationsFile_Version;
File.WriteAllText(languageFilePath, JsonSerializer.Serialize(TranslationRules, typeof(Dictionary<string, string>)), Encoding.UTF8); File.WriteAllText(languageFilePath,
JsonSerializer.Serialize(TranslationRules, typeof(Dictionary<string, string>)),
Encoding.UTF8);
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.chat_done, languageFilePath)); ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.chat_done, languageFilePath));
return; return;
} }
fetckFileTask.Dispose(); fetckFileTask.Dispose();
} }
else else
@ -275,7 +300,8 @@ namespace MinecraftClient.Protocol.Message
} }
catch (IOException) catch (IOException)
{ {
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.chat_save_fail, languageFilePath), acceptnewlines: true); ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.chat_save_fail, languageFilePath),
acceptnewlines: true);
} }
catch (Exception e) catch (Exception e)
{ {
@ -289,7 +315,9 @@ namespace MinecraftClient.Protocol.Message
httpClient.Dispose(); httpClient.Dispose();
} }
TranslationRules = JsonSerializer.Deserialize<Dictionary<string, string>>((byte[])MinecraftAssets.ResourceManager.GetObject("en_us.json")!)!; TranslationRules =
JsonSerializer.Deserialize<Dictionary<string, string>>(
(byte[])MinecraftAssets.ResourceManager.GetObject("en_us.json")!)!;
ConsoleIO.WriteLine(Translations.chat_use_default); ConsoleIO.WriteLine(Translations.chat_use_default);
} }
@ -310,7 +338,12 @@ namespace MinecraftClient.Protocol.Message
/// <returns>Returns the formatted text according to the given data</returns> /// <returns>Returns the formatted text according to the given data</returns>
private static string TranslateString(string rulename, List<string> using_data) private static string TranslateString(string rulename, List<string> using_data)
{ {
if (!RulesInitialized) { InitRules(); RulesInitialized = true; } if (!RulesInitialized)
{
InitRules();
RulesInitialized = true;
}
if (TranslationRules.ContainsKey(rulename)) if (TranslationRules.ContainsKey(rulename))
{ {
int using_idx = 0; int using_idx = 0;
@ -334,8 +367,8 @@ namespace MinecraftClient.Protocol.Message
//Using specified string or int with %1$s, %2$s... //Using specified string or int with %1$s, %2$s...
else if (char.IsDigit(rule[i + 1]) else if (char.IsDigit(rule[i + 1])
&& i + 3 < rule.Length && rule[i + 2] == '$' && i + 3 < rule.Length && rule[i + 2] == '$'
&& (rule[i + 3] == 's' || rule[i + 3] == 'd')) && (rule[i + 3] == 's' || rule[i + 3] == 'd'))
{ {
int specified_idx = rule[i + 1] - '1'; int specified_idx = rule[i + 1] - '1';
if (using_data.Count > specified_idx) if (using_data.Count > specified_idx)
@ -347,8 +380,10 @@ namespace MinecraftClient.Protocol.Message
} }
} }
} }
result.Append(rule[i]); result.Append(rule[i]);
} }
return result.ToString(); return result.ToString();
} }
else return "[" + rulename + "] " + string.Join(" ", using_data); else return "[" + rulename + "] " + string.Join(" ", using_data);
@ -371,6 +406,7 @@ namespace MinecraftClient.Protocol.Message
{ {
colorcode = Color2tag(JSONData2String(data.Properties["color"], "", links)); colorcode = Color2tag(JSONData2String(data.Properties["color"], "", links));
} }
if (data.Properties.ContainsKey("clickEvent") && links != null) if (data.Properties.ContainsKey("clickEvent") && links != null)
{ {
Json.JSONData clickEvent = data.Properties["clickEvent"]; Json.JSONData clickEvent = data.Properties["clickEvent"];
@ -382,12 +418,14 @@ namespace MinecraftClient.Protocol.Message
links.Add(clickEvent.Properties["value"].StringValue); links.Add(clickEvent.Properties["value"].StringValue);
} }
} }
if (data.Properties.ContainsKey("extra")) if (data.Properties.ContainsKey("extra"))
{ {
Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray(); Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray();
foreach (Json.JSONData item in extras) foreach (Json.JSONData item in extras)
extra_result = extra_result + JSONData2String(item, colorcode, links) + "§r"; extra_result = extra_result + JSONData2String(item, colorcode, links) + "§r";
} }
if (data.Properties.ContainsKey("text")) if (data.Properties.ContainsKey("text"))
{ {
return colorcode + JSONData2String(data.Properties["text"], colorcode, links) + extra_result; return colorcode + JSONData2String(data.Properties["text"], colorcode, links) + extra_result;
@ -405,7 +443,10 @@ namespace MinecraftClient.Protocol.Message
using_data.Add(JSONData2String(array[i], colorcode, links)); using_data.Add(JSONData2String(array[i], colorcode, links));
} }
} }
return colorcode + TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) + extra_result;
return colorcode +
TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) +
extra_result;
} }
else return extra_result; else return extra_result;
@ -415,6 +456,7 @@ namespace MinecraftClient.Protocol.Message
{ {
result += JSONData2String(item, colorcode, links); result += JSONData2String(item, colorcode, links);
} }
return result; return result;
case Json.JSONData.DataType.String: case Json.JSONData.DataType.String:
@ -443,90 +485,70 @@ namespace MinecraftClient.Protocol.Message
switch (key) switch (key)
{ {
case "text": case "text":
{ {
message = (string)value; message = (string)value;
} }
break; break;
case "extra": case "extra":
{
object[] extras = (object[])value;
for (var i = 0; i < extras.Length; i++)
{ {
object[] extras = (object[])value; var extraDict = extras[i] switch
for (var i = 0; i < extras.Length; i++)
{ {
try int => new Dictionary<string, object> { { "text", $"{extras[i]}" } },
string => new Dictionary<string, object>
{ {
var extraDict = extras[i] switch { "text", (string)extras[i] }
{ },
int => new Dictionary<string, object> { { "text", $"{extras[i]}" } }, _ => (Dictionary<string, object>)extras[i]
string => new Dictionary<string, object> };
{
{ "text", (string)extras[i] }
},
_ => (Dictionary<string, object>)extras[i]
};
extraBuilder.Append(NbtToString(extraDict) + "§r"); extraBuilder.Append(NbtToString(extraDict) + "§r");
}
catch
{
ConsoleIO.WriteLine("[DEBUG] Full NBT object:" + JsonSerializer.Serialize(nbt));
ConsoleIO.WriteLine("[DEBUG] Full extras object:" + JsonSerializer.Serialize(extras));
ConsoleIO.WriteLine("[DEBUG] Value in question:" + JsonSerializer.Serialize(extras[i]));
ConsoleIO.WriteLine("[DEBUG] Full string builder so far:" + extraBuilder.ToString());
throw;
}
}
} }
}
break; break;
case "translate": case "translate":
{
if (nbt.TryGetValue("translate", out object translate))
{ {
if (nbt.TryGetValue("translate", out object translate)) var translateKey = (string)translate;
List<string> translateString = new();
if (nbt.TryGetValue("with", out object withComponent))
{ {
var translateKey = (string)translate; var withs = (object[])withComponent;
List<string> translateString = new(); for (var i = 0; i < withs.Length; i++)
if (nbt.TryGetValue("with", out object withComponent))
{ {
var withs = (object[])withComponent; var withDict = withs[i] switch
for (var i = 0; i < withs.Length; i++)
{ {
try int => new Dictionary<string, object> { { "text", $"{withs[i]}" } },
string => new Dictionary<string, object>
{ {
var withDict = withs[i] switch { "text", (string)withs[i] }
{ },
int => new Dictionary<string, object> { { "text", $"{withs[i]}" } }, _ => (Dictionary<string, object>)withs[i]
string => new Dictionary<string, object> };
{
{ "text", (string)withs[i] }
},
_ => (Dictionary<string, object>)withs[i]
};
translateString.Add(NbtToString(withDict)); translateString.Add(NbtToString(withDict));
}
catch
{
ConsoleIO.WriteLine("[DEBUG] Full NBT object:" + JsonSerializer.Serialize(nbt));
ConsoleIO.WriteLine("[DEBUG] Full withs object:" + JsonSerializer.Serialize(withs));
ConsoleIO.WriteLine("[DEBUG] Value in question:" + JsonSerializer.Serialize(withs[i]));
ConsoleIO.WriteLine("[DEBUG] Full string builder so far:" + extraBuilder.ToString());
throw;
}
}
} }
message = TranslateString(translateKey, translateString);
} }
message = TranslateString(translateKey, translateString);
} }
}
break; break;
case "color": case "color":
{
if (nbt.TryGetValue("color", out object color))
{ {
if (nbt.TryGetValue("color", out object color)) colorCode = Color2tag((string)color);
{
colorCode = Color2tag((string)color);
}
} }
}
break; break;
} }
} }
return colorCode + message + extraBuilder.ToString(); return colorCode + message + extraBuilder.ToString();
} }
} }
} }