Skip to content

Commit

Permalink
LaunchGame
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Nov 3, 2022
1 parent 848392f commit 9e344f5
Show file tree
Hide file tree
Showing 42 changed files with 1,367 additions and 310 deletions.
6 changes: 3 additions & 3 deletions src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace Snap.Hutao.Core;
/// </summary>
internal static class CoreEnvironment
{
private const string CryptographyKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\";
private const string MachineGuidValue = "MachineGuid";

// 计算过程:https://gist.github.com/Lightczx/373c5940b36e24b25362728b52dec4fd

/// <summary>
Expand Down Expand Up @@ -71,6 +68,9 @@ internal static class CoreEnvironment
WriteIndented = true,
};

private const string CryptographyKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\";
private const string MachineGuidValue = "MachineGuid";

static CoreEnvironment()
{
Version = Package.Current.Id.Version.ToVersion();
Expand Down
1 change: 0 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/Database/DbCurrent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internal class DbCurrent<TEntity, TMessage>
/// </summary>
/// <param name="dbSet">数据集</param>
/// <param name="messenger">消息器</param>
///
public DbCurrent(DbSet<TEntity> dbSet, IMessenger messenger)
{
this.dbSet = dbSet;
Expand Down
3 changes: 2 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/Database/DbSetExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Snap.Hutao.Model.Entity;

namespace Snap.Hutao.Core.Database;

Expand Down Expand Up @@ -91,4 +92,4 @@ public static int UpdateAndSave<TEntity>(this DbSet<TEntity> dbSet, TEntity enti
dbSet.Update(entity);
return dbSet.Context().SaveChanges();
}
}
}
95 changes: 95 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/Database/SettingEntryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.EntityFrameworkCore;
using Snap.Hutao.Model.Entity;

namespace Snap.Hutao.Core.Database;

/// <summary>
/// 设置帮助类
/// </summary>
public static class SettingEntryHelper
{
/// <summary>
/// 获取或添加一个对应的设置
/// </summary>
/// <param name="dbSet">设置集</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>设置</returns>
public static SettingEntry SingleOrAdd(this DbSet<SettingEntry> dbSet, string key, string value)
{
SettingEntry? entry = dbSet.SingleOrDefault(entry => key == entry.Key);

if (entry == null)
{
entry = new(key, value);
dbSet.Add(entry);
dbSet.Context().SaveChanges();
}

return entry;
}

/// <summary>
/// 获取或添加一个对应的设置
/// </summary>
/// <param name="dbSet">设置集</param>
/// <param name="key">键</param>
/// <param name="valueFactory">值工厂</param>
/// <returns>设置</returns>
public static SettingEntry SingleOrAdd(this DbSet<SettingEntry> dbSet, string key, Func<string> valueFactory)
{
SettingEntry? entry = dbSet.SingleOrDefault(entry => key == entry.Key);

if (entry == null)
{
entry = new(key, valueFactory());
dbSet.Add(entry);
dbSet.Context().SaveChanges();
}

return entry;
}

/// <summary>
/// 获取 Boolean 值
/// </summary>
/// <param name="entry">设置</param>
/// <returns>值</returns>
public static bool GetBoolean(this SettingEntry entry)
{
return bool.Parse(entry.Value!);
}

/// <summary>
/// 设置 Boolean 值
/// </summary>
/// <param name="entry">设置</param>
/// <param name="value">值</param>
public static void SetBoolean(this SettingEntry entry, bool value)
{
entry.Value = value.ToString();
}

/// <summary>
/// 获取 Int32 值
/// </summary>
/// <param name="entry">设置</param>
/// <returns>值</returns>
public static int GetInt32(this SettingEntry entry)
{
return int.Parse(entry.Value!);
}

/// <summary>
/// 设置 Int32 值
/// </summary>
/// <param name="entry">设置</param>
/// <param name="value">值</param>
public static void SetInt32(this SettingEntry entry, int value)
{
entry.Value = value.ToString();
}
}
18 changes: 17 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/IO/Ini/IniSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Snap.Hutao.Core.IO.Ini;
internal static class IniSerializer
{
/// <summary>
/// 异步反序列化
/// 反序列化
/// </summary>
/// <param name="fileStream">文件流</param>
/// <returns>Ini 元素集合</returns>
Expand Down Expand Up @@ -44,4 +44,20 @@ public static IEnumerable<IniElement> Deserialize(FileStream fileStream)
}
}
}

/// <summary>
/// 序列化
/// </summary>
/// <param name="fileStream">写入的流</param>
/// <param name="elements">元素</param>
public static void Serialize(FileStream fileStream, IEnumerable<IniElement> elements)
{
using (TextWriter writer = new StreamWriter(fileStream))
{
foreach (IniElement element in elements)
{
writer.WriteLine(element.ToString());
}
}
}
}
26 changes: 26 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/LifeCycle/Activation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Snap.Hutao.Core.Threading;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.Navigation;
using System.Security.Principal;

namespace Snap.Hutao.Core.LifeCycle;

Expand All @@ -20,6 +21,19 @@ internal static class Activation

private static readonly SemaphoreSlim ActivateSemaphore = new(1);

/// <summary>
/// 获取是否提升了权限
/// </summary>
/// <returns>是否提升了权限</returns>
public static bool GetElevated()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}

/// <summary>
/// 响应激活事件
/// 激活事件一般不会在UI线程上触发
Expand Down Expand Up @@ -70,6 +84,18 @@ private static async Task HandleActivationCoreAsync(AppActivationArguments args)

case LaunchGame:
{
await ThreadHelper.SwitchToMainThreadAsync();
if (!MainWindow.IsPresent)
{
_ = Ioc.Default.GetRequiredService<LaunchGameWindow>();
}
else
{
await Ioc.Default
.GetRequiredService<INavigationService>()
.NavigateAsync<View.Page.LaunchGamePage>(INavigationAwaiter.Default, true).ConfigureAwait(false);
}

break;
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/Snap.Hutao/Snap.Hutao/Core/Windowing/ExtendedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ namespace Snap.Hutao.Core.Windowing;
/// 窗口管理器
/// 主要包含了针对窗体的 P/Inoke 逻辑
/// </summary>
internal sealed class ExtendedWindow
/// <typeparam name="TWindow">窗体类型</typeparam>
internal sealed class ExtendedWindow<TWindow>
where TWindow : Window, IExtendedWindowSource
{
private readonly HWND handle;
private readonly AppWindow appWindow;

private readonly Window window;
private readonly TWindow window;
private readonly FrameworkElement titleBar;

private readonly ILogger<ExtendedWindow> logger;
private readonly WindowSubclassManager subclassManager;
private readonly ILogger<ExtendedWindow<TWindow>> logger;
private readonly WindowSubclassManager<TWindow> subclassManager;

private readonly bool useLegacyDragBar;

Expand All @@ -36,19 +38,19 @@ internal sealed class ExtendedWindow
/// </summary>
/// <param name="window">窗口</param>
/// <param name="titleBar">充当标题栏的元素</param>
private ExtendedWindow(Window window, FrameworkElement titleBar)
private ExtendedWindow(TWindow window, FrameworkElement titleBar)
{
this.window = window;
this.titleBar = titleBar;
logger = Ioc.Default.GetRequiredService<ILogger<ExtendedWindow>>();
logger = Ioc.Default.GetRequiredService<ILogger<ExtendedWindow<TWindow>>>();

handle = (HWND)WindowNative.GetWindowHandle(window);

WindowId windowId = Win32Interop.GetWindowIdFromWindow(handle);
appWindow = AppWindow.GetFromWindowId(windowId);

useLegacyDragBar = !AppWindowTitleBar.IsCustomizationSupported();
subclassManager = new(handle, useLegacyDragBar);
subclassManager = new(window, handle, useLegacyDragBar);

InitializeWindow();
}
Expand All @@ -57,11 +59,10 @@ private ExtendedWindow(Window window, FrameworkElement titleBar)
/// 初始化
/// </summary>
/// <param name="window">窗口</param>
/// <param name="titleBar">标题栏</param>
/// <returns>实例</returns>
public static ExtendedWindow Initialize(Window window, FrameworkElement titleBar)
public static ExtendedWindow<TWindow> Initialize(TWindow window)
{
return new(window, titleBar);
return new(window, window.TitleBar);
}

private static void UpdateTitleButtonColor(AppWindowTitleBar appTitleBar)
Expand Down Expand Up @@ -103,7 +104,8 @@ private void InitializeWindow()
appWindow.Title = "胡桃";

ExtendsContentIntoTitleBar();
Persistence.RecoverOrInit(appWindow);

Persistence.RecoverOrInit(appWindow, window.PersistSize, window.InitSize);

// Log basic window state here.
(string pos, string size) = GetPostionAndSize(appWindow);
Expand All @@ -115,14 +117,18 @@ private void InitializeWindow()
logger.LogInformation(EventIds.BackdropState, "Apply {name} : {result}", nameof(SystemBackdrop), micaApplied ? "succeed" : "failed");

bool subClassApplied = subclassManager.TrySetWindowSubclass();
logger.LogInformation(EventIds.SubClassing, "Apply {name} : {result}", nameof(WindowSubclassManager), subClassApplied ? "succeed" : "failed");
logger.LogInformation(EventIds.SubClassing, "Apply {name} : {result}", nameof(WindowSubclassManager<TWindow>), subClassApplied ? "succeed" : "failed");

window.Closed += OnWindowClosed;
}

private void OnWindowClosed(object sender, WindowEventArgs args)
{
Persistence.Save(appWindow);
if (window.PersistSize)
{
Persistence.Save(appWindow);
}

subclassManager?.Dispose();
}

Expand Down Expand Up @@ -155,4 +161,4 @@ private void UpdateDragRectangles(AppWindowTitleBar appTitleBar)
RectInt32 dragRect = new RectInt32(48, 0, (int)titleBar.ActualWidth, (int)titleBar.ActualHeight).Scale(scale);
appTitleBar.SetDragRectangles(dragRect.Enumerate().ToArray());
}
}
}
37 changes: 37 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/Windowing/IExtendedWindowSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.UI.Xaml;
using Windows.Graphics;
using Windows.Win32.UI.WindowsAndMessaging;

namespace Snap.Hutao.Core.Windowing;

/// <summary>
/// 为扩展窗体提供必要的选项
/// </summary>
/// <typeparam name="TWindow">窗体类型</typeparam>
internal interface IExtendedWindowSource
{
/// <summary>
/// 提供的标题栏
/// </summary>
FrameworkElement TitleBar { get; }

/// <summary>
/// 是否持久化尺寸
/// </summary>
bool PersistSize { get; }

/// <summary>
/// 初始大小
/// </summary>
SizeInt32 InitSize { get; }

/// <summary>
/// 处理最大最小信息
/// </summary>
/// <param name="pInfo">信息指针</param>
/// <param name="scalingFactor">缩放比</param>
unsafe void ProcessMinMaxInfo(MINMAXINFO* pInfo, double scalingFactor);
}
21 changes: 13 additions & 8 deletions src/Snap.Hutao/Snap.Hutao/Core/Windowing/Persistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ internal static class Persistence
/// 设置窗体位置
/// </summary>
/// <param name="appWindow">应用窗体</param>
public static void RecoverOrInit(AppWindow appWindow)
/// <param name="persistSize">持久化尺寸</param>
/// <param name="size">初始尺寸</param>
public static void RecoverOrInit(AppWindow appWindow, bool persistSize, SizeInt32 size)
{
// Set first launch size.
HWND hwnd = (HWND)Win32Interop.GetWindowFromWindowId(appWindow.Id);
SizeInt32 size = TransformSizeForWindow(new(1200, 741), hwnd);
RectInt32 rect = StructMarshal.RectInt32(size);
SizeInt32 transformedSize = TransformSizeForWindow(size, hwnd);
RectInt32 rect = StructMarshal.RectInt32(transformedSize);

RectInt32 target = (CompactRect)LocalSetting.Get(SettingKeys.WindowRect, (ulong)(CompactRect)rect);
if (target.Width * target.Height < 848 * 524)
if (persistSize)
{
target = rect;
RectInt32 persistedSize = (CompactRect)LocalSetting.Get(SettingKeys.WindowRect, (ulong)(CompactRect)rect);
if (persistedSize.Width * persistedSize.Height > 848 * 524)
{
rect = persistedSize;
}
}

TransformToCenterScreen(ref target);
appWindow.MoveAndResize(target);
TransformToCenterScreen(ref rect);
appWindow.MoveAndResize(rect);
}

/// <summary>
Expand Down
Loading

0 comments on commit 9e344f5

Please sign in to comment.