Update GameService.cs

This commit is contained in:
Lightczx
2023-07-30 22:37:40 +08:00
parent 4c337a79b9
commit 4f6c2905d2

View File

@@ -1,6 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved. // Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license. // Licensed under the MIT license.
using Microsoft.EntityFrameworkCore;
using Snap.Hutao.Core; using Snap.Hutao.Core;
using Snap.Hutao.Core.Database; using Snap.Hutao.Core.Database;
using Snap.Hutao.Core.ExceptionService; using Snap.Hutao.Core.ExceptionService;
@@ -29,6 +30,7 @@ internal sealed partial class GameService : IGameService
{ {
private readonly PackageConverter packageConverter; private readonly PackageConverter packageConverter;
private readonly IServiceProvider serviceProvider; private readonly IServiceProvider serviceProvider;
private readonly IGameDbService gameDbService;
private readonly LaunchOptions launchOptions; private readonly LaunchOptions launchOptions;
private readonly RuntimeOptions hutaoOptions; private readonly RuntimeOptions hutaoOptions;
private readonly ITaskContext taskContext; private readonly ITaskContext taskContext;
@@ -40,19 +42,7 @@ internal sealed partial class GameService : IGameService
/// <inheritdoc/> /// <inheritdoc/>
public ObservableCollection<GameAccount> GameAccountCollection public ObservableCollection<GameAccount> GameAccountCollection
{ {
get get => gameAccounts ??= gameDbService.GetGameAccountCollection();
{
if (gameAccounts == null)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
gameAccounts = appDbContext.GameAccounts.ToObservableCollection();
}
}
return gameAccounts;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -83,7 +73,6 @@ internal sealed partial class GameService : IGameService
if (result.IsOk) if (result.IsOk)
{ {
// Save result. // Save result.
await taskContext.SwitchToMainThreadAsync();
appOptions.GamePath = result.Value; appOptions.GamePath = result.Value;
} }
else else
@@ -419,18 +408,32 @@ internal sealed partial class GameService : IGameService
private static bool LaunchSchemeMatchesExecutable(LaunchScheme launchScheme, string gameFileName) private static bool LaunchSchemeMatchesExecutable(LaunchScheme launchScheme, string gameFileName)
{ {
return (launchScheme.IsOversea && gameFileName == GenshinImpactFileName) return (launchScheme.IsOversea, gameFileName) switch
|| (!launchScheme.IsOversea && gameFileName == YuanShenFileName); {
(true, GenshinImpactFileName) => true,
(false, YuanShenFileName) => true,
_ => false,
};
} }
} }
[ConstructorGenerated] [ConstructorGenerated]
[Injection(InjectAs.Singleton, typeof(IGameDbSservice))] [Injection(InjectAs.Singleton, typeof(IGameDbService))]
internal sealed partial class GameDbSservice : IGameDbSservice internal sealed partial class GameDbService : IGameDbService
{ {
private readonly IServiceProvider serviceProvider;
public ObservableCollection<GameAccount> GetGameAccountCollection()
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return appDbContext.GameAccounts.AsNoTracking().ToObservableCollection();
}
}
} }
internal interface IGameDbSservice internal interface IGameDbService
{ {
ObservableCollection<GameAccount> GetGameAccountCollection();
} }