Skip to content

Commit 10569b2

Browse files
committed
fix windows encoding issue
1 parent 13a70cd commit 10569b2

4 files changed

Lines changed: 45 additions & 31 deletions

File tree

ProjBobcat/ProjBobcat/Class/Helper/InitHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23
using Microsoft.Extensions.DependencyInjection;
34
using ProjBobcat.Class.Helper.Download;
45
using ProjBobcat.Interface;
@@ -16,13 +17,12 @@ public static IServiceCollection UseProjBobcat(
1617
var coreSettings = coreSettingsProvider();
1718
var userAgent = coreSettings.DefaultUserAgent ?? DownloadHelper.DefaultUserAgent;
1819

20+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
21+
1922
services.AddSingleton(_ => coreSettings);
2023

2124
services.AddHttpClient(DownloadHelper.DefaultDownloadClientName,
22-
client =>
23-
{
24-
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
25-
});
25+
client => { client.DefaultRequestHeaders.Add("User-Agent", userAgent); });
2626

2727
services.AddHttpClient<IModrinthApiService, ModrinthApiService>(client =>
2828
{

ProjBobcat/ProjBobcat/DefaultComponent/Installer/ModPackInstaller/CurseForgeInstaller.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task InstallTaskAsync()
4646
ArgumentNullException.ThrowIfNull(manifest, "无法读取到 CurseForge 的 manifest 文件");
4747

4848
var idPath = Path.Combine(this.RootPath, GamePathHelper.GetGamePath(this.GameId));
49-
49+
5050
this.NeedToDownload = manifest.Files?.Length ?? 0;
5151

5252
var fileIds = manifest.Files
@@ -214,8 +214,6 @@ .. await GetModProjectDetails(this.CurseForgeApiService, missingProjectIds, true
214214
}
215215

216216
var modPackFullPath = Path.GetFullPath(this.ModPackPath);
217-
218-
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
219217
var gbk = Encoding.GetEncoding("GBK");
220218

221219
await using var modPackFs = File.OpenRead(modPackFullPath);
@@ -267,7 +265,8 @@ .. await GetModProjectDetails(this.CurseForgeApiService, missingProjectIds, true
267265
TotalDownloaded == 0 &&
268266
downloadFiles.Count != 0)
269267
{
270-
throw new Exception("我们无法下载这个整合包中的模组,这可能是因为您和 CurseForge 的网络连接不稳定导致的。尽管如此,我们还是完成了整合包安装的其他步骤。您可以稍后尝试重新安装或是手动下载整合包模组。");
268+
throw new Exception(
269+
"我们无法下载这个整合包中的模组,这可能是因为您和 CurseForge 的网络连接不稳定导致的。尽管如此,我们还是完成了整合包安装的其他步骤。您可以稍后尝试重新安装或是手动下载整合包模组。");
271270
}
272271

273272
if (!this.FailedFiles.IsEmpty)
@@ -398,7 +397,8 @@ async Task<CurseForgeAddonInfo[]> RetryLogicAsync()
398397
var rightTask = GetModProjectDetails(curseForgeApiService, ids[mid..], true);
399398
var files = await Task.WhenAll(leftTask, rightTask);
400399

401-
return [
400+
return
401+
[
402402
.. files[0],
403403
.. files[1]
404404
];

ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLaunchArgumentParser.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Text;
5-
using ProjBobcat.Class;
1+
using ProjBobcat.Class;
62
using ProjBobcat.Class.Helper;
73
using ProjBobcat.Class.Model;
84
using ProjBobcat.Class.Model.Auth;
95
using ProjBobcat.Class.Model.LauncherProfile;
106
using ProjBobcat.Class.Model.Version;
117
using ProjBobcat.Interface;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Globalization;
11+
using System.IO;
12+
using System.Text;
1213

1314
namespace ProjBobcat.DefaultComponent.Launch;
1415

@@ -129,9 +130,19 @@ public IEnumerable<string> ParseJvmArguments(
129130

130131
#region Set Output Encoding
131132

132-
yield return "-Dfile.encoding=UTF-8";
133-
yield return "-Dstdout.encoding=UTF-8";
134-
yield return "-Dstderr.encoding=UTF-8";
133+
if (OperatingSystem.IsWindows())
134+
{
135+
var ansi = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
136+
yield return $"-Dfile.encoding=windows-{ansi}";
137+
yield return $"-Dstdout.encoding=windows-{ansi}";
138+
yield return $"-Dstderr.encoding=windows-{ansi}";
139+
}
140+
else
141+
{
142+
yield return "-Dfile.encoding=UTF-8";
143+
yield return "-Dstdout.encoding=UTF-8";
144+
yield return "-Dstderr.encoding=UTF-8";
145+
}
135146

136147
#endregion
137148

@@ -184,21 +195,22 @@ public IEnumerable<string> ParseGameArguments(
184195
Guid.Empty.ToString("D"))
185196
.Replace("-", string.Empty).ToUpper();
186197
var clientIdBytes = Encoding.ASCII.GetBytes(clientIdUpper);
187-
var clientId = Convert.ToBase64String(clientIdBytes);
188-
198+
var clientId = Convert.ToBase64String(clientIdBytes);
199+
189200
var castVersionInfo = (VersionInfo)versionInfo;
190201

191202
var userType = authResult switch
192203
{
193204
MicrosoftAuthResult => "msa",
194-
YggdrasilAuthResult when new ComparableVersion(castVersionInfo.GameBaseVersion) > new ComparableVersion("1.18.2") => "msa",
205+
YggdrasilAuthResult when new ComparableVersion(castVersionInfo.GameBaseVersion) >
206+
new ComparableVersion("1.18.2") => "msa",
195207
_ => "Mojang"
196208
};
197209
var xuid = authResult is MicrosoftAuthResult microsoftAuthResult
198210
? microsoftAuthResult.XBoxUid ?? Guid.Empty.ToString("N")
199211
: Guid.Empty.ToString("N");
200212

201-
213+
202214
var assetRoot = Path.Combine(this.RootPath, GamePathHelper.GetAssetsRoot());
203215
var mcArgumentsDic = new Dictionary<string, string>
204216
{

ProjBobcat/ProjBobcat/DefaultComponent/Launch/GameCore/DefaultGameCore.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
using System;
1+
using Microsoft.Extensions.Configuration;
2+
using ProjBobcat.Class;
3+
using ProjBobcat.Class.Helper;
4+
using ProjBobcat.Class.Model;
5+
using ProjBobcat.Class.Model.YggdrasilAuth;
6+
using ProjBobcat.DefaultComponent.Authenticator;
7+
using ProjBobcat.Event;
8+
using System;
29
using System.Collections.Frozen;
310
using System.Collections.Generic;
411
using System.Diagnostics;
12+
using System.Globalization;
513
using System.IO;
614
using System.IO.Compression;
715
using System.Linq;
@@ -11,13 +19,6 @@
1119
using System.Threading.Tasks;
1220
using Windows.Win32;
1321
using Windows.Win32.Foundation;
14-
using Microsoft.Extensions.Configuration;
15-
using ProjBobcat.Class;
16-
using ProjBobcat.Class.Helper;
17-
using ProjBobcat.Class.Model;
18-
using ProjBobcat.Class.Model.YggdrasilAuth;
19-
using ProjBobcat.DefaultComponent.Authenticator;
20-
using ProjBobcat.Event;
2122
using FileInfo = System.IO.FileInfo;
2223

2324
namespace ProjBobcat.DefaultComponent.Launch.GameCore;
@@ -76,9 +77,10 @@ private static async Task<ProcessStartInfo> StartGameInShellInWindows(
7677
string rootPath,
7778
string command)
7879
{
80+
var ansi = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
7981
var tempScriptPath = Path.Combine(Path.GetTempPath(), "launch_java_command.bat");
8082
var fileContent = $"""
81-
chcp 65001
83+
chcp {ansi}
8284
@echo off
8385
cd /d "{rootPath}"
8486
{command}
@@ -87,7 +89,7 @@ @echo off
8789

8890
fileContent = CrLfRegex().Replace(fileContent, "\r\n");
8991

90-
await File.WriteAllTextAsync(tempScriptPath, fileContent);
92+
await File.WriteAllTextAsync(tempScriptPath, fileContent, Encoding.GetEncoding(ansi));
9193

9294
return new ProcessStartInfo("cmd.exe", $"/k \"{tempScriptPath}\"")
9395
{

0 commit comments

Comments
 (0)