Skip to content

Commit

Permalink
prevent user service capture scoped app db context
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Oct 30, 2022
1 parent 62d0fb5 commit 848392f
Show file tree
Hide file tree
Showing 32 changed files with 880 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Context/Database/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public AppDbContext(DbContextOptions<AppDbContext> options)
/// </summary>
public DbSet<AvatarInfo> AvatarInfos { get; set; } = default!;

/// <summary>
/// 游戏内账号
/// </summary>
public DbSet<GameAccount> GameAccounts { get; set; } = default!;

/// <summary>
/// 构造一个临时的应用程序数据库上下文
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/Threading/ThreadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace Snap.Hutao.Core.Threading;
internal static class ThreadHelper
{
/// <summary>
/// 异步切换到主线程
/// 使用此静态方法以 异步切换到 主线程
/// </summary>
/// <remarks>使用 <see cref="Task.Yield"/> 异步切换到 后台线程</remarks>
/// <returns>等待体</returns>
public static DispatherQueueSwitchOperation SwitchToMainThreadAsync()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Model/Binding/Gacha/HistoryWish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public class HistoryWish : WishBase
/// <summary>
/// 版本
/// </summary>
public string Version { get; set; }
public string Version { get; set; } = default!;

/// <summary>
/// 卡池图片
/// </summary>
public Uri BannerImage { get; set; } = default!;

/// <summary>
/// 五星Up
Expand Down
42 changes: 42 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Binding/LaunchGame/LaunchScheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Model.Binding.LaunchGame;

/// <summary>
/// 服务器方案
/// </summary>
/// <summary>
/// 启动方案
/// </summary>
public class LaunchScheme
{
/// <summary>
/// 构造一个新的启动方案
/// </summary>
/// <param name="name">名称</param>
/// <param name="channel">通道</param>
/// <param name="cps">通道描述字符串</param>
/// <param name="subChannel">子通道</param>
public LaunchScheme(string name, string channel, string subChannel)
{
Name = name;
Channel = channel;
SubChannel = subChannel;
}

/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }

/// <summary>
/// 通道
/// </summary>
public string Channel { get; set; }

/// <summary>
/// 子通道
/// </summary>
public string SubChannel { get; set; }
}
25 changes: 25 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Binding/LaunchGame/SchemeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Model.Binding.LaunchGame;

/// <summary>
/// 启动类型
/// </summary>
public enum SchemeType
{
/// <summary>
/// 国际服
/// </summary>
Mihoyo,

/// <summary>
/// 国服官服
/// </summary>
Officical,

/// <summary>
/// 渠道服
/// </summary>
Bilibili,
}
47 changes: 47 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Entity/GameAccount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Core.Database;
using Snap.Hutao.Model.Binding.LaunchGame;
using Snap.Hutao.Web.Hoyolab;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Snap.Hutao.Model.Entity;

/// <summary>
/// 游戏内账号
/// </summary>
[Table("game_accounts")]
public class GameAccount : ISelectable
{
/// <summary>
/// 内部Id
/// </summary>
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid InnerId { get; set; }

/// <inheritdoc/>
public bool IsSelected { get; set; }

/// <summary>
/// 对应的Uid
/// </summary>
public string? AttachUid { get; set; }

/// <summary>
/// 服务器类型
/// </summary>
public SchemeType Type { get; set; }

/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = default!;

/// <summary>
/// MIHOYOSDK_ADL_PROD_CN_h3123967166
/// </summary>
public string MihoyoSDK { get; set; } = default!;
}
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Model/Entity/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ public static User Create(Cookie cookie)
{
return new() { Cookie = cookie };
}
}
}
5 changes: 5 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/Metadata/GachaEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class GachaEvent
/// </summary>
public string Version { get; set; } = default!;

/// <summary>
/// 卡池图
/// </summary>
public Uri Banner { get; set; } = default!;

/// <summary>
/// 开始时间
/// </summary>
Expand Down
35 changes: 35 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Model/NamedValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using CommunityToolkit.Mvvm.ComponentModel;

namespace Snap.Hutao.Model;

/// <summary>
/// 封装带有名称描述的值
/// 在绑定枚举变量时非常有用
/// </summary>
/// <typeparam name="T">包含值的类型</typeparam>
public class NamedValue<T>
{
/// <summary>
/// 构造一个新的命名的值
/// </summary>
/// <param name="name">命名</param>
/// <param name="value">值</param>
public NamedValue(string name, T value)
{
Name = name;
Value = value;
}

/// <summary>
/// 名称
/// </summary>
public string Name { get; }

/// <summary>
/// 值
/// </summary>
public T Value { get; }
}
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Model/Selectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public bool IsSelected
/// 存放的对象
/// </summary>
public T Value { get => value; set => SetProperty(ref this.value, value); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public HistoryWish ToHistoryWish()

// fill
Version = gachaEvent.Version,
BannerImage = gachaEvent.Banner,
OrangeUpList = orangeUpCounter.ToStatisticsList(),
PurpleUpList = purpleUpCounter.ToStatisticsList(),
OrangeList = orangeCounter.ToStatisticsList(),
Expand Down
102 changes: 66 additions & 36 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/GameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using Snap.Hutao.Context.Database;
using Snap.Hutao.Core;
using Snap.Hutao.Core.Database;
using Snap.Hutao.Core.IO.Ini;
using Snap.Hutao.Core.Threading;
using Snap.Hutao.Model.Entity;
using Snap.Hutao.Service.Game.Locator;
using Snap.Hutao.Service.Game.Unlocker;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;

Expand All @@ -27,6 +29,8 @@ internal class GameService : IGameService
private readonly IMemoryCache memoryCache;
private readonly SemaphoreSlim gameSemaphore = new(1);

private ObservableCollection<GameAccount>? gameAccounts;

/// <summary>
/// 构造一个新的游戏服务
/// </summary>
Expand Down Expand Up @@ -129,51 +133,77 @@ public void OverwriteGamePath(string path)
}

/// <inheritdoc/>
public async ValueTask LaunchAsync(LaunchConfiguration configuration)
public MultiChannel GetMultiChannel()
{
(bool isOk, string gamePath) = await GetGamePathAsync().ConfigureAwait(false);
string gamePath = GetGamePathSkipLocator();
string configPath = Path.Combine(gamePath, "config.ini");

if (isOk)
using (FileStream stream = File.OpenRead(configPath))
{
if (gameSemaphore.CurrentCount == 0)
{
return;
}
List<IniElement> elements = IniSerializer.Deserialize(stream).ToList();
string? channel = elements.OfType<IniParameter>().FirstOrDefault(p => p.Key == "channel")?.Value;
string? subChannel = elements.OfType<IniParameter>().FirstOrDefault(p => p.Key == "sub_channel")?.Value;

return new(channel, subChannel);
}
}

public async Task<ObservableCollection<GameAccount>> GetGameAccountCollectionAsync()
{
if (gameAccounts == null)
{

}

return gameAccounts;
}

/// <inheritdoc/>
public async ValueTask LaunchAsync(LaunchConfiguration configuration)
{
if (gameSemaphore.CurrentCount == 0)
{
return;
}

string gamePath = GetGamePathSkipLocator();

string commandLine = new CommandLineBuilder()
.Append("-window-mode", configuration.WindowMode)
.Append("-screen-fullscreen", configuration.IsFullScreen ? 1 : 0)
.Append("-screen-width", configuration.ScreenWidth)
.Append("-screen-height", configuration.ScreenHeight)
.Append("-monitor", configuration.Monitor)
.Build();
string commandLine = new CommandLineBuilder()
.AppendIf("-popupwindow", configuration.IsBorderless)
.Append("-screen-fullscreen", configuration.IsFullScreen ? 1 : 0)
.Append("-screen-width", configuration.ScreenWidth)
.Append("-screen-height", configuration.ScreenHeight)
.Append("-monitor", configuration.Monitor)
.Build();

Process game = new()
Process game = new()
{
StartInfo = new()
{
StartInfo = new()
{
Arguments = commandLine,
FileName = gamePath,
UseShellExecute = true,
Verb = "runas",
WorkingDirectory = Path.GetDirectoryName(gamePath),
},
};

using (await gameSemaphore.EnterAsync().ConfigureAwait(false))
Arguments = commandLine,
FileName = gamePath,
UseShellExecute = true,
Verb = "runas",
WorkingDirectory = Path.GetDirectoryName(gamePath),
},
};

using (await gameSemaphore.EnterAsync().ConfigureAwait(false))
{
if (configuration.UnlockFPS)
{
if (configuration.UnlockFPS)
{
IGameFpsUnlocker unlocker = new GameFpsUnlocker(game, configuration.TargetFPS);
IGameFpsUnlocker unlocker = new GameFpsUnlocker(game, configuration.TargetFPS);

await unlocker.UnlockAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(10000), TimeSpan.FromMilliseconds(2000)).ConfigureAwait(false);
}
else
TimeSpan findModuleDelay = TimeSpan.FromMilliseconds(100);
TimeSpan findModuleLimit = TimeSpan.FromMilliseconds(10000);
TimeSpan adjustFpsDelay = TimeSpan.FromMilliseconds(2000);
await unlocker.UnlockAsync(findModuleDelay, findModuleLimit, adjustFpsDelay).ConfigureAwait(false);
}
else
{
if (game.Start())
{
if (game.Start())
{
await game.WaitForExitAsync().ConfigureAwait(false);
}
await game.WaitForExitAsync().ConfigureAwait(false);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/IGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ internal interface IGameService
/// <returns>游戏路径,当路径无效时会设置并返回 <see cref="string.Empty"/></returns>
string GetGamePathSkipLocator();

/// <summary>
/// 获取多通道值
/// </summary>
/// <returns>多通道值</returns>
MultiChannel GetMultiChannel();

/// <summary>
/// 异步启动
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal struct LaunchConfiguration
public bool IsFullScreen { get; set; }

/// <summary>
/// Override fullscreen windowed mode. Accepted values are exclusive or borderless.
/// 是否为无边框窗口
/// </summary>
public string WindowMode { get; private set; }
public bool IsBorderless { get; private set; }

/// <summary>
/// 是否启用解锁帧率
Expand Down
Loading

0 comments on commit 848392f

Please sign in to comment.