Skip to content

Commit

Permalink
Adding support for repairing mechanism on Bilibili clients
Browse files Browse the repository at this point in the history
+ This includes Genshin and Star Rail
  • Loading branch information
neon-nyan committed Oct 7, 2023
1 parent 70a68d8 commit 0b14708
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
76 changes: 76 additions & 0 deletions CollapseLauncher/Classes/Interfaces/Class/ProgressBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.IO.Hashing;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
Expand Down Expand Up @@ -356,6 +358,80 @@ protected void ResetStatusAndProgressProperty()
_progressPerFileSize = 0;
}

protected async ValueTask FetchBilibiliSDK(CancellationToken token)
{
// Check whether the sdk is not null,
if (_gameVersionManager.GameAPIProp.data.sdk == null) return;

// Set the SDK DLL path
string sdkDllPath;
string sdkDllDir;
FileInfo sdkDllFile;

// Get the URL and get the remote stream of the zip file
string url = _gameVersionManager.GameAPIProp.data.sdk.path;
await using (Stream HttpResponse = await FallbackCDNUtil.GetHttpStreamFromResponse(url, token))
using (ZipArchive zip = new ZipArchive(HttpResponse, ZipArchiveMode.Read, true))
{
// Iterate the Zip Entry
foreach (var entry in zip.Entries)
{
// Get the filename of the entry without ext.
string fileName = Path.GetFileNameWithoutExtension(entry.FullName);

// If the entry is the "sdk_pkg_version", then override the info to sdk_pkg_version
switch (fileName)
{
case "PCGameSDK":
// Set the SDK DLL path
sdkDllPath = Path.Combine(_gamePath, $"{Path.GetFileNameWithoutExtension(_gameVersionManager.GamePreset.GameExecutableName)}_Data", "Plugins", "PCGameSDK.dll");
sdkDllDir = Path.GetDirectoryName(sdkDllPath);
sdkDllFile = new FileInfo(sdkDllPath);

// Create the folder of the SDK DLL if doesn't exist
if (!Directory.Exists(sdkDllDir)) Directory.CreateDirectory(sdkDllDir);
break;
case "sdk_pkg_version":
// Set the SDK DLL path to be used for sdk_pkg_version
sdkDllPath = Path.Combine(_gamePath, "sdk_pkg_version");
sdkDllFile = new FileInfo(sdkDllPath);
break;
default:
continue;
}

// Do check if sdkDllFile is not null
if (sdkDllFile != null)
{
// Try create the file if not exist or open an existing one
await using (Stream sdkDllStream = sdkDllFile.Open(entry.Length < sdkDllFile.Length ? FileMode.Create : FileMode.OpenOrCreate))
{
// Initiate the Crc32 hash
Crc32 hash = new Crc32();

// Append the SDK DLL stream to hash and get the result
await hash.AppendAsync(sdkDllStream, token);
byte[] hashByte = hash.GetHashAndReset();
uint hashInt = BitConverter.ToUInt32(hashByte);

// If the hash is the same, then skip
if (hashInt == entry.Crc32) continue;

// Set total activity string as "Loading Indexes..."
_status.ActivityStatus = Lang._GameRepairPage.Status2;
_status.IsProgressPerFileIndetermined = true;
await using (Stream entryStream = entry.Open())
{
// Reset the SDK DLL stream pos and write the data
sdkDllStream.Position = 0;
await entryStream.CopyToAsync(sdkDllStream, token);
}
}
}
}
}
}

protected IEnumerable<T2> EnforceHTTPSchemeToAssetIndex(IEnumerable<T2> assetIndex)
{
const string HTTPSScheme = "https://";
Expand Down
3 changes: 3 additions & 0 deletions CollapseLauncher/Classes/RepairManagement/Genshin/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ private async ValueTask<List<PkgVersionProperties>> Fetch(List<PkgVersionPropert
// Build persistent manifest
_isParsePersistentManifestSuccess = await BuildPersistentManifest(_httpClient, assetIndex, hashtableManifest, token);

// Force-Fetch the Bilibili SDK (if exist :pepehands:)
await FetchBilibiliSDK(token);

// Clear hashtableManifest
hashtableManifest.Clear();

Expand Down
3 changes: 3 additions & 0 deletions CollapseLauncher/Classes/RepairManagement/StarRail/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private async Task Fetch(List<FilePropertiesRemote> assetIndex, CancellationToke
// Read Video metadata and convert to FilePropertiesRemote
await _innerGameVersionManager.StarRailMetadataTool.ReadVideoMetadataInformation(token);
ConvertSRMetadataToAssetIndex(_innerGameVersionManager.StarRailMetadataTool.MetadataVideo, assetIndex);

// Force-Fetch the Bilibili SDK (if exist :pepehands:)
await FetchBilibiliSDK(token);
}
finally
{
Expand Down

0 comments on commit 0b14708

Please sign in to comment.