Skip to content

Commit da2b3ee

Browse files
author
Stewart Miles
committed
Silence duplicate warnings about being unable to set console encoding.
In some versions of Unity (e.g 2019.1.11f1) it's not possible to set the console encoding. This changes the CommandLine module to warn only once when this occurs. Fixes #214 Change-Id: I8f50d9d252b37621627a12e80429b446e34183b0
1 parent 652b68b commit da2b3ee

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

source/PlayServicesResolver/src/CommandLine.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace GooglePlayServices
1919
using System.Collections.Generic;
2020
using System.Diagnostics;
2121
using System.IO;
22+
using System.Text;
2223
using System.Text.RegularExpressions;
2324
using System.Threading;
2425
using System;
@@ -220,7 +221,7 @@ private void Read()
220221
byte[] copy = new byte[bytesRead];
221222
Array.Copy(buffer, copy, copy.Length);
222223
DataReceived(new StreamData(
223-
handle, System.Text.Encoding.UTF8.GetString(copy), copy,
224+
handle, Encoding.UTF8.GetString(copy), copy,
224225
complete));
225226
}
226227
}
@@ -511,6 +512,11 @@ public static Result Run(string toolPath, string arguments, string workingDirect
511512
envVars: envVars, ioHandler: ioHandler, useShellExecution: false);
512513
}
513514

515+
/// <summary>
516+
/// Whether the console configuration warning has been displayed.
517+
/// </summary>
518+
private static bool displayedConsoleConfigurationWarning = false;
519+
514520
/// <summary>
515521
/// Execute a command line tool.
516522
/// </summary>
@@ -537,19 +543,31 @@ public static Result RunViaShell(
537543
Dictionary<string, string> envVars = null,
538544
IOHandler ioHandler = null, bool useShellExecution = false,
539545
bool stdoutRedirectionInShellMode = true) {
540-
System.Text.Encoding inputEncoding = Console.InputEncoding;
541-
System.Text.Encoding outputEncoding = Console.OutputEncoding;
546+
bool consoleConfigurationWarning = false;
547+
Encoding inputEncoding = Console.InputEncoding;
548+
Encoding outputEncoding = Console.OutputEncoding;
542549
// Android SDK manager requires the input encoding to be set to
543550
// UFT8 to pipe data to the tool.
544551
// Cocoapods requires the output encoding to be set to UTF8 to
545552
// retrieve output.
546553
try {
547-
Console.InputEncoding = System.Text.Encoding.UTF8;
548-
Console.OutputEncoding = System.Text.Encoding.UTF8;
554+
var utf8Type = Encoding.UTF8.GetType();
555+
// Only compare the type of the encoding class since the configuration shouldn't
556+
// matter.
557+
if (inputEncoding.GetType() != utf8Type) {
558+
Console.InputEncoding = Encoding.UTF8;
559+
}
560+
if (outputEncoding.GetType() != utf8Type) {
561+
Console.OutputEncoding = Encoding.UTF8;
562+
}
549563
} catch (Exception e) {
550-
UnityEngine.Debug.LogWarning(String.Format(
551-
"Unable to set console input / output encoding to UTF8 (e.g en_US.UTF8-8). " +
552-
"Some commands may fail. {0}", e));
564+
if (!displayedConsoleConfigurationWarning) {
565+
UnityEngine.Debug.LogWarning(String.Format(
566+
"Unable to set console input / output encoding from {0} & {1} to {2} " +
567+
"(e.g en_US.UTF8-8). Some commands may fail. {3}",
568+
inputEncoding, outputEncoding, Encoding.UTF8, e));
569+
consoleConfigurationWarning = true;
570+
}
553571
}
554572

555573
// Mono 3.x on Windows can't execute tools with single quotes (apostrophes) in the path.
@@ -655,13 +673,21 @@ public static Result RunViaShell(
655673
result.message = FormatResultMessage(toolPath, arguments, result.stdout,
656674
result.stderr, result.exitCode);
657675
try {
658-
Console.InputEncoding = inputEncoding;
659-
Console.OutputEncoding = outputEncoding;
676+
if (Console.InputEncoding != inputEncoding) {
677+
Console.InputEncoding = inputEncoding;
678+
}
679+
if (Console.OutputEncoding != outputEncoding) {
680+
Console.OutputEncoding = outputEncoding;
681+
}
660682
} catch (Exception e) {
661-
UnityEngine.Debug.LogWarning(String.Format(
662-
"Unable to restore console input / output encoding to {0} & {1}. {2}",
663-
inputEncoding, outputEncoding, e));
683+
if (!displayedConsoleConfigurationWarning) {
684+
UnityEngine.Debug.LogWarning(String.Format(
685+
"Unable to restore console input / output encoding to {0} & {1}. {2}",
686+
inputEncoding, outputEncoding, e));
687+
consoleConfigurationWarning = true;
688+
}
664689
}
690+
if (consoleConfigurationWarning) displayedConsoleConfigurationWarning = true;
665691
return result;
666692
}
667693

0 commit comments

Comments
 (0)