Skip to content

Commit 233c615

Browse files
committed
improve log resolver and download logic
1 parent 0bf3ffc commit 233c615

7 files changed

Lines changed: 240 additions & 127 deletions

File tree

ProjBobcat/ProjBobcat/Class/Helper/Download/DownloadHelper.Multipart.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,28 @@ public static async Task MultiPartDownloadTaskAsync(
131131

132132
var filePath = Path.Combine(downloadFile.DownloadPath, downloadFile.FileName);
133133
var trials = downloadSettings.RetryCount <= 0 ? 1 : downloadSettings.RetryCount;
134-
var downloadUrl = downloadFile.GetDownloadUrl();
135-
136-
// Check if server supports partial downloads
137-
using var checkCts = new CancellationTokenSource(TimeSpan.FromSeconds(8));
138-
var urlInfo = await CheckPartialDownloadSupportAsync(downloadUrl, downloadSettings, checkCts.Token);
139-
140-
// Fallback to normal download if not supported or file too small
141-
if (urlInfo is not { SupportsRangeRequests: true } || urlInfo.FileLength < MinimumChunkSize)
142-
{
143-
downloadFile.RetryCount = 0;
144-
await DownloadData(downloadFile, downloadSettings);
145-
return;
146-
}
147134

148135
var exceptions = new List<Exception>();
149136

150137
// Retry loop for multipart download
151138
while (downloadFile.RetryCount < trials)
152139
{
140+
// Pick URL per attempt so MultiSourceDownloadFile can rotate mirrors when RetryCount advances.
141+
var downloadUrl = downloadFile.GetDownloadUrl();
142+
143+
using var checkCts = new CancellationTokenSource(TimeSpan.FromSeconds(8));
144+
var urlInfo = await CheckPartialDownloadSupportAsync(downloadUrl, downloadSettings, checkCts.Token);
145+
146+
// Fallback to normal download if not supported or file too small
147+
if (urlInfo is not { SupportsRangeRequests: true } || urlInfo.FileLength < MinimumChunkSize)
148+
{
149+
downloadFile.RetryCount = 0;
150+
await DownloadData(downloadFile, downloadSettings);
151+
return;
152+
}
153+
154+
exceptions.Clear();
155+
153156
// Initialize for multipart download
154157
var globalSpeedCalculator = new DownloadSpeedCalculator();
155158
using var chunkManager = new ChunkManager(downloadSettings);

ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using System;
22
using System.Collections.Frozen;
33
using System.Collections.Generic;
44
using System.Globalization;
5+
using System.Runtime.InteropServices;
56
using System.Text;
67

78
namespace ProjBobcat.Class.Helper;
@@ -75,11 +76,12 @@ public static class EncodingHelper
7576

7677
public static Encoding GetUtf8NoBomOrAnsi(bool preferUtf8)
7778
{
78-
var encoding = preferUtf8
79-
? new UTF8Encoding(false)
80-
: Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
79+
// macOS and Linux use UTF-8 natively, and Java 18+ defaults to UTF-8 output.
80+
// ANSI code pages are only meaningful on Windows.
81+
if (preferUtf8 || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
82+
return new UTF8Encoding(false);
8183

82-
return encoding;
84+
return Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
8385
}
8486

8587
public static string GetJavaCharsetForAnsi(int cp)

ProjBobcat/ProjBobcat/Class/LaunchWrapper.cs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -89,58 +89,46 @@ void ProcessOnExited(object? sender, EventArgs e)
8989
void ProcessOnErrorDataReceived(object sender, DataReceivedEventArgs e)
9090
{
9191
if (string.IsNullOrEmpty(e.Data)) return;
92+
if (this.GameCore is not GameCoreBase coreBase) return;
9293

93-
if (this.GameCore is GameCoreBase coreBase)
94+
if (this.GameCore.GameLogResolver != null)
95+
{
96+
var entry = this.GameCore.GameLogResolver.Resolve(e.Data);
97+
coreBase.OnLogGameData(sender, EntryToEventArgs(entry));
98+
}
99+
else
100+
{
94101
coreBase.OnLogGameData(sender, new GameLogEventArgs
95102
{
96103
LogType = GameLogType.Unknown,
97104
RawContent = e.Data
98105
});
106+
}
99107
}
100108

101109
void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs e)
102110
{
103111
if (string.IsNullOrEmpty(e.Data)) return;
104112
if (this.GameCore.GameLogResolver == null) return;
113+
if (this.GameCore is not GameCoreBase coreBase) return;
105114

106-
var totalPrefix = this.GameCore.GameLogResolver.ResolveTotalPrefix(e.Data);
107-
var type = this.GameCore.GameLogResolver.ResolveLogType(string.IsNullOrEmpty(totalPrefix)
108-
? e.Data
109-
: totalPrefix);
110-
111-
if (type is GameLogType.ExceptionMessage or GameLogType.StackTrace)
112-
{
113-
var exceptionMsg = this.GameCore.GameLogResolver.ResolveExceptionMsg(e.Data);
114-
var stackTrace = this.GameCore.GameLogResolver.ResolveStackTrace(e.Data);
115-
116-
if (this.GameCore is GameCoreBase gameCoreBase)
117-
gameCoreBase.OnLogGameData(sender, new GameLogEventArgs
118-
{
119-
LogType = type,
120-
RawContent = e.Data,
121-
StackTrace = stackTrace,
122-
ExceptionMsg = exceptionMsg
123-
});
124-
125-
return;
126-
}
127-
128-
var time = this.GameCore.GameLogResolver.ResolveTime(totalPrefix);
129-
var source = this.GameCore.GameLogResolver.ResolveSource(totalPrefix);
130-
131-
132-
if (this.GameCore is GameCoreBase coreBase)
133-
coreBase.OnLogGameData(sender, new GameLogEventArgs
134-
{
135-
GameId = this.LaunchSettings.Version,
136-
LogType = type,
137-
RawContent = e.Data,
138-
Content = e.Data[totalPrefix.Length..],
139-
Source = source,
140-
Time = time
141-
});
115+
var entry = this.GameCore.GameLogResolver.Resolve(e.Data);
116+
coreBase.OnLogGameData(sender, EntryToEventArgs(entry));
142117
}
143118

119+
GameLogEventArgs EntryToEventArgs(GameLogEntry entry) => new()
120+
{
121+
GameId = this.LaunchSettings.Version,
122+
LogType = entry.LogType,
123+
RawContent = entry.RawContent,
124+
Content = entry.Content,
125+
Source = entry.Source,
126+
Thread = entry.Thread,
127+
Time = entry.Time,
128+
ExceptionMsg = entry.ExceptionMsg,
129+
StackTrace = entry.StackTrace
130+
};
131+
144132
void Dispose(bool disposing)
145133
{
146134
if (!this._disposedValue)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace ProjBobcat.Class.Model;
2+
3+
public readonly record struct GameLogEntry
4+
{
5+
public GameLogType LogType { get; init; }
6+
public string? Time { get; init; }
7+
public string? Thread { get; init; }
8+
public string? Source { get; init; }
9+
public string? Content { get; init; }
10+
public string? ExceptionMsg { get; init; }
11+
public string? StackTrace { get; init; }
12+
public string? RawContent { get; init; }
13+
}

0 commit comments

Comments
 (0)