Skip to content

Commit

Permalink
a new install game service
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost authored Jul 23, 2024
1 parent 7e8ef78 commit 435f978
Show file tree
Hide file tree
Showing 13 changed files with 1,419 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/Starward.Core/HoYoPlay/HoYoPlayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public async Task<List<GameChannelSDK>> GetGameChannelSDKAsync(string launcherId
string url = BuildUrl("getGameChannelSDKs", launcherId, language);
foreach (var gameId in gameIds)
{
url += $"&game_id[]={gameId.Id}";
url += $"&game_ids[]={gameId.Id}";
}
if (LauncherId.IsBilibili(launcherId))
{
Expand All @@ -259,7 +259,7 @@ public async Task<List<GameChannelSDK>> GetGameChannelSDKAsync(string launcherId
/// <exception cref="miHoYoApiException"></exception>
public async Task<GameChannelSDK?> GetGameChannelSDKAsync(string launcherId, string language, GameId gameId, CancellationToken cancellationToken = default)
{
string url = BuildUrl("getGameChannelSDKs", launcherId, language) + $"&game_id[]={gameId.Id}";
string url = BuildUrl("getGameChannelSDKs", launcherId, language) + $"&game_ids[]={gameId.Id}";
if (LauncherId.IsBilibili(launcherId))
{
url += "&channel=14&sub_channel=0";
Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task<List<GameDeprecatedFileConfig>> GetGameDeprecatedFileConfigAsy
string url = BuildUrl("getGameDeprecatedFileConfigs", launcherId, language);
foreach (var gameId in gameIds)
{
url += $"&game_id[]={gameId.Id}";
url += $"&game_ids[]={gameId.Id}";
}
if (LauncherId.IsBilibili(launcherId))
{
Expand All @@ -335,7 +335,7 @@ public async Task<List<GameDeprecatedFileConfig>> GetGameDeprecatedFileConfigAsy
/// <exception cref="miHoYoApiException"></exception>
public async Task<GameDeprecatedFileConfig?> GetGameDeprecatedFileConfigAsync(string launcherId, string language, GameId gameId, CancellationToken cancellationToken = default)
{
string url = BuildUrl("getGameDeprecatedFileConfigs", launcherId, language) + $"&game_id[]={gameId.Id}";
string url = BuildUrl("getGameDeprecatedFileConfigs", launcherId, language) + $"&game_ids[]={gameId.Id}";
if (LauncherId.IsBilibili(launcherId))
{
url += "&channel=14&sub_channel=0";
Expand Down Expand Up @@ -378,7 +378,7 @@ public async Task<List<GameConfig>> GetGameConfigAsync(string launcherId, string
string url = BuildUrl("getGameConfigs", launcherId, language);
foreach (var gameId in gameIds)
{
url += $"&game_id[]={gameId.Id}";
url += $"&game_ids[]={gameId.Id}";
}
return await CommonGetAsync<List<GameConfig>>(url, "launch_configs", cancellationToken);
}
Expand All @@ -395,7 +395,7 @@ public async Task<List<GameConfig>> GetGameConfigAsync(string launcherId, string
/// <exception cref="miHoYoApiException"></exception>
public async Task<GameConfig?> GetGameConfigAsync(string launcherId, string language, GameId gameId, CancellationToken cancellationToken = default)
{
string url = BuildUrl("getGameConfigs", launcherId, language) + $"&game_id[]={gameId.Id}";
string url = BuildUrl("getGameConfigs", launcherId, language) + $"&game_ids[]={gameId.Id}";
var list = await CommonGetAsync<List<GameConfig>>(url, "launch_configs", cancellationToken);
return list.FirstOrDefault(x => x.GameId == gameId);
}
Expand Down Expand Up @@ -430,7 +430,7 @@ public async Task<List<GameBranch>> GetGameBranchAsync(string launcherId, string
string url = BuildUrl("getGameBranches", launcherId, language);
foreach (var gameId in gameIds)
{
url += $"&game_id[]={gameId.Id}";
url += $"&game_ids[]={gameId.Id}";
}
return await CommonGetAsync<List<GameBranch>>(url, "game_branches", cancellationToken);
}
Expand All @@ -448,7 +448,7 @@ public async Task<List<GameBranch>> GetGameBranchAsync(string launcherId, string
/// <exception cref="miHoYoApiException"></exception>
public async Task<GameBranch?> GetGameBranchAsync(string launcherId, string language, GameId gameId, CancellationToken cancellationToken = default)
{
string url = BuildUrl("getGameBranches", launcherId, language) + $"&game_id[]={gameId.Id}";
string url = BuildUrl("getGameBranches", launcherId, language) + $"&game_ids[]={gameId.Id}";
var list = await CommonGetAsync<List<GameBranch>>(url, "game_branches", cancellationToken);
return list.FirstOrDefault(x => x.GameId == gameId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Starward.Core/HoYoPlay/LauncherId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool IsBilibili(string launcherId)
return biz switch
{
GameBiz.hk4e_cn or GameBiz.hkrpg_cn or GameBiz.bh3_cn or GameBiz.nap_cn => ChinaOfficial,
GameBiz.hk4e_global or GameBiz.hkrpg_global or GameBiz.nap_global => GlobalOfficial,
GameBiz.hk4e_global or GameBiz.hkrpg_global or GameBiz.bh3_global or GameBiz.nap_global => GlobalOfficial,
GameBiz.hk4e_bilibili => BilibiliGenshin,
GameBiz.hkrpg_bilibili => BilibiliStarRail,
GameBiz.nap_bilibili => BilibiliZZZ,
Expand Down
2 changes: 2 additions & 0 deletions src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ private static void BuildServiceProvider()
sc.AddSingleton<LauncherBackgroundService>();
sc.AddSingleton<GamePackageService>();
sc.AddSingleton<ZZZGachaService>();
sc.AddTransient<Services.Download.InstallGameService>();
sc.AddTransient<Services.Download.GenshinInstallGameService>();

_serviceProvider = sc.BuildServiceProvider();
if (!string.IsNullOrWhiteSpace(UserDataFolder))
Expand Down
1 change: 1 addition & 0 deletions src/Starward/Pages/GameLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
IsTextScaleFactorEnabled="False" />
</Button>


</StackPanel>


Expand Down
139 changes: 139 additions & 0 deletions src/Starward/Services/Download/GenshinInstallGameService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using Microsoft.Extensions.Logging;
using Starward.Core;
using Starward.Core.HoYoPlay;
using Starward.Services.Launcher;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Starward.Services.Download;

internal class GenshinInstallGameService : InstallGameService
{


public GenshinInstallGameService(ILogger<GenshinInstallGameService> logger, HttpClient httpClient, GameLauncherService launcherService, GamePackageService packageService, HoYoPlayService hoYoPlayService) : base(logger, httpClient, launcherService, packageService, hoYoPlayService)
{

}




public override async Task StartRepairGameAsync(CancellationToken cancellationToken = default)
{
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
var prefix = _gamePackage.Main.Major!.ResListUrl;
if (string.IsNullOrWhiteSpace(prefix))
{
throw new NotSupportedException($"Repairing game ({CurrentGameBiz}) is not supported.");
}
_gameFileItems = await GetPkgVersionsAsync(prefix, "pkg_version");
_gameFileItems.AddRange(await GetAudioPkgVersionsAsync(prefix));
foreach (var item in _gameFileItems)
{
_installItemQueue.Enqueue(item);
}
if (CurrentGameBiz.IsBilibili())
{
await PrepareBilibiliChannelSDKAsync(InstallGameItemType.Verify);
}
await MoveAudioAssetsFromPersistentToStreamAssetsAsync();
_installTask = InstallGameTask.Repair;
StartTask(InstallGameState.Verify);
}



public override async Task StartUpdateGameAsync(CancellationToken cancellationToken = default)
{
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
GamePackageResource resource;
var localVersion = await _launcherService.GetLocalGameVersionAsync(CurrentGameBiz, _installPath);
if (_gamePackage.Main.Patches.FirstOrDefault(x => x.Version == localVersion?.ToString()) is GamePackageResource _resource_tmp)
{
resource = _resource_tmp;
}
else
{
resource = _gamePackage.Main.Major!;
}
await PrepareDownloadGamePackageResourceAsync(resource);
if (CurrentGameBiz.IsBilibili())
{
await PrepareBilibiliChannelSDKAsync(InstallGameItemType.Download);
}
await MoveAudioAssetsFromPersistentToStreamAssetsAsync();
_installTask = InstallGameTask.Update;
StartTask(InstallGameState.Download);
}



private async Task<List<InstallGameItem>> GetAudioPkgVersionsAsync(string prefix)
{
string lang = await GetAudioLanguageAsync();
if (string.IsNullOrWhiteSpace(lang))
{
lang = LanguageUtil.FilterLanguage(CultureInfo.CurrentUICulture.Name);
await SetAudioLanguageAsync(lang);
}
List<InstallGameItem> list = [];
if (lang.Contains("zh-cn"))
{
list.AddRange(await GetPkgVersionsAsync(prefix, "Audio_Chinese_pkg_version"));
}
if (lang.Contains("en-us"))
{
list.AddRange(await GetPkgVersionsAsync(prefix, "Audio_English(US)_pkg_version"));
}
if (lang.Contains("ja-jp"))
{
list.AddRange(await GetPkgVersionsAsync(prefix, "Audio_Japanese_pkg_version"));
}
if (lang.Contains("ko-kr"))
{
list.AddRange(await GetPkgVersionsAsync(prefix, "Audio_Korean_pkg_version"));
}
return list;
}




protected async Task MoveAudioAssetsFromPersistentToStreamAssetsAsync()
{
string dataName = CurrentGameBiz switch
{
GameBiz.hk4e_cn => "YuanShen_Data",
GameBiz.hk4e_global => "GenshinImpact_Data",
_ => "",
};
if (!string.IsNullOrWhiteSpace(dataName))
{
var source = Path.Combine(_installPath, $@"{dataName}\Persistent\AudioAssets");
var target = Path.Combine(_installPath, $@"{dataName}\StreamingAssets\AudioAssets");
if (Directory.Exists(source))
{
await Task.Run(() =>
{
var files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
foreach (var file in files)
{
var relative = Path.GetRelativePath(source, file);
var dest = Path.Combine(target, relative);
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
File.Move(file, dest, true);
}
});
}
}
}


}
46 changes: 46 additions & 0 deletions src/Starward/Services/Download/InstallGameItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;

namespace Starward.Services.Download;

internal class InstallGameItem
{


public InstallGameItemType Type { get; set; }


public string FileName { get; set; }


public string Url { get; set; }


public string Path { get; set; }


public List<string> PackageFiles { get; set; }


public string TargetPath { get; set; }


public long Size { get; set; }


public long DecompressedSize { get; set; }


public string MD5 { get; set; }


public bool WriteAsTempFile { get; set; }


public void EnsureValid()
{
// todo
}


}
14 changes: 14 additions & 0 deletions src/Starward/Services/Download/InstallGameItemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Starward.Services.Download;

internal enum InstallGameItemType
{

None,

Download,

Verify,

Decompress,

}
Loading

0 comments on commit 435f978

Please sign in to comment.