Files
AquaDX/AquaMai/AquaMai.Mods/GameSystem/Window.cs
Menci 37044dae01 [RF] AquaMai configuration refactor (#82)
更新了配置文件格式,原有的配置文件将被自动无缝迁移,详情请见新的配置文件中的注释(例外:`SlideJudgeTweak` 不再默认启用)
旧配置文件将被重命名备份,如果更新到此版本遇到 Bug 请联系我们

Updated configuration file schema. The old config file will be migrated automatically and seamlessly. See the comments in the new configuration file for details. (Except for `SlideJudgeTweak` is no longer enabled by default)
Your old configuration file will be renamed as a backup. If you encounter any bug with this version, please contact us.
2024-11-25 01:25:19 +08:00

108 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AquaMai.Config.Attributes;
using UnityEngine;
namespace AquaMai.Mods.GameSystem;
[ConfigSection(
en: "Windowed Mode / Window Settings.",
zh: "窗口化/窗口设置")]
public class Window
{
[ConfigEntry(
en: "Window the game.",
zh: "窗口化游戏")]
private static readonly bool windowed = false;
[ConfigEntry(
en: """
Window width (and height) for windowed mode, rendering resolution for fullscreen mode.
If set to 0, windowed mode will remember the user-set size, fullscreen mode will use the current display resolution.
""",
zh: """
宽度(和高度)窗口化时为游戏窗口大小,全屏时为渲染分辨率
如果设为 0窗口化将记住用户设定的大小全屏时将使用当前显示器分辨率
""")]
private static readonly int width = 0;
[ConfigEntry(
en: "Height, as above.",
zh: "高度,同上")]
private static readonly int height = 0;
private const int GWL_STYLE = -16;
private const int WS_WHATEVER = 0x14CF0000;
private static IntPtr hwnd = IntPtr.Zero;
public static void OnBeforePatch()
{
if (windowed)
{
var alreadyWindowed = Screen.fullScreenMode == FullScreenMode.Windowed;
if (width == 0 || height == 0)
{
Screen.fullScreenMode = FullScreenMode.Windowed;
}
else
{
alreadyWindowed = false;
Screen.SetResolution(width, height, FullScreenMode.Windowed);
}
hwnd = GetWindowHandle();
if (alreadyWindowed)
{
SetResizeable();
}
else
{
Task.Run(async () =>
{
await Task.Delay(3000);
// Screen.SetResolution has delay
SetResizeable();
});
}
}
else
{
var width = Window.width == 0 ? Display.main.systemWidth : Window.width;
var height = Window.height == 0 ? Display.main.systemHeight : Window.height;
Screen.SetResolution(width, height, FullScreenMode.FullScreenWindow);
}
}
public static void SetResizeable()
{
if (hwnd == IntPtr.Zero) return;
SetWindowLongPtr(hwnd, GWL_STYLE, WS_WHATEVER);
}
private delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("Kernel32.dll")]
static extern int GetCurrentThreadId();
static IntPtr GetWindowHandle()
{
IntPtr returnHwnd = IntPtr.Zero;
var threadId = GetCurrentThreadId();
EnumThreadWindows(threadId,
(hWnd, lParam) =>
{
if (returnHwnd == IntPtr.Zero) returnHwnd = hWnd;
return true;
}, IntPtr.Zero);
return returnHwnd;
}
[DllImport("user32.dll")]
static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, int dwNewLong);
}