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 fc8da53 commit c4496b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private static void BuildServiceProvider()
sc.AddSingleton<LauncherBackgroundService>();
sc.AddSingleton<GamePackageService>();
sc.AddSingleton<ZZZGachaService>();
sc.AddSingleton<Services.Download.InstallGameService>();

_serviceProvider = sc.BuildServiceProvider();
if (!string.IsNullOrWhiteSpace(UserDataFolder))
Expand Down
84 changes: 69 additions & 15 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace Starward.Services.Download;

internal abstract class InstallGameService
internal class InstallGameService
{


Expand All @@ -40,16 +40,27 @@ internal abstract class InstallGameService



public InstallGameService(ILogger<InstallGameService> logger, HttpClient httpClient, GameLauncherService launcherService, GamePackageService packageService, HoYoPlayService hoYoPlayService)
{
_logger = logger;
_httpClient = httpClient;
_launcherService = launcherService;
_packageService = packageService;
_hoYoPlayService = hoYoPlayService;
}



public GameBiz CurrentGameBiz { get; protected set; }



public abstract bool CanRepairGameFiles { get; }
public virtual bool CanRepairGameFiles { get; }




public abstract bool CanRepairAudioFiles { get; }
public virtual bool CanRepairAudioFiles { get; }


public EventHandler<InstallGameState> InstallGameStateChanged;
Expand All @@ -75,6 +86,9 @@ internal abstract class InstallGameService
protected InstallGameTask _installTask;


protected GamePackage _gamePackage;


protected List<InstallGameItem> _gamePackageItems;


Expand Down Expand Up @@ -104,6 +118,11 @@ public void Initialize(GameBiz gameBiz, string installPath)
var temp = Path.Combine(installPath, Random.Shared.Next(1000_0000, int.MaxValue).ToString());
File.Create(temp).Dispose();
File.Delete(temp);
var files = Directory.GetFiles(installPath, "*", SearchOption.AllDirectories);
foreach (var file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
}
CurrentGameBiz = gameBiz;
_installPath = installPath;
_initialized = true;
Expand All @@ -114,9 +133,9 @@ public void Initialize(GameBiz gameBiz, string installPath)

public virtual async Task StartInstallGameAsync(CancellationToken cancellationToken = default)
{
var package = await _packageService.GetGamePackageAsync(CurrentGameBiz);
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
_gamePackageItems = new List<InstallGameItem>();
foreach (var item in package.Main.Major!.GamePackages)
foreach (var item in _gamePackage.Main.Major!.GamePackages)
{
_gamePackageItems.Add(new InstallGameItem
{
Expand All @@ -131,7 +150,7 @@ public virtual async Task StartInstallGameAsync(CancellationToken cancellationTo
});
}
_audioPackageItems = new List<InstallGameItem>();
foreach (var item in await GetAudioPackageFilesFromGameResourceAsync(package.Main.Major!))
foreach (var item in await GetAudioPackageFilesFromGameResourceAsync(_gamePackage.Main.Major!))
{
_audioPackageItems.Add(new InstallGameItem
{
Expand Down Expand Up @@ -163,8 +182,8 @@ public virtual async Task StartInstallGameAsync(CancellationToken cancellationTo

public virtual async Task StartRepairGameAsync(CancellationToken cancellationToken = default)
{
var package = await _packageService.GetGamePackageAsync(CurrentGameBiz);
var prefix = package.Main.Major!.ResListUrl;
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
var prefix = _gamePackage.Main.Major!.ResListUrl;
if (string.IsNullOrWhiteSpace(prefix))
{
throw new NotSupportedException($"Repairing game ({CurrentGameBiz}) is not supported.");
Expand All @@ -183,20 +202,20 @@ public virtual async Task StartRepairGameAsync(CancellationToken cancellationTok

public virtual async Task StartPredownloadAsync(CancellationToken cancellationToken = default)
{
var package = await _packageService.GetGamePackageAsync(CurrentGameBiz);
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
GamePackageResource resource;
var localVersion = await _launcherService.GetLocalGameVersionAsync(CurrentGameBiz, _installPath);
if (package.PreDownload is null)
if (_gamePackage.PreDownload is null)
{
throw new InvalidOperationException($"Predownload of ({CurrentGameBiz}) is not enabled.");
}
if (package.PreDownload.Patches.FirstOrDefault(x => x.Version == localVersion?.ToString()) is GamePackageResource _resource_temp)
if (_gamePackage.PreDownload.Patches.FirstOrDefault(x => x.Version == localVersion?.ToString()) is GamePackageResource _resource_temp)
{
resource = _resource_temp;
}
else
{
resource = package.PreDownload.Major!;
resource = _gamePackage.PreDownload.Major!;
}
_gamePackageItems = new List<InstallGameItem>();
foreach (var item in resource.GamePackages)
Expand Down Expand Up @@ -245,16 +264,16 @@ public virtual async Task StartPredownloadAsync(CancellationToken cancellationTo

public virtual async Task StartUpdateGameAsync(CancellationToken cancellationToken = default)
{
var package = await _packageService.GetGamePackageAsync(CurrentGameBiz);
_gamePackage = await _packageService.GetGamePackageAsync(CurrentGameBiz);
GamePackageResource resource;
var localVersion = await _launcherService.GetLocalGameVersionAsync(CurrentGameBiz, _installPath);
if (package.Main.Patches.FirstOrDefault(x => x.Version == localVersion?.ToString()) is GamePackageResource _resource_tmp)
if (_gamePackage.Main.Patches.FirstOrDefault(x => x.Version == localVersion?.ToString()) is GamePackageResource _resource_tmp)
{
resource = _resource_tmp;
}
else
{
resource = package.Main.Major!;
resource = _gamePackage.Main.Major!;
}
_gamePackageItems = new List<InstallGameItem>();
foreach (var item in resource.GamePackages)
Expand Down Expand Up @@ -406,11 +425,46 @@ protected async Task CleanGameDeprecatedFilesAsync()
File.Delete(path);
}
}
await WriteConfigFileAsync();
CurrentTaskFinished();
}



protected async Task WriteConfigFileAsync()
{
string version = _gamePackage.Main.Major?.Version ?? "";
string sdk_version = "";
string cps = "", channel = "1", sub_channel = "1";
if (CurrentGameBiz.IsBilibiliServer())
{
cps = "bilibili";
channel = "14";
sub_channel = "0";
}
else if (CurrentGameBiz.IsChinaServer())
{
cps = "mihoyo";
}
else if (CurrentGameBiz.IsGlobalServer())
{
cps = "hoyoverse";
}
string config = $"""
[General]
channel={channel}
cps={cps}
game_version={version}
sub_channel={sub_channel}
sdk_version={sdk_version}
game_biz={CurrentGameBiz}
""";
_logger.LogInformation("Write config.ini (game_version={version})", version);
await File.WriteAllTextAsync(Path.Combine(_installPath, "config.ini"), config).ConfigureAwait(false);
}



protected void Finish()
{
State = InstallGameState.Finish;
Expand Down

0 comments on commit c4496b1

Please sign in to comment.