Read all files as UTF-8 (#1035)

Fix encoding issues with some languages such as Chinese
by forcing UTF-8 everywhere. Configuration files written
in ANSI/Latin-1 and other encodings will need conversion.
This commit is contained in:
ORelio 2020-05-29 20:23:03 +02:00
parent cf9bc4c3d5
commit 20fb2323a4
9 changed files with 12 additions and 10 deletions

View file

@ -399,7 +399,7 @@ namespace MinecraftClient
{ {
string[] lines = null; string[] lines = null;
ChatBots.Script.LookForScript(ref script); ChatBots.Script.LookForScript(ref script);
try { lines = File.ReadAllLines(script); } try { lines = File.ReadAllLines(script, Encoding.UTF8); }
catch (Exception e) { throw new CSharpException(CSErrorType.FileReadError, e); } catch (Exception e) { throw new CSharpException(CSErrorType.FileReadError, e); }
return CSharpRunner.Run(this, tickHandler, lines, args, localVars); return CSharpRunner.Run(this, tickHandler, lines, args, localVars);
} }

View file

@ -791,7 +791,7 @@ namespace MinecraftClient
{ {
//Read all lines from file, remove lines with no text, convert to lowercase, //Read all lines from file, remove lines with no text, convert to lowercase,
//remove duplicate entries, convert to a string array, and return the result. //remove duplicate entries, convert to a string array, and return the result.
return File.ReadAllLines(file) return File.ReadAllLines(file, Encoding.UTF8)
.Where(line => !String.IsNullOrWhiteSpace(line)) .Where(line => !String.IsNullOrWhiteSpace(line))
.Select(line => line.ToLower()) .Select(line => line.ToLower())
.Distinct().ToArray(); .Distinct().ToArray();

View file

@ -45,7 +45,7 @@ namespace MinecraftClient.ChatBots
if (Settings.DebugMessages) if (Settings.DebugMessages)
LogToConsole("Loading messages from file: " + System.IO.Path.GetFullPath(Settings.AutoRelog_KickMessagesFile)); LogToConsole("Loading messages from file: " + System.IO.Path.GetFullPath(Settings.AutoRelog_KickMessagesFile));
dictionary = System.IO.File.ReadAllLines(Settings.AutoRelog_KickMessagesFile); dictionary = System.IO.File.ReadAllLines(Settings.AutoRelog_KickMessagesFile, Encoding.UTF8);
for (int i = 0; i < dictionary.Length; i++) for (int i = 0; i < dictionary.Length; i++)
{ {

View file

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace MinecraftClient.ChatBots namespace MinecraftClient.ChatBots
@ -143,7 +144,7 @@ namespace MinecraftClient.ChatBots
if (Settings.DebugMessages) if (Settings.DebugMessages)
LogToConsole("Loading matches from file: " + System.IO.Path.GetFullPath(matchesFile)); LogToConsole("Loading matches from file: " + System.IO.Path.GetFullPath(matchesFile));
foreach (string lineRAW in File.ReadAllLines(matchesFile)) foreach (string lineRAW in File.ReadAllLines(matchesFile, Encoding.UTF8))
{ {
string line = lineRAW.Split('#')[0].Trim(); string line = lineRAW.Split('#')[0].Trim();
if (line.Length > 0) if (line.Length > 0)

View file

@ -143,7 +143,7 @@ namespace MinecraftClient.ChatBots
{ {
if (System.IO.File.Exists(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR)) if (System.IO.File.Exists(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR))
{ {
string[] dico = System.IO.File.ReadAllLines(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR); string[] dico = System.IO.File.ReadAllLines(English ? Settings.Hangman_FileWords_EN : Settings.Hangman_FileWords_FR, Encoding.UTF8);
return dico[new Random().Next(dico.Length)]; return dico[new Random().Next(dico.Length)];
} }
else else

View file

@ -132,7 +132,7 @@ namespace MinecraftClient.ChatBots
//Load the given file from the startup parameters //Load the given file from the startup parameters
if (LookForScript(ref file)) if (LookForScript(ref file))
{ {
lines = System.IO.File.ReadAllLines(file); lines = System.IO.File.ReadAllLines(file, Encoding.UTF8);
csharp = file.EndsWith(".cs"); csharp = file.EndsWith(".cs");
thread = null; thread = null;

View file

@ -47,7 +47,7 @@ namespace MinecraftClient.ChatBots
if (Settings.DebugMessages) if (Settings.DebugMessages)
LogToConsole("Loading tasks from '" + System.IO.Path.GetFullPath(tasksfile) + "'"); LogToConsole("Loading tasks from '" + System.IO.Path.GetFullPath(tasksfile) + "'");
TaskDesc current_task = null; TaskDesc current_task = null;
String[] lines = System.IO.File.ReadAllLines(tasksfile); String[] lines = System.IO.File.ReadAllLines(tasksfile, Encoding.UTF8);
foreach (string lineRAW in lines) foreach (string lineRAW in lines)
{ {
string line = lineRAW.Split('#')[0].Trim(); string line = lineRAW.Split('#')[0].Trim();

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
using System.Text;
namespace MinecraftClient.Mapping.BlockPalettes namespace MinecraftClient.Mapping.BlockPalettes
{ {
@ -11,7 +12,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
public static class BlockPaletteGenerator public static class BlockPaletteGenerator
{ {
/// <summary> /// <summary>
/// Generate mapping from Minecraft blocks.jsom /// Generate mapping from Minecraft blocks.json
/// </summary> /// </summary>
/// <param name="blocksJsonFile">path to blocks.json</param> /// <param name="blocksJsonFile">path to blocks.json</param>
/// <param name="outputClass">output path for blocks.cs</param> /// <param name="outputClass">output path for blocks.cs</param>
@ -23,7 +24,7 @@ namespace MinecraftClient.Mapping.BlockPalettes
HashSet<int> knownStates = new HashSet<int>(); HashSet<int> knownStates = new HashSet<int>();
Dictionary<string, HashSet<int>> blocks = new Dictionary<string, HashSet<int>>(); Dictionary<string, HashSet<int>> blocks = new Dictionary<string, HashSet<int>>();
Json.JSONData palette = Json.ParseJson(File.ReadAllText(blocksJsonFile)); Json.JSONData palette = Json.ParseJson(File.ReadAllText(blocksJsonFile, Encoding.UTF8));
foreach (KeyValuePair<string, Json.JSONData> item in palette.Properties) foreach (KeyValuePair<string, Json.JSONData> item in palette.Properties)
{ {
//minecraft:item_name => ItemName //minecraft:item_name => ItemName

View file

@ -1633,7 +1633,7 @@ namespace MinecraftClient
public void OnExplosion(Location location, float strength, int affectedBlocks) public void OnExplosion(Location location, float strength, int affectedBlocks)
{ {
foreach (ChatBot bot in bots.ToArray()) foreach (ChatBot bot in bots.ToArray())
bot.OnExplosion(explode, strength, ExplosionRecordCount); bot.OnExplosion(location, strength, affectedBlocks);
} }
/// <summary> /// <summary>