Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jul 23, 2024
1 parent c4496b1 commit 80bec65
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static void BuildServiceProvider()
sc.AddSingleton<LauncherBackgroundService>();
sc.AddSingleton<GamePackageService>();
sc.AddSingleton<ZZZGachaService>();
sc.AddSingleton<Services.Download.InstallGameService>();
sc.AddTransient<Services.Download.InstallGameService>();

_serviceProvider = sc.BuildServiceProvider();
if (!string.IsNullOrWhiteSpace(UserDataFolder))
Expand Down
142 changes: 127 additions & 15 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public InstallGameService(ILogger<InstallGameService> logger, HttpClient httpCli
protected GamePackage _gamePackage;


protected GameChannelSDK? _channelSDK;


protected List<InstallGameItem> _gamePackageItems;


Expand All @@ -98,6 +101,9 @@ public InstallGameService(ILogger<InstallGameService> logger, HttpClient httpCli
protected List<InstallGameItem> _gameFileItems;


protected InstallGameItem? _gameSDKItem;


protected ConcurrentQueue<InstallGameItem> _verifyFailedItems = new();


Expand Down Expand Up @@ -173,6 +179,27 @@ public virtual async Task StartInstallGameAsync(CancellationToken cancellationTo
{
_installItemQueue.Enqueue(item);
}
if (CurrentGameBiz.IsBilibili())
{
_channelSDK = await _hoYoPlayService.GetGameChannelSDKAsync(CurrentGameBiz);
if (_channelSDK is not null)
{
string name = Path.GetFileName(_channelSDK.ChannelSDKPackage.Url);
_gameSDKItem = new InstallGameItem
{
Type = InstallGameItemType.Download,
FileName = name,
Path = Path.Combine(_installPath, name),
MD5 = _channelSDK.ChannelSDKPackage.MD5,
Size = _channelSDK.ChannelSDKPackage.Size,
DecompressedSize = _channelSDK.ChannelSDKPackage.DecompressedSize,
Url = _channelSDK.ChannelSDKPackage.Url,
WriteAsTempFile = true,
};
_installItemQueue.Enqueue(_gameSDKItem);
}
}

StartTask(InstallGameState.Download);
}

Expand All @@ -188,12 +215,32 @@ public virtual async Task StartRepairGameAsync(CancellationToken cancellationTok
{
throw new NotSupportedException($"Repairing game ({CurrentGameBiz}) is not supported.");
}
_gameFileItems = await GetPkgVersionsAsync(prefix, "pkg_versoin");
_gameFileItems = await GetPkgVersionsAsync(prefix, "pkg_version");
_installTask = InstallGameTask.Repair;
foreach (var item in _gameFileItems)
{
_installItemQueue.Enqueue(item);
}
if (CurrentGameBiz.IsBilibili())
{
_channelSDK = await _hoYoPlayService.GetGameChannelSDKAsync(CurrentGameBiz);
if (_channelSDK is not null)
{
string name = Path.GetFileName(_channelSDK.ChannelSDKPackage.Url);
_gameSDKItem = new InstallGameItem
{
Type = InstallGameItemType.Download,
FileName = name,
Path = Path.Combine(_installPath, name),
MD5 = _channelSDK.ChannelSDKPackage.MD5,
Size = _channelSDK.ChannelSDKPackage.Size,
DecompressedSize = _channelSDK.ChannelSDKPackage.DecompressedSize,
Url = _channelSDK.ChannelSDKPackage.Url,
WriteAsTempFile = false,
};
_installItemQueue.Enqueue(_gameSDKItem);
}
}
StartTask(InstallGameState.Verify);
}

Expand Down Expand Up @@ -314,6 +361,26 @@ public virtual async Task StartUpdateGameAsync(CancellationToken cancellationTok
{
_installItemQueue.Enqueue(item);
}
if (CurrentGameBiz.IsBilibili())
{
_channelSDK = await _hoYoPlayService.GetGameChannelSDKAsync(CurrentGameBiz);
if (_channelSDK is not null)
{
string name = Path.GetFileName(_channelSDK.ChannelSDKPackage.Url);
_gameSDKItem = new InstallGameItem
{
Type = InstallGameItemType.Download,
FileName = name,
Path = Path.Combine(_installPath, name),
MD5 = _channelSDK.ChannelSDKPackage.MD5,
Size = _channelSDK.ChannelSDKPackage.Size,
DecompressedSize = _channelSDK.ChannelSDKPackage.DecompressedSize,
Url = _channelSDK.ChannelSDKPackage.Url,
WriteAsTempFile = true,
};
_installItemQueue.Enqueue(_gameSDKItem);
}
}
StartTask(InstallGameState.Download);
}

Expand Down Expand Up @@ -416,8 +483,11 @@ protected async Task<List<InstallGameItem>> GetPkgVersionsAsync(string prefix, s
protected async Task CleanGameDeprecatedFilesAsync()
{
State = InstallGameState.Clean;
var files = await _hoYoPlayService.GetGameDeprecatedFilesAsync(CurrentGameBiz);
foreach (var file in files)
foreach (var file in Directory.GetFiles(_installPath, "*_tmp", SearchOption.AllDirectories))
{
File.Delete(file);
}
foreach (var file in await _hoYoPlayService.GetGameDeprecatedFilesAsync(CurrentGameBiz))
{
var path = Path.Combine(_installPath, file.Name);
if (File.Exists(path))
Expand All @@ -434,19 +504,19 @@ protected async Task CleanGameDeprecatedFilesAsync()
protected async Task WriteConfigFileAsync()
{
string version = _gamePackage.Main.Major?.Version ?? "";
string sdk_version = "";
string sdk_version = _channelSDK?.Version ?? "";
string cps = "", channel = "1", sub_channel = "1";
if (CurrentGameBiz.IsBilibiliServer())
if (CurrentGameBiz.IsBilibili())
{
cps = "bilibili";
channel = "14";
sub_channel = "0";
}
else if (CurrentGameBiz.IsChinaServer())
else if (CurrentGameBiz.IsChinaOfficial())
{
cps = "mihoyo";
}
else if (CurrentGameBiz.IsGlobalServer())
else if (CurrentGameBiz.IsGlobalOfficial())
{
cps = "hoyoverse";
}
Expand Down Expand Up @@ -532,7 +602,8 @@ public void Continue()
{
try
{
StartTask(State);
Debug.WriteLine("Continue");
StartTask(_pausedState);
}
catch (Exception ex)
{
Expand All @@ -547,6 +618,7 @@ public void Pause()
{
try
{
Debug.WriteLine("Pause");
_pausedState = State;
State = InstallGameState.None;
_cancellationTokenSource?.Cancel();
Expand Down Expand Up @@ -611,7 +683,7 @@ protected void OnInstallOrUpdateTaskFinished()
{
FromDownloadToVerify();
}
if (State is InstallGameState.Verify)
else if (State is InstallGameState.Verify)
{
if (_verifyFailedItems.IsEmpty)
{
Expand All @@ -622,11 +694,11 @@ protected void OnInstallOrUpdateTaskFinished()
FromVerifyToDownload();
}
}
if (State is InstallGameState.Decompress)
else if (State is InstallGameState.Decompress)
{
_ = CleanGameDeprecatedFilesAsync();
}
if (State is InstallGameState.Clean)
else if (State is InstallGameState.Clean)
{
Finish();
}
Expand All @@ -636,19 +708,42 @@ protected void OnInstallOrUpdateTaskFinished()

protected void OnRepairTaskFinished()
{
// verify -> download -> clean
Debug.WriteLine($"OnRepairTaskFinished - {State}");
// verify -> download -> decompress (SDK) -> clean
if (State is InstallGameState.Verify)
{
if (!_verifyFailedItems.IsEmpty)
{
FromVerifyToDownload();
}
else
{
if (_gameSDKItem is not null)
{
_gameSDKItem.Type = InstallGameItemType.Decompress;
_gameSDKItem.PackageFiles = [_gameSDKItem.Path];
_gameSDKItem.TargetPath = _installPath;
_installItemQueue.Enqueue(_gameSDKItem);
}
StartTask(InstallGameState.Decompress);
}
}
if (State is InstallGameState.Download)
else if (State is InstallGameState.Download)
{
if (_gameSDKItem is not null)
{
_gameSDKItem.Type = InstallGameItemType.Decompress;
_gameSDKItem.PackageFiles = [_gameSDKItem.Path];
_gameSDKItem.TargetPath = _installPath;
_installItemQueue.Enqueue(_gameSDKItem);
}
StartTask(InstallGameState.Decompress);
}
else if (State is InstallGameState.Decompress)
{
_ = CleanGameDeprecatedFilesAsync();
}
if (State is InstallGameState.Clean)
else if (State is InstallGameState.Clean)
{
Finish();
}
Expand Down Expand Up @@ -690,6 +785,11 @@ protected void FromDownloadToVerify()
item.Type = InstallGameItemType.Verify;
_installItemQueue.Enqueue(item);
}
if (_gameSDKItem is not null)
{
_gameSDKItem.Type = InstallGameItemType.Verify;
_installItemQueue.Enqueue(_gameSDKItem);
}
StartTask(InstallGameState.Verify);
}

Expand Down Expand Up @@ -721,9 +821,17 @@ protected void FromVerifyToDecompress()
foreach (var item in _audioPackageItems)
{
item.Type = InstallGameItemType.Decompress;
item.PackageFiles = [item.Path];
item.TargetPath = _installPath;
_installItemQueue.Enqueue(item);
}
if (_gameSDKItem is not null)
{
_gameSDKItem.Type = InstallGameItemType.Decompress;
_gameSDKItem.PackageFiles = [_gameSDKItem.Path];
_gameSDKItem.TargetPath = _installPath;
_installItemQueue.Enqueue(_gameSDKItem);
}
StartTask(InstallGameState.Decompress);
}

Expand Down Expand Up @@ -817,11 +925,13 @@ protected async Task ExecuteTaskAsync(CancellationToken cancellationToken = defa
{
// cacel
_installItemQueue.Enqueue(item);
return;
}
}
}
catch (Exception ex)
{
State = InstallGameState.Error;
_logger.LogError(ex, nameof(ExecuteTaskAsync));
Error?.Invoke(this, ex);
}
Expand Down Expand Up @@ -898,11 +1008,13 @@ protected async Task VerifyItemAsync(InstallGameItem item, CancellationToken can
}
else
{
file_target = item.WriteAsTempFile ? file_tmp : file;
_verifyFailedItems.Enqueue(item);
return;
}
using var fs = File.OpenRead(file_target);
if (fs.Length != item.Size)
{
fs.Dispose();
File.Delete(file);
_verifyFailedItems.Enqueue(item);
return;
Expand Down
16 changes: 16 additions & 0 deletions src/Starward/Services/HoYoPlayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,20 @@ public async Task<List<GameDeprecatedFile>> GetGameDeprecatedFilesAsync(GameBiz



public async Task<GameChannelSDK?> GetGameChannelSDKAsync(GameBiz biz)
{
if (biz.ToGame() is GameBiz.Honkai3rd && biz.IsGlobalOfficial())
{
biz = GameBiz.bh3_global;
}
var launcherId = LauncherId.FromGameBiz(biz);
var gameId = GameId.FromGameBiz(biz);
if (launcherId != null && gameId != null)
{
return await _client.GetGameChannelSDKAsync(launcherId, "en-us", gameId);
}
return null;
}


}
7 changes: 5 additions & 2 deletions src/Starward/Services/InstallGame/FileSliceStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ protected override void Dispose(bool disposing)

}

foreach (var fs in _fileStreams)
if (_fileStreams is not null)
{
fs.Dispose();
foreach (var fs in _fileStreams)
{
fs.Dispose();
}
}

disposedValue = true;
Expand Down

0 comments on commit 80bec65

Please sign in to comment.