Skip to content

Commit 1f08e14

Browse files
committed
Fix manual cherry-pick codes
1 parent 3064ff9 commit 1f08e14

File tree

19 files changed

+409
-357
lines changed

19 files changed

+409
-357
lines changed

CollapseLauncher/Classes/CachesManagement/Honkai/Check.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ private void CheckUnusedAssets(List<CacheAsset> assetIndex, List<CacheAsset> ret
9494
// Add asset to the returnAsset
9595
CacheAsset asset = new CacheAsset
9696
{
97-
BasePath = Path.GetDirectoryName(fileInfo.FullName),
98-
N = Path.GetFileName(fileInfo.FullName),
99-
DataType = CacheAssetType.Unused,
100-
CS = fileInfo.Length,
101-
CRC = null,
102-
Status = CacheAssetStatus.Unused,
97+
BasePath = Path.GetDirectoryName(fileInfo.FullName),
98+
N = Path.GetFileName(fileInfo.FullName),
99+
DataType = CacheAssetType.Unused,
100+
CS = fileInfo.Length,
101+
CRC = null,
102+
Status = CacheAssetStatus.Unused,
103103
IsUseLocalPath = true
104104
};
105105
returnAsset!.Add(asset);
@@ -193,4 +193,4 @@ private void AddGenericCheckAsset(CacheAsset asset, CacheAssetStatus assetStatus
193193
);
194194
}
195195
}
196-
}
196+
}

CollapseLauncher/Classes/CachesManagement/Honkai/Fetch.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ private async Task<List<CacheAsset>> Fetch(CancellationToken token)
3838
.SetAllowedDecompression(DecompressionMethods.None)
3939
.Create();
4040

41-
// Use a new DownloadClient for fetching
42-
DownloadClient downloadClient = DownloadClient.CreateInstance(httpClientNew);
43-
4441
// Build _gameRepoURL from loading Dispatcher and Gateway
45-
await BuildGameRepoURL(downloadClient.GetHttpClient(), token);
42+
await BuildGameRepoURL(httpClientNew, token);
4643

4744
// Iterate type and do fetch
4845
await Parallel.ForEachAsync(
4946
Enum.GetValues<CacheAssetType>(),
5047
new ParallelOptions
5148
{
5249
MaxDegreeOfParallelism = ThreadCount,
53-
CancellationToken = token
50+
CancellationToken = token
5451
},
5552
async (type, ctx) =>
5653
{
@@ -62,16 +59,16 @@ await Parallel.ForEachAsync(
6259
{
6360
// uint = Count of the assets available
6461
// long = Total size of the assets available
65-
(int, long) count = await FetchByType(type, downloadClient, returnAsset, ctx);
62+
(int, long) count = await FetchByType(type, httpClientNew, returnAsset, ctx);
6663

6764
// Write a log about the metadata
68-
LogWriteLine($"Cache Metadata [T: {type}]:", LogType.Default, true);
69-
LogWriteLine($" Cache Count = {count.Item1}", LogType.NoTag, true);
70-
LogWriteLine($" Cache Size = {SummarizeSizeSimple(count.Item2)}", LogType.NoTag, true);
65+
LogWriteLine($"Cache Metadata [T: {type}]:", LogType.Default, true);
66+
LogWriteLine($" Cache Count = {count.Item1}", LogType.NoTag, true);
67+
LogWriteLine($" Cache Size = {SummarizeSizeSimple(count.Item2)}", LogType.NoTag, true);
7168

7269
// Increment the Total Size and Count
7370
Interlocked.Add(ref ProgressAllCountTotal, count.Item1);
74-
Interlocked.Add(ref ProgressAllSizeTotal, count.Item2);
71+
Interlocked.Add(ref ProgressAllSizeTotal, count.Item2);
7572
}
7673
break;
7774
default:
@@ -83,7 +80,7 @@ await Parallel.ForEachAsync(
8380
return returnAsset;
8481
}
8582

86-
private async Task BuildGameRepoURL(HttpClient downloadClient, CancellationToken token)
83+
private async Task BuildGameRepoURL(HttpClient client, CancellationToken token)
8784
{
8885
KianaDispatch dispatch = null;
8986
Exception lastException = null;
@@ -101,10 +98,13 @@ private async Task BuildGameRepoURL(HttpClient downloadClient, CancellationToken
10198
string key = GameVersionManager.GamePreset.DispatcherKey;
10299

103100
// Try assign dispatcher
104-
dispatch = await KianaDispatch.GetDispatch(downloadClient, baseURL,
101+
dispatch = await KianaDispatch.GetDispatch(client,
102+
baseURL,
105103
GameVersionManager.GamePreset.GameDispatchURLTemplate,
106104
GameVersionManager.GamePreset.GameDispatchChannelName,
107-
key, GameVersion.VersionArray, token);
105+
key,
106+
GameVersion.VersionArray,
107+
token);
108108
lastException = null;
109109
break;
110110
}
@@ -119,13 +119,13 @@ private async Task BuildGameRepoURL(HttpClient downloadClient, CancellationToken
119119

120120
// Get gatewayURl and fetch the gateway
121121
GameGateway =
122-
await KianaDispatch.GetGameserver(downloadClient, dispatch!, GameVersionManager.GamePreset.GameGatewayDefault!, token);
122+
await KianaDispatch.GetGameserver(client, dispatch!, GameVersionManager.GamePreset.GameGatewayDefault!, token);
123123
GameRepoURL = BuildAssetBundleURL(GameGateway);
124124
}
125125

126126
private static string BuildAssetBundleURL(KianaDispatch gateway) => CombineURLFromString(gateway!.AssetBundleUrls![0], "/{0}/editor_compressed/");
127127

128-
private async Task<(int, long)> FetchByType(CacheAssetType type, DownloadClient downloadClient, List<CacheAsset> assetIndex, CancellationToken token)
128+
private async Task<(int, long)> FetchByType(CacheAssetType type, HttpClient client, List<CacheAsset> assetIndex, CancellationToken token)
129129
{
130130
// Set total activity string as "Fetching Caches Type: <type>"
131131
Status.ActivityStatus = string.Format(Lang!._CachesPage!.CachesStatusFetchingType!, type);
@@ -143,9 +143,7 @@ private async Task BuildGameRepoURL(HttpClient downloadClient, CancellationToken
143143
#endif
144144

145145
// Get a direct HTTP Stream
146-
await using HttpResponseInputStream remoteStream = await HttpResponseInputStream.CreateStreamAsync(
147-
downloadClient.GetHttpClient(), assetIndexURL, null, null, null, null, null, token);
148-
146+
await using Stream remoteStream = (await client.TryGetCachedStreamFrom(assetIndexURL, token: token)).Stream;
149147
await using XORStream stream = new XORStream(remoteStream);
150148

151149
// Build the asset index and return the count and size of each type
@@ -233,7 +231,7 @@ private IEnumerable<CacheAsset> EnumerateCacheTextAsset(CacheAssetType type, IEn
233231
}
234232

235233
// Set base URL and Path and add it to asset index
236-
content.BaseURL = baseUrl;
234+
content.BaseURL = baseUrl;
237235
content.DataType = type;
238236
content.BasePath = GetAssetBasePathByType(type);
239237

@@ -249,7 +247,7 @@ private async ValueTask<ValueTuple<int, long>> BuildAssetIndex(CacheAssetType ty
249247

250248
// Set isFirst flag as true if type is Data and
251249
// also convert type as lowered string.
252-
250+
253251
// Unused as of Aug 4th 2024, bonk @bagusnl if not true
254252
// bool isFirst = type == CacheAssetType.Data;
255253
// bool isNeedReadLuckyNumber = type == CacheAssetType.Data;
@@ -347,17 +345,16 @@ private static bool IsValidRegionFile(string input, string lang)
347345

348346
public KianaDispatch GetCurrentGateway() => GameGateway;
349347

350-
public async Task<(List<CacheAsset>, string, string, int)> GetCacheAssetList(
351-
DownloadClient downloadClient, CacheAssetType type, CancellationToken token)
348+
public async Task<(List<CacheAsset>, string, string, int)> GetCacheAssetList(HttpClient client, CacheAssetType type, CancellationToken token)
352349
{
353350
// Initialize asset index for the return
354351
List<CacheAsset> returnAsset = [];
355352

356353
// Build _gameRepoURL from loading Dispatcher and Gateway
357-
await BuildGameRepoURL(downloadClient.GetHttpClient(), token);
354+
await BuildGameRepoURL(client, token);
358355

359356
// Fetch the progress
360-
_ = await FetchByType(type, downloadClient, returnAsset, token);
357+
_ = await FetchByType(type, client, returnAsset, token);
361358

362359
// Return the list and base asset bundle repo URL
363360
return (returnAsset, GameGateway!.ExternalAssetUrls!.FirstOrDefault(), BuildAssetBundleURL(GameGateway),

CollapseLauncher/Classes/CachesManagement/Honkai/HonkaiCache.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ internal partial class HonkaiCache : ProgressBase<CacheAsset>, ICache, ICacheBas
1414
{
1515
#region Properties
1616

17-
private const string CacheRegionalCheckName = "sprite";
18-
private string GameLang { get; }
19-
private byte[] GameSalt { get; set; }
20-
private KianaDispatch GameGateway { get; set; }
21-
private List<CacheAsset> UpdateAssetIndex { get; set; }
22-
private int LuckyNumber { get; set; }
17+
private const string CacheRegionalCheckName = "sprite";
18+
private string GameLang { get; }
19+
private byte[] GameSalt { get; set; }
20+
private KianaDispatch GameGateway { get; set; }
21+
private List<CacheAsset> UpdateAssetIndex { get; set; }
22+
private int LuckyNumber { get; set; }
2323
#endregion
2424

2525
public HonkaiCache(UIElement parentUI, IGameVersion gameVersionManager)
@@ -112,4 +112,4 @@ public void Dispose()
112112
GC.SuppressFinalize(this);
113113
}
114114
}
115-
}
115+
}

CollapseLauncher/Classes/CachesManagement/Honkai/Update.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CollapseLauncher.Helper.StreamUtility;
21
using CollapseLauncher.Helper;
2+
using CollapseLauncher.Helper.StreamUtility;
33
using CollapseLauncher.Interfaces;
44
using Hi3Helper;
55
using Hi3Helper.Http;
@@ -24,7 +24,7 @@ internal partial class HonkaiCache
2424
private async Task<bool> Update(List<CacheAsset> updateAssetIndex, List<CacheAsset> assetIndex, CancellationToken token)
2525
{
2626
// Initialize new proxy-aware HttpClient
27-
using HttpClient client = new HttpClientBuilder<SocketsHttpHandler>()
27+
using HttpClient client = new HttpClientBuilder()
2828
.UseLauncherConfig(DownloadThreadWithReservedCount)
2929
.SetUserAgent(UserAgent)
3030
.SetAllowedDecompression(DecompressionMethods.None)
@@ -39,7 +39,7 @@ private async Task<bool> Update(List<CacheAsset> updateAssetIndex, List<CacheAss
3939
UpdateStatus();
4040

4141
// Iterate the asset index and do update operation
42-
ObservableCollection<IAssetProperty> assetProperty = [.. AssetEntry];
42+
ObservableCollection<IAssetProperty> assetProperty = [.. AssetEntry];
4343

4444
ConcurrentDictionary<(CacheAsset, IAssetProperty), byte> runningTask = new();
4545
if (IsBurstDownloadEnabled)
@@ -105,7 +105,7 @@ private void UpdateCacheVerifyList(List<CacheAsset> assetIndex)
105105
string listFile = Path.Combine(GamePath!, "Data", "Verify.txt");
106106

107107
// Initialize listFile File Stream
108-
using FileStream fs = new FileStream(listFile, FileMode.Create, FileAccess.Write);
108+
using FileStream fs = new FileStream(listFile, FileMode.Create, FileAccess.Write);
109109
using StreamWriter sw = new StreamWriter(fs);
110110
// Iterate asset index and generate the path for the cache path
111111
for (var index = 0; index < assetIndex!.Count; index++)
@@ -114,7 +114,7 @@ private void UpdateCacheVerifyList(List<CacheAsset> assetIndex)
114114
// Yes, the path is written in this way. Idk why miHoYo did this...
115115
// Update 6.8: They finally notices that they use "//" instead of "/"
116116
string basePath = GetAssetBasePathByType(asset!.DataType)!.Replace('\\', '/');
117-
string path = basePath + "/" + asset.ConcatN;
117+
string path = basePath + "/" + asset.ConcatN;
118118
sw.WriteLine(path);
119119
}
120120
}
@@ -141,19 +141,19 @@ private async Task UpdateCacheAsset((CacheAsset AssetIndex, IAssetProperty Asset
141141
// Other than unused file, do this action
142142
else
143143
{
144-
#if DEBUG
144+
#if DEBUG
145145
LogWriteLine($"Downloading cache [T: {asset.AssetIndex.DataType}]: {asset.AssetIndex.N} at URL: {asset.AssetIndex.ConcatURL}", LogType.Debug, true);
146-
#endif
146+
#endif
147147

148148
await RunDownloadTask(asset.AssetIndex.CS, fileInfo, asset.AssetIndex.ConcatURL, downloadClient, downloadProgress, token);
149149

150-
#if !DEBUG
150+
#if !DEBUG
151151
LogWriteLine($"Downloaded cache [T: {asset.AssetIndex.DataType}]: {asset.AssetIndex.N}", LogType.Default, true);
152-
#endif
152+
#endif
153153
}
154154

155155
// Remove Asset Entry display
156156
PopRepairAssetEntry(asset.AssetProperty);
157157
}
158158
}
159-
}
159+
}

CollapseLauncher/Classes/Helper/HttpClientBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ private static string GetDefaultUserAgent()
5454
+ $"WinAppSDK/{LauncherConfig.WindowsAppSdkVersion}";
5555
}
5656

57+
public static HttpClient CreateDefaultClient(int maxConnection = 32, bool isSkipDnsInit = false)
58+
=> new HttpClientBuilder()
59+
.UseLauncherConfig()
60+
.Create();
61+
5762
public HttpClientBuilder<THandler> UseExternalProxy(string host, string? username = null, SecureString? password = null)
5863
{
5964
// Try to create the Uri

0 commit comments

Comments
 (0)